Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Story Args and Controls | Writing and Organizing Stories
Building UI Systems in React with Storybook

bookStory Args and Controls

When you want to make your stories in Storybook interactive and customizable, you use Args and Controls. Args allow you to define the values that get passed as props to your React components in each story. This means you can write a single story and let users change the props in real time through the Storybook UI, without having to rewrite or duplicate story code for every possible prop combination.

Controls are the part of the Storybook UI that let users interact with these args. When you define args for your story, Storybook automatically generates UI controlsβ€”like text fields, color pickers, or dropdownsβ€”based on the type of each arg. This way, you and your teammates can experiment with different prop values, see the results instantly, and better understand how your components behave in different scenarios.

Suppose you have a simple Button component that takes label and backgroundColor as props. You can write a story that uses args to make both of these props adjustable from the Storybook UI. Here's what the code for such a story might look like:

import React from "react";
import { Button } from "./Button";

export default {
  title: "Example/Button",
  component: Button,
  argTypes: {
    backgroundColor: { control: "color" },
    label: { control: "text" },
  },
};

const Template = (args) => <Button {...args} />;

export const Primary = Template.bind({});
Primary.args = {
  label: "Click me",
  backgroundColor: "#1ea7fd",
};

With this setup, when you view the Primary button story in Storybook, you'll see controls for both the label and backgroundColor props. You can type a new label or pick a different color, and the button will update instantly in the preview pane. This makes it easy to test your component's flexibility and appearance, and it helps designers and developers collaborate more effectively.

Note
Study More

For advanced usage and more examples, visit the Storybook Controls documentation.

question mark

Which statement best describes how Args and Controls work together in Storybook?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 2

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

bookStory Args and Controls

Swipe to show menu

When you want to make your stories in Storybook interactive and customizable, you use Args and Controls. Args allow you to define the values that get passed as props to your React components in each story. This means you can write a single story and let users change the props in real time through the Storybook UI, without having to rewrite or duplicate story code for every possible prop combination.

Controls are the part of the Storybook UI that let users interact with these args. When you define args for your story, Storybook automatically generates UI controlsβ€”like text fields, color pickers, or dropdownsβ€”based on the type of each arg. This way, you and your teammates can experiment with different prop values, see the results instantly, and better understand how your components behave in different scenarios.

Suppose you have a simple Button component that takes label and backgroundColor as props. You can write a story that uses args to make both of these props adjustable from the Storybook UI. Here's what the code for such a story might look like:

import React from "react";
import { Button } from "./Button";

export default {
  title: "Example/Button",
  component: Button,
  argTypes: {
    backgroundColor: { control: "color" },
    label: { control: "text" },
  },
};

const Template = (args) => <Button {...args} />;

export const Primary = Template.bind({});
Primary.args = {
  label: "Click me",
  backgroundColor: "#1ea7fd",
};

With this setup, when you view the Primary button story in Storybook, you'll see controls for both the label and backgroundColor props. You can type a new label or pick a different color, and the button will update instantly in the preview pane. This makes it easy to test your component's flexibility and appearance, and it helps designers and developers collaborate more effectively.

Note
Study More

For advanced usage and more examples, visit the Storybook Controls documentation.

question mark

Which statement best describes how Args and Controls work together in Storybook?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 2
some-alt