@bbc/psammead-section-label

中文文档
7.1.13发布于 2022/03/30Apache-2.0ESM + CJS近 30 天下载 4714

React styled component for a section label

原始使用文档
文档目录9 节
  1. Description
  2. Installation
  3. Props
  4. Usage
  5. When to use this component
  6. Accessibility notes
  7. Contributing
  8. Code of Conduct
  9. License

psammead-section-label - Known Vulnerabilities Dependency Status peerDependencies Status Storybook GitHub license npm version PRs Welcome

Description

The @bbc/psammead-section-label package exports one component - a Section Label.

For colours and font family it uses @bbc/psammead-styles and @bbc/gel-foundations for spacing and GEL Typography implemented in Styled Components.

The only provided child should be the title for the section, provided as a string, which will be wrapped in an <h2> element by the component – (see the Accessibility notes).

Installation

npm install @bbc/psammead-section-label

Props

ArgumentTypeRequiredDefaultExample
barbooleannotruefalse
mobileDividerbooleannotruefalse
visuallyHiddenbooleannofalsetrue
childrenstringyesN/A'Most Read'
dirstringno'ltr''rtl'
hrefstringnonull'https://www.bbc/com/igbo/egwuregwu'
idstringnoN/Atop-stories-id
labelIdstringnoN/Atop-stories-label
linkTextstringnonull'See More'
scriptobjectyesN/A{ canon: { groupA: { fontSize: '28', lineHeight: '32',}, groupB: { fontSize: '32', lineHeight: '36' }, groupD: { fontSize: '44', lineHeight: '48' } }, trafalgar: { groupA: { fontSize: '20', lineHeight: '24' }, groupB: { fontSize: '24', lineHeight: '28' }, groupD: { fontSize: '32', lineHeight: '36' } } }
servicestringyesN/A'news'
backgroundColorstringnoC_GHOSTC_LUNAR
overrideHeadingAsstringnonullstrong

Usage

import SectionLabel from '@bbc/psammead-section-label';
import { latin } from '@bbc/gel-foundations/scripts';

const WrappingComponent = () => (
  <div aria-labelledby="example-section-label">
    <SectionLabel
      script={latin}
      dir="ltr"
      labelId="example-section-label"
      service="news"
    >
      Example section
    </SectionLabel>
  </div>
);

Or, without a horizontal bar:

import SectionLabel from '@bbc/psammead-section-label';
import { latin } from '@bbc/gel-foundations/scripts';

const WrappingComponent = () => (
  <div aria-labelledby="example-section-label">
    <SectionLabel
      script={latin}
      dir="ltr"
      bar={false}
      labelId="example-section-label"
      service="news"
    >
      Example section
    </SectionLabel>
  </div>
);

On mobile, this component places a dividing line above the title. This can be disabled by setting the mobileDivider prop to false:

import SectionLabel from '@bbc/psammead-section-label';
import { latin } from '@bbc/gel-foundations/scripts';

const WrappingComponent = () => (
  <div aria-labelledby="example-section-label">
    <SectionLabel
      script={latin}
      dir="ltr"
      mobileDivider={false}
      labelId="example-section-label"
      service="news"
    >
      Example section
    </SectionLabel>
  </div>
);

You can also visually hide the SectionLabel for all breakpoints by adding the visuallyHidden prop:

import SectionLabel from '@bbc/psammead-section-label';
import { latin } from '@bbc/gel-foundations/scripts';

const WrappingComponent = () => (
  <div aria-labelledby="example-section-label">
    <SectionLabel
      script={latin}
      dir="ltr"
      visuallyHidden={true}
      labelId="example-section-label"
      service="news"
    >
      Example section
    </SectionLabel>
  </div>
);

You can even use this component as a link (typically to an index of content related to the section): NB. when doing this both the href and the linkText must be supplied. If either is missing, the link will not be rendered.

import SectionLabel from '@bbc/psammead-section-label';
import { latin } from '@bbc/gel-foundations/scripts';

const WrappingComponent = () => (
  <div aria-labelledby="example-section-label">
    <SectionLabel
      script={latin}
      dir="ltr"
      href="https://www.bbc.com/news/index"
      id="example-section-label"
      linkText="See More"
      service="news"
    >
      Example section
    </SectionLabel>
  </div>
);

You can override the header(h2) to any custom HTML element that fits your accesibility requirement depending on what use case or semantic meaning you want to portray in your pages with the overrideHeadingAs prop.

import SectionLabel from '@bbc/psammead-section-label';
import { latin } from '@bbc/gel-foundations/scripts';

const WrappingComponent = () => (
  <div aria-labelledby="example-section-label">
    <SectionLabel
      script={latin}
      dir="ltr"
      labelId="example-section-label"
      service="news"
      overrideHeadingAs="strong"
    >
      Example section
    </SectionLabel>
  </div>
);

When to use this component

This component should be used to signal the beginning of a grouping of story promos. It should not wrap the story promos or contain any content other than that section's title (aka 'strapline').

Accessibility notes

Although this component has the appearance of a horizontal rule, it does not use an <hr> tag, and therefore does not have the associated semantic meaning.

This component wraps the title string in an <h2> element. The labelId prop will be applied to the <h2> as an id attribute, allowing the content of the element to be referenced by an aria-labelledby attribute. See the examples above.

Setting the visuallyHidden prop to true visually hides this component for all breakpoints, however it will still be available to screen-readers and other assistive technology.

When supplied with an href and the linkText, the section label contains an <a> link, which is aria-labelledby the labelId described above. The linkText is only expected to be useful to visual users, so is marked as aria-hidden="true" to prevent announcement to screen readers. This aria-hidden="true" isn't strictly required - setting the aria-labelledby attribute should prevent screen readers from reading out the linkText. However, it adding it makes clear to screen readers and other developers that the linkText is designed to be ignored by screen readers.

Contributing

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-section-label 共发布 115 个版本,首次发布于 2019/05/21;过去 12 个月发布了 0 个版本。

版本发布日期距今
7.1.13latest2022/03/304 年前
7.1.122022/03/294 年前
7.1.112022/01/064 年前
7.1.102021/10/065 年前
7.1.92021/09/305 年前
7.1.82021/09/225 年前

其他发布通道:alpha → 6.0.0-alpha.0。

关于 @bbc/psammead-section-label 的常见问题

@bbc/psammead-section-label 是什么?

React styled component for a section label

如何安装 @bbc/psammead-section-label?

在项目目录运行 npm install @bbc/psammead-section-label,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:react(>=16.9.0)、prop-types(^15.6.2)、@emotion/styled(^11.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@bbc/psammead-section-label 支持 TypeScript 吗?

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

@bbc/psammead-section-label 支持 ESM 还是 CommonJS?

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

@bbc/psammead-section-label 可以商用吗?

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

@bbc/psammead-section-label 的下载量有多少?

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

@bbc/psammead-section-label 还在维护吗?

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