导航

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;  }}