Text settings

Check stories with larger text and WCAG text spacing

View on GithubNew to Storybook?Get started

Text settings

CI npm version License: MIT

storybook-addon-text-settings is a Storybook addon for checking that a story's layout still works when users change their text settings:

These criteria are identical in WCAG 2.1 and 2.2.

The addon simulates what users' tools do: the browser's text size setting, and the text spacing bookmarklet or extensions used in WCAG testing.

A note on 1.4.4. WCAG accepts any one scaling mechanism, so a page that only works with browser zoom still passes 1.4.4. This addon assumes the user's mechanism is the browser's text size setting and tests only that. A problem it shows is real for those users, but not a WCAG failure by itself.

Contents

Installation

npx storybook add storybook-addon-text-settings

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-text-settings

and add it to .storybook/main.ts:

const config = {
  addons: ['storybook-addon-text-settings'],
};
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 textSettings from 'storybook-addon-text-settings';
export default definePreview({ addons: [textSettings()] });

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

Checking a component

Does your component still work for users who enlarge text or change its spacing?

  1. Open the story and click the toolbar button (a big "A", a small "a" and an arrow).
  2. Text size (1.4.4): tick Font size. Text grows to 200%, as with a larger default font size in the browser. Text that stays small is set in px, or the page fixes its root font size; the popover says when it is the root.
  3. Text spacing (1.4.12): untick Font size and tick the other four. The WCAG test applies all four at once, at the values already in the fields.
  4. Look for lost content: text cut off by a fixed height or overflow: hidden, overlapping text, labels that no longer fit their buttons. Try narrow widths too with Storybook's viewport tool: a layout often breaks only there.
  5. Share what you found: the settings are in the URL, so the link shows the same thing to anyone who opens it.
  6. Keep it tested: add a story whose parameters turn the settings on, so visual tests and play functions check it on every run.

Reset to defaults brings the story back as it was.

Usage

Open the toolbar button (a big "A", a small "a" and an arrow). The popover has a master switch and, for each setting, a checkbox and an editable value. The addon changes nothing until you turn a setting on; the button is highlighted while the addon is changing the page.

Setting Default Criterion
Font size 200% 1.4.4
Line height 1.5 1.4.12
Letter spacing 0.12em 1.4.12
Word spacing 0.16em 1.4.12
Paragraph spacing 2em 1.4.12

The defaults are the WCAG test values, which are minimums content must survive. You can go higher or lower (a note appears below the WCAG value). Values accept 0.12 and 0,12 (a comma is always the decimal mark, so 1,000 reads as 1); anything that is not a plain decimal number from 0.01 to 1000 (1e2 and 0x10 are not) is rejected and the last valid value stays in use. The error shows when you leave the field, so half-typed text such as 0. is not flagged.

Reset to defaults clears your changes; it is disabled while there are none. Undo reset brings them back until you change something else.

The rules live in one <style> element in <head>. If the page removes it (for example by replacing the head's content), the addon puts it back a moment later.

Font size

Simulates the browser's default font size setting. The project's own root font size is the starting point: with html { font-size: 62.5% } and 200% the root becomes 20px, as it would for a real user with a 32px default. If the project fixes the root size in absolute units (for example html { font-size: 16px }), the browser setting has no effect, and the addon does not scale it either. The popover then shows a notice saying so.

The addon measures again after the story has rendered, when the window is resized, when a stylesheet <link> or <style> element anywhere in the document is added, removed or edited (for example by a hot reload, or a story that renders its own <style>), when a <link> stylesheet or a <style> with @import has loaded, and when any attribute of <html> changes (such as class or data-theme). So the result follows a root size that changes later in those ways. A change made only through the CSS object model, such as insertRule() or a new adoptedStyleSheets list, is not noticed until one of those happens.

Text spacing

Uses the same rules as the bookmarklet used in WCAG testing, with !important, so a component's ordinary values cannot override them. As with the bookmarklet, two kinds of rule still win: one that is itself !important (the addon's selector is *, so any other selector is more specific), and an inline !important style. Text that keeps its spacing that way is a finding in itself: users' own tools cannot change it either.

Line height, letter spacing and word spacing apply to every element. Paragraph spacing, as in the bookmarklet, is margin-bottom on <p> elements only: spacing between paragraphs built from other elements (<div>, list items) does not change, and in a vertical writing mode the margin is still at the bottom, not after the paragraph.

Parameters

parameters: {
  textSettings: {
    disable: true, // hide the addon and apply nothing for this story
    defaults: {
      // same shape as the global; may also turn settings on
      letterSpacing: { on: true },
      lineHeight: { on: true, value: 1.8 },
    },
  },
}

Defaults merge in Storybook's usual order: built-in defaults, project, component, story, then the user's own choices in the toolbar.

Do not set the textSettings global through a story's globals: since Storybook 9 that locks the toolbar. Use parameters.textSettings.defaults. The exception is a story or test mode made for automated tests, where a fixed value is the point.

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

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

The types TextSettingsInput and TextSettingsParameters are also exported by name.

The global (public API)

Settings are stored in the textSettings global, so they persist in the URL and can be shared. The global is sparse: it only holds what the user changed. Values are plain numbers with fixed units (200 means 200%, 0.12 means 0.12em), because Storybook drops a whole global from the URL if any value contains characters such as %.

textSettings: {
  enabled: true, // master switch; missing means on
  fontSize: { on: true, value: 200 },
  letterSpacing: { on: true, value: 0.12 },
}

Keys: enabled, fontSize, lineHeight, letterSpacing, wordSpacing, paragraphSpacing; each setting has on and value. Unknown or invalid fields are ignored with a console warning. An incompatible change to this shape comes with a new major version.

Known limitations

  • Shadow DOM: the injected stylesheet does not reach inside shadow roots, so web components only get inherited properties. Text spacing results for them are unreliable.
  • Text in px does not scale with the font size setting. That is a real finding for users of that setting, not a bug in the addon.
  • Keyword font sizes, em/rem media queries and unstyled form controls don't react as they would to the browser's real setting.
  • Fixed root detection reads inline styles and every stylesheet the browser lets it read, including @imported and script-made (adoptedStyleSheets) ones. A cross-origin stylesheet is not readable; the popover says when that may matter. rem inside the root's own font-size value is not handled.
  • An inline !important font size on <html> cannot be overridden, so the root is not scaled then; the popover says so. An !important rule in a stylesheet is handled.
  • Play functions and tests: the rules are written before the story's play function runs, so play and snapshot tools see them. Root font size rules that a story adds itself in an effect arrive later; the addon measures again after the story has rendered, so only code that runs right after play could see the earlier value. Put such rules in the preview's global CSS or add them in a loader.
  • Docs pages are not supported. The addon applies only in the story canvas.
  • This simulates text-only resizing, not browser zoom.
  • Firefox has a separate monospace size setting; the addon scales monospace text by the same factor as other text.
  • Together with storybook-addon-cascade-disco: this addon's !important spacing rules override that addon's line-height, letter-spacing and word-spacing, and the root font size changes what its 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