網站導覽

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,這一列的列尾放置群組的 V 形箭頭。群組展開時,箭頭翻轉。搭配 nav-item 使用
nav-chevron群組 summary 末端的 V 形箭頭圖示,群組展開時翻轉
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;  }}