Cascade disco

Expose components that rely on inherited CSS

View on GithubNew to Storybook?Get started

Cascade disco

CI npm version License: MIT

storybook-addon-cascade-disco changes inherited (cascading) CSS properties on the story's <body>, to expose components that rely on inheritance instead of setting values themselves: for example a component that takes its color or font-family from the page.

Switch a property on and everything that inherits it turns bright magenta, huge, vertical or unreadable, so it is obvious which parts took the value. Animation cycles through the values.

Contents

Installation

npx storybook add storybook-addon-cascade-disco

This installs the package and adds it to addons in .storybook/main.ts. With CSF factories it also adds it to definePreview in .storybook/preview.ts.

Or by hand:

npm install -D storybook-addon-cascade-disco

and add it to .storybook/main.ts:

const config = {
  addons: ['storybook-addon-cascade-disco'],
};
export default config;

If your preview uses CSF factories (definePreview), add the addon there too, or it does nothing. Storybook then takes the preview's settings only from there, not from main.ts. This also gives the types:

import cascadeDisco from 'storybook-addon-cascade-disco';
export default definePreview({ addons: [cascadeDisco()] });

Requires Storybook 10 (10.3 or later) or Storybook 11. It works with every web framework, since it only injects CSS into the preview iframe.

Usage

Open the toolbar button (two disco balls). The popover has:

  • A master switch, on by default. It turns everything off without losing the choices. While nothing is on, a hint under it says to tick a property.
  • Animation: speed in changes per second (default 1) and order. A new speed is used when you press Enter or leave the field, and a running animation carries on from where it is, a whole step after its last change. When nothing that is on has more than one value (or only form control inheritance is on), there is nothing to animate: the popover says so, and nothing runs. Sequential moves one property per step, all at once moves every property. Animation is always off when Storybook loads and is never stored in the URL. See Flashing for the speed limits.
  • A checkbox for each property, all off by default: the addon changes nothing until you turn one on. Each property has a list of values, an active value (the one applied while animation is off) and a Page value, which leaves the property alone so animation can switch between the page's own value and an extreme one. All values and the Page value are selected by default; the first extreme value is active.
  • Custom variables: add CSS custom properties (name without --) with your own values. An edited value is applied when you press Enter or leave the field; Escape closes the popover without applying it. New values and variables are added with Enter or the Add button. So that a shared link cannot add an address for a story to request, values that load a file (url(), image-set(), src() and the like) and values with // or a web address (https:) are not accepted, nor are control characters such as text direction marks. A value can still refer to another variable with var(): if your own CSS has a variable that holds an address, a link can make the story use it wherever the addon's variable is used. A link, and the popover, hold at most 50 variables, 20 values each (the Page value not counted), 100 characters per name and 500 per value. Values in your own story parameters are neither checked nor limited. When you edit a variable from them, the link holds its whole list, and the values your parameters give that variable are accepted from it unchecked and not counted. The popover says when a variable has no value in the story, or is set on an element inside the story where a value on the body cannot reach it.
  • Form control inheritance: buttons, inputs, selects and textareas ignore most inherited properties by default. This checkbox makes them inherit font and color, and any of letter-spacing, word-spacing, text-transform, text-indent, text-align, white-space and cursor that is turned on, like a typical CSS reset.
  • Reset to defaults, which also stops the animation. Undo reset brings the settings back until you change something else; the animation stays off.

The button is highlighted while the addon is changing the page. While the animation is running (on, with something to animate), a small twinkle turns and fades on one disco ball in the icon, then on the other (with reduced motion, one twinkle stays still on the big ball instead).

Flashing

Fast changes between strong colours can trigger photosensitive seizures, in you or in someone who sees your screen. The addon limits the animation like this:

  • It only runs after you turn it on in the popover. It is off whenever Storybook loads, and a link cannot turn it on.
  • Up to 3 changes per second need nothing more. WCAG 2.3.1 allows three flashes per second, and a flash is a pair of opposing changes, so this is on the careful side.
  • Faster speeds need the Allow more than 3 changes per second checkbox, which is off whenever Storybook loads. Without it, a higher speed runs at 3.
  • The checkbox is not available while your system is set to reduce motion (prefers-reduced-motion).
  • The speed is never above 10 changes per second, which keeps it out of the range that is most likely to trigger seizures (about 15 to 25 flashes per second).

These limits lower the risk; they do not make every combination of values safe. Large areas that switch between strong colours are a risk at lower speeds too, especially with all at once.

Everything is written into one <style id="storybook-addon-cascade-disco"> element in the preview iframe, as body { … !important }. !important is there to beat the project's own <body> rules (such as body.dark { color: … }); elements inside the story can still set their own values, so the component is tested exactly as with plain inheritance. Two kinds of project rule still win over the addon: a body rule that is itself !important and has a more specific selector than plain body, and an inline !important style on <body>. The property then keeps the project's value.

Properties

color, cursor, direction, font-family, font-size, font-style, font-weight, hyphens, letter-spacing, line-height, list-style, quotes, text-align, text-indent, text-transform, visibility, white-space, word-spacing and writing-mode. The popover shows the CSS of each value next to its name.

Parameters

// .storybook/preview.ts, meta.parameters or a story
parameters: {
  cascadeDisco: {
    disable: true, // hide the addon and apply nothing for this story
    defaults: {
      properties: {
        color: { on: true, values: ['magenta', 'lime'], active: 'lime' },
      },
      variables: { 'brand-color': { on: true, values: ['#ff0', 'red'] } },
      formControls: true,
    },
  },
}

defaults has the same shape as the global, but variable names and values are plain text. Precedence, lowest to highest: built-in defaults, project, component and story parameters, then the user's own choices.

Don't set the cascadeDisco global with globals in a story: it locks the toolbar for the story. Use defaults.

With CSF factories, the addon must also be in definePreview (see Installation), which also gives the types.

npx storybook add imports storybook-addon-cascade-disco/preview as a namespace (import * as …) instead. That works too, without the types.

The types CascadeDiscoInput and CascadeDiscoParameters are also exported by name.

The global (public API)

The settings are stored in the cascadeDisco global, so they are shared by URL. Its shape is public API (shared links and test setups depend on it); an incompatible change to it comes with a new major version. It is sparse: only what the user changed.

cascadeDisco: {
  enabled: true,         // master switch
  formControls: false,
  properties: {
    color: { on: true, values: ['magenta', null], active: 'magenta' },
  },
  variables: {
    'brand-color': { on: true, values: ['_23ff0', 'red'], active: 'red' },
  },
}
  • Built-in values are stable string keys (magenta, huge, rtl), not CSS.
  • null inside values, or as active, is the Page value.
  • values: null is an empty list. Storybook's URL format cannot hold an empty list, so the addon stores it this way; values: [] is read the same.
  • Free text (variable names and values) is escaped, because Storybook's URL format only allows [a-zA-Z0-9 _-] in strings: each byte outside [a-zA-Z0-9 -] becomes _ plus two hex digits, and the first character is escaped unless it is a letter.
  • Unknown fields and invalid values are dropped with a console warning.

What can still hide problems

Inheritance only fills in what nothing else sets, so a component can look fine because:

  • Global project CSS targets elements directly (p, button { font-family: … }, * { color: … }).
  • Browser defaults: form controls don't inherit font, color and more (see form control inheritance).
  • A wrapper (decorator, theme provider) sets e.g. color on an element around the story. The addon can't tell it from the component itself.

Known limitations

  • Docs pages are not supported: the addon applies only in the story canvas.
  • hyphens: auto needs a lang attribute on the content.
  • text-transform: full-width has no effect in Chrome, Edge and Opera.
  • Shadow DOM: inherited properties cross into shadow roots, but styles inside win.
  • With storybook-addon-text-settings, its !important rules for line-height, letter-spacing and word-spacing override this addon's values, and its root font size changes what the relative font-size values compute to.

Support

Please open an issue with a minimal reproduction (https://storybook.new).

License

MIT

Made by
  • timpii
    timpii
Work with
    Angular
    Ember
    HTML
    Marko
    Mithril
    Preact
    Rax
    React
    Riot
    Svelte
    Vue
    Web Components
Tags