I want to get this:
My stories.tsx code looks like this:
export default {
title: "Components/Switch",
argTypes: {
color: { control: "select", options: ["primary", "secondary"] },
},
};
However, the page just fails to render when I do that.
To reproduce:
Clone this repository: https://github.com/jauggy/storybook-args-error
npm i
npm run storybook
Select the Switch component on the left menu.
To use the Controls addon, you need to write your stories using args. Storybook will automatically generate UI controls based on your args and what it can infer about your component. Still, you can configure the controls further using argTypes, see below.
ArgTypes are a first-class feature in Storybook for specifying the behaviour of Args. By specifying the type of an arg, you constrain the values that it can take and provide information about args that are not explicitly set (i.e., not required).
Storybook Addon Knobs allow you to edit props dynamically using the Storybook UI. You can also use Knobs as a dynamic variable inside stories in Storybook. Framework Support. This is what Knobs looks like: Checkout the above Live Storybook or watch this video.
Storybook Controls Addon Storybook Controls gives you a graphical UI to interact with a component's arguments dynamically, without needing to code. It creates an addon panel next to your component examples ("stories"), so you can edit them live.
Storybook stories (component examples) are functions that return a rendered component. Control values are passed to your Story functions as arguments, and you can also declare the initial values succinctly:
Storybook stories (component examples) are functions that return a rendered component. Control values are passed to your Story functions as arguments, and you can also declare the initial values succinctly: Controls works with Docs, Storybook’s automated documentation generator for UI components, including both DocsPage and MDX.
Or bootstrap Storybook into an existing app: To create your first Controls story, create a function that takes an Args object as its first input, and then annotate the function with the data you want to receive:
you should send an object to the control
property. Like this:
export default {
title: "Components/Switch",
argTypes: {
color: { control: { type: "select", options: ["primary", "secondary"] },
},
};
Update after Storybook v7 control.options will be deprecated for more info go to: https://github.com/storybookjs/storybook/blob/next/MIGRATION.md#deprecated-controloptions
You could also provide there an enum
:
const SwitchTypeEnum {
PRIMARY = "primary",
SECONDARY = "secondary",
}
export default {
title: "Components/Switch",
argTypes: {
table: {
summary: "SwitchTypeEnum",
defaultValue: { summary: "SwitchTypeEnum.PRIMARY" },
},
color: { control: { type: "select", options: SwitchTypeEnum },
},
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With