背景擴充功能可讓您從工具列中預先定義的背景顏色清單中選擇,來變更套用到 Story 的背景顏色。如有需要,您可以使用 parameters.backgrounds.default 參數設定 Story 預設為特定背景顏色。
Button.stories.ts|tsx
// Replace your-framework with the name of your frameworkimport type { Meta, StoryObj } from '@storybook/your-framework';import { Button } from './Button';const meta: Meta<typeof Button> = { component: Button, parameters: { backgrounds: { // 👇 Set default background value for all component stories default: 'Gray', }, },};export default meta;type Story = StoryObj<typeof Button>;export const OnDark: Story = { parameters: { backgrounds: { // 👇 Override default background value for this story default: 'Dark', }, },};
顧名思義,此方法只會設定 Story 的預設背景顏色。您在檢視 Story 時仍然可以使用工具列變更背景顏色。
當您使用 globals 為 Story (或組件的 Stories) 指定背景顏色時,將會套用背景顏色,且無法使用工具列變更。當您想要確保 Story 始終在特定背景顏色上渲染時,這會很有用。
Button.stories.ts|tsx
// Replace your-framework with the name of your frameworkimport type { Meta, StoryObj } from '@storybook/your-framework';import { Button } from './Button';const meta: Meta<typeof Button> = { component: Button, globals: { // 👇 Set background value for all component stories backgrounds: { value: 'gray', grid: false }, },};export default meta;type Story = StoryObj<typeof Button>;export const OnDark: Story = { globals: { // 👇 Override background value for this story backgrounds: { value: 'dark' }, },};
// Replace your-framework with the name of your frameworkimport type { Meta } from '@storybook/your-framework';import { Button } from './Button';// To apply a set of backgrounds to all stories of Button:const meta: Meta<typeof Button> = { component: Button, parameters: { backgrounds: { grid: { cellSize: 20, opacity: 0.5, cellAmount: 5, offsetX: 16, // Default is 0 if story has 'fullscreen' layout, 16 if layout is 'padded' offsetY: 16, // Default is 0 if story has 'fullscreen' layout, 16 if layout is 'padded' }, }, },};export default meta;