Storybook Jotai 附加组件
一个用于 Storybook 的附加组件和装饰器,用于 Jotai,并在面板中跟踪状态。
待办事项:在此处添加屏幕截图
安装
yarn add -D storybook-addon-jotai
在 .storybook/main.js
中注册附加组件
module.exports = {
stories: ['../stories/**/*.stories.tsx'],
addons: ['storybook-addon-jotai'],
};
用法
给定一个简单的组件
import { useAtom, atom } from 'jotai';
const userAtom = atom(null);
export const User = () => {
const [user] = useAtom(userAtom);
return (
<div>
{user ? (
<div>
<div>{`Logged in as ${user.name}`}</div>
<Button size="small" label="Log out" onClick={() => setUser(null)} />
</div>
) : (
<div>
<div>No one is signed in</div>
<Button size="small" label="Log in" onClick={() => setUser({ name: 'John' })}/>
</div>
)}
</div>
);
};
您可以编写一个故事,例如
import { withJotai } from 'storybook-addon-jotai';
import { User, userAtom } from '../components/User';
import { Header } from './Header';
export default {
title: 'Example/User',
component: User,
decorators: [withJotai],
};
const Template = (args) => <User {...args} />;
export const JohnLoggedIn = Template.bind({});
JohnLoggedIn.parameters = {
jotai: {
atoms: {
user: userAtom,
},
values: {
user: {
name: 'John Doe',
},
},
},
};
export const JaneLoggedIn = Template.bind({});
JaneLoggedIn.parameters = {
jotai: {
atoms: {
user: userAtom,
},
values: {
user: {
name: 'Jane Doe',
},
},
},
};
export const LoggedOut = Template.bind({});
LoggedOut.args = {};
开发脚本
yarn start
在监视模式下运行 babel 并启动 Storybookyarn build
构建和打包您的附加组件代码
发布管理
创建发布
推送到 GitHub 时自动创建