Args
觀看影片教學
故事是一個元件,具有一組定義元件應如何渲染的引數。「Args」是 Storybook 用於在單一 JavaScript 物件中定義這些引數的機制。Args 可用於動態變更 props、slots、樣式、輸入等。它允許 Storybook 及其附加元件即時編輯元件。您不需要修改底層元件程式碼即可使用 args。
當 arg 的值變更時,元件會重新渲染,讓您可以透過影響 args 的附加元件,在 Storybook 的 UI 中與元件互動。
了解如何以及為何在簡介中撰寫故事。如需 args 如何運作的詳細資訊,請繼續閱讀。
Args 物件
args
物件可以在故事、元件和全域層級定義。它是一個 JSON 可序列化的物件,由字串索引鍵組成,這些索引鍵具有相符的有效值類型,可以傳遞至您框架的元件。
故事 args
若要定義單一故事的 args,請使用 args
CSF 故事索引鍵
import type { Meta, StoryObj } from '@storybook/react';
import { Button } from './Button';
const meta: Meta<typeof Button> = {
component: Button,
};
export default meta;
type Story = StoryObj<typeof Button>;
export const Primary: Story = {
args: {
primary: true,
label: 'Button',
},
};
這些 args 將僅適用於它們附加到的故事,但您可以透過 JavaScript 物件重複使用來重複使用它們
// Replace your-framework with the name of your framework
import type { Meta, StoryObj } from '@storybook/your-framework';
import { Button } from './Button';
const meta: Meta<typeof Button> = {
component: Button,
};
export default meta;
type Story = StoryObj<typeof Button>;
export const Primary: Story = {
args: {
primary: true,
label: 'Button',
},
};
export const PrimaryLongName: Story = {
args: {
...Primary.args,
label: 'Primary with a really long name',
},
};
在上面的範例中,我們使用 ES 2015 的物件擴展功能。
元件 args
您也可以在元件層級定義 args;它們將適用於元件的所有故事,除非您覆寫它們。若要執行此操作,請在 default
CSF 匯出上使用 args
索引鍵
import type { Meta } from '@storybook/react';
import { Button } from './Button';
const meta: Meta<typeof Button> = {
component: Button,
//👇 Creates specific argTypes
argTypes: {
backgroundColor: { control: 'color' },
},
args: {
//👇 Now all Button stories will be primary.
primary: true,
},
};
export default meta;
type Story = StoryObj<typeof Button>;
全域 args
您也可以在全域層級定義 args;它們將適用於每個元件的故事,除非您覆寫它們。若要執行此操作,請在 preview.js
的預設匯出中定義 args
屬性
// Replace your-renderer with the renderer you are using (e.g., react, vue3, angular, etc.)
import { Preview } from '@storybook/your-renderer';
const preview: Preview = {
// The default value of the theme arg for all stories
args: { theme: 'light' },
};
export default preview;
對於大多數全域 args 的使用案例,全域設定是更好的工具,可定義全域套用的設定,例如佈景主題。使用全域設定可讓使用者透過工具列選單變更值。
Args 組件
您可以將故事的引數分開,以在其他故事中組成。以下是如何組合相同元件的多個故事的 args。
// Replace your-framework with the name of your framework
import type { Meta, StoryObj } from '@storybook/your-framework';
import { Button } from './Button';
const meta: Meta<typeof Button> = {
component: Button,
};
export default meta;
type Story = StoryObj<typeof Button>;
export const Primary: Story = {
args: {
primary: true,
label: 'Button',
},
};
export const Secondary: Story = {
args: {
...Primary.args,
primary: false,
},
};
如果您發現自己為元件的大部分故事重複使用相同的 args,則應考慮使用元件層級 args。
當為由其他元件組成的複合元件撰寫故事時,Args 非常有用。複合元件通常會將其引數原封不動地傳遞給其子元件,類似地,它們的故事也可以是其子元件故事的組合。透過 args,您可以直接組合引數
import type { Meta, StoryObj } from '@storybook/react';
import { Page } from './Page';
//👇 Imports all Header stories
import * as HeaderStories from './Header.stories';
const meta: Meta<typeof Page> = {
component: Page,
};
export default meta;
type Story = StoryObj<typeof Page>;
export const LoggedIn: Story = {
args: {
...HeaderStories.LoggedIn.args,
},
};
Args 可以修改元件的任何層面
您可以在故事中使用 args 來設定元件的外觀,類似於在應用程式中會執行的操作。例如,以下說明如何使用 footer
arg 來填入子元件
import type { Meta, StoryObj } from '@storybook/react';
import { Page } from './Page';
type PagePropsAndCustomArgs = React.ComponentProps<typeof Page> & { footer?: string };
const meta: Meta<PagePropsAndCustomArgs> = {
component: Page,
render: ({ footer, ...args }) => (
<Page {...args}>
<footer>{footer}</footer>
</Page>
),
};
export default meta;
type Story = StoryObj<PagePropsAndCustomArgs>;
export const CustomFooter: Story = {
args: {
footer: 'Built with Storybook',
},
};