๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ‘จ‍๐Ÿ’ป web.dev/fe

Storybook loaders ๋กœ story์— ๋น„๋™๊ธฐ ๋ฐ์ดํ„ฐ ์ฃผ์ž…ํ•˜๊ธฐ

by HandHand 2023. 6. 24.

 

๐Ÿ“Œ loaders ๋กœ ๋ฐ์ดํ„ฐ ์ „๋‹ฌํ•˜๊ธฐ

Storybook 6.1 ๋ถ€ํ„ฐ ์‹คํ—˜์  ๊ธฐ๋Šฅ์œผ๋กœ loaders ๊ฐ€ ์ถ”๊ฐ€๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

์•ˆ์ •ํ™” ๊ธฐ๊ฐ„์„ ๊ฑฐ์ณ Storybook 7.0 ์—์„œ๋Š” stable ๋กœ ๋ณ€๊ฒฝ๋˜์—ˆ๋Š”๋ฐ์š”.

๊ธฐ์กด์— args ๋ฅผ ํ†ตํ•ด ์ปดํฌ๋„ŒํŠธ์— ๋ฐ์ดํ„ฐ๋ฅผ ์ „๋‹ฌํ•ด์ฃผ๋Š” ๋Œ€์‹ , loaders ์— ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•จ์œผ๋กœ์„œ ์Šคํ† ๋ฆฌ๋ถ render context ์— ์ด๋ฅผ ํ• ๋‹นํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์ด ์ถ”๊ฐ€๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

 

๋ชจ๋“  ์Šคํ† ๋ฆฌ์— ์ ์šฉ๋˜๋Š”์ง€ ์œ ๋ฌด์— ๋”ฐ๋ผ global loader ์™€ story loader ๋‘ ๊ฐ€์ง€ ๋ฐฉ์‹์œผ๋กœ ๋‚˜๋‰˜๋Š”๋ฐ,

์ด๋ฒˆ ๊ฒฝ์šฐ์—๋Š” ๊ฐœ๋ณ„ ์Šคํ† ๋ฆฌ์— ์ฃผ์ž…์ด ํ•„์š”ํ•ด์„œ story loader ๋ฐฉ์‹์„ ์‚ฌ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค.

 

๐Ÿ“Œ ์˜ˆ์‹œ๋กœ ์‚ดํŽด๋ด…์‹œ๋‹ค.

๋‹ค์Œ๊ณผ ๊ฐ™์€ ์›น ์ปดํฌ๋„ŒํŠธ๊ฐ€ ์žˆ๋‹ค๊ณ  ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

ํ•ด๋‹น ์ปดํฌ๋„ŒํŠธ๋Š” ์ž‘์€ ์บ”๋ฒ„์Šค์— ์ด๋ฏธ์ง€ ๋ฐ์ดํ„ฐ๋ฅผ ๋ Œ๋”๋งํ•˜๋Š” ์—ญํ• ์„ ์œ„ํ•ด ๋งŒ๋“ค์–ด์กŒ์Šต๋‹ˆ๋‹ค.

ํ˜„์žฌ๋Š” ๊ณ ์ •๋œ ๋ฏธ๋ฆฌ๋ณด๊ธฐ ์ด๋ฏธ์ง€๋ฅผ ๊ทธ๋ ค์ฃผ๊ณ  ์žˆ๋Š”๋ฐ, ์ด๋ฅผ ๋™์ ์œผ๋กœ ๋ฐ”๊พธ๊ณ  ์‹ถ์€ ์ƒํ™ฉ์ž…๋‹ˆ๋‹ค.

 

import { html } from 'lit-html'

export const Basic = ({ caption, selected }) =>
  html`<v-canvas-preview caption="${caption}" selected="${selected}"></v-canvas-preview>`
Basic.args = {
  caption: 'ํ”„๋ฆฌ๋ทฐ ์„ค๋ช…',
  selected: true,
}

 

์ด๋ฅผ ์œ„ํ•ด์„œ ์บ”๋ฒ„์Šค์— ImageData ๋ฅผ ์ „๋‹ฌํ•ด์ค˜์•ผํ•ฉ๋‹ˆ๋‹ค.

๋จผ์ € ImageData ๋ฅผ ์ƒ์„ฑํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค.

ํ…Œ์ŠคํŠธ๋ฅผ ์œ„ํ•ด ์ƒ˜ํ”Œ ์ด๋ฏธ์ง€๋ฅผ ์ด์šฉํ•ด์„œ ImageData ๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.

 

async function getImageData() {
  return new Promise((resolve, reject) => {
    const canvas = document.createElement('canvas')
        canvas.width = 1000
    canvas.height = 1000

    const image = new Image()
    image.src = 'assets/images/test.png'
    image.addEventListener('load', () => {
            const ctx = canvas.getContext('2d')
      ctx?.drawImage(image, 0, 0, 130, 130)

      const imageData = ctx?.getImageData(0, 0, 130, 130)

      if (imageData) {
        resolve(imageData)
      } else {
        reject()
      }
    })
  })
}

 

canvas ์— ๋ถˆ๋Ÿฌ์˜จ ์ด๋ฏธ์ง€๋ฅผ ๊ทธ๋ฆฐ ๋‹ค์Œ, ์Šค๋ƒ…์ƒท์„ ์ƒ์„ฑํ•ด์„œ ImageData ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค.

์ด๋ฏธ์ง€ ๋กœ๋”ฉ์ด ๋น„๋™๊ธฐ๋กœ ์ด๋ฃจ์–ด์ง€๊ธฐ ๋•Œ๋ฌธ์— ์œ„ ํ•จ์ˆ˜ ๋˜ํ•œ ๋น„๋™๊ธฐ Promise ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋ฉฐ,

์ผ๋ฐ˜์ ์ธ args ์ „๋‹ฌ ๋ฐฉ์‹์œผ๋กœ๋Š” ์Šคํ† ๋ฆฌ๋ถ์— ๋ Œ๋”๋งํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.

๋Œ€์‹  loaders ๋ฅผ ์ด์šฉํ•ด์„œ ์ปดํฌ๋„ŒํŠธ์— ๋น„๋™๊ธฐ ๋ฐ์ดํ„ฐ๋ฅผ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋‹ค์Œ๊ณผ ๊ฐ™์ด loaders ๋ฅผ ์Šคํ† ๋ฆฌ์— ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.

 

export const Basic = (
  { caption, selected },
  { loaded: { imageData } }
) => {
  console.log(imageData) // โœ… ๋กœ๊ทธ๋กœ ํ™•์ธํ•ด๋ณด์ž

  return html`<v-canvas-preview caption="${caption}" selected="${selected}"></v-canvas-preview>`
}

Basic.loaders = [
  async () => ({
    imageData: await getImageData(),
  }),
]

 

 

๋ฐ์ดํ„ฐ๊ฐ€ ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ์ „๋‹ฌ๋˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•ด๋ด…๋‹ˆ๋‹ค.

์ด์ œ ์ด ๊ฐ’์„ ์ปดํฌ๋„ŒํŠธ์— ๋„˜๊ฒจ์ค€ ๋’ค ์›ํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ์ฒ˜๋ฆฌํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

์ด๋ฒˆ ์˜ˆ์ œ์—์„œ๋Š” element ์˜ property ๋กœ ์ด ๊ฐ’์„ ์ง€์ •ํ•ด์ค๋‹ˆ๋‹ค.

ImageData ๋กœ ์บ”๋ฒ„์Šค์— ๋ Œ๋”๋งํ•˜๋Š” ๋กœ์ง์€ v-canvas-preview ๋‚ด์— ์ •์˜๋˜์–ด์žˆ์œผ๋ฉฐ

์ด๋ฒˆ ํฌ์ŠคํŠธ์—์„œ๋Š” ๋ฒ”์œ„๋ฅผ ๋ฒ—์–ด๋‚˜๋Š” ๋‚ด์šฉ์ด๊ธฐ ๋•Œ๋ฌธ์— ๊ตฌํ˜„์„ ์ƒ๋žตํ•ฉ๋‹ˆ๋‹ค.

 

export const Basic = (
  { caption, selected },
  { loaded: { imageData } }
) => {
  const loadImageData = () => {
    setTimeout(() => {
      const $preview = document.querySelector('v-canvas-preview')
      if ($preview) {
        $preview.imageData = { image: imageData }
      }
    }, 0)
  }

  loadImageData()

  return html`<v-canvas-preview caption="${caption}" selected="${selected}"></v-canvas-preview>`
}

 

 

๐Ÿ“Œ ์ฐธ๊ณ ์ž๋ฃŒ

Loaders

 

Loaders

Storybook is a frontend workshop for building UI components and pages in isolation. Thousands of teams use it for UI development, testing, and documentation. It’s open source and free.

storybook.js.org

 

๋ฐ˜์‘ํ˜•

๐Ÿ’ฌ ๋Œ“๊ธ€