文件
Storybook 文件

圖片、字型和資源

元件通常依賴圖片、影片、字型和其他資源來按照使用者預期的方式呈現。在您的 story 檔案中,有很多方法可以使用這些資源。

將資源導入 stories

您可以透過導入(或請求)任何媒體資源來導入它們。它與我們的預設配置開箱即用。但是,如果您使用自訂 webpack 配置,則需要新增 file loader 來處理所需的檔案。

之後,您可以在您的 stories 中使用任何資源

MyComponent.stories.ts|tsx
import type { Meta, StoryObj } from '@storybook/react';
 
import imageFile from './static/image.png';
 
import { MyComponent } from './MyComponent';
 
const meta: Meta<typeof MyComponent> = {
  component: MyComponent,
};
 
export default meta;
type Story = StoryObj<typeof MyComponent>;
 
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 配置元素來指定目錄

.storybook/main.ts
// Replace your-framework with the framework you are using (e.g., react-webpack5, vue3-vite)
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 中使用它。

MyComponent.stories.ts|tsx
import type { Meta, StoryObj } from '@storybook/react';
 
import { MyComponent } from './MyComponent';
 
const meta: Meta<typeof MyComponent> = {
  component: MyComponent,
};
 
export default meta;
type Story = StoryObj<typeof MyComponent>;
 
// Assume image.png is located in the "public" directory.
export const WithAnImage: Story = {
  render: () => <img src="/image.png" alt="my image" />,
};

您也可以傳遞以逗號分隔且不含空格的目錄列表,而不是單個目錄。

.storybook/main.ts
// Replace your-framework with the framework you are using (e.g., react-webpack5, vue3-vite)
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;

甚至可以使用配置物件來定義目錄

.storybook/main.ts
// Replace your-framework with the framework you are using (e.g., react-webpack5, vue3-vite)
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 並引用它們。在本範例中,我們使用佔位符圖片服務。

MyComponent.stories.ts|tsx
import type { Meta, StoryObj } from '@storybook/react';
 
import { MyComponent } from './MyComponent';
 
const meta: Meta<typeof MyComponent> = {
  component: MyComponent,
};
 
export default meta;
type Story = StoryObj<typeof MyComponent>;
 
export const WithAnImage: Story = {
  render: () => (
    <img src="https://storybook.dev.org.tw/images/placeholders/350x150.png" alt="My CDN placeholder" />
  ),
};

絕對路徑與相對路徑

有時,您可能想要將您的 Storybook 部署到子路徑中,例如 https://example.com/storybook

在這種情況下,您需要讓所有圖片和媒體檔案都使用相對路徑。否則,瀏覽器將無法找到這些檔案。

如果您透過導入來載入靜態內容,這是自動的,您無需執行任何操作。

假設您正在 靜態目錄 中與您的 Storybook 一起提供資源。在這種情況下,您需要使用相對路徑來載入圖片或使用 base 元素。

在 Stories 中引用字型

在設定 Storybook 從您的靜態資料夾提供資源後,您可以在 Storybook 中引用這些資源。例如,您可以引用自訂字型並將其應用於您的 stories。若要執行此操作,請在配置目錄(即 .storybook)內建立 preview-head.html 檔案,並新增 <link /> 標籤以引用您的字型。

.storybook/preview-head.html
<!--
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>