@canonical/global-nav

中文文档
4.0.0发布于 2026/11/03ISCCommonJS近 30 天下载 1.7万

A script and stylesheet that displays the Canonical global nav across the top of a site

原始使用文档
文档目录10 节
  1. Important - Do not use 3.7.x versions
  2. Usage
  3. Fonts
  4. Options
  5. Building the Global nav
  6. Running the project locally
  7. Watching changed files
  8. How to add inline svgs
  9. Vanilla updates
  10. Release process

Canonical Global Nav

This project contains the JavaScript and styles to add an "All Canonical" navigation dropdown item to the Vanilla navigation pattern.

The dropdown contains a list of Canonical eco-system websites, giving a user the ability to jump around the core sites easily.

Important - Do not use 3.7.x versions

Changes regarding sliding navigations for dropdown menus were introduced to global-nav in versions 3.7.x, unaware of the fact that this project only should contain code for the "All Canonical" link displayed in the navigation of most of our websites.

All those changes have been reverted in version 3.8.0, which contains the same code of version 3.6.4 (the release before 3.7.0).

Usage

Use a node package manager to install this component and then link the JS file into the head of your site, with optional settings.

The styles will automatically be injected into the page's <head>.

  1. Install via yarn or npm
yarn add @canonical/global-nav

...or...

npm install @canonical/global-nav --save
  1. You can then install the library either by directly linking to it or via ES6 imports.

To consume the library directly, add a link to the JS file containing an IIFE and run the canonicalGlobalNav.createNav() function;

<script src="/node_modules/@canonical/global-nav/dist/iife.js"></script>
<script>
  canonicalGlobalNav.createNav();
</script>

To import it, simply call it from your site-wide JS file;

import { createNav } from '@canonical/global-nav';
createNav();
  1. You will then need to add the .global-nav class to a ul.p-navigation__items element within the navigation pattern. The module will look for this class and add the dropdown as the first item in the list.

Fonts

From version 4.0.0 the injected styles no longer include the Ubuntu font. The nav uses "Ubuntu variable" but expects the page to load it.

  • If your site uses Vanilla, you already have it: Vanilla's base styles include the font.
  • If your site hosts the font itself, nothing changes: the nav uses your @font-face rules.
  • Otherwise, add these rules to your site's CSS:
@font-face {
  font-family: 'Ubuntu variable';
  font-stretch: 100%;
  font-style: normal;
  font-weight: 100 800;
  font-display: swap;
  src: url('https://assets.ubuntu.com/v1/f1ea362b-Ubuntu%5Bwdth,wght%5D-latin-v0.896a.woff2') format('woff2');
}

@font-face {
  font-family: 'Ubuntu variable';
  font-stretch: 100%;
  font-style: italic;
  font-weight: 100 800;
  font-display: swap;
  src: url('https://assets.ubuntu.com/v1/90b59210-Ubuntu-Italic%5Bwdth,wght%5D-latin-v0.896a.woff2') format('woff2');
}

These are the same files Vanilla loads. Without them the nav falls back to a system font.

Options

The createNav function takes an object of options with the following property:

  • breakpoint: The point, in pixels, at which the navigation switches between desktop and mobile layouts. The default is 620px, which is meant to reflect the default value of $breakpoint-navigation-threshold in Vanilla (see Vanilla's breakpoint documentation).

  • mobileContainerSelector and desktopContainerSelector: Selectors (can be 'id' or 'class') of where to attach the mobile and desktop views of the global-nav. This will also circumvent the default eventListeners being attached as they are dependent on the default structure, so custom JS will be nessacary. Also in this case, the class global-nav should still be used to indicate the position of the 'All Canonical' tab button. If ony one is used it will render the default global nav.

If the $breakpoint-navigation-threshold Vanilla variable is overridden in your project, you will need to set this option on the global nav.

For example, to set the breakpoint to 1036:

<script src="/node_modules/@canonical/global-nav/dist/index.js"></script>

<script>
  canonicalGlobalNav.createNav({
    breakpoint: 1036,
    mobileContainerSelector: 'global-nav-mobile',
    desktopContainerSelector: 'global-nav-desktop',
  });
</script>

If you're importing;

import { createNav } from '@canonical/global-nav';
createNav({ breakpoint: 1036 });

Building the Global nav

To build the JS into the /dist folder, run:

./run build

Running the project locally

The simplest way to run the site locally is to first install Docker (on Linux you may need to add your user to the docker group), and then use the ./run script:

./run

You can also use the dotrun snap, by running:

dotrun

Once the containers are setup, you can visit http://127.0.0.1:8300 in your browser.

Watching changed files

For working on Sass files and JS files , you may want to dynamically watch for changes to rebuild the dist files whenever something changes.

To setup the watcher, open a new terminal window and run:

./run watch

Before submitting your pull request, run the tests - which checks both the JS and Sass for errors.

./run test

How to add inline svgs

Just because this was a bit of a pain, here is what I did.

  1. Shrink the svg as much as possible
  2. Upload it to the asset server for others - OPTIONAL
  3. View it in a browser and grab the source code.
  4. Convert the quotes from double " to single ' - CRITICAL
  5. Encode the svg
  6. Add this with data:image/svg+xml, in the right place in product-details.js

Vanilla updates

This project uses the Vanilla framework as a dependency in two ways: the SCSS files are imported directly to build the styles of the global-nav itself (via the vanilla-framework npm package, as defined in dependencied in package.json) and for styling the demo page (via direct link in the index.html file).

When updating Vanilla to latest version both dependencies should be updated.

Release process

The package is versioned using semantic versioning and published to the NPM registry.

To cut a new release run:

npm version [patch|minor|major]

This will trigger the prepublishonly script which will ensure requisite artefacts are built before publishing.

Code licensed LGPLv3 by Canonical Ltd.

With ♥ from Canonical

版本与发布记录

@canonical/global-nav 共发布 46 个版本,首次发布于 2019/03/08;过去 12 个月发布了 2 个版本。

版本发布日期距今
4.0.0latest2026/11/035 天前
3.9.02026/06/164 个月前
3.8.02025/10/211.1 年前
3.7.32025/09/031.2 年前
3.7.22025/08/121.2 年前
3.7.12025/08/041.3 年前

关于 @canonical/global-nav 的常见问题

@canonical/global-nav 是什么?

A script and stylesheet that displays the Canonical global nav across the top of a site

如何安装 @canonical/global-nav?

在项目目录运行 npm install @canonical/global-nav,或使用 pnpm add / yarn add / bun add。

@canonical/global-nav 支持 TypeScript 吗?

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

@canonical/global-nav 支持 ESM 还是 CommonJS?

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

@canonical/global-nav 可以商用吗?

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

@canonical/global-nav 的下载量有多少?

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

@canonical/global-nav 还在维护吗?

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