@bbc/psammead-storybook-helpers

中文文档
9.0.17发布于 2021/09/30Apache-2.0ESM + CJS近 30 天下载 9529

A collection of common values that are used in storybook by the Psammead components.

原始使用文档
文档目录9 节
  1. Exports
  2. Installation
  3. Usage
  4. TEXT_VARIANTS
  5. withServicesKnob
  6. buildRTLSubstories
  7. Contributing
  8. Code of Conduct
  9. License

psammead-storybook-helpers - Known Vulnerabilities Dependency Status peerDependencies Status GitHub license npm version PRs Welcome

This package provides a collection of common values that are used in storybook by the Psammead components.

Exports

TEXT_VARIANTS - A list of text samples in different languages, with the script and direction that should be used for that language.

themes - An object containing the Storybook themes we use.

withServicesKnob - Is a function that returns a storybook decorator function that adds a Select a service dropdown to the knobs panel. When a service is selected from the dropdown it does 2 things:

  1. Provides the decorated stories with the following properties that can be passed into components:
  • text: A short string of text in the language of the chosen service.
  • longText: A long string of text in the language of the chosen service (we can use this to stress test components).
  • dir: The reading directionality of the chosen service e.g. ltr or rtl.
  • script: The chosen service's script typography settings e.g. the font-size and line-heights.
  • service: The name of the chosen service e.g. arabic.
  • variant: The variant value of a chosen service, e.g serbianLat will have variant lat. Non variant service will default to default.
  • articlePath: A path to an article in the relevant service.
  • selectedService: The name of the selected service as it appears in the dropdown of available services.
  1. Toggles the layout directionality of the chosen service.

The withServicesKnob function accepts an options argument with 2 properties:

  • defaultService(String): The default selected service of the services dropdown e.g. arabic. The default is news.
  • services(Array): A list of services that the dropdown will display. The default is all services.

buildRTLSubstories - a function to create right-to-left variants of stories as substories. Internally it uses the withServicesKnob to set the default service as arabic.

The buildRTLSubstories function accepts 2 arguments.

  • storyKind(String) - This is the story kind that you want you want to create RTL substories from. This will normally be the first argument you pass into storiesOf e.g. storiesOf('Components/Paragraph', module). This parameter is required.
  • options(Object) - Available options:
    • include(Array) - A list of specific story names to create RTL substories of. If this is not provided then all stories will have RTL substories.

Installation

npm install @bbc/psammead-storybook-helpers --save-dev

Usage

TEXT_VARIANTS

import { select } from '@storybook/addon-knobs';
import { TEXT_VARIANTS } from '@bbc/psammead-storybook-helpers';

const label = 'Languages';
const defaultValue = 'This is a caption';
const groupIdentifier = 'CAPTION VARIANTS';

<Caption>
  {select(label, TEXT_VARIANTS, TEXT_VARIANTS.news, groupIdentifier).text}
</Caption>;

withServicesKnob

storiesOf('Components/Paragraph', module)
  .addDecorator(withKnobs)
  .addDecorator(withServicesKnob()) // default selected service is `news`
  .add('A paragraph with English text', ({ text, script, service }) => (
    <Paragraph script={script} service={service}>
      {text}
    </Paragraph>
  ));

To set a default service:

storiesOf('Components/Paragraph', module)
  .addDecorator(withKnobs)
  .addDecorator(
    withServicesKnob({
      defaultService: 'arabic',
      services: ['news', 'arabic', 'amharic'],
    }),
  ) // default selected service is `arabic` and the available services in the dropdown are `news`, `arabic`, `amharic`
  .add('A paragraph with Arabic text', ({ text, script, service }) => (
    <Paragraph script={script} service={service}>
      {text}
    </Paragraph>
  ));

If you want to add this decorator to a single story rather than a series of stories as documented above, perhaps because you need each story to have a different default service, then you need to decorate each story directly instead of using the addDecorator method. An example of how you could write this is shown below:

const arabicServiceDecorator = withServicesKnob({
  defaultService: 'arabic',
});

const pashtoServiceDecorator = withServicesKnob({
  defaultService: 'pashto',
});

storiesOf('Components/Paragraph', module)
  .addDecorator(withKnobs)
  .add('A paragraph with Arabic text', () =>
    arabicServiceDecorator(({ text, script, service }) => (
      <Paragraph script={script} service={service}>
        {text}
      </Paragraph>
    )),
  )
  .add('A paragraph with Pashto text', () =>
    pashtoServiceDecorator(({ text, script, service }) => (
      <Paragraph script={script} service={service}>
        {text}
      </Paragraph>
    )),
  );

You can include links to articles in the relevant services and variants

const BASE_URL = 'https://www.bbc.com';

storiesOf('Components/Paragraph/Link', module)
  .addDecorator(withKnobs)
  .addDecorator(withServicesKnob()) // default selected service is `news`
  .add(
    'A paragraph with an inline link to an article',
    ({ text, script, service, articlePath }) => (
      <Paragraph script={script} service={service}>
        <InlineLink href={`${BASE_URL}${articlePath}`}>{text}</InlineLink>
      </Paragraph>
    ),
  );

The above example dismisses the use of the addDecorator method and decorates the story directly.

buildRTLSubstories

import { buildRTLSubstories } from '@bbc/psammead-storybook-helpers';

// create RTL variants of all stories of a kind
buildRTLSubstories('Components/Paragraph');
import { buildRTLSubstories } from '@bbc/psammead-storybook-helpers';

// create RTL variants of specific stories of a kind
buildRTLSubstories('Components/Paragraph', {
  include: ['containing an inline link'],
});

Contributing

When adding a new export to this utility package the export tests also need to be updated. When removing an exisiting export from this utility package the export tests need to be updated and the package version requires a major change (EG: 1.2.1 -> 2.0.0) as this would be considered a breaking change due to functionality being removed.

Psammead is completely open source. We are grateful for any contributions, whether they be new components, bug fixes or general improvements. Please see our primary contributing guide which can be found at the root of the Psammead respository.

Code of Conduct

We welcome feedback and help on this work. By participating in this project, you agree to abide by the code of conduct. Please take a moment to read it.

License

Psammead is Apache 2.0 licensed.

版本与发布记录

@bbc/psammead-storybook-helpers 共发布 79 个版本,首次发布于 2019/04/04;过去 12 个月发布了 0 个版本。

版本发布日期距今
9.0.17latest2021/09/305 年前
9.0.162021/09/175 年前
9.0.152021/09/165 年前
9.0.142021/03/165 年前
9.0.132021/03/105 年前
9.0.122021/03/025 年前

关于 @bbc/psammead-storybook-helpers 的常见问题

@bbc/psammead-storybook-helpers 是什么?

A collection of common values that are used in storybook by the Psammead components.

如何安装 @bbc/psammead-storybook-helpers?

在项目目录运行 npm install @bbc/psammead-storybook-helpers,或使用 pnpm add / yarn add / bun add。

@bbc/psammead-storybook-helpers 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/bbc__psammead-storybook-helpers 是否存在。

@bbc/psammead-storybook-helpers 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@bbc/psammead-storybook-helpers 可以商用吗?

@bbc/psammead-storybook-helpers 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@bbc/psammead-storybook-helpers 的下载量有多少?

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

@bbc/psammead-storybook-helpers 还在维护吗?

最新版本 9.0.17 发布于 2021/09/30(5 年前);至今共发布 79 个版本,其中过去 12 个月发布了 0 个;首次发布于 2019/04/04。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。