Skip to content

Commit bc60fb8

Browse files
author
jerrywu
committed
docs: storybook v7
1 parent 647543f commit bc60fb8

35 files changed

+996
-131
lines changed

.eslintrc.js

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ module.exports = {
66
'@vue/typescript/recommended',
77
'eslint:recommended', // https://eslint.org/docs/rules/
88
'plugin:jsx-a11y/strict', // https://www.npmjs.com/package/eslint-plugin-jsx-a11y
9+
'plugin:storybook/recommended',
910
],
1011
env: {
1112
browser: true,

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ pnpm-lock.yaml
1515
yarn.lock
1616

1717
src/index.css
18+
storybook-static
1819
playground/dist
1920
playground/storybook-static
2021

.storybook/main.ts

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import type { StorybookConfig } from '@storybook/vue3-vite';
2+
import { join, dirname } from 'path';
3+
4+
/**
5+
* This function is used to resolve the absolute path of a package.
6+
* It is needed in projects that use Yarn PnP or are set up within a monorepo.
7+
*/
8+
function getAbsolutePath(value: string): any {
9+
return dirname(require.resolve(join(value, 'package.json')));
10+
}
11+
12+
const config: StorybookConfig = {
13+
stories: ['../src/**/*.mdx', '../src/**/*.stories.@(js|jsx|mjs|ts|tsx)'],
14+
addons: [
15+
getAbsolutePath('@storybook/addon-links'),
16+
getAbsolutePath('@storybook/addon-actions'),
17+
getAbsolutePath('@storybook/addon-essentials'),
18+
getAbsolutePath('@storybook/addon-onboarding'),
19+
getAbsolutePath('@storybook/addon-interactions'),
20+
],
21+
framework: {
22+
name: '@storybook/vue3-vite',
23+
options: {},
24+
},
25+
docs: {
26+
autodocs: 'tag',
27+
},
28+
};
29+
30+
export default config;
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
<script>
22
window.global = window;
3-
</script>
3+
</script>
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
import type { Preview } from '@storybook/vue3';
12
import { MINIMAL_VIEWPORTS } from '@storybook/addon-viewport';
2-
import '../../src/styles/main.scss';
3+
import '../src/styles/main.scss';
34

45
const customViewports = {
56
pixel2: {
@@ -25,25 +26,23 @@ const customViewports = {
2526
},
2627
};
2728

28-
// https://storybook.js.org/docs/react/essentials/viewport
29-
export const parameters = {
30-
padded: false,
31-
actions: { argTypesRegex: "^on[A-Z].*" },
32-
controls: {
33-
matchers: {
34-
color: /(background|color)$/i,
35-
date: /Date$/,
29+
const preview: Preview = {
30+
parameters: {
31+
padded: false,
32+
actions: { argTypesRegex: '^on[A-Z].*' },
33+
controls: {
34+
matchers: {
35+
color: /(background|color)$/i,
36+
date: /Date$/,
37+
},
3638
},
37-
},
38-
options: {
39-
storySort: {
40-
order: ["Intro"],
41-
},
42-
},
43-
viewport: {
44-
viewports: {
45-
...MINIMAL_VIEWPORTS,
46-
...customViewports,
39+
viewport: {
40+
viewports: {
41+
...MINIMAL_VIEWPORTS,
42+
...customViewports,
43+
},
4744
},
4845
},
4946
};
47+
48+
export default preview;

package.json

+15-3
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@
4949
"playground"
5050
],
5151
"scripts": {
52-
"storybook": "npm run storybook -w playground",
52+
"storybook": "storybook dev -p 6006",
53+
"build:storybook": "cross-env NO_DTS=1 storybook build",
5354
"build": "tsup && npm run build:css",
5455
"dev": "npm run dev -w playground",
5556
"dev:use-bundle": "run-p tsup-w dev-pack",
@@ -59,13 +60,13 @@
5960
"build:scss": "sass --no-source-map --style=compressed src/styles/main.scss src/index.css",
6061
"build:postcss": "postcss src/index.css -o dist/index.css",
6162
"build:vite": "vite build && npm run build:css",
62-
"build:storybook": "npm run build:storybook -w playground",
6363
"preview:docs": "npm run docs:serve -w docs",
6464
"lint": "eslint --ext .ts,.tsx,.vue ./src",
6565
"tsup-w": "tsup --watch",
6666
"dev-pack": "npm run dev:usepack -w playground",
6767
"prepare": "chmod a+x .husky/* && husky install",
6868
"prepublishOnly": "npm run build",
69+
"postinstall": "rimraf rf node_modules/@types/react",
6970
"release": "bumpp --commit --tag --push",
7071
"stylelint": "stylelint \"**/*.{css,less,scss,vue}\"",
7172
"test": "vitest",
@@ -84,6 +85,14 @@
8485
"@commitlint/cli": "^17.6.7",
8586
"@commitlint/config-conventional": "^17.6.7",
8687
"@rollup/plugin-babel": "^6.0.3",
88+
"@storybook/addon-essentials": "^7.2.0",
89+
"@storybook/addon-interactions": "^7.2.0",
90+
"@storybook/addon-links": "^7.2.0",
91+
"@storybook/addon-onboarding": "^1.0.8",
92+
"@storybook/blocks": "^7.2.0",
93+
"@storybook/testing-library": "^0.2.0",
94+
"@storybook/vue3": "^7.2.0",
95+
"@storybook/vue3-vite": "^7.2.0",
8796
"@swc/core": "^1.3.73",
8897
"@testing-library/jest-dom": "^5.17.0",
8998
"@testing-library/user-event": "^14.4.3",
@@ -101,13 +110,15 @@
101110
"babel-loader": "^9.1.3",
102111
"browserslist-to-esbuild": "^1.2.0",
103112
"bumpp": "^9.1.1",
113+
"cross-env": "^7.0.3",
104114
"cssnano": "^6.0.1",
105115
"esbuild-plugin-babel": "^0.2.3",
106116
"eslint": "^8.46.0",
107117
"eslint-config-airbnb-base": "^15.0.0",
108118
"eslint-config-airbnb-typescript": "^17.1.0",
109119
"eslint-plugin-import": "^2.28.0",
110120
"eslint-plugin-jsx-a11y": "^6.7.1",
121+
"eslint-plugin-storybook": "^0.6.13",
111122
"eslint-plugin-vue": "^9.16.1",
112123
"husky": "^8.0.3",
113124
"jsdom": "^22.1.0",
@@ -123,11 +134,12 @@
123134
"rimraf": "^5.0.1",
124135
"sass": "^1.64.2",
125136
"shelljs": "^0.8.5",
137+
"storybook": "^7.2.0",
126138
"stylelint": "^15.10.2",
127139
"stylelint-config-recommended-vue": "^1.5.0",
128140
"stylelint-config-standard": "^34.0.0",
129141
"tsup": "^7.1.0",
130-
"typescript": "~5.1.6",
142+
"typescript": "~5.0.4",
131143
"vite": "^4.4.8",
132144
"vite-plugin-dts": "^3.4.0",
133145
"vitest": "^0.34.1",

playground/.storybook/main.js

-36
This file was deleted.

playground/package.json

-16
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,16 @@
33
"version": "1.0.0",
44
"scripts": {
55
"build": "tsc && vue-tsc && vite build",
6-
"build:storybook": "build-storybook",
76
"dev": "vite --host --open",
87
"dev:usepack": "cross-env USEPACK=true vite --host --port 5000 --open",
98
"preview": "vite preview",
10-
"storybook": "start-storybook -p 6006",
119
"typecheck": "tsc && vue-tsc"
1210
},
1311
"dependencies": {
1412
"vue-router": "^4.2.4"
1513
},
1614
"devDependencies": {
17-
"@storybook/addon-actions": "~6.5.16",
18-
"@storybook/addon-docs": "~6.5.16",
19-
"@storybook/addon-essentials": "~6.5.16",
20-
"@storybook/addon-links": "~6.5.16",
21-
"@storybook/builder-vite": "0.4.2",
22-
"@storybook/vue3": "~6.5.16",
2315
"cross-env": "^7.0.3",
2416
"vue-loader": "^17.2.2"
25-
},
26-
"overrides": {
27-
"@storybook/addon-essentials": {
28-
"babel-loader": "^9.1.0"
29-
},
30-
"@storybook/vue3": {
31-
"babel-loader": "^9.1.0"
32-
}
3317
}
3418
}

src/stories/Button.stories.ts

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import type { Meta, StoryObj } from '@storybook/vue3';
2+
3+
import Button from './Button.vue';
4+
5+
// More on how to set up stories at: https://storybook.js.org/docs/vue/writing-stories/introduction
6+
const meta = {
7+
title: 'Example/Button',
8+
component: Button,
9+
// This component will have an automatically generated docsPage entry: https://storybook.js.org/docs/vue/writing-docs/autodocs
10+
tags: ['autodocs'],
11+
argTypes: {
12+
size: { control: 'select', options: ['small', 'medium', 'large'] },
13+
backgroundColor: { control: 'color' },
14+
onClick: { action: 'clicked' },
15+
},
16+
args: { primary: false }, // default value
17+
} satisfies Meta<typeof Button>;
18+
19+
export default meta;
20+
type Story = StoryObj<typeof meta>;
21+
/*
22+
*👇 Render functions are a framework specific feature to allow you control on how the component renders.
23+
* See https://storybook.js.org/docs/vue/api/csf
24+
* to learn how to use render functions.
25+
*/
26+
export const Primary: Story = {
27+
args: {
28+
primary: true,
29+
label: 'Button',
30+
},
31+
};
32+
33+
export const Secondary: Story = {
34+
args: {
35+
primary: false,
36+
label: 'Button',
37+
},
38+
};
39+
40+
export const Large: Story = {
41+
args: {
42+
label: 'Button',
43+
size: 'large',
44+
},
45+
};
46+
47+
export const Small: Story = {
48+
args: {
49+
label: 'Button',
50+
size: 'small',
51+
},
52+
};

src/stories/Button.vue

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<template>
2+
<button
3+
type="button"
4+
:class="classes"
5+
:style="style"
6+
@click="onClick"
7+
>
8+
{{ label }}
9+
</button>
10+
</template>
11+
12+
<script lang="ts" setup>
13+
import './button.scss';
14+
import { computed } from 'vue';
15+
16+
const props = withDefaults(defineProps<{
17+
/**
18+
* The label of the button
19+
*/
20+
label: string,
21+
/**
22+
* primary or secondary button
23+
*/
24+
primary?: boolean,
25+
/**
26+
* size of the button
27+
*/
28+
size?: 'small' | 'medium' | 'large',
29+
/**
30+
* background color of the button
31+
*/
32+
backgroundColor?: string,
33+
34+
}>(), { primary: false });
35+
36+
const emit = defineEmits<{
37+
(e: 'click', id: number): void;
38+
}>();
39+
40+
const classes = computed(() => ({
41+
'storybook-button': true,
42+
'storybook-button--primary': props.primary,
43+
'storybook-button--secondary': !props.primary,
44+
[`storybook-button--${props.size || 'medium'}`]: true,
45+
}));
46+
47+
const style = computed(() => ({
48+
backgroundColor: props.backgroundColor,
49+
}));
50+
51+
const onClick = () => {
52+
emit('click', 1);
53+
};
54+
55+
</script>

0 commit comments

Comments
 (0)