網站導覽

v2.1.0
表單

檢查清單

一份會統計已完成項目數的簡短檢查清單。

本頁內容

檢查清單適合列出少量待辦事項,例如設定步驟或審查要點。每個步驟都以標籤包住原生核取方塊、標題和一行提示。完成後,步驟會淡化,標題上會加一條細刪除線,清單也會自動更新計數。

檢查清單

<div class="card max-w(400) mx-auto">  <div class="card-header">    <h3 class="card-title">Get Atlas ready</h3>  </div>  <ul class="checklist">    <li class="checklist-item">      <label class="checklist-label">        <input type="checkbox" class="checkbox" checked>        <span class="checklist-body">          <span class="checklist-title">Create a project</span>          <span class="checklist-description">Atlas was created on 3 November.</span>        </span>      </label>    </li>    <li class="checklist-item">      <label class="checklist-label">        <input type="checkbox" class="checkbox" checked>        <span class="checklist-body">          <span class="checklist-title">Connect a repository</span>          <span class="checklist-description">Deploy every push to the main branch.</span>        </span>      </label>    </li>    <li class="checklist-item">      <label class="checklist-label">        <input type="checkbox" class="checkbox">        <span class="checklist-body">          <span class="checklist-title">Add a domain</span>          <span class="checklist-description">Point a domain you own at the project.</span>        </span>        <span class="checklist-end"><span class="badge badge-sm">2 min</span></span>      </label>    </li>    <li class="checklist-item">      <label class="checklist-label">        <input type="checkbox" class="checkbox">        <span class="checklist-body">          <span class="checklist-title">Invite your team</span>          <span class="checklist-description">Members can deploy and read the logs.</span>        </span>      </label>    </li>    <li class="checklist-count">steps done</li>  </ul></div>
<div className="card max-w(400) mx-auto">  <div className="card-header">    <h3 className="card-title">Get Atlas ready</h3>  </div>  <ul className="checklist">    <li className="checklist-item">      <label className="checklist-label">        <input type="checkbox" className="checkbox" defaultChecked />        <span className="checklist-body">          <span className="checklist-title">Create a project</span>          <span className="checklist-description">Atlas was created on 3 November.</span>        </span>      </label>    </li>    <li className="checklist-item">      <label className="checklist-label">        <input type="checkbox" className="checkbox" defaultChecked />        <span className="checklist-body">          <span className="checklist-title">Connect a repository</span>          <span className="checklist-description">Deploy every push to the main branch.</span>        </span>      </label>    </li>    <li className="checklist-item">      <label className="checklist-label">        <input type="checkbox" className="checkbox" />        <span className="checklist-body">          <span className="checklist-title">Add a domain</span>          <span className="checklist-description">Point a domain you own at the project.</span>        </span>        <span className="checklist-end"><span className="badge badge-sm">2 min</span></span>      </label>    </li>    <li className="checklist-item">      <label className="checklist-label">        <input type="checkbox" className="checkbox" />        <span className="checklist-body">          <span className="checklist-title">Invite your team</span>          <span className="checklist-description">Members can deploy and read the logs.</span>        </span>      </label>    </li>    <li className="checklist-count">steps done</li>  </ul></div>
<div class="card max-w(400) mx-auto">  <div class="card-header">    <h3 class="card-title">Get Atlas ready</h3>  </div>  <ul class="checklist">    <li class="checklist-item">      <label class="checklist-label">        <input type="checkbox" class="checkbox" checked>        <span class="checklist-body">          <span class="checklist-title">Create a project</span>          <span class="checklist-description">Atlas was created on 3 November.</span>        </span>      </label>    </li>    <li class="checklist-item">      <label class="checklist-label">        <input type="checkbox" class="checkbox" checked>        <span class="checklist-body">          <span class="checklist-title">Connect a repository</span>          <span class="checklist-description">Deploy every push to the main branch.</span>        </span>      </label>    </li>    <li class="checklist-item">      <label class="checklist-label">        <input type="checkbox" class="checkbox">        <span class="checklist-body">          <span class="checklist-title">Add a domain</span>          <span class="checklist-description">Point a domain you own at the project.</span>        </span>        <span class="checklist-end"><span class="badge badge-sm">2 min</span></span>      </label>    </li>    <li class="checklist-item">      <label class="checklist-label">        <input type="checkbox" class="checkbox">        <span class="checklist-body">          <span class="checklist-title">Invite your team</span>          <span class="checklist-description">Members can deploy and read the logs.</span>        </span>      </label>    </li>    <li class="checklist-count">steps done</li>  </ul></div>
<template>  <div class="card max-w(400) mx-auto">    <div class="card-header">      <h3 class="card-title">Get Atlas ready</h3>    </div>    <ul class="checklist">      <li class="checklist-item">        <label class="checklist-label">          <input type="checkbox" class="checkbox" checked>          <span class="checklist-body">            <span class="checklist-title">Create a project</span>            <span class="checklist-description">Atlas was created on 3 November.</span>          </span>        </label>      </li>      <li class="checklist-item">        <label class="checklist-label">          <input type="checkbox" class="checkbox" checked>          <span class="checklist-body">            <span class="checklist-title">Connect a repository</span>            <span class="checklist-description">Deploy every push to the main branch.</span>          </span>        </label>      </li>      <li class="checklist-item">        <label class="checklist-label">          <input type="checkbox" class="checkbox">          <span class="checklist-body">            <span class="checklist-title">Add a domain</span>            <span class="checklist-description">Point a domain you own at the project.</span>          </span>          <span class="checklist-end"><span class="badge badge-sm">2 min</span></span>        </label>      </li>      <li class="checklist-item">        <label class="checklist-label">          <input type="checkbox" class="checkbox">          <span class="checklist-body">            <span class="checklist-title">Invite your team</span>            <span class="checklist-description">Members can deploy and read the logs.</span>          </span>        </label>      </li>      <li class="checklist-count">steps done</li>    </ul>  </div></template>
勾選一個步驟,清單下方的計數就會更新,無須腳本。

#自動統計完成數

計數由兩個 CSS 計數器處理。每個 checklist-item 都會讓總數加一,項目透過 :has() 判斷自己的核取方塊是否已勾選,若已勾選就讓完成數加一。checklist-count 會顯示這兩個數字。

計數器依文件順序運作,因此計數要寫在所有步驟之後的標記中,此時兩個數字才統計完畢。計數元素本身的文字會顯示在兩個數字之後,可自行翻譯。--checklist-of 設定兩個數字之間的文字,預設為 ' / '。若要顯示「2 of 4」,請設為 ' of '。

計數是 CSS 產生的內容,會隨時更新,但變動時螢幕閱讀器不會主動宣告。若完成清單是重要事件,請另外提供狀態訊息。

#步驟

標籤涵蓋整列,點按文字的任何位置都能勾選方框。checklist-title 刻意使用行內元素。完成時,刪除線是沿著文字延伸的背景,即使標題換行,仍維持一條線。checklist-end 用來放步驟末端的資訊,例如所需時間。

各列以從文字起點開始的細線分隔,和清單相同。檢查清單本身沒有背景面板,可放進卡片。

#無障礙

  • 使用 <ul>。步驟必須依序完成時,請用 <ol>。
  • 這裡的核取方塊由讀者自行設定。若步驟由系統完成,請用步驟指示器呈現進度。讀者無法變更的核取方塊是停用的控制項,不能拿來當作完成紀錄。
  • 加上刪除線的標題維持淡色文字,仍有足夠的對比可供閱讀。完成狀態由核取方塊傳達,無須依賴刪除線。

#類別

類別說明
checklist清單使用 ul,步驟須依序完成時則使用 ol。清單會統計步驟總數及已勾選的步驟數
checklist-item每個 li 代表一個步驟並計入總數,核取方塊勾選時也計入已完成數
checklist-label標籤由核取方塊和後方的文字組成。點按列內任一處皆可切換勾選狀態,觸控螢幕上的列高為 44px
checklist-body步驟文字,標題在上、說明在下
checklist-title步驟名稱放在行內元素中。步驟完成後,名稱會加上細刪除線
checklist-description標題下方以較淡文字顯示的一行說明
checklist-end放在步驟末尾的內容,例如連結、徽章或時間
checklist-count已完成的步驟數與總步驟數顯示在說明文字前方。這段進度資訊在標記中放在最後一個步驟之後

#自訂屬性

屬性預設值說明
--checklist-of' / '計數中兩個數字之間的分隔符號
--checklist-gap.75rem核取方塊與文字之間的間距

#原始碼

@master/ui/components/checklist.css
/* * Checklist: a short list of things to do, each a native checkbox with a title and a line of help. * A row is a label, so the whole of it checks the box. A step that is done steps back: its title turns to * the muted ink and a hairline is drawn through it from its start, the way a pen would. * The list counts itself. CSS counters add up the rows and, with :has(), the checked ones; checklist-count * writes the two numbers after the rows, so "2 / 5" is always true without a script. * Rows part on hairlines that start at the text, as a list's do. A checklist has no surface of its own. */@layer components.ui {  .checklist {    --checklist-gap: .75rem;    --checklist-padding-y: .6875rem;    /* What stands between the two numbers of the count. */    --checklist-of: ' / ';    display: flex;    flex-direction: column;    min-width: 0;    margin: 0;    padding: 0;    color: var(--color-text-body);    font-size: .875rem;    line-height: 1.45;    list-style: none;    counter-reset: checklist-total checklist-done;    /* On touch a row is 44px: the same line of words with a little more room over and under it. */    @media (pointer: coarse) {      --checklist-padding-y: .75rem;    }  }  /* A step. It counts itself, and counts itself done while its checkbox is checked. */  .checklist-item {    position: relative;    min-width: 0;    margin: 0;    padding: 0;    counter-increment: checklist-total;    &:has(.checkbox:checked) {      counter-increment: checklist-total checklist-done;    }    /* The hairline under a step that another follows; it starts where the words do, past the 18px checkbox. */    &:has(+ .checklist-item)::after {      content: '';      position: absolute;      inset-inline: calc(1.125rem + var(--checklist-gap)) 0;      inset-block-end: 0;      height: 1px;      background: var(--color-line-hair);      pointer-events: none;      /* Forced colors would turn the hairline's fill to the canvas: it keeps a system color. */      @media (forced-colors: active) {        background: GrayText;      }    }  }  /* The label: the checkbox, then the words. The box is level with the first line of the title. */  .checklist-label {    display: flex;    align-items: flex-start;    gap: var(--checklist-gap);    min-width: 0;    padding: var(--checklist-padding-y) 0;    cursor: pointer;    touch-action: manipulation;    -webkit-tap-highlight-color: transparent;    &:has(.checkbox:disabled) {      cursor: not-allowed;    }    .checkbox {      flex: none;      margin-block-start: calc((1lh - var(--check-size)) / 2);    }  }  /* A block, not a grid: an item of a grid is made a block, and the title has to stay inline. */  .checklist-body {    display: block;    flex: 1 1 0;    /* The words and the end share the title's baseline; the box stands on the first line, as the box does. */    align-self: baseline;    min-width: 0;  }  /*   * The title is inline, so the stroke through it is one line however the title wraps: it is a background   * that grows from nothing to the title's whole length.   */  .checklist-title {    background: linear-gradient(currentColor, currentColor) 0 calc(50% + .0625em) / 0 1px no-repeat;    color: var(--color-text-strong);    font-weight: var(--font-weight-control);    /* A long word or an address breaks inside the column, as the line of help under it does. */    overflow-wrap: anywhere;    transition-property: background-size, color;    transition-duration: var(--duration-base), var(--duration-quick);    transition-timing-function: var(--easing-glide);    /* Right to left the pen starts at the right. */    &:dir(rtl) {      background-position-x: 100%;    }    @media (prefers-reduced-motion: reduce) {      transition: none;    }  }  .checklist-item:has(.checkbox:checked) .checklist-title {    background-size: 100% 1px;    color: var(--color-text-muted);  }  .checklist-item:has(.checkbox:disabled) .checklist-title {    color: var(--color-text-disabled);    /* Forced colors would give it the page's text color back: a step that cannot be ticked keeps the system's disabled ink. */    @media (forced-colors: active) {      color: GrayText;    }  }  .checklist-description {    display: block;    margin: .125rem 0 0;    color: var(--color-text-muted);    font-size: .8125rem;    text-wrap: pretty;    overflow-wrap: anywhere;  }  /*   * Something at the end of a step: a link to where it is done, a badge, a time. It belongs to the title, so it   * stands on the title's first line, as the box at the start does, not in the middle of the title and the line of   * help under it.   */  .checklist-end {    display: inline-flex;    flex: none;    align-items: center;    align-self: baseline;    gap: .5rem;    color: var(--color-text-muted);    font-size: .8125rem;    /* A time or a count, in figures of one width, as at the end of a list's row. */    font-variant-numeric: tabular-nums;  }  /*   * The count: how many steps are done, of how many. It comes after the steps in the markup, where the   * counters have their final values; its own words follow the numbers.   */  .checklist-count {    margin: .625rem 0 0;    color: var(--color-text-muted);    font-size: .8125rem;    font-variant-numeric: tabular-nums;    &::before {      content: counter(checklist-done) var(--checklist-of) counter(checklist-total) ' ';      color: var(--color-text-strong);      font-weight: var(--font-weight-control);    }  }}