Vue Storybook6 - ๊ฐ๋จํ ๋ฒํผ ๋ง๋ค๊ธฐ
๋ชฉ์ฐจ
- 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ํ์ผ๋ก ๋ง๋ค์ด์ง๊ฒ ๋ฉ๋๋ค. ๊ทธ๋ผ ๋ค์ ํฌ์คํ
์ ๊ธฐ๋ํด์ฃผ์ธ์~ ์๋