์นดํ…Œ๊ณ ๋ฆฌ ์—†์Œ

Vue Storybook6 - ๊ฐ„๋‹จํ•œ ๋ฒ„ํŠผ ๋งŒ๋“ค๊ธฐ

eddie0329 2020. 11. 29. 01:27
๋ฐ˜์‘ํ˜•

๋ชฉ์ฐจ

  • Introduction
  • Implement MyButton
  • MyButton story
  • Conclusion
  • Reference

๐Ÿ“Œ Introduction

์•ˆ๋…•ํ•˜์„ธ์š”. ์˜ค๋Š˜์€ ๊ฐ„๋‹จํ•˜๊ฒŒ ๋ทฐ๋กœ MyButton์„ ๋งŒ๋“ค๊ณ  ๊ทธ๊ฑธ storybook์œผ๋กœ ํ•œ๋ฒˆ ํ™œ์šฉ ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๐ŸŽ‰

๐Ÿ“Œ Implement MyButton

MyButton์€ ๊ฐ„๋‹จํ•˜๊ฒŒ ์ƒ‰์„ props๋กœ ๋ฐ›์•„ ๋ณ€๊ฒฝํ•˜๋Š” ๊ฒƒ์œผ๋กœ ๊ตฌํ˜„์„ ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

<template>
  <button @click="onClick" :class="['my-button', btnTypeClass]">
    <slot></slot>
  </button>
</template>
export default {
  name: "MyButton",
  props: {
    btnType: {
      type: String,
      default: "blue",
    },
  },
  computed: {
    btnTypeClass() {
      return `my-button__${this.btnType}`;
    },
  },
  methods: {
    onClick() {
      this.$emit("click");
    },
  },
};
.my-button {
  border-radius: 20px;
  border: none;
  color: white;
}
.my-button__blue {
  background-color: blue;
}
.my-button__red {
  background-color: red;
}

์ด์ œ ์ด๋ ‡๊ฒŒ ๋ฐ›์€ MyButton์„ ํ•œ๋ฒˆ storybook์œผ๋กœ ๊ตฌํ˜„์„ ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

๐Ÿ“Œ MyButton story

์ผ๋‹จ ๋จผ์ € storybook์€ react์— ์ตœ์ ํ™”๊ฐ€ ๋˜์–ด์žˆ๋‹ค๋Š” ๋А๋‚Œ์„ ๋งŽ์ด ๋ฐ›์•˜์Šต๋‹ˆ๋‹ค.

๊ทธ๋ž˜์„œ props๋ฅผ ๊ฑด๋‚ด์ฃผ๋Š” ๋ฐฉ์‹์ด react์™€ ํก์‚ฌํ•ด์š”. ํ•œ๋ฒˆ ์ฝ”๋“œ๋ฅผ ๋ณผ๊ฒŒ์š”.

import MyButton from "../components/MyButton";

// ์ด๋ฆ„์„ ์ •์˜ํ•ด์ค˜์šฉ! 
export default { title: "MyButton" };

// ์ด๋ ‡๊ฒŒ ๊ธฐ๋ณธ์ ์ธ ํ…œํ”Œ๋ฆฟ์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
const Template = (args, { argTypes }) => ({
  components: { MyButton },
  props: Object.keys(argTypes),
  methods: {
    onClick() {
      console.log("HELLO");
    },
  },
  template: `
  <div>
    <my-button :btn-type="btnType" @click="onClick">HELLO</my-button>
  </div>
  `,
});

// ์ด์ œ ๊ฐ๊ฐ์˜ ์Šคํ† ๋ฆฌ๋ฅผ ๋ณด์—ฌ์ฃผ๋Š” ๋ถ€๋ถ„ ์ž…๋‹ˆ๋‹ค.
export const Primary = Template.bind({});
// ๋งˆ์น˜ react ์ฒ˜๋Ÿผ ์ด๋ ‡๊ฒŒ props๋ฅผ ์ „๋‹ฌํ•ด์š”.
Primary.args = { btnType: "blue" };

export const Secondary = Template.bind({});
Secondary.args = { btnType: "red" };

์‹ ๊ธฐํ•œ๊ฑด ์ด์ œ storybook6์—๋Š” ์ž๋™์ ์œผ๋กœ addons์˜ ๊ธฐ๋Šฅ์ด ๋ถ™์–ด์„œ story๊ฐ€ ์ƒ์„ฑ์ด ๋ฉ๋‹ˆ๋‹ค.๐ŸŽ‰ ํ•œ๋ฒˆ ์Šคํฌ๋ฆฐ ์ƒท์œผ๋กœ ํ™•์ธ ํ•ด๋ณผ๊ฒŒ์šฉ.

์ด๋ ‡๊ฒŒ ์ƒ์„ฑ์ด ๋˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ Secondary๋„ ์ด๋ ‡๊ฒŒ ์ƒ์„ฑ์ด ๋ฉ๋‹ˆ๋‹ค.

๊ทธ๋ฆฌ๊ณ  props๋ฅผ ์ž๋™์ ์œผ๋กœ ์ด๋ ‡๊ฒŒ controlํ•  ์ˆ˜ ์žˆ์–ด์š”.

๐Ÿ“Œ Conclusion

์ด์ œ ๋‹ค์Œ์€ ํ•œ๋ฒˆ ๋ฌธ์„œ๋ฅผ ์ž‘์„ฑํ•ด๋ณผ๊ฒŒ์š”. docs๋Š” mdxํŒŒ์ผ๋กœ ๋งŒ๋“ค์–ด์ง€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿผ ๋‹ค์Œ ํฌ์ŠคํŒ…์„ ๊ธฐ๋Œ€ํ•ด์ฃผ์„ธ์š”~ ์•ˆ๋…•

๐Ÿ“Œ Reference

๋ฐ˜์‘ํ˜•