Research
Security News
Malicious npm Package Targets Solana Developers and Hijacks Funds
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
storybook-addon-pseudo-states
Advanced tools
The storybook-addon-pseudo-states npm package is a Storybook addon that allows you to easily visualize and test CSS pseudo-states (like :hover, :focus, :active, etc.) for your components. This is particularly useful for UI development, as it helps you ensure that your components look and behave correctly in different states without having to manually trigger those states.
Visualize CSS Pseudo-States
This feature allows you to visualize different CSS pseudo-states like :hover, :focus, and :active for your components. By using the `withPseudo` decorator and setting the `pseudo` parameters, you can see how your component looks in these states.
import { withPseudo } from 'storybook-addon-pseudo-states';
export default {
title: 'Button',
component: Button,
decorators: [withPseudo],
};
export const PseudoStates = () => <Button>Hover me</Button>;
PseudoStates.parameters = {
pseudo: { hover: true, focus: true, active: true },
};
Custom Pseudo-States
This feature allows you to define and visualize custom pseudo-states for your components. By setting custom pseudo-state names in the `pseudo` parameters, you can test how your component behaves in these custom states.
import { withPseudo } from 'storybook-addon-pseudo-states';
export default {
title: 'CustomButton',
component: CustomButton,
decorators: [withPseudo],
};
export const CustomPseudoStates = () => <CustomButton>Custom State</CustomButton>;
CustomPseudoStates.parameters = {
pseudo: { 'custom-state': true },
};
The storybook-addon-a11y package helps you test the accessibility of your components in Storybook. While it doesn't focus on CSS pseudo-states, it provides tools to ensure your components are accessible, which can complement the visual testing provided by storybook-addon-pseudo-states.
Toggle CSS pseudo states for your components in Storybook.
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.
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.
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"],
}
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.
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.
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" },
}
FAQs
CSS pseudo states for Storybook
We found that storybook-addon-pseudo-states demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.