Let's talk about console.log
-driven development. We all do it. So, instead of fighting it, let's upgrade it with Storybook Actions.
Storybook actions are a feature of Storybook 6 and 7.
There's an
Knowing that a change is on the horizon, let's focus on the features of the current API that will remain (or migrate easily with codemods).
In a fresh Storybook 7 installation (
The included Button stories log the common event — onClick
— when clicked. Logs can then be expanded to show full event details.
Heading stories also log events but this time with custom events onLogin
and onCreateAccount
.
These events are logged when the respective buttons are clicked.
Actions pair perfectly with
Let's define actions for the example Button component.
meta
.argTypes
property if one doesn't exist.onClick
property (this needs to match the component prop name).{ action: "clicked" }
. (The logged text can be anything we like.)// Button.stories.tsx
// (surronding code omitted)
const meta = {
title: "Example/Button",
component: Button,
tags: ["autodocs"],
argTypes: {
backgroundColor: { control: "color" },
onClick: { action: "onClick" },
},
} satisfies Meta<typeof Button>;
Now click your button in Storybook and watch the Actions log!
Let's add an interaction to the example Button component.
play
function to the story object.canvasElement
, find the button.// Button.stories.tsx
// (surronding code omitted)
export const Primary: Story = {
args: {
primary: true,
label: "Button",
},
play: async ({ canvasElement }) => {
const canvas = within(canvasElement);
const button = await canvas.getByRole("button", {
name: /Button/i,
});
await userEvent.click(button);
},
};
Now the story will run the interaction when it loads. Logging both the interaction and the action — in their respective tabs.
console.log
-driven development is something I do when driving out a component implementation. It's a quick, cheap way to ensure that events are hooked up (early in the process).
But log statements has one huge drawback: they live in component code. And I'm guilty of pushing a log (or a few dozen) into production.
Storybook actions let you validate implementation in story files, leaving component code alone.
Also published here.