图片、字体和资源
组件通常依赖于图片、视频、字体和其他资源来按用户预期进行渲染。在你的 story 文件中有许多使用这些资源的方法。
将资源导入到 stories 中
你可以通过导入(或请求)任何媒体资源来导入它们。我们的默认配置开箱即用。但是,如果你使用的是自定义 webpack 配置,你将需要添加 file loader 来处理请求的文件。
之后,你就可以在你的 stories 中使用任何资源了
// Replace your-framework with the framework you are using, e.g. react-vite, nextjs, nextjs-vite, etc.
import type { Meta, StoryObj } from '@storybook/your-framework';
import imageFile from './static/image.png';
import { MyComponent } from './MyComponent';
const meta = {
component: MyComponent,
} satisfies Meta<typeof MyComponent>;
export default meta;
type Story = StoryObj<typeof meta>;
const image = {
src: imageFile,
alt: 'my image',
};
export const WithAnImage: Story = {
render: () => <img src={image.src} alt={image.alt} />,
};通过 Storybook 配置提供静态文件
我们建议通过 Storybook 提供静态文件,以确保你的组件始终拥有加载它们所需的资源。我们推荐这种技术用于组件经常使用的资源,例如徽标、字体和图标。
在启动 Storybook 时配置一个包含资源所在的目录(或目录列表)。在你的主 Storybook 配置文件(例如 .storybook/main.js|ts)中使用 staticDirs 配置项来指定目录。
// Replace your-framework with the framework you are using, e.g. react-vite, nextjs, vue3-vite, etc.
import type { StorybookConfig } from '@storybook/your-framework';
const config: StorybookConfig = {
framework: '@storybook/your-framework',
stories: ['../src/**/*.mdx', '../src/**/*.stories.@(js|jsx|mjs|ts|tsx)'],
staticDirs: ['../public', '../static'],
};
export default config;这里的 ../public 是你的静态目录。现在可以在组件或 story 中像这样使用它。
// Replace your-framework with the framework you are using, e.g. react-vite, nextjs, nextjs-vite, etc.
import type { Meta, StoryObj } from '@storybook/your-framework';
import { MyComponent } from './MyComponent';
const meta = {
component: MyComponent,
} satisfies Meta<typeof MyComponent>;
export default meta;
type Story = StoryObj<typeof meta>;
// Assume image.png is located in the "public" directory.
export const WithAnImage: Story = {
render: () => <img src="/image.png" alt="my image" />,
};你也可以传递一个用逗号分隔(无空格)的目录列表,而不是单个目录。
// Replace your-framework with the framework you are using, e.g. react-vite, nextjs, vue3-vite, etc.
import type { StorybookConfig } from '@storybook/your-framework';
const config: StorybookConfig = {
framework: '@storybook/your-framework',
stories: ['../src/**/*.mdx', '../src/**/*.stories.@(js|jsx|mjs|ts|tsx)'],
staticDirs: ['../public', '../static'],
};
export default config;甚至可以使用配置对象来定义目录
// Replace your-framework with the framework you are using, e.g. react-vite, nextjs, vue3-vite, etc.
import type { StorybookConfig } from '@storybook/your-framework';
const config: StorybookConfig = {
framework: '@storybook/your-framework',
stories: ['../src/**/*.mdx', '../src/**/*.stories.@(js|jsx|mjs|ts|tsx)'],
staticDirs: [{ from: '../my-custom-assets/images', to: '/assets' }],
};
export default config;从 CDN 引用资源
将文件上传到在线 CDN 并引用它们。在此示例中,我们正在使用一个占位符图像服务。
// Replace your-framework with the framework you are using, e.g. react-vite, nextjs, nextjs-vite, etc.
import type { Meta, StoryObj } from '@storybook/your-framework';
import { MyComponent } from './MyComponent';
const meta = {
component: MyComponent,
} satisfies Meta<typeof MyComponent>;
export default meta;
type Story = StoryObj<typeof meta>;
export const WithAnImage: Story = {
render: () => (
<img src="https://storybook.org.cn/images/placeholders/350x150.png" alt="My CDN placeholder" />
),
};绝对路径与相对路径
有时,你可能想将你的 Storybook 部署到一个子路径,例如 https://example.com/storybook。
在这种情况下,你需要让所有图片和媒体文件使用相对路径。否则,浏览器将无法找到这些文件。
如果你通过导入加载静态内容,这是自动完成的,你不需要做任何事情。
如果你在 静态目录 中与 Storybook 一起提供资源,那么你需要使用相对路径来加载图片或使用 base 元素。
在 Stories 中引用字体
在配置 Storybook 从静态文件夹提供资源后,你可以在 Storybook 中引用这些资源。例如,你可以引用自定义字体并将其应用于你的 stories。为此,在配置目录(即 .storybook)内创建一个 preview-head.html 文件,并添加一个 <link /> 标签来引用你的字体。
<!--
Pull in static files served from your Static directory or the internet
Example:
`main.js|ts` is configured with staticDirs: ['../public'] and your font is located in the `fonts`
directory inside your `public` directory
-->
<link rel="preload" href="/fonts/my-font.woff2" />
<!-- Or you can load custom head-tag JavaScript: -->
<script src="https://use.typekit.net/xxxyyy.js"></script>
<script>
try {
Typekit.load();
} catch (e) {}
</script>