@ergosign/storybook-addon-pseudo-states-react

中文文档
0.0.1-alpha.61发布于 2023/07/25MITTypeScriptCommonJS零依赖近 30 天下载 1.3万

Storybook decorator to enable automatic generation and displaying of CSS pseudo states for components.

原始使用文档
文档目录5 节
  1. Framework Support
  2. Getting started
  3. Advanced setup
  4. Show/Hide Toolbar-Button
  5. Usage

Storybook Addon Pseudo States

Storybook Addon Pseudo States allows you to automatically display pseudo states (and attribute states) of a component in Storybook's preview area.

example

Framework Support

FrameworkDisplay StatesTool-Button to show/hide
Angular++
React++
Lit++
HTML++
Vue++

Getting started

First of all, you need to install Pseudo States into your project as a dev dependency.

npm install @ergosign/storybook-addon-pseudo-states-react --save-dev

When using create-react-app and the related preset, configure it as an addon for your Storybook environment (located in the Storybook config directory).

For version < 5.3.x

Import the addon in your addons.js file:

import "@ergosign/storybook-addon-pseudo-states-react/preset-postcss";
For version >= 5.3.x

Add it to the addons section in your main.js file.

module.exports = {
  "addons": [
    '@ergosign/storybook-addon-pseudo-states-react/preset-postcss'
  ]
}

In case you have an other project configuration, check out the Advanced setup section, to see how to get it working with different settings.

To see what's needed to use the pseudo addon, have a look at the Usage section.

Advanced setup

With PostCSS Preset

This project comes with a dependency to the postcss-pseudo-classes package. Unfortunately, the latest version is only tagged and not released.

We provide a preset-postcss preset that adds postcss-loader to Storybook's custom webpack config. Add this preset to your configuration (located in the Storybook config directory)

For version < 5.3.x

Import the addon in your addons.js file:

import "@ergosign/storybook-addon-pseudo-states-react/preset-postcss";
For version >= 5.3.x

Add it to the addons section in your main.js file.

module.exports = {
  "addons": [
    '@ergosign/storybook-addon-pseudo-states-react/preset-postcss'
  ]
}

This creates for each css pseudo class an equivalent as normal css class (for instance :hover to \:hover), so that you can use it in element's class attribute (<div class=":hover">Element in hover state</div>).

You can modify post css loader options (see type definition of PseudoStatesPresetOptions):

module.exports = {
   presets: [
       {
            name:"@ergosign/storybook-addon-pseudo-states-react/preset-postcss",
            options: {
                rules: [/\.scss$|\.sass$/, ".sass", ...],
                cssLoaderOptions: CssLoaderOptions,
                postCssLoaderPseudoClassesPluginOptions: {
                    prefix: 'pseudo-states--', // default for angular
                    blacklist: [':nth-child', ':nth-of-type']
                }
            }
        }     
    ] 
}

If you set another prefix you have to set the same for the addon, too. Therefore, add the following to your .storybook/preview.js:

addParameters({
    withPseudo: {
        prefix: "still-pseudo-states--",
    },
});

Own Webpack config (but automatically generated with PostCss)

When you have configured your own webpack config but still want to use this addon with PostCSS, add postcss-loader to you webpack config.

ATTENTION: When using CSS-Modules, you have to take care that no [hash] is used as localIdentName in your css-loader options.

module.exports = {
  module: {
    rules: [
      {
        test: /\.(scss|css)$/,
        use: [
          {
            loader: 'style-loader',
          },
          {
            loader: 'css-loader',
            options: {
              // ATTENTION when using css modules
              modules: {
                // !!! must not use [hash]'
                localIdentName: '[path][name]__[local]',
              },
            },
          },
          // Add loader here
          {
            loader: 'postcss-loader',
          },
          {
            loader: 'sass-loader',
          },
        ],
      },
    ],
  },
};

Aditionally, you have to enable the postcss-peudo-classes module it your postcss.config.js

module.exports = {
  plugins: {
    'postcss-pseudo-classes': {
      // prefix: 'pseudoclass--',
    },
  },
};
When using a custom `prefix` parameter, use the same for postcss-pseudo-classes
module.exports = {
  plugins: {
    'postcss-pseudo-classes': {
      prefix: 'pseudoclass-example-prefix',
    },
  },
};

Manually

When you do not want that the pseudo classes are generated for you, you can provide your own rule on which the plugin looks for your css classes.

Per default, the prefix used in the addon to look for css classes is \:. To add the needed styles you have to add fakle classes consisting of prefix + pseudostate by yourself.

For example: When you want the hover and focus states to be shown by the plugin, you have to add \:hover and \:focus classes to your styles by yourself.

For the default prefix this may look like this:

.element {
  //element styling

  &:hover,
  &\:hover {
    // hover styling
  }
}
With a custom prefix

If you want to specify your own prefix, set it as prefix value in the addons parameters object.

To change the prefix to .pseudoclass-- you have to adjust the parameter like this:

// in your story
parameters: {
    withPseudo: {
        selector: "element",
        prefix: "pseudoclass--"
    }
}

And use the specified prefix in your styling definition:

.element {
  //element styling

  &:hover,
  &.pseudoclass--hover {
    // hover styling
  }
}

With babel plugin for Emotion.js

If you use emotion.js to style your components might want to consider using babel-plugin-storybook-addon-pseudo-states-emotion. It will create pseudo-classes for your components.

With a babel-plugin-storybook-addon-pseudo-states-emotion

This babel-plugin was made for this exact use-case. If you have questions or issues please look at the documentation or contact the repository owners.

Configure:

Please look at the options for babel or webpack configurations in storybook:

  1. https://storybook.js.org/docs/react/configure/babel
  2. https://storybook.js.org/docs/react/configure/webpack#extending-storybooks-webpack-config

Here's an example of the plugin configuration. (please refer to the original documentation)

Basic integration

{
  "plugins": ["babel-plugin-storybook-addon-pseudo-states-emotion"]
}

Adding a custom prefix as supported by the addon

{
  "plugins": [
    [
      "babel-plugin-storybook-addon-pseudo-states-emotion",
      {"prefix": "pseudoclass--" }
    ]
  ]
}

Show/Hide Toolbar-Button

You can enable a toolbar button that toggles the Pseudo States in the Preview area.

See Framework Support which Frameworks support this feature.

Enable the button by adding it to your addon configuration file (located in the Storybook config directory)

For version < 5.3.x

Import the addon in your addons.js file:

import "@ergosign/storybook-addon-pseudo-states-react/register";
For version >= 5.3.x

Add it to the addons section in your main.js file.

module.exports = {
  "addons": [
    '@ergosign/storybook-addon-pseudo-states-react/register'
  ]
}

Usage

WARNING: withPseudo should always the first element in your decorators array because it alters the template of the story.

General

Component Story Format (CSF, recommended)
import { withPseudo } from '@ergosign/storybook-addon-pseudo-states-react';

const section = {
  title: 'Button',
  decorators: [withPseudo],
  parameters: {
    withPseudo: { selector: 'button' },
  },
};
export default section;

export const Story = () => {
  return {
    component: ButtonComponent,
  };
};
storyOf Format
import { withPseudo } from '@ergosign/storybook-addon-pseudo-states-react';

storiesOf('Button', module)
  .addDecorator(withPseudo)
  .addParameters({
    withPseudo: {
      selector: 'button', // css selector of pseudo state's host element
      pseudos: ['focus', 'hover', 'hover & focus', 'active'],
      attributes: ['disabled', 'readonly', 'error'],
    },
  })
  .add('Icon Button', () => <Button />);

There is a default configuration for selector, pseudos and attributes. Thus, you can leave withPseudo options empty.

Parameters & Types

See Types

版本与发布记录

@ergosign/storybook-addon-pseudo-states-react 共发布 36 个版本,首次发布于 2020/03/13;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.0.1-alpha.61latest2023/07/253 年前
0.0.1-alpha.602023/07/253 年前
0.0.1-alpha.592022/03/074 年前
0.0.1-alpha.582022/03/054 年前
0.0.1-alpha.572022/03/054 年前
0.0.1-alpha.562022/03/054 年前

关于 @ergosign/storybook-addon-pseudo-states-react 的常见问题

@ergosign/storybook-addon-pseudo-states-react 是什么?

Storybook decorator to enable automatic generation and displaying of CSS pseudo states for components.

如何安装 @ergosign/storybook-addon-pseudo-states-react?

在项目目录运行 npm install @ergosign/storybook-addon-pseudo-states-react,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:@storybook/addons(>=5.3.8)、@storybook/api(>=5.3.8)、@storybook/components(>=5.3.8)、@storybook/core-events(>=5.3.8)、@storybook/node-logger(>=5.3.8)、react(>=16.12.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@ergosign/storybook-addon-pseudo-states-react 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@ergosign/storybook-addon-pseudo-states-react 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@ergosign/storybook-addon-pseudo-states-react 可以商用吗?

@ergosign/storybook-addon-pseudo-states-react 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@ergosign/storybook-addon-pseudo-states-react 的下载量有多少?

根据 npm 官方下载统计,@ergosign/storybook-addon-pseudo-states-react 在 2026/10/14 – 2026/11/12 这 30 天内被下载 12,602 次,日均约 420 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@ergosign/storybook-addon-pseudo-states-react 还在维护吗?

最新版本 0.0.1-alpha.61 发布于 2023/07/25(3 年前);至今共发布 36 个版本,其中过去 12 个月发布了 0 个;首次发布于 2020/03/13。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。