Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

storybook-addon-pseudo-states

chromaui812kMIT4.0.2TypeScript support: included

CSS pseudo states for Storybook

storybook-addons, pseudo states, style, test

readme

Storybook Pseudo States

Toggle CSS pseudo states for your components in Storybook.

Published on npm Tested with Chromatic

Introduction

This addon attempts to "force" your components' pseudo states. It rewrites all document stylesheets to add a class name selector to any rules that target a pseudo-class (:hover, :focus, etc.). The tool then allows you to toggle these class names on the story container (#root) or any other root element you want (via the rootSelector param). Additionally, you can set the pseudo property on your story parameters to set a default value for each pseudo class. This makes it possible to test such states with Chromatic.

Limitations

Because this addon rewrites your stylesheets rather than toggle the actual browser behavior like DevTools does, it won't render any of the default user agent (browser) styles. Unfortunately there's no JavaScript API to toggle real pseudo states without using a browser extension.

Getting Started

This addon requires Storybook 6.1 or later. Install the latest with npx sb upgrade --prerelease

First, install the addon:

npm i -D storybook-addon-pseudo-states

Then, add "storybook-addon-pseudo-states" to the addons array in your .storybook/main.js:

module.exports = {
  addons: ["storybook-addon-pseudo-states"],
}

Setting default story states

You can have your stories automatically use a specific set of pseudo states, by setting the pseudo property on parameters:

export const Hover = () => <Button>Label</Button>
Hover.parameters = { pseudo: { hover: true } }

This is what enables snapshot testing your pseudo states in Chromatic.

Targeting specific elements

If you don't want to force or toggle pseudo styles to all elements that use them, but rather only enable them on specific elements, you can set a string or array value instead of a boolean:

export const Buttons = () => (
  <>
    <Button id="one">Hover</Button>
    <Button id="two">Hover focus</Button>
    <Button id="three">Hover focus active</Button>
  </>
)
Buttons.parameters = {
  pseudo: {
    hover: ["#one", "#two", "#three"],
    focus: ["#two", "#three"],
    active: "#three",
  },
}

This accepts a single CSS selector (string), or an array of CSS selectors on which to enable that pseudo style.

Overriding the default root element

By default, we use #storybook-root (or #root before Storybook 7) element as the root element for all pseudo classes. If you need to render elements outside Storybook's root element, you can set parameters.pseudo.rootSelector to override it. This is convenient for portals, dialogs, tooltips, etc.

For example, consider a Dialog component that inject itself to the document's body node:

export const DialogButton = () => (
  <Dialog>
    <Button>Hover</Button>
  </Dialog>
)

DialogButton.parameters = {
  pseudo: { hover: true, rootSelector: "body" },
}

changelog

v4.0.2 (Wed Sep 04 2024)

🐛 Bug Fix

Authors: 1


v4.0.1 (Tue Sep 03 2024)

🐛 Bug Fix

  • Fix CJS entrypoint for manager code, and drop some unused things #126 (@ghengeveld)

Authors: 1


v4.0.0 (Thu Aug 29 2024)

💥 Breaking Change

Authors: 1


v3.1.1 (Wed May 08 2024)

🐛 Bug Fix

  • Avoid invalid CSS generation for styles using vendor pseudo-elements #120 (@m-akinc)

Authors: 1


v3.1.0 (Mon May 06 2024)

🚀 Enhancement

🐛 Bug Fix

Authors: 2


v3.0.1 (Sat Mar 30 2024)

🐛 Bug Fix

  • switch off older Icons reference #111 #112 (@wegry)

Authors: 1


v3.0.0 (Mon Mar 25 2024)

💥 Breaking Change

🐛 Bug Fix

Authors: 3


v2.2.1 (Tue Mar 05 2024)

🐛 Bug Fix

  • Avoid generating invalid selector with :has #105 (@m-akinc)

Authors: 1


v2.2.0 (Mon Mar 04 2024)

🚀 Enhancement

  • Support grouping rules like @media and @layer and fix shadow DOM support #103 (@m-akinc)

🐛 Bug Fix

Authors: 2


v2.1.2 (Wed Oct 11 2023)

🐛 Bug Fix

Authors: 2


v2.1.1 (Mon Aug 21 2023)

🐛 Bug Fix

  • Selecting specific element also applies pseudo state to all its descendants alternate #85 (@JonathanKolnik)

Authors: 1


v2.1.0 (Thu Jun 22 2023)

🚀 Enhancement

Authors: 2


v2.0.2 (Thu Jun 22 2023)

🐛 Bug Fix

Authors: 2


v2.0.1 (Wed Apr 19 2023)

🐛 Bug Fix

  • Remove redundant regex negative lookbehind #73 (@filipw01)

Authors: 1


v2.0.0 (Wed Apr 12 2023)

💥 Breaking Change

🐛 Bug Fix

Authors: 5


v1.15.5 (Thu Mar 23 2023)

🐛 Bug Fix

⚠️ Pushed to main

Authors: 2


v1.15.2 (Tue Dec 13 2022)

🐛 Bug Fix

Authors: 2


v1.15.1 (Wed Jun 22 2022)

🐛 Bug Fix

Authors: 1


v1.15.0 (Thu Jun 16 2022)

🚀 Enhancement

Authors: 3


v1.14.1 (Wed Jun 15 2022)

⚠️ Pushed to main

Authors: 1


v1.0.0 (Tue May 24 2022)

🚀 Enhancement

🐛 Bug Fix

⚠️ Pushed to main

Authors: 5


v1.0.0 (Sat Feb 27 2021)

🐛 Bug Fix

⚠️ Pushed to main

Authors: 1