storybook-rtl-addon
一个 Storybook 插件,允许用户在 ltr 和 rtl 之间切换。
安装
安装以下 npm 模块
npm i --save-dev storybook-rtl-addon
或使用 yarn
yarn add -D storybook-rtl-addon
然后,将以下内容添加到 .storybook/main.js 中
module.exports = {
stories: ['../**/*.stories.[tj]sx'],
addons: ['storybook-rtl-addon'],
};
故事集成
使用 hook 获取当前方向
import { useDirection } from 'storybook-rtl-addon';
function MyComponent(context) {
const direction = useDirection(context);
return <div dir={direction}>{children}</div>;
}
或通过 addDecorator 使用它
import { addDecorator } from '@storybook/react';
import { withDirection } from 'storybook-rtl-addon';
addDecorator(withDirection);
您也可以像下面这样监听方向更改事件
import addons from '@storybook/addons';
import { Direction_MODE_EVENT_NAME } from 'storybook-rtl-addon';
// get channel to listen to event emitter
const channel = addons.getChannel();
// create a component that listens for the event change
function MyComponent({ children }) {
// this example uses hook but you can also use class component as well
const [direction, setDirection] = useState();
useEffect(() => {
// listen to direction change
channel.on(Direction_MODE_EVENT_NAME, setDirection);
return () => channel.off(Direction_MODE_EVENT_NAME, setDirection);
}, [channel, setDirection]);
return <div dir={direction}>{children}</div>;
}
选项
- setDirectionKnob
setDirectionKnob
当设置为 true 时,将设置 knob-direction
。
// preview.js
export const parameters = {
setLocaleToKnob: true,
};