Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

difference between knobs and controls in Storybook?

I am new to the storybook. When I go through the documentation and videos about the storybook I read about knobs Addon. Knobs addon and control looks similar. What is the difference between those two things?

like image 383
Gmv Avatar asked Dec 29 '20 12:12

Gmv


People also ask

What are knobs in storybook?

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.

Are storybook knobs deprecated?

We are deprecating Knobs in favor of @storybook/addon-controls. Storybook Addon Knobs allow you to edit props dynamically using the Storybook UI.

What are ArgTypes in storybook?

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).


2 Answers

Controls were introduced with Storybook version 6. They replace knobs for most of the use cases. However, there may be some edge cases were you still want to use knobs for dynamic values. For example, see this Github discussion on this topic: https://github.com/storybookjs/storybook/issues/11984

like image 97
Pawel Sas Avatar answered Nov 02 '22 03:11

Pawel Sas


controls addon is a companion to the docs addon so it interfaces with the ArgsTable which by itself is designed to automatically extract your components' propTypes & defaultProps (although I found this not to work)

enter image description here

So, with Knobs you define each prop (which you wish to be dynamic) yourself, manually, and this requires some more manual sync when your component changes and also more work, and also Knobs variables definitions might be scattered all across your story's file, where controls are all defined in one place, though the same "order" can also be done with Knobs, it does not enforces it (for good reasons).

If you want to have an interactive propTypes documentation for your components, then I suggest using controls with addon-docs, and I've been using knobs for years, but that's it, it's time to upgrade.

If, for some reason, your component's propTypes where not auto-detected (in the story) then you can define then (with controls) like so:

import Alert from './';

export default {
    title: 'General/Alert',
    component: Alert,
    parameters: {
        controls: { expanded: true }, // Show full documentation for each property
    },
    argTypes: {
        type: {
            description: 'Alert.Types',
            defaultValue: Alert.Types.WARNING,
            table: {
                type: {
                    summary: 'string',
                },
                defaultValue: {
                    summary: Alert.Types.WARNING,
                },
            },
            options: Alert.Types,
            control: {
                type: 'select', // for selecting between the array of options above
            },
        },

        title: {
            defaultValue: '',
            table: {
                type: {
                    summary: 'string',
                },
            },
            description: 'An optional title',
            control: {
                type: 'text',
            },
        },

        onClose: {
            table: {
                type: {
                    summary: 'func',
                },
            },
            description: '× button click callback',
            control: { type: null },
        },

        children: {
            description: 'The message body (mandatory)',
            type : {
                required: true,
            },
            table: {
                type: {
                    summary: 'node',
                },
            },
            control: { type: null },
        },
    },
}

//...export your story...

Notes:

  • How to migrate dynamic knobs to controls?
like image 36
vsync Avatar answered Nov 02 '22 04:11

vsync