Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor Storybook <Button /> #213

Merged
merged 12 commits into from
Dec 7, 2020
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ module.exports = {
"plugin:import/typescript",
"plugin:react/recommended",
"plugin:jest/recommended",
"plugin:mdx/recommended",
],
parser: "@typescript-eslint/parser",
parserOptions: {
Expand Down Expand Up @@ -166,5 +167,9 @@ module.exports = {
"@typescript-eslint/no-var-requires": "off",
},
},
{
files: ["*.mdx"],
extends: ["plugin:mdx/overrides"],
},
],
};
36 changes: 26 additions & 10 deletions .scaffdog/component.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,19 +61,35 @@ describe("{{ input | pascal }} component testing", () => {

```

# `{{ input }}/{{ input }}.stories.tsx`
# `{{ input | pascal }}/{{ input | pascal }}.stories.mdx`

```typescript
import * as React from "react";
import {{ input }} from "./index";
import { Meta, Story, ArgsTable, Canvas } from "@storybook/addon-docs/blocks";
import {{ input | pascal }} from "./index";

export default {
title: "{{ input }}",
component: {{ input }},
};
<Meta
title="{{ input | pascal }}"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

めちゃくちゃ細かいんだけど

https://storybook.js.org/docs/react/writing-docs/mdx#writing-stories

<Meta
    title=""
>

のtitleのところを例えば Component/{{ input | pascal }} みたいにディレクトリ切る感じで書いていくと

スクリーンショット 2020-12-07 15 03 03

こんな感じ(ここだと <Meta title="MDX/Badge">)になるんだけど、分けていった方がみやすいかな?って思ったんだけどどうでしょう!

  • Component
  • Layout

とかくらいに分かれるかなーっていうイメージ

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ああ、全然良いと思います:+1:

別PRでやりましょうか !!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

そうしましょう!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

この辺にちょろっと書いた
#212 (comment)

component={ {{ input | pascal }} }
argTypes={
{ onClick: { action: "clicked" } }
}
Comment on lines +73 to +75
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/>

export const Overview = () => (
<{{ input }} />
);
# {{ input | pascal }}

<ArgsTable of={ {{ input | pascal }} } />

## Samples

### Something

<Canvas>
<Story
name="something"
args={{}}
Copy link
Contributor Author

@youchann youchann Dec 7, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

実際に生成するとargs= みたいな感じで、何も書かれない。
args={{{{}}}}みたいに書くとシンタックスエラーみたいなのが出る

>
{(args) => <{{ input | pascal }} {...args} />}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

<!--- This is your Story template function, shown here in React -->

export const Template = (args) => <Badge {...args } />

Buttonのやつみる限り、複数回 {(args) => <{{ input | pascal }} {...args} /> これを書いてるから、{Template.bind({})} の方がちょっと楽かな?と思い!
使わなかった理由があったら教えて欲しい!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

良い質問ですね!!(書き忘れていた)

時間あれば手元でやっていただけると嬉しいのですが、出力されるコピペ可能なコードにおいて、Buttonというコンポーネント名で出力されなかったんですよね.....
なのでベタがきしている。といったかんじです!

image

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

自分の手元で再現してみた。

image

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

なるほど、MDXCreateElementは export const Template = (args) => <Badge {...args } /> で出てくるのか!
だったら PreviewのところはちゃんとしたComponent名で出て欲しいから、この記法の方が良さそうだね!

</Story>
</Canvas>

```
13 changes: 9 additions & 4 deletions .storybook/main.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
module.exports = {
stories: ["../src/components/**/*.stories.tsx"],
stories: [
"../src/components/**/*.stories.tsx",
"../src/components/**/*.stories.mdx",
],
addons: [
"@storybook/addon-actions",
"@storybook/addon-essentials",
"@storybook/addon-knobs",
"@storybook/addon-notes",
// TODO: Remove this addon after replace to `.mdx`.
"@storybook/addon-storysource",
],
reactOptions: {
fastRefresh: true,
strictMode: true,
// TODO: fix warn "Rendered more hooks than during the previous render."
// knobを完全に排除できたタイミングで再度調査
// strictMode: true,
Comment on lines +14 to +16
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

memo

},
};
2 changes: 2 additions & 0 deletions .storybook/preview.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import * as React from "react";
import { ThemeProvider, createTheme } from "../src/themes";
import "@storybook/addon-console";
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.



const theme = createTheme();

Expand Down
13 changes: 8 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"deploy": "gh-pages -d example/build",
"storybook": "start-storybook -s ./assets -p 6006",
"build-storybook": "build-storybook",
"lint": "eslint 'src/**/*.{ts,tsx}'",
"lint": "eslint 'src/**/*.{ts,tsx,mdx}'",
"generate": "yarn scaffdog generate",
"format": "yarn lint --fix"
},
Expand All @@ -40,10 +40,10 @@
"styled-components": ">= 5.X"
},
"devDependencies": {
"@storybook/addon-actions": "^6.1.6",
"@storybook/addon-knobs": "^6.1.6",
"@storybook/addon-notes": "^5.3.21",
"@storybook/addon-storysource": "^6.1.6",
"@storybook/addon-console": "^1.2.2",
"@storybook/addon-essentials": "^6.1.9",
"@storybook/addon-knobs": "^6.1.9",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

こいつ残してるのは、前のやつとの互換とるためかな?
knobsとessentialsに含まれてるcontrolsがほぼ同じやつだと認識してるんだけど、もしかして理解足りてない...?w

Copy link
Contributor Author

@youchann youchann Dec 7, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

そうですね。前のやつと互換を取るためです!

knobsとessentialsに含まれてるcontrolsがほぼ同じやつだと認識

合ってます!がライブラリとしては別モノなので、一気に乗り換えるためには修正は必要でして。。。
https://github.com/storybookjs/storybook/tree/next/addons/controls#how-do-i-migrate-from-addon-knobs

いずれ全て変わるので、一気にやらなくても良いかなという判断です!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

"@storybook/addon-storysource": "^6.1.10",
"@storybook/react": "^6.1.6",
"@svgr/rollup": "^2.4.1",
"@testing-library/jest-dom": "^5.11.0",
Expand All @@ -64,12 +64,15 @@
"eslint-config-prettier": "^6.15.0",
"eslint-plugin-import": "^2.22.1",
"eslint-plugin-jest": "^24.1.3",
"eslint-plugin-mdx": "^1.8.2",
"eslint-plugin-prettier": "^3.1.4",
"eslint-plugin-react": "^7.21.5",
"eslint-plugin-react-hooks": "^4.2.0",
"gh-pages": "^1.2.0",
"jest": "^26.1.0",
"prettier": "^2.1.2",
"react": ">= 16.6.1",
"react-dom": ">= 16.6.1",
"rollup": "^2.33.3",
"rollup-plugin-commonjs": "^10.1.0",
"rollup-plugin-imagemin": "^0.4.1",
Expand Down
3 changes: 2 additions & 1 deletion src/components/ActionButton/ActionButton.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ function createButtons(props: Props[], text = "ボタン") {

export default {
title: "ActionButton",
component: ActionButton,
parameters: {
component: ActionButton,
docs: { page: null },
},
};

Expand Down
3 changes: 3 additions & 0 deletions src/components/Backdrop/Backdrop.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import Button from "../Button";
export default {
title: "Backdrop",
component: Backdrop,
parameters: {
docs: { page: null },
},
Comment on lines +10 to +12
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

現行の.stories.tsxはDocsタブ常にデフォルトのものが表示されるが、

  • 情報量が足りない
  • コンポーネントによっては見辛い

ということで共通で見れないようにした。

};

export const Overview: React.FunctionComponent = () => {
Expand Down
3 changes: 2 additions & 1 deletion src/components/Badge/Badge.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ const Column = styled.div`

export default {
title: "Badge",
component: Badge,
parameters: {
component: Badge,
docs: { page: null },
},
};

Expand Down
126 changes: 126 additions & 0 deletions src/components/Button/Button.stories.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
import { Meta, Story, ArgsTable, Canvas } from "@storybook/addon-docs/blocks";
import Button from "./Button";

<Meta
title="Button"
component={Button}
argTypes={{ onClick: { action: "clicked" } }}
/>

# Button

<ArgsTable of={Button} />

## Samples

### Colors

<Canvas>
<Story
name="primary"
args={{
color: "primary",
children: "primary",
}}
>
{(args) => <Button {...args} />}
</Story>
<Story
name="secondary"
args={{
color: "secondary",
children: "secondary",
}}
>
{(args) => <Button {...args} />}
</Story>
Comment on lines +18 to +36
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.map()で書きたいが、今の所かけない。

ref: storybookjs/storybook#8392

<Story
name="danger"
args={{
color: "danger",
children: "danger",
}}
>
{(args) => <Button {...args} />}
</Story>
</Canvas>

### Sizes

<Canvas>
<Story
name="small"
args={{
size: "small",
children: "small",
}}
>
{(args) => <Button {...args} />}
</Story>
<Story
name="medium"
args={{
size: "medium",
children: "medium",
}}
>
{(args) => <Button {...args} />}
</Story>
<Story
name="large"
args={{
size: "large",
children: "large",
}}
>
{(args) => <Button {...args} />}
</Story>
</Canvas>

### Disabled

<Canvas>
<Story
name="disabled"
args={{
disabled: true,
children: "disabled",
}}
>
{(args) => <Button {...args} />}
</Story>
</Canvas>

### Use "href" props

<Canvas>
<Story
name="Use href props"
args={{
href: "https://google.com",
children: "Link to google.com",
}}
>
{(args) => <Button {...args} />}
</Story>
</Canvas>

### Use with router library like "react-router"

Here's an example code.

But We have plan to change like [Material-UI](https://material-ui.com/guides/composition/#link).

```tsx
// Get "className" prop to enable <Button /> style.
const LinkToHome = ({ className, children }) => (
// "href" prop defines not via <Button />
<Link className={className} href="/home">
{children}
</Link>
);

const ButtonWithRouterLink = ({ children }) => (
<Button component={LinkToHome}>{children}</Button>
);
```
Loading