Skip to content

Latest commit

 

History

History
144 lines (80 loc) · 6.07 KB

decorators.mdx

File metadata and controls

144 lines (80 loc) · 6.07 KB
title sidebar
Decorators
order title
3
Decorators

A decorator is a way to wrap a story in extra “rendering” functionality. Many addons define decorators to augment your stories with extra rendering or gather details about how your story renders.

When writing stories, decorators are typically used to wrap stories with extra markup or context mocking.

Wrap stories with extra markup

Some components require a “harness” to render in a useful way. For instance, if a component runs right up to its edges, you might want to space it inside Storybook. With Svelte, you'll need to take additional steps to set it up properly.

Story without padding

Start by creating a new Svelte component that will act as a decorator. This component will wrap your story and provide the required spacing or layout elements.

{/* prettier-ignore-start */}

{/* prettier-ignore-end */}

Update your story to include the component and reference it to apply the required spacing or other functionality for all its stories.

{/* prettier-ignore-start */}

{/* prettier-ignore-end */}

Some components require a “harness” to render in a useful way. For instance, if a component runs right up to its edges, you might want to space it inside Storybook. Use a decorator to add spacing for all stories of the component.

Story without padding

{/* prettier-ignore-start */}

{/* prettier-ignore-end */}

Story with padding

“Context” for mocking

The second argument to a decorator function is the story context which contains the properties:

  • args - the story arguments. You can use some args in your decorators and drop them in the story implementation itself.
  • argTypes- Storybook's argTypes allow you to customize and fine-tune your stories args.
  • globals - Storybook-wide globals. In particular you can use the toolbars feature to allow you to change these values using Storybook’s UI.
  • hooks - Storybook's API hooks (e.g., useArgs).
  • parameters- the story's static metadata, most commonly used to control Storybook's behavior of features and addons.
  • viewMode- Storybook's current active window (e.g., canvas, docs).

This context can be used to adjust the behavior of your decorator based on the story's arguments or other metadata. For example, you could create a decorator that allows you to optionally apply a layout to the story, by defining parameters.pageLayout = 'page' (or 'page-mobile'): :

{/* prettier-ignore-start */}

{/* prettier-ignore-end */}

For another example, see the section on [configuring the mock provider](./mocking-data-and-modules/mocking-providers.mdx#configuring-the-mock-provider), which demonstrates how to use the same technique to change which theme is provided to the component.

Using decorators to provide data

If your components are “connected” and require side-loaded data to render, you can use decorators to provide that data in a mocked way without having to refactor your components to take that data as an arg. There are several techniques to achieve this. Depending on exactly how you are loading that data. Read more in the building pages in Storybook section.

Story decorators

To define a decorator for a single story, use the decorators property in the Story component if you are using Svelte CSF with the native templating syntax, or use the decorators key on a CSF named export:

To define a decorator for a single story, use the decorators key on a named export:

{/* prettier-ignore-start */}

{/* prettier-ignore-end */}

It is useful to ensure that the story remains a “pure” rendering of the component under test and that any extra HTML or components are used only as decorators. In particular the Source Doc Block works best when you do this.

Component decorators

To define a decorator for all component stories, include the decorators property in the defineMeta function of a Svelte CSF story file. Alternatively, use the decorators key on the default CSF export:

To define a decorator for all stories of a component, use the decorators key of the default CSF export:

{/* prettier-ignore-start */}

{/* prettier-ignore-end */}

Global decorators

We can also set a decorator for all stories via the decorators export of your .storybook/preview.js|ts file (this is the file where you configure all stories):

{/* prettier-ignore-start */}

{/* prettier-ignore-end */}

Decorator inheritance

Like parameters, decorators can be defined globally, at the component level, and for a single story (as we’ve seen).

All decorators relevant to a story will run in the following order once the story renders:

  • Global decorators, in the order they are defined
  • Component decorators, in the order they are defined
  • Story decorators, in the order they are defined, starting from the innermost decorator and working outwards and up the hierarchy in the same order