Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Building UI components with Storybook

Building UI components with Storybook

An introduction to Storybook features.

Benoît Burgener

November 03, 2019
Tweet

More Decks by Benoît Burgener

Other Decks in Programming

Transcript

  1. BUILDING UI COMPONENTS WITH Storybook Benoît Burgener · @LeBenLeBen ·

    Webmardi · November 5, 2019 · Liip, Lausanne
  2. × KSS 2011 ! × Hologram 2013 ! × Pattern

    Lab 2013 × Fabricator 2014 ! × Fractal 2015 × Styleguidist 2015 × …
  3. /*doc --- title: Alert name: alert category: basics --- ```html_example

    <div class='alert'>Hello</div> ``` */ .alert { … }
  4. --- notes: | These are notes written in `markdown` ---

    <div class="component">My Component</div>
  5. {% extends "@page-layout" %} <h1>{{ home.greeting }}, {{ name }}!</h1>

    {% block content %} {% render '@button', { variant: 'primary' } %} {% endblock content %}
  6. StoriesOf API import React from 'react'; import { storiesOf }

    from '@storybook/react'; import Button from './Button'; storiesOf('Atoms|Button', module) .add('with text', () => <Button>Hello Button</Button>) .add('with some emoji', () => ( <Button> <span role="img" aria-label="so cool"> ! " # $ </span> </Button> )); src/stories/button.stories.js
  7. Component Story Format (CSF) import React from 'react'; import Button

    from './Button'; export default { component: Button, title: 'Atoms|Button', }; export const text = () => (<Button>Hello Button</Button>); export const emoji = () => ( <Button> <span role="img" aria-label="so cool"> ! " # $ </span> </Button> ); src/stories/button.stories.js
  8. import { configure } from '@storybook/react'; // Load our global

    stylesheet into stories view import '@/assets/tailwind.css'; // Load stories configure( require.context('../src/stories', true, /\.stories\.(js|mdx)$/), module ); .storybook/config.js
  9. import React from 'react'; import { withKnobs, text, boolean }

    from '@storybook/addon-knobs'; import Button from './Button'; export default { component: Button, title: 'Atoms|Button', decorators: [withKnobs] }; export const text = () => ( <Button disabled={boolean('Disabled', false)}> {text('Label', 'Hello Storybook')} </Button> );
  10. import React from 'react'; import { action } from '@storybook/addon-actions';

    import Button from './Button'; export default { component: Button, title: 'Atoms|Button', }; export const text = () => ( <Button onClick="action('clicked')"> Hello button </Button> );
  11. × Knobs × Actions × Source × Notes × Viewport

    × Storyshots × Backgrounds × Accessibility × Console × Links And many more community addons…
  12. import { create } from '@storybook/theming'; export default create({ base:

    'light', colorPrimary: '#00d5ff', colorSecondary: '#2625a5', textColor: '#2625a5', brandTitle: 'Webmardi', brandImage: require('./theme/webmardi.svg'), }); .storybook/theme.js
  13. <script> /** * Styled `a` or `button` element */ export

    default { props: { /** * The HTML tag used for the button. */ tag: { type: String, default: 'button', }, … }, … } </script> src/components/Btn.vue
  14. MDX stories import { Meta } from '@storybook/addon-docs/blocks'; <Meta title="Docs|About"

    /> # Webmardi Web Styleguide This project is based on [Tailwind CSS](https://tailwindcss.com/). For the sake of clarity, its classes are not documented here. You can find a dedicated documentation at [tailwindcss.com/docs](https://tailwindcss.com/docs). ## Usage The styleguide is available as a npm package, you can install it by running: ... src/stories/about.stories.mdx
  15. import { Meta, Story, Preview } from '@storybook/addon-docs/blocks'; import {

    Checkbox } from './Checkbox'; <Meta title="MDX|Checkbox" component={Checkbox} /> # Checkbox With `MDX` we can define a story for `Checkbox` right in the middle of our markdown documentation. <Preview> <Story name="all checkboxes"> <form> <Checkbox id="Unchecked" label="Unchecked" /> <Checkbox id="Checked" label="Checked" checked /> <Checkbox appearance="secondary" id="second" label="Secondary" checked /> </form> </Story> </Preview>
  16. import { Meta, ColorPalette, ColorItem } from '@storybook/addon-docs/blocks'; <Meta title="Docs|Colors"

    /> # Colors <ColorPalette> <ColorItem title="North Star Blue" subtitle="$primary-color" colors={['#2625a5']} /> ... </ColorPalette> src/stories/colors.stories.mdx
  17. package.json { "scripts": { "storybook:build": "build-storybook -c .storybook -o build",

    "storybook:build:docs": "build-storybook -c .storybook -o build --docs", } } # Create a build npm run storybook:build # Preview the result npx http-server build
  18. RECAP × Build components in isolation × Mock hard-to-reach use

    cases × Supercharge your workflow with addons × Match your project brand easily with theming × Generate a styleguide automatically