@storybook/addon-controls
Controls for component properties
Last updated 3 days ago by shilman .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install @storybook/addon-controls 
SYNC missed versions from official npm registry.

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.

Framework Support

Screenshot

Installation

Controls is part of essentials and so is installed in all new Storybooks by default. If you need to add it to your Storybook, you can run:

npm i -D @storybook/addon-controls

Then, add following content to .storybook/main.js:

module.exports = {
  addons: ['@storybook/addon-controls'],
};

Usage

The usage is documented in the documentation.

FAQs

How will this replace addon-knobs?

Addon-knobs is one of Storybook's most popular addons with over 1M weekly downloads, so we know lots of users will be affected by this change. Knobs is also a mature addon, with various options that are not available in addon-controls.

Therefore, rather than deprecating addon-knobs immediately, we will continue to release knobs with the Storybook core distribution until 7.0. This will give us time to improve Controls based on user feedback, and also give knobs users ample time to migrate.

If you are somehow tied to knobs or prefer the knobs interface, we are happy to take on maintainers for the knobs project. If this interests you, hop on our Discord.

How do I migrate from addon-knobs?

If you're already using Storybook Knobs you should consider migrating to Controls.

You're probably using it for something that can be satisfied by one of the cases described above.

Let's walk through two examples: migrating knobs to auto-generated args and knobs to custom args.

Knobs to auto-generated args

First, let's consider a knobs version of a basic story that fills in the props for a component:

import { text } from '@storybook/addon-knobs';
import { Button } from './Button';

export const Basic = () => <Button label={text('Label', 'hello')} />;

This fills in the Button's label based on a knob, which is exactly the auto-generated use case above. So we can rewrite it using auto-generated args:

export const Basic = (args) => <Button {...args} />;
Basic.args = { label: 'hello' };

Knobs to manually-configured args

Similarly, we can also consider a story that uses knob inputs to change its behavior:

import range from 'lodash/range';
import { number, text } from '@storybook/addon-knobs';

export const Reflow = () => {
  const count = number('Count', 10, { min: 0, max: 100, range: true });
  const label = number('Label', 'reflow');
  return (
    <>
      {range(count).map((i) => (
        <Button label={`button ${i}`} />
      ))}
    </>
  );
};

And again, as above, this can be rewritten using fully custom args:

export const Reflow = ({ count, label, ...args }) => (
  <>{range(count).map((i) => <Button label={`${label} ${i}` {...args}} />)}</>
);
Reflow.args = { count: 3, label: 'reflow' };
Reflow.argTypes = {
  count: { control: { type: 'range', min: 0, max: 20 } }
};

My controls aren't being auto-generated. What should I do?

There are a few known cases where controls can't be auto-generated:

  • You're using a framework for which automatic generation isn't supported
  • You're trying to generate controls for a component defined in an external library

With a little manual work you can still use controls in such cases. Consider the following example:

import { Button } from 'some-external-library';

export default {
  title: 'Button',
  argTypes: {
    label: { control: 'text' },
    borderWidth: { control: { type: 'number', min: 0, max: 10 }},
  },
};

export const Basic = (args) => <Button {...args} />;
Basic.args = {
  label: 'hello',
  borderWidth: 1,
};

The argTypes annotation (which can also be applied to individual stories if needed), gives Storybook the hints it needs to generate controls in these unsupported cases. See control annotations for a full list of control types.

It's also possible that your Storybook is misconfigured. If you think this might be the case, please search through Storybook's Github issues, and file a new issue if you don't find one that matches your use case.

How can I disable controls for certain fields on a particular story?

The argTypes annotation can be used to hide controls for a particular row, or even hide rows.

Suppose you have a Button component with borderWidth and label properties (auto-generated or otherwise) and you want to hide the borderWidth row completely and disable controls for the label row on a specific story. Here's how you'd do that:

import { Button } from 'button';

export default {
  title: 'Button',
  component: Button,
};

export const CustomControls = (args) => <Button {...args} />;
CustomControls.argTypes = {
  borderWidth: { table: { disable: true } },
  label: { control: { disable: true } },
};

Like story parameters, args and argTypes annotations are hierarchically merged, so story-level annotations overwrite component-level annotations.

How do controls work with MDX?

MDX compiles to component story format (CSF) under the hood, so there's a direct mapping for every example above using the args and argTypes props.

Consider this example in CSF:

import { Button } from './Button';
export default {
  title: 'Button',
  component: Button,
  argTypes: {
    background: { control: 'color' },
  },
};

const Template = (args) => <Button {...args} />;
export const Basic = Template.bind({});
Basic.args = { label: 'hello', background: '#ff0' };

Here's the MDX equivalent:

import { Meta, Story } from '@storybook/addon-docs/blocks';
import { Button } from './Button';

<Meta title="Button" component={Button} argTypes={{ background: { control: 'color' } }} />

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

<Story name="Basic" args={{ label: 'hello', background: '#ff0' }}>
  {Template.bind({})}
</Story>

For more info, see a full Controls example in MDX for Vue.

Current Tags

  • 6.0.27                                ...           latest (3 days ago)
  • 6.1.0-alpha.29                                ...           next (3 days ago)

113 Versions

  • 6.1.0-alpha.29                                ...           3 days ago
  • 6.0.27                                ...           3 days ago
  • 6.1.0-alpha.28                                ...           3 days ago
  • 6.1.0-alpha.27                                ...           7 days ago
  • 6.1.0-alpha.26                                ...           8 days ago
  • 6.1.0-alpha.25                                ...           10 days ago
  • 6.1.0-alpha.24                                ...           11 days ago
  • 6.1.0-alpha.23                                ...           14 days ago
  • 6.1.0-alpha.22                                ...           16 days ago
  • 6.1.0-alpha.21                                ...           18 days ago
  • 6.0.26                                ...           21 days ago
  • 6.1.0-alpha.20                                ...           21 days ago
  • 6.0.25                                ...           22 days ago
  • 6.0.24                                ...           22 days ago
  • 6.0.23                                ...           22 days ago
  • 6.1.0-alpha.19                                ...           23 days ago
  • 6.1.0-alpha.18                                ...           a month ago
  • 6.1.0-alpha.17                                ...           a month ago
  • 6.0.22                                ...           a month ago
  • 6.1.0-alpha.16                                ...           a month ago
  • 6.1.0-alpha.15                                ...           a month ago
  • 6.1.0-alpha.14                                ...           a month ago
  • 6.1.0-alpha.12                                ...           a month ago
  • 6.1.0-alpha.11                                ...           a month ago
  • 6.1.0-alpha.10                                ...           a month ago
  • 6.1.0-alpha.9                                ...           a month ago
  • 6.1.0-alpha.8                                ...           a month ago
  • 6.1.0-alpha.7                                ...           2 months ago
  • 6.1.0-alpha.6                                ...           2 months ago
  • 6.1.0-alpha.4                                ...           2 months ago
  • 6.1.0-alpha.3                                ...           2 months ago
  • 6.1.0-alpha.1                                ...           2 months ago
  • 6.1.0-alpha.0                                ...           2 months ago
  • 6.0.21                                ...           2 months ago
  • 6.0.20                                ...           2 months ago
  • 6.0.19                                ...           2 months ago
  • 6.0.18                                ...           2 months ago
  • 6.0.17                                ...           2 months ago
  • 6.0.16                                ...           2 months ago
  • 6.0.15                                ...           2 months ago
  • 6.0.14                                ...           2 months ago
  • 6.0.13                                ...           2 months ago
  • 6.0.12                                ...           2 months ago
  • 6.0.11                                ...           2 months ago
  • 6.0.10                                ...           2 months ago
  • 6.0.9                                ...           2 months ago
  • 6.0.7                                ...           2 months ago
  • 6.0.6                                ...           2 months ago
  • 6.0.5                                ...           2 months ago
  • 6.0.4                                ...           2 months ago
  • 6.0.3                                ...           2 months ago
  • 6.0.2                                ...           2 months ago
  • 6.0.1                                ...           2 months ago
  • 6.0.0                                ...           2 months ago
  • 6.0.0-rc.30                                ...           3 months ago
  • 6.0.0-rc.29                                ...           3 months ago
  • 6.0.0-rc.28                                ...           3 months ago
  • 6.0.0-rc.27                                ...           3 months ago
  • 6.0.0-rc.26                                ...           3 months ago
  • 6.0.0-rc.25                                ...           3 months ago
  • 6.0.0-rc.24                                ...           3 months ago
  • 6.0.0-rc.23                                ...           3 months ago
  • 6.0.0-rc.22                                ...           3 months ago
  • 6.0.0-rc.21                                ...           3 months ago
  • 6.0.0-rc.20                                ...           3 months ago
  • 6.0.0-rc.19                                ...           3 months ago
  • 6.0.0-rc.18                                ...           3 months ago
  • 6.0.0-rc.16                                ...           3 months ago
  • 6.0.0-rc.15                                ...           3 months ago
  • 6.0.0-rc.14                                ...           3 months ago
  • 6.0.0-rc.13                                ...           3 months ago
  • 6.0.0-rc.12                                ...           3 months ago
  • 6.0.0-rc.11                                ...           3 months ago
  • 6.0.0-rc.10                                ...           3 months ago
  • 6.0.0-rc.9                                ...           3 months ago
  • 6.0.0-rc.8                                ...           3 months ago
  • 6.0.0-rc.5                                ...           3 months ago
  • 6.0.0-rc.3                                ...           3 months ago
  • 6.0.0-rc.2                                ...           4 months ago
  • 6.0.0-rc.1                                ...           4 months ago
  • 6.0.0-rc.0                                ...           4 months ago
  • 6.0.0-beta.46                                ...           4 months ago
  • 6.0.0-beta.45                                ...           4 months ago
  • 6.0.0-beta.44                                ...           4 months ago
  • 6.0.0-beta.43                                ...           4 months ago
  • 6.0.0-beta.42                                ...           4 months ago
  • 6.0.0-beta.41                                ...           4 months ago
  • 6.0.0-beta.40                                ...           4 months ago
  • 6.0.0-beta.39                                ...           4 months ago
  • 6.0.0-beta.38                                ...           4 months ago
  • 6.0.0-beta.37                                ...           4 months ago
  • 6.0.0-beta.36                                ...           4 months ago
  • 6.0.0-beta.35                                ...           4 months ago
  • 6.0.0-beta.34                                ...           4 months ago
  • 6.0.0-beta.33                                ...           4 months ago
  • 6.0.0-beta.32                                ...           4 months ago
  • 6.0.0-beta.31                                ...           4 months ago
  • 6.0.0-beta.30                                ...           4 months ago
  • 6.0.0-beta.29                                ...           4 months ago
  • 6.0.0-beta.28                                ...           4 months ago
  • 6.0.0-beta.27                                ...           4 months ago
  • 6.0.0-beta.26                                ...           4 months ago
  • 6.0.0-beta.25                                ...           4 months ago
  • 6.0.0-beta.24                                ...           4 months ago
  • 6.0.0-beta.23                                ...           5 months ago
  • 6.0.0-beta.22                                ...           5 months ago
  • 6.0.0-beta.21                                ...           5 months ago
  • 6.0.0-beta.20                                ...           5 months ago
  • 6.0.0-beta.19                                ...           5 months ago
  • 6.0.0-beta.18                                ...           5 months ago
  • 6.0.0-beta.17                                ...           5 months ago
  • 6.0.0-beta.16                                ...           5 months ago
  • 6.0.0-beta.15                                ...           5 months ago

Copyright 2014 - 2016 © taobao.org |