網站導覽

v2.1.0
網站導覽

步驟

以編號清單顯示讀者目前在簡短流程中的哪一步,例如結帳或設定流程。

本頁內容

步驟元件顯示讀者目前在結帳或設定等簡短流程中的哪一步。它以有序清單呈現,並由 CSS 計數器為標記編號。只要在目前的步驟加上 aria-current="step",其餘狀態會由樣式表判定。

步驟

<ol class="steps">  <li class="steps-item"><span class="steps-title">Account</span></li>  <li class="steps-item" aria-current="step"><span class="steps-title">Billing</span></li>  <li class="steps-item"><span class="steps-title">Confirm</span></li></ol>
<ol className="steps">  <li className="steps-item"><span className="steps-title">Account</span></li>  <li className="steps-item" aria-current="step"><span className="steps-title">Billing</span></li>  <li className="steps-item"><span className="steps-title">Confirm</span></li></ol>
<ol class="steps">  <li class="steps-item"><span class="steps-title">Account</span></li>  <li class="steps-item" aria-current="step"><span class="steps-title">Billing</span></li>  <li class="steps-item"><span class="steps-title">Confirm</span></li></ol>
<template>  <ol class="steps">    <li class="steps-item"><span class="steps-title">Account</span></li>    <li class="steps-item" aria-current="step"><span class="steps-title">Billing</span></li>    <li class="steps-item"><span class="steps-title">Confirm</span></li>  </ol></template>

#單一屬性

一個屬性就能決定三種狀態:

  • 目前的步驟帶有 aria-current="step"。它的標記像一張浮起的小面板,帶有墨色外框。
  • 排在目前步驟之前的步驟皆為已完成:.steps-item:has(~ .steps-item[aria-current="step"])。標記是鏤空勾號的墨色圓形,勾號透出底下的表面色,在兩種主題下都清楚可見。從該步驟延伸出去的線也會變成墨色。
  • 其餘步驟皆為等待中,編號顯示在凹槽裡。

將屬性移到下一個項目,三種狀態就會一併更新。整個流程結束時,已沒有目前步驟。在清單上加上 steps-complete,即可將所有步驟標為完成。

全部完成

<ol class="steps steps-complete">  <li class="steps-item"><span class="steps-title">Account</span></li>  <li class="steps-item"><span class="steps-title">Billing</span></li>  <li class="steps-item"><span class="steps-title">Confirm</span></li></ol>
<ol className="steps steps-complete">  <li className="steps-item"><span className="steps-title">Account</span></li>  <li className="steps-item"><span className="steps-title">Billing</span></li>  <li className="steps-item"><span className="steps-title">Confirm</span></li></ol>
<ol class="steps steps-complete">  <li class="steps-item"><span class="steps-title">Account</span></li>  <li class="steps-item"><span class="steps-title">Billing</span></li>  <li class="steps-item"><span class="steps-title">Confirm</span></li></ol>
<template>  <ol class="steps steps-complete">    <li class="steps-item"><span class="steps-title">Account</span></li>    <li class="steps-item"><span class="steps-title">Billing</span></li>    <li class="steps-item"><span class="steps-title">Confirm</span></li>  </ol></template>

#垂直排列

steps-vertical 將步驟由上到下排列,適合窄欄,或每個步驟都需要一句說明的情況。用 steps-body 包住文字,再加入 steps-description。

垂直排列

<ol class="steps steps-vertical max-w(320) mx-auto">  <li class="steps-item">    <span class="steps-body">      <span class="steps-title">Create the project</span>      <span class="steps-description">Named atlas, in the Master workspace.</span>    </span>  </li>  <li class="steps-item" aria-current="step">    <span class="steps-body">      <span class="steps-title">Connect a repository</span>      <span class="steps-description">Choose where the code lives.</span>    </span>  </li>  <li class="steps-item">    <span class="steps-body">      <span class="steps-title">Deploy</span>      <span class="steps-description">The first build takes about a minute.</span>    </span>  </li></ol>
<ol className="steps steps-vertical max-w(320) mx-auto">  <li className="steps-item">    <span className="steps-body">      <span className="steps-title">Create the project</span>      <span className="steps-description">Named atlas, in the Master workspace.</span>    </span>  </li>  <li className="steps-item" aria-current="step">    <span className="steps-body">      <span className="steps-title">Connect a repository</span>      <span className="steps-description">Choose where the code lives.</span>    </span>  </li>  <li className="steps-item">    <span className="steps-body">      <span className="steps-title">Deploy</span>      <span className="steps-description">The first build takes about a minute.</span>    </span>  </li></ol>
<ol class="steps steps-vertical max-w(320) mx-auto">  <li class="steps-item">    <span class="steps-body">      <span class="steps-title">Create the project</span>      <span class="steps-description">Named atlas, in the Master workspace.</span>    </span>  </li>  <li class="steps-item" aria-current="step">    <span class="steps-body">      <span class="steps-title">Connect a repository</span>      <span class="steps-description">Choose where the code lives.</span>    </span>  </li>  <li class="steps-item">    <span class="steps-body">      <span class="steps-title">Deploy</span>      <span class="steps-description">The first build takes about a minute.</span>    </span>  </li></ol>
<template>  <ol class="steps steps-vertical max-w(320) mx-auto">    <li class="steps-item">      <span class="steps-body">        <span class="steps-title">Create the project</span>        <span class="steps-description">Named atlas, in the Master workspace.</span>      </span>    </li>    <li class="steps-item" aria-current="step">      <span class="steps-body">        <span class="steps-title">Connect a repository</span>        <span class="steps-description">Choose where the code lives.</span>      </span>    </li>    <li class="steps-item">      <span class="steps-body">        <span class="steps-title">Deploy</span>        <span class="steps-description">The first build takes about a minute.</span>      </span>    </li>  </ol></template>

如果讀者可以返回先前的步驟,將已完成步驟的標題設為連結。等待中的步驟維持純文字。

以連結呈現

<nav aria-label="Checkout">  <ol class="steps">    <li class="steps-item"><a class="steps-title link link-quiet" href="#cart">Cart</a></li>    <li class="steps-item"><a class="steps-title link link-quiet" href="#shipping">Shipping</a></li>    <li class="steps-item" aria-current="step"><span class="steps-title">Payment</span></li>    <li class="steps-item"><span class="steps-title">Review</span></li>  </ol></nav>
<nav aria-label="Checkout">  <ol className="steps">    <li className="steps-item"><a className="steps-title link link-quiet" href="#cart">Cart</a></li>    <li className="steps-item">      <a className="steps-title link link-quiet" href="#shipping">Shipping</a>    </li>    <li className="steps-item" aria-current="step"><span className="steps-title">Payment</span></li>    <li className="steps-item"><span className="steps-title">Review</span></li>  </ol></nav>
<nav aria-label="Checkout">  <ol class="steps">    <li class="steps-item"><a class="steps-title link link-quiet" href="#cart">Cart</a></li>    <li class="steps-item"><a class="steps-title link link-quiet" href="#shipping">Shipping</a></li>    <li class="steps-item" aria-current="step"><span class="steps-title">Payment</span></li>    <li class="steps-item"><span class="steps-title">Review</span></li>  </ol></nav>
<template>  <nav aria-label="Checkout">    <ol class="steps">      <li class="steps-item"><a class="steps-title link link-quiet" href="#cart">Cart</a></li>      <li class="steps-item">        <a class="steps-title link link-quiet" href="#shipping">Shipping</a>      </li>      <li class="steps-item" aria-current="step"><span class="steps-title">Payment</span></li>      <li class="steps-item"><span class="steps-title">Review</span></li>    </ol>  </nav></template>

#無障礙

  • 使用 <ol>,因為步驟順序本身具有意義,而且清單會讓輔助科技報讀步驟總數。
  • aria-current="step" 會告知輔助科技目前是哪一步。勾號和墨色只提供視覺提示,螢幕閱讀器使用者可從清單順序判斷哪些步驟已完成。
  • 水平排列時,以四到五個簡短標題為限。步驟更多時,使用 steps-vertical。

#類別

類別說明
steps步驟清單使用 ol。CSS 計數器為標記編號,相鄰標記以線條相連
steps-vertical將步驟逐一垂直排列,適用於窄欄或每個步驟都有一句說明的情況。搭配 steps 使用
steps-complete所有步驟均已完成,目前沒有進行中的步驟。搭配 steps 使用
steps-item每個步驟使用 li。待進行時,標記是凹槽中的數字。當目前步驟移到它後方時,標記會變成以前景色填滿、帶有勾號的圓形。
steps-body步驟同時有標題和說明時,將兩者包在一起
steps-title步驟名稱在橫向清單中維持單行,在垂直清單中可換行。
steps-description標題下方的一行低調文字。

#自訂屬性

屬性預設值說明
--steps-marker1.5rem標記直徑

#原始碼

@master/ui/components/steps.css
/* * Steps: where a reader is in a short sequence. An ordered list whose markers are numbered by a CSS counter. * Only the current step is marked, with aria-current="step": the steps before it are complete and take ink * and a check, and the steps after it wait in a well. A line joins each marker to the next. When the whole * sequence is done, steps-complete on the list completes every step. */@layer components.ui {  .steps {    --steps-marker: 1.5rem;    --steps-gap: .75rem;    display: flex;    gap: var(--steps-gap);    min-width: 0;    margin: 0;    padding: 0;    color: var(--color-text-muted);    font-size: .8125rem;    line-height: 1.4;    list-style: none;    counter-reset: steps;    /* Every step is done: none is current, and the list rests in body ink. */    &.steps-complete {      color: var(--color-text-body);    }    /* One step under another, for a narrow column or steps that carry a sentence each. */    &.steps-vertical {      flex-direction: column;      gap: 0;    }  }  .steps-item {    position: relative;    display: flex;    flex: 1 1 0;    align-items: center;    gap: .5rem;    min-width: 0;    counter-increment: steps;    /*     * The last step has nothing to lead to, so it takes only the room it needs; where there is less, it still     * shrinks, so its title is cut like the others' instead of running out of the list.     */    &:last-child {      flex: 0 1 auto;    }    /* The marker: a number in a well while the step waits. */    &::before {      content: counter(steps);      display: grid;      flex: none;      place-items: center;      width: var(--steps-marker);      height: var(--steps-marker);      border-radius: 999px;      background: var(--color-surface-inset);      color: var(--color-text-muted);      box-shadow: var(--shadow-pressed);      font: var(--font-weight-control) .71875rem/1 var(--font-family-sans);      font-variant-numeric: tabular-nums;    }    /* The line to the next step. */    &:not(:last-child)::after {      content: '';      flex: 1 1 1rem;      min-width: 1rem;      height: 1px;      background: var(--color-line-divider);    }    /* Current: the marker rises as a small sheet with an ink contour, and the words take strong ink. */    &[aria-current="step"] {      color: var(--color-text-strong);      &::before {        @apply --surface-sheet;        color: var(--color-text-strong);        box-shadow: 0 0 0 .5px var(--color-text-strong), 0 0 0 1px color-mix(in oklab, var(--color-text-strong) 22%, transparent), 0 1px 2px -1px var(--color-shade-1), 0 2px 4px -2px var(--color-shade-2);      }    }    /*     * Complete: every step that has the current one after it, or every step once the list is marked     * steps-complete. An ink disc with the check cut out of it, so the mark is the surface showing through     * and reads on moonstone and obsidian alike.     */    &:is(:has(~ .steps-item[aria-current="step"]), .steps-complete .steps-item) {      color: var(--color-text-body);      &::before {        content: '';        background: var(--color-surface-inverse);        box-shadow: none;        mask:          url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16' fill='none' stroke='black' stroke-width='1.6' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='m4 8.4 2.6 2.6 5.4-6'/%3E%3C/svg%3E") center / .875rem no-repeat,          linear-gradient(black, black);        mask-composite: exclude;      }      &::after {        background: var(--color-text-strong);      }    }    @media (forced-colors: active) {      &::before {        outline: 1px solid CanvasText;        outline-offset: -1px;      }      &[aria-current="step"]::before {        outline: 2px solid Highlight;      }      /* The ink disc would be forced to the canvas, and its check with it: it keeps the text color instead. */      &:is(:has(~ .steps-item[aria-current="step"]), .steps-complete .steps-item)::before {        forced-color-adjust: none;        background: CanvasText;      }      /* As heavy as the line's own rules, the waiting one and the completed one, so neither keeps a theme color. */      &:not(:last-child)::after, &:is(:has(~ .steps-item[aria-current="step"]), .steps-complete .steps-item)::after {        background: CanvasText;      }    }  }  /* The words of a step: a title and an optional line under it. */  .steps-body {    display: grid;    gap: .0625rem;    min-width: 0;  }  /* One line, cut with an ellipsis where the step's room ends, so it never runs over the line to the next one. */  .steps-title {    min-width: 0;    margin: 0;    overflow: hidden;    font-weight: var(--font-weight-control);    text-overflow: ellipsis;    white-space: nowrap;  }  .steps-description {    margin: 0;    color: var(--color-text-muted);    font-size: .75rem;    font-weight: 400;    text-wrap: pretty;    overflow-wrap: anywhere;  }  /* Vertical: the marker stands on a column and the line runs down from it to the next marker. */  .steps-vertical .steps-item {    flex: none;    align-items: flex-start;    gap: .75rem;    padding-block-end: 1.25rem;    &:last-child {      padding-block-end: 0;    }    /* On the marker's middle, a half pixel back so the 1px line is centered as a timeline's and a trace's are. */    &:not(:last-child)::after {      position: absolute;      inset-block: calc(var(--steps-marker) + .25rem) .25rem;      inset-inline-start: calc(var(--steps-marker) / 2 - .5px);      flex: none;      width: 1px;      min-width: 0;      height: auto;    }  }  /* The first line stands on the middle of the marker, whether the title is alone or in a steps-body. */  .steps-vertical :is(.steps-body, .steps-item:not(:has(.steps-body)) .steps-title) {    padding-block-start: calc((var(--steps-marker) - 1lh) / 2);  }  .steps-vertical .steps-title {    white-space: normal;  }}