Storybook RTL 方向插件
一个 Storybook 工具插件,用于切换 HTML 的 dir 属性(在 LTR 和 RTL 之间)。
- dir="ltr"或- dir="rtl"样式将被添加到- html标签中,工具栏区域会显示- LTR/RTL图标。
安装
首先,安装软件包。
npm install --save-dev sb-rtl-direction-addon
# using yarn
yarn add --dev sb-rtl-direction-addon
# using pnpm
pnpm add --save-dev sb-rtl-direction-addon
然后,在 .storybook/main.js 中将其注册为插件。
// .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 = {
  // ...rest of config
  addons: [
    '@storybook/addon-essentials'
    'sb-rtl-direction-addon', // 👈 register the addon here
  ],
};
export default config;
用法
该插件将在 Storybook UI 的工具栏部分可用。
结合本地化
您可以与本地化同步以设置默认方向。
// preview.js
export const globalTypes = {
  locale: {
    name: "Locale",
    description: "Internationalization locale",
    defaultValue: "en",
    toolbar: {
      icon: "globe",
      items: [
        { value: "en-US", right: "LTR", title: "English (United States)" },
        { value: "es", right: "LTR", title: "Spanish" },
        { value: "ar", right: "RTL", title: "Arabic" },
        { value: "ar-OM", right: "RTL", title: "Arabic (Oman)" },
        { value: "pa-IN", right: "LTR", title: "Punjabi (India)" },
        { value: "pa-PK", right: "RTL", title: "Punjabi (Pakistan)" },
      ],
    },
  },
};
export const globals = {
  rtlDirection: false,
};
export const parameters = {
  rtlDirection: {
    // Collection to set as RTL (You can add language or with add country code specifically)
    autoLocales: ["ar", "pa-PK"],
    // Condition to reload the page each time locale is updated
    reload: true,
  },
};