A Storybook Addon and Decorator for Jotai and track the state in a Panel.
TODO: Add screenshot here
yarn add -D storybook-addon-jotai
Register the addon in .storybook/main.js
module.exports = {
stories: ['../stories/**/*.stories.tsx'],
addons: ['storybook-addon-jotai'],
};
Given a simple component:
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>
);
};
You can write a story as
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
runs babel in watch mode and starts Storybookyarn build
build and package your addon code
Automatically created when pushing to GitHub