@bbc/psammead-navigation

中文文档
9.2.22发布于 2022/04/13Apache-2.0ESM + CJS近 30 天下载 4500

A navigation bar to use on index pages

原始使用文档
文档目录22 节
  1. Description
  2. Exports
  3. Installation
  4. Props
  5. Navigation
  6. NavigationUl
  7. NavigationLi
  8. ScrollableNavigation
  9. CanonicalDropdown
  10. AmpDropdown
  11. DropdownLi
  12. CanonicalMenuButton
  13. AmpMenuButton
  14. Navigation Usage
  15. Dropdown Navigation Usage
  16. Canonical Menu Button Usage
  17. Amp Menu Button Usage
  18. When to use this component
  19. Accessibility notes
  20. Contributing
  21. Code of Conduct
  22. License

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

Description

The @bbc/psammead-navigation package is a set of two components, NavigationUl and NavigationLi. They use ul and li HTML elements respectively. The package also includes a Hamburger Menu and a Dropdown Navigation which are visible for breakpoints under 600px.

Exports

/dropdown - Adds a dropdown navigation with hamburger menu which is visible for breakpoints under 600px /scrollable - Makes the navigation to be scrollable under 600px.

Installation

npm install @bbc/psammead-navigation

Props

Navigation

ArgumentTypeRequiredDefaultExample
childrennodeYesN/A<ScrollableNavigation dir={dir}><NavigationUl><NavigationLi url="/" script={latin} active="true">Home</NavigationLi><NavigationLi url="/sport" script={latin}>{Sport}</NavigationLi></NavigationUl><ScrollableNavigation/>
dirstringNo'ltr''rtl'
isOpenbooleanNofalsetrue
ampOpenClassstringNonull'open'
brandBackgroundColourstringYesN/A'#B80000'
brandForegroundColourstringYesN/A'#FDFDFD'
brandBorderColourstringYesN/A'#EAB3B3'
brandHighlightColourstring YesNoN/A'#FFFFFF'

NavigationUl

ArgumentTypeRequiredDefaultExample
childrennodeYesN/A<NavigationLi url="/" script={latin} active="true">Home</NavigationLi><NavigationLi url="/sport" script={latin}>{Sport}</NavigationLi>

NavigationLi

ArgumentTypeRequiredDefaultExample
urlstringYesN/A/sport
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', }, }, }
activebooleanNofalsetrue
currentPageTextstringNonullCurrent page
servicestringYesN/A'news'
dirstringNo'ltr''rtl'
brandForegroundColourstringYesN/A'#FDFDFD'
brandBorderColourstringYesN/A'#EAB3B3'
brandHighlightColourstring YesNoN/A'#FFFFFF'

ScrollableNavigation

ArgumentTypeRequiredDefaultExample
childrennodeYesN/A<NavigationUl><NavigationLi url="/" script={latin} active="true">Home</NavigationLi><NavigationLi url="/sport" script={latin}>{Sport}</NavigationLi></NavigationUl>
dirstringNo'ltr''rtl'
brandBackgroundColourstringYesN/A'#B80000'
brandHighlightColourstring YesNoN/A'#FFFFFF'

CanonicalDropdown

ArgumentTypeRequiredDefaultExample
childrennodeYesN/A<DropdownUl><DropdownLi script={latin} service='news' key='sport' url='/sport' active="false"> Sport </DropdownLi></DropdownUl>
isOpenboolYesN/Afalse

AmpDropdown

ArgumentTypeRequiredDefaultExample
childrennodeYesN/A<DropdownUl><DropdownLi script={latin} service='news' key='sport' url='/sport' active="false"> Sport </DropdownLi></DropdownUl>

DropdownLi

ArgumentTypeRequiredDefaultExample
childrenstringYesN/A'Sport'
urlstringYesN/A/sport
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'
activebooleanNofalsetrue
currentPageTextstringNonullCurrent page
dirstringNoltrrtl

CanonicalMenuButton

ArgumentTypeRequiredDefaultExample
announcedTextstringYesN/A'Menu'
onClickfunctionYesN/A() => { console.log("Handle onClick action"); }
isOpenboolYesN/Afalse
dirstringno'ltr''rtl'
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', }, }, }

AmpMenuButton

ArgumentTypeRequiredDefaultExample
announcedTextstringYesN/A'Menu'
onTogglestringYesN/A"menu.toggleVisibility" - must be an action on a valid AMP target
dirstringno'ltr''rtl'
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', }, }, }

Navigation Usage

import React from 'react';
import { ScrollableNavigation } from '@bbc/psammead-navigation/scrollable';
import Navigation, {
  NavigationUl,
  NavigationLi,
} from '@bbc/psammead-navigation';
import { latin } from '@bbc/gel-foundations/scripts';

<Navigation
  brandBackgroundColour='#B80000'
  brandForegroundColour='#FDFDFD'
  brandBorderColour='#EAB3B3'
  brandHighlightColour='#FFFFFF'
>
  <ScrollableNavigation
    brandBackgroundColour='#B80000'
    brandHighlightColour='#FFFFFF'
  >
    <NavigationUl>
      <NavigationLi
        url="/"
        script={latin}
        active
        currentPageText="Current Page"
        service="news"
        brandForegroundColour='#FDFDFD'
        brandBorderColour='#EAB3B3'
        brandHighlightColour='#FFFFFF'
      >
        Home
      </NavigationLi>
      <NavigationLi
        url="/sport"
        script={latin}
        service="news"
        brandForegroundColour='#FDFDFD'
        brandBorderColour='#EAB3B3'
        brandHighlightColour='#FFFFFF'
      >
        {Sport}
      </NavigationLi>
      <NavigationLi
        url="/weather"
        script={latin}
        service="news"
        brandForegroundColour='#FDFDFD'
        brandBorderColour='#EAB3B3'
        brandHighlightColour='#FFFFFF'
      >
        {Weather}
      </NavigationLi>
    </NavigationUl>
  </ScrollableNavigation>
</Navigation>;

Dropdown Navigation Usage

import React from 'react';
import {
  Dropdown,
  DropdownUl,
  DropdownLi,
} from '@bbc/psammead-navigation/dropdown';
import { latin } from '@bbc/gel-foundations/scripts';

<CanonicalDropdown isOpen={isOpen}>
  <DropdownUl>
    <DropdownLi
      script={latin}
      service="news"
      key="Home"
      url="/"
      active
      currentPageText="Current page"
    >
      Home
    </DropdownLi>
    <DropdownLi script={latin} service="news" key="Sport" url="/sport">
      Sport
    </DropdownLi>
  </DropdownUl>
</CanonicalDropdown>

Canonical Menu Button Usage

import React from 'react';
import { CanonicalMenuButton } from '@bbc/psammead-navigation/dropdown';
import { latin } from '@bbc/gel-foundations/scripts';

<CanonicalMenuButton
  announcedText="Menu"
  isOpen
  dir={dir}
  onClick={() => {
    console.log('Handle onClick action');
  }}
  script={latin}
/>

Amp Menu Button Usage

import React from 'react';
import { AmpMenuButton } from '@bbc/psammead-navigation/dropdown';
import { latin } from '@bbc/gel-foundations/scripts';

<AmpMenuButton
  announcedText="Menu"
  onToggle="menu.toggleVisibility"
  script={latin}
  dir={dir}
/>

Note that in order for the AmpMenuButton toggling to work correctly, an id should be added to the Navigation component. This id can be passed in as a prop to the component. Similarly, ScrollableNavigation also requires an id to be added to it.

When to use this component

The Navigation is designed to show a navigation bar on all pages, which will show all sections on a site. If there are too many items to fit on one line, the items will wrap to the next lines.

On the other hand, with ScrollableNavigation we can make the list to remain on one line and to be horizontally scrollable to allow access to further links, under 600px.

Accessibility notes

The Navigation has a navigation landmark to provide a way to identify links that are intended to be used for navigation.

We have added the role list and listitem to the NavigationUl and NavigationList respectively, due to a VoiceOver bug to reinstate the list semantics.

We have also added visually hidden text to let the user know which item in both regular and dropdown Navigation is the current page. Note the use of visually hidden text here is due to lack of support at this time for the aria-current page attribute. Also note the use of role="text" to stop text splitting in VoiceOver.

We have added an aria-expanded attribute to the menu button to indicate whether the menu is collapsed or expanded.

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-navigation 共发布 151 个版本,首次发布于 2019/06/25;过去 12 个月发布了 0 个版本。

版本发布日期距今
9.2.22latest2022/04/134 年前
9.2.212022/04/084 年前
9.2.202022/03/304 年前
9.2.192022/03/294 年前
9.2.182022/03/284 年前
9.2.172022/01/064 年前

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

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

@bbc/psammead-navigation 是什么?

A navigation bar to use on index pages

如何安装 @bbc/psammead-navigation?

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

@bbc/psammead-navigation 支持 TypeScript 吗?

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

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

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

@bbc/psammead-navigation 可以商用吗?

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

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

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

@bbc/psammead-navigation 还在维护吗?

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