Skip to content

johnf/storybook-addon-jotai

Repository files navigation

Storybook Jotai Addon

A Storybook Addon and Decorator for Jotai and track the state in a Panel.

TODO: Add screenshot here

Install

yarn add -D storybook-addon-jotai

Register the addon in .storybook/main.js

module.exports = {
  stories: ['../stories/**/*.stories.tsx'],
  addons: ['storybook-addon-jotai'],
};

Usage

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 = {};

Development scripts

  • yarn start runs babel in watch mode and starts Storybook
  • yarn build build and package your addon code

Release Management

Creating a release

Automatically created when pushing to GitHub