导航

v2.1.0
导航

纵向导航

用于侧边栏或设置页面的纵向导航链接,当前项会微微浮起。

本页内容

纵向导航用于侧边栏或设置页面,将各个入口纵向排列。各行在交互前保持低调,当前项像分段控件的当前选项一样,以一小块凸起的面板突出显示。在 <nav> 上使用 .nav,并在每个链接上使用 nav-item。

纵向导航

<div class="tray max-w(240) mx-auto p(8)">  <nav class="nav" aria-label="Workspace">    <a class="nav-item" href="#overview" aria-current="page">      <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">        <path d="M3 13L3 6.9624A1 1 0 0 1 3.3524 6.2005L7.3524 2.8005A1 1 0 0 1 8.6476 2.8005L12.6476 6.2005A1 1 0 0 1 13 6.9624L13 13A1 1 0 0 1 12 14L4 14A1 1 0 0 1 3 13Z" /><path d="M6.5 14L6.5 11A1 1 0 0 1 7.5 10L8.5 10A1 1 0 0 1 9.5 11L9.5 14" />      </svg> Overview    </a>    <a class="nav-item" href="#projects">      <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">        <rect x="2.5" y="2.5" width="4" height="4" rx="1" /><rect x="9.5" y="2.5" width="4" height="4" rx="1" /><rect x="2.5" y="9.5" width="4" height="4" rx="1" /><rect x="9.5" y="9.5" width="4" height="4" rx="1" />      </svg> Projects <span class="nav-end">12</span>    </a>    <a class="nav-item" href="#domains">      <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">        <circle cx="8" cy="8" r="6" /><path d="M8.7707 2.0497A6.0005 6.0005 0 0 0 7.2293 2.0497Q6.1162 3.1938 5.4813 4.6077Q4.8542 6.0044 4.7648 7.5H2.0209A5.9913 5.9913 0 0 0 2.0209 8.5H4.7648Q4.8542 9.9956 5.4813 11.3923Q6.1162 12.8062 7.2293 13.9503A6.0005 6.0005 0 0 0 8.7707 13.9503Q9.8838 12.8062 10.5187 11.3923Q11.1458 9.9956 11.2352 8.5H13.9791A5.9913 5.9913 0 0 0 13.9791 7.5H11.2352Q11.1458 6.0044 10.5187 4.6077Q9.8838 3.1938 8.7707 2.0497ZM9.6064 5.0173Q10.1455 6.2181 10.2331 7.5H5.7669Q5.8545 6.2181 6.3936 5.0173Q6.971 3.7314 8 2.6921Q9.029 3.7314 9.6064 5.0173ZM6.3936 10.9827Q5.8545 9.7819 5.7669 8.5H10.2331Q10.1455 9.7819 9.6064 10.9827Q9.029 12.2686 8 13.3079Q6.971 12.2686 6.3936 10.9827Z" fill="currentColor" stroke="none" fill-rule="evenodd" />      </svg> Domains    </a>    <a class="nav-item" aria-disabled="true">      <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">        <rect x="1.6667" y="3.3333" width="12.6667" height="9.3333" rx="1" /><path d="M1.6667 6.6667L14.3333 6.6667" /><path d="M4 10L6.6667 10" />      </svg> Billing    </a>  </nav></div>
import HomeIcon from '@master/icons-react/native/16/soft/home'import GridIcon from '@master/icons-react/native/16/soft/grid'import GlobeIcon from '@master/icons-react/native/16/soft/globe'import CreditCardIcon from '@master/icons-react/native/16/soft/credit-card'<div className="tray max-w(240) mx-auto p(8)">  <nav className="nav" aria-label="Workspace">    <a className="nav-item" href="#overview" aria-current="page">      <HomeIcon className="icon" /> Overview    </a>    <a className="nav-item" href="#projects">      <GridIcon className="icon" /> Projects <span className="nav-end">12</span>    </a>    <a className="nav-item" href="#domains"><GlobeIcon className="icon" /> Domains</a>    <a className="nav-item" aria-disabled="true"><CreditCardIcon className="icon" /> Billing</a>  </nav></div>
<script>  import HomeIcon from '@master/icons-svelte/native/16/soft/home'  import GridIcon from '@master/icons-svelte/native/16/soft/grid'  import GlobeIcon from '@master/icons-svelte/native/16/soft/globe'  import CreditCardIcon from '@master/icons-svelte/native/16/soft/credit-card'</script><div class="tray max-w(240) mx-auto p(8)">  <nav class="nav" aria-label="Workspace">    <a class="nav-item" href="#overview" aria-current="page"><HomeIcon class="icon" /> Overview</a>    <a class="nav-item" href="#projects">      <GridIcon class="icon" /> Projects <span class="nav-end">12</span>    </a>    <a class="nav-item" href="#domains"><GlobeIcon class="icon" /> Domains</a>    <a class="nav-item" aria-disabled="true"><CreditCardIcon class="icon" /> Billing</a>  </nav></div>
<script setup>import HomeIcon from '@master/icons-vue/native/16/soft/home'import GridIcon from '@master/icons-vue/native/16/soft/grid'import GlobeIcon from '@master/icons-vue/native/16/soft/globe'import CreditCardIcon from '@master/icons-vue/native/16/soft/credit-card'</script><template>  <div class="tray max-w(240) mx-auto p(8)">    <nav class="nav" aria-label="Workspace">      <a class="nav-item" href="#overview" aria-current="page">        <HomeIcon class="icon" /> Overview      </a>      <a class="nav-item" href="#projects">        <GridIcon class="icon" /> Projects <span class="nav-end">12</span>      </a>      <a class="nav-item" href="#domains"><GlobeIcon class="icon" /> Domains</a>      <a class="nav-item" aria-disabled="true"><CreditCardIcon class="icon" /> Billing</a>    </nav>  </div></template>

#标签与计数

nav-label 为一组导航项添加标签,并归属于这组导航项:标签上方的空间是导航项间距的数倍,把它和前一组分开;下方的空间很小,让标签和它带领的导航项读作一组。位于列表开头时,上方只保留少许空间。nav-end 在行末放置计数、快捷键或徽章,其中的数字等宽显示。

标签与计数

<nav class="nav max-w(240) mx-auto" aria-label="Inbox">  <div class="nav-label">Inbox</div>  <a class="nav-item" href="#unread" aria-current="page">Unread <span class="nav-end">24</span></a>  <a class="nav-item" href="#mentions">Mentions <span class="nav-end">3</span></a>  <a class="nav-item" href="#archive">Archive</a>  <div class="nav-label">Teams</div>  <a class="nav-item" href="#design">Design</a>  <a class="nav-item" href="#engineering">    Engineering <span class="badge badge-sm nav-end">New</span>  </a></nav>
<nav className="nav max-w(240) mx-auto" aria-label="Inbox">  <div className="nav-label">Inbox</div>  <a className="nav-item" href="#unread" aria-current="page">    Unread <span className="nav-end">24</span>  </a>  <a className="nav-item" href="#mentions">Mentions <span className="nav-end">3</span></a>  <a className="nav-item" href="#archive">Archive</a>  <div className="nav-label">Teams</div>  <a className="nav-item" href="#design">Design</a>  <a className="nav-item" href="#engineering">    Engineering <span className="badge badge-sm nav-end">New</span>  </a></nav>
<nav class="nav max-w(240) mx-auto" aria-label="Inbox">  <div class="nav-label">Inbox</div>  <a class="nav-item" href="#unread" aria-current="page">Unread <span class="nav-end">24</span></a>  <a class="nav-item" href="#mentions">Mentions <span class="nav-end">3</span></a>  <a class="nav-item" href="#archive">Archive</a>  <div class="nav-label">Teams</div>  <a class="nav-item" href="#design">Design</a>  <a class="nav-item" href="#engineering">    Engineering <span class="badge badge-sm nav-end">New</span>  </a></nav>
<template>  <nav class="nav max-w(240) mx-auto" aria-label="Inbox">    <div class="nav-label">Inbox</div>    <a class="nav-item" href="#unread" aria-current="page">      Unread <span class="nav-end">24</span>    </a>    <a class="nav-item" href="#mentions">Mentions <span class="nav-end">3</span></a>    <a class="nav-item" href="#archive">Archive</a>    <div class="nav-label">Teams</div>    <a class="nav-item" href="#design">Design</a>    <a class="nav-item" href="#engineering">      Engineering <span class="badge badge-sm nav-end">New</span>    </a>  </nav></template>

#分组

分组使用带 nav-group 的原生 <details> 元素。其中的 <summary> 是一行,带有 nav-item 和 nav-toggle,并在末端包含一个 nav-chevron 图标。下方各行放在 nav-sub 内,沿细引导线缩进。展开、折叠及键盘操作由浏览器处理。页面内查找命中已折叠的分组时,浏览器会展开该分组。支持 interpolate-size 时,分组会逐渐展开至自身高度,其余情况下会立即展开。

分组

<nav class="nav max-w(240) mx-auto" aria-label="Project">  <a class="nav-item" href="#overview">    <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">      <path d="M3 13L3 6.9624A1 1 0 0 1 3.3524 6.2005L7.3524 2.8005A1 1 0 0 1 8.6476 2.8005L12.6476 6.2005A1 1 0 0 1 13 6.9624L13 13A1 1 0 0 1 12 14L4 14A1 1 0 0 1 3 13Z" /><path d="M6.5 14L6.5 11A1 1 0 0 1 7.5 10L8.5 10A1 1 0 0 1 9.5 11L9.5 14" />    </svg> Overview  </a>  <details class="nav-group" open>    <summary class="nav-item nav-toggle">      <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">        <path d="M7.5892 2.215A0.4996 0.4996 0 0 0 7.5 2.5L7.5006 10.0245A0.5 0.5 0 0 0 8.4946 10.0734L8.5 10L8.4986 2.4621A0.5007 0.5007 0 0 0 8.4108 2.215A0.7044 0.7044 0 0 0 7.5892 2.215Z" fill="currentColor" stroke="none" fill-rule="evenodd" /><path d="M5 5.5L7.8586 2.6414A0.2 0.2 0 0 1 8.1414 2.6414L11 5.5" /><path d="M2.5 10.5L2.5 12.5A1 1 0 0 0 3.5 13.5L12.5 13.5A1 1 0 0 0 13.5 12.5L13.5 10.5" />      </svg> Deployments<svg class="icon nav-chevron" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">        <path d="M6 3.5L10.3586 7.8586A0.2 0.2 0 0 1 10.3586 8.1414L6 12.5" transform="rotate(90 8 8)" />      </svg>    </summary>    <div class="nav-sub">      <a class="nav-item" href="#production" aria-current="page">Production</a>      <a class="nav-item" href="#preview">Preview</a>    </div>  </details>  <details class="nav-group">    <summary class="nav-item nav-toggle">      <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">        <path d="M2.5 4.5L4.5 4.5" /><circle cx="6" cy="4.5" r="1.5" /><path d="M7.5 4.5L13.5 4.5" /><path d="M2.5 11.5L8.5 11.5" /><circle cx="10" cy="11.5" r="1.5" /><path d="M11.5 11.5L13.5 11.5" />      </svg> Settings<svg class="icon nav-chevron" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">        <path d="M6 3.5L10.3586 7.8586A0.2 0.2 0 0 1 10.3586 8.1414L6 12.5" transform="rotate(90 8 8)" />      </svg>    </summary>    <div class="nav-sub">      <a class="nav-item" href="#general">General</a>      <a class="nav-item" href="#environment">Environment</a>    </div>  </details></nav>
import HomeIcon from '@master/icons-react/native/16/soft/home'import UploadIcon from '@master/icons-react/native/16/soft/upload'import ChevronDownIcon from '@master/icons-react/native/16/soft/chevron-down'import SlidersIcon from '@master/icons-react/native/16/soft/sliders'<nav className="nav max-w(240) mx-auto" aria-label="Project">  <a className="nav-item" href="#overview"><HomeIcon className="icon" /> Overview</a>  <details className="nav-group" open>    <summary className="nav-item nav-toggle">      <UploadIcon className="icon" /> Deployments<ChevronDownIcon className="icon nav-chevron" />    </summary>    <div className="nav-sub">      <a className="nav-item" href="#production" aria-current="page">Production</a>      <a className="nav-item" href="#preview">Preview</a>    </div>  </details>  <details className="nav-group">    <summary className="nav-item nav-toggle">      <SlidersIcon className="icon" /> Settings<ChevronDownIcon className="icon nav-chevron" />    </summary>    <div className="nav-sub">      <a className="nav-item" href="#general">General</a>      <a className="nav-item" href="#environment">Environment</a>    </div>  </details></nav>
<script>  import HomeIcon from '@master/icons-svelte/native/16/soft/home'  import UploadIcon from '@master/icons-svelte/native/16/soft/upload'  import ChevronDownIcon from '@master/icons-svelte/native/16/soft/chevron-down'  import SlidersIcon from '@master/icons-svelte/native/16/soft/sliders'</script><nav class="nav max-w(240) mx-auto" aria-label="Project">  <a class="nav-item" href="#overview"><HomeIcon class="icon" /> Overview</a>  <details class="nav-group" open>    <summary class="nav-item nav-toggle">      <UploadIcon class="icon" /> Deployments<ChevronDownIcon class="icon nav-chevron" />    </summary>    <div class="nav-sub">      <a class="nav-item" href="#production" aria-current="page">Production</a>      <a class="nav-item" href="#preview">Preview</a>    </div>  </details>  <details class="nav-group">    <summary class="nav-item nav-toggle">      <SlidersIcon class="icon" /> Settings<ChevronDownIcon class="icon nav-chevron" />    </summary>    <div class="nav-sub">      <a class="nav-item" href="#general">General</a>      <a class="nav-item" href="#environment">Environment</a>    </div>  </details></nav>
<script setup>import HomeIcon from '@master/icons-vue/native/16/soft/home'import UploadIcon from '@master/icons-vue/native/16/soft/upload'import ChevronDownIcon from '@master/icons-vue/native/16/soft/chevron-down'import SlidersIcon from '@master/icons-vue/native/16/soft/sliders'</script><template>  <nav class="nav max-w(240) mx-auto" aria-label="Project">    <a class="nav-item" href="#overview"><HomeIcon class="icon" /> Overview</a>    <details class="nav-group" open>      <summary class="nav-item nav-toggle">        <UploadIcon class="icon" /> Deployments<ChevronDownIcon class="icon nav-chevron" />      </summary>      <div class="nav-sub">        <a class="nav-item" href="#production" aria-current="page">Production</a>        <a class="nav-item" href="#preview">Preview</a>      </div>    </details>    <details class="nav-group">      <summary class="nav-item nav-toggle">        <SlidersIcon class="icon" /> Settings<ChevronDownIcon class="icon nav-chevron" />      </summary>      <div class="nav-sub">        <a class="nav-item" href="#general">General</a>        <a class="nav-item" href="#environment">Environment</a>      </div>    </details>  </nav></template>

#无障碍

  • 用带有无障碍名称的 <nav> 包裹列表,并用 aria-current="page" 标记当前页面。
  • 链接不可用时,使用 aria-disabled="true" 并移除 href。
  • 在一个页面内切换同级页面时,使用标签页。显示当前页面的上级路径时,使用面包屑导航。

#类名

类名说明
nav用于带标签的 nav 元素,呈现导航列表。
nav-item用链接、按钮或分组的 summary 作为导航项。交互前保持低调,当前项呈现为小型凸起面板。
nav-end位于行末的计数、快捷键或徽标,数字采用等宽字形。
nav-label一组导航项上方的弱化标签,离它带领的导航项近,离前一组远
nav-group用 details 将一组行折叠在 summary 下方。
nav-toggle用于分组的 summary:该行在行末容纳分组的折角箭头,分组展开时箭头翻转与 nav-item 一同使用
nav-chevron分组 summary 末端的折角箭头图标,分组展开时翻转
nav-sub组内各行相对图标列缩进,并沿细线对齐

#自定义属性

属性默认值说明
--nav-item-height2rem行高,触屏上至少为 2.75rem
--nav-item-radiusvar(--radius-control-sm)行圆角半径

#源码

@master/ui/components/nav.css
/* * Nav: a vertical list of destinations for a sidebar or a settings page. * Rows are quiet until touched; the current one rises as a small sheet, the way the current option of a * segmented control does. A group is a native <details>, so it folds without script. */@layer components.ui {  .nav {    --nav-item-height: 2rem;    --nav-item-radius: var(--radius-control-sm);    --icon-size: 1rem;    display: flex;    flex-direction: column;    gap: 2px;    min-width: 0;    margin: 0;    padding: 0;    font-size: .84375rem;    line-height: 1.2;    list-style: none;  }  /* A destination: a link with aria-current="page", a button, or the <summary> of a group. */  .nav-item {    --icon-offset: -.0625rem;    --icon-color: var(--color-text-muted);    position: relative;    display: flex;    align-items: center;    gap: .625rem;    min-width: 0;    min-height: var(--nav-item-height);    margin: 0;    /* One line is centred by the height; the block padding shows only when a label wraps, and keeps it off the edge. */    padding: .25rem .5rem;    border: 0;    border-radius: calc(var(--nav-item-radius) * var(--corner-scale));    corner-shape: var(--corner-shape);    background: transparent;    color: var(--color-text-body);    font: inherit;    font-feature-settings: inherit;    text-align: start;    text-decoration: none;    list-style: none;    appearance: none;    user-select: none;    -webkit-user-select: none;    -webkit-tap-highlight-color: transparent;    overflow-wrap: anywhere;    cursor: pointer;    touch-action: manipulation;    &::-webkit-details-marker {      display: none;    }    /* A row answers at once: no fade between rows as the pointer travels. */    @media (hover: hover) {      &:hover:not([aria-current="page"], [aria-selected="true"], :disabled, [aria-disabled="true"]) {        --icon-color: currentColor;        background: var(--color-surface-chip);        color: var(--color-text-strong);      }    }    /* Pressed: a step deeper than the pointer's tone, so a tap is answered where there is no pointer. */    &:active:not([aria-current="page"], [aria-selected="true"], :disabled, [aria-disabled="true"]) {      background: var(--color-surface-inset-hover);      color: var(--color-text-strong);    }    &:is([aria-current="page"], [aria-selected="true"]) {      --icon-color: currentColor;      @apply --surface-sheet;      color: var(--color-text-strong);      box-shadow: var(--shadow-raised);    }    /* The keys leave an ink outline inside the row, as in a menu, so focus and hover stay different facts. */    &:focus-visible {      outline: 2px solid var(--color-focus);      outline-offset: -2px;    }    &:is(:disabled, [aria-disabled="true"]) {      --icon-color: currentColor;      color: var(--color-text-disabled);      cursor: not-allowed;    }    @media (pointer: coarse) {      min-height: max(var(--nav-item-height), 2.75rem);    }    @media (forced-colors: active) {      &:is([aria-current="page"], [aria-selected="true"]) {        outline: 2px solid Highlight;        outline-offset: -2px;      }      /* Written after the current row's outline, of equal weight, so the row the keys are on still says so. */      &:focus-visible {        outline-color: CanvasText;      }      &:is(:disabled, [aria-disabled="true"]) {        color: GrayText;      }    }  }  /* A count, a shortcut or a badge at the end of a row. */  .nav-end {    margin-inline-start: auto;    padding-inline-start: .75rem;    color: var(--color-text-muted);    font-size: .75rem;    font-variant-numeric: tabular-nums;  }  /*   * A quiet caption over a set of rows. It belongs to the rows under it: the room above parts it from the set before,   * several times the 2px between rows, and the room below is small, so the caption reads with the set it leads.   * First in the list, it has nothing to part from and keeps only a little room.   */  .nav-label {    margin: 0;    padding: 1.25rem .5rem .25rem;    color: var(--color-text-muted);    font-size: .71875rem;    &:first-child {      padding-block-start: .25rem;    }  }  /* A <details> that folds a set of rows under its summary; the chevron turns over when it is open. */  .nav-group {    display: flex;    flex-direction: column;    gap: 2px;    min-width: 0;    interpolate-size: allow-keywords;    &::details-content {      block-size: 0;      overflow-y: clip;      opacity: 0;      transition-property: block-size, opacity, content-visibility;      transition-duration: var(--duration-base);      transition-timing-function: var(--easing-glide);      transition-behavior: allow-discrete;    }    &[open]::details-content {      block-size: auto;      opacity: 1;    }    &[open] .nav-toggle .nav-chevron {      rotate: x 180deg;    }    /* A closed group nested in an open one keeps its own chevron. */    &[open] .nav-group:not([open]) .nav-toggle .nav-chevron {      rotate: none;    }    @media (prefers-reduced-motion: reduce) {      &::details-content {        transition-property: opacity, content-visibility;      }    }  }  /* The summary of a group: a row with a chevron at its end. */  .nav-toggle .nav-chevron {    flex: none;    width: .875rem;    height: .875rem;    /* At the end of the row, without the offset a row gives its leading icon. */    margin-inline: auto 0;    color: var(--color-text-muted);    transition: rotate var(--duration-base) var(--easing-glide);    @media (prefers-reduced-motion: reduce) {      transition: none;    }    /* The chevron is a fill cut by a mask: kept out of forced colors, or the canvas would paint over it. */    @media (forced-colors: active) {      color: CanvasText;    }  }  /*   * A folded group that holds the current page says so on its own row: strong ink and a dot before the chevron,   * so the way to the current page stays in sight, apart from a group that is merely open. Beside a nav-end the   * dot follows it instead of sharing the free space with it.   */  .nav-group:not([open]):has(.nav-item[aria-current="page"]) .nav-toggle {    --icon-color: currentColor;    color: var(--color-text-strong);    &::before {      content: '';      flex: none;      order: 1;      width: .3125rem;      height: .3125rem;      margin-inline-start: auto;      border-radius: 50%;      corner-shape: round;      background: currentColor;    }    &:has(.nav-end)::before {      margin-inline-start: .5rem;    }    &::after {      order: 2;      margin-inline-start: .375rem;    }    /* The dot is a fill: kept out of forced colors, as the chevron is, or the canvas would paint over it. */    @media (forced-colors: active) {      &::before {        forced-color-adjust: none;        background: CanvasText;      }    }  }  /* Rows under a group, set in from the icon column on a hairline guide. */  .nav-sub {    display: flex;    flex-direction: column;    gap: 2px;    margin-block: 2px;    margin-inline: calc(.5rem + var(--icon-size) / 2) 0;    padding-block: 0;    padding-inline: calc(.625rem + var(--icon-size) / 2 - 1px) 0;    border-inline-start: 1px solid var(--color-line-hair);    list-style: none;  }}