網站導覽

v2.1.0
回饋

處理中

區域重新載入內容時,以遮罩和旋轉指示器呈現處理狀態。

本頁內容

區域正在重新取得內容時,可以標記為處理中,例如卡片套用新的篩選條件、表格載入另一頁,或表單儲存資料。將 busy 加在區域上,並在處理期間設定 aria-busy="true"。遮罩沿用區域的表面色,淡化原有內容,中央則顯示旋轉指示器。原有內容會留在原位,新內容載入時版面就不會跳動。

處理中

<article class="card busy w-full max-w(320)" id="revenue" aria-label="Revenue">  <header class="card-header">    <h3 class="card-title">Revenue</h3>    <p class="card-description">$48,200 this month, up 12% on October.</p>  </header>  <footer class="card-footer">    <button class="btn btn-sm" type="button" id="revenue-refresh">Refresh</button>  </footer></article><script>var card = document.getElementById('revenue')document.getElementById('revenue-refresh').addEventListener('click', function () {  card.setAttribute('aria-busy', 'true')  card.inert = true  setTimeout(function () {    card.removeAttribute('aria-busy')    card.inert = false  }, 1800)})</script>
按下「重新整理」,卡片會顯示兩秒的處理中狀態。

#用一個屬性控制狀態

單獨加上類別不會顯示任何效果。要在同一元素上設定 aria-busy="true" 才會進入處理中狀態。螢幕閱讀器與樣式表都依據這個屬性,因此兩者呈現的狀態會一致。處理結束時,請移除這個屬性或將它設為 false。

遮罩會等候 160ms 再淡入。若作業立即完成,遮罩就不會一閃而過,也不會提示使用者等待已經就緒的內容。

處理期間

<article class="card busy w-full max-w(320)" aria-busy="true" aria-label="Revenue" inert>  <header class="card-header">    <h3 class="card-title">Revenue</h3>    <p class="card-description">$48,200 this month, up 12% on October.</p>  </header>  <footer class="card-footer"><button class="btn btn-sm" type="button">Refresh</button></footer></article>
<article className="card busy w-full max-w(320)" aria-busy="true" aria-label="Revenue" inert>  <header className="card-header">    <h3 className="card-title">Revenue</h3>    <p className="card-description">$48,200 this month, up 12% on October.</p>  </header>  <footer className="card-footer">    <button className="btn btn-sm" type="button">Refresh</button>  </footer></article>
<article class="card busy w-full max-w(320)" aria-busy="true" aria-label="Revenue" inert>  <header class="card-header">    <h3 class="card-title">Revenue</h3>    <p class="card-description">$48,200 this month, up 12% on October.</p>  </header>  <footer class="card-footer"><button class="btn btn-sm" type="button">Refresh</button></footer></article>
<template>  <article class="card busy w-full max-w(320)" aria-busy="true" aria-label="Revenue" inert>    <header class="card-header">      <h3 class="card-title">Revenue</h3>      <p class="card-description">$48,200 this month, up 12% on October.</p>    </header>    <footer class="card-footer"><button class="btn btn-sm" type="button">Refresh</button></footer>  </article></template>

#遮罩

遮罩以面板、卡片及對話框公開的 --surface-color 混合而成,因此遮罩會用卡片本身的顏色淡化內容,而不會套上灰色。區域若未設定自己的表面色,就使用頁面底色。遮罩沿用區域的圓角,在旋轉指示器正後方完全遮住內容,往外則逐漸變淡,讓圓環不會與文字重疊。

遮罩會阻擋指標與下方內容互動。若也要阻擋鍵盤操作,請像第一個範例一樣,在區域處理期間加上 inert 屬性,結束後再移除。

--busy-size 設定旋轉指示器的直徑,預設為 20px。

#處理中與骨架畫面

正在替換現有內容時,使用處理中狀態。首次載入時,畫面上還沒有可淡化的內容,請用骨架畫面呈現預期的版面。若只有單一控制項需要等候,就在該控制項內放置旋轉指示器。按鈕本身有 aria-busy 狀態。

#無障礙

  • aria-busy="true" 會告知輔助科技這個區域正在更新,讓它可以等到屬性移除後,再讀出區域內的變更。
  • 作業開始時,這個屬性不會觸發朗讀。請在處理中區域之外的即時區域,或在啟動作業的控制項上,以文字告知使用者。
  • 處理期間,將區域設為 inert,避免遮罩下的控制項取得焦點或遭到觸發。
  • 使用者偏好減少動態效果時,旋轉指示器會轉得較慢。在強制色彩模式下,遮罩使用系統的畫布色。

#類別

類別說明
busy用於區域本身。帶有 aria-busy="true" 時,與區域背景同色的遮罩會淡化內容,中央顯示旋轉式載入指標。遮罩沿用區域的圓角,延遲 160ms 後顯示。

#自訂屬性

屬性預設值說明
--busy-size1.25rem旋轉式載入指標的直徑

#原始碼

@master/ui/components/busy.css
/* * Busy: a region whose content is being fetched again. * While the region carries aria-busy="true", a veil in the color of the surface it lies on steps its content * back and a spinner turns in the middle, where the veil is whole. What was there stays where it was, so * nothing jumps when the new content arrives; for content that has never loaded, use a skeleton instead. * The veil waits 160ms before it shows: work that ends at once never flashes. It takes the region's own * corner and keeps the pointer off what is under it; add the inert attribute to keep the keyboard out too. */@layer components.ui {  .busy {    --busy-size: 1.25rem;    --busy-width: max(1.5px, calc(var(--busy-size) / 9));    --busy-surface: var(--surface-color, var(--color-surface-base));    position: relative;    &[aria-busy="true"] {      /*       * The veil and the spinner stack inside the region only: without its own stacking context their z-index       * would lift them over a sticky header of the page that scrolls over the region.       */      isolation: isolate;      /* The veil. */      &::before {        content: '';        position: absolute;        z-index: 2;        inset: 0;        border-radius: inherit;        corner-shape: inherit;        /* The veil is whole behind the spinner and thins out around it, so the ring never stands on a word. */        background:          radial-gradient(circle at center, var(--busy-surface) calc(var(--busy-size) * .7), transparent calc(var(--busy-size) * 1.7)),          color-mix(in oklab, var(--busy-surface) 62%, transparent);        cursor: progress;        animation: busy-arrive var(--duration-base) var(--easing-glide) 160ms both;      }      /* The spinner: the same fine ring as the spinner component, in the strong ink. */      &::after {        content: '';        position: absolute;        z-index: 3;        inset: 0;        width: var(--busy-size);        height: var(--busy-size);        margin: auto;        border-radius: 999px;        background: conic-gradient(from 90deg, color-mix(in oklab, var(--color-text-strong) 12%, transparent) 0 18%, var(--color-text-strong));        mask: radial-gradient(farthest-side, transparent calc(100% - var(--busy-width) - .5px), black calc(100% - var(--busy-width)), black calc(100% - .5px), transparent);        pointer-events: none;        animation: busy-arrive var(--duration-base) var(--easing-glide) 160ms both, busy-rotate .72s linear infinite;      }      @media (prefers-reduced-motion: reduce) {        &::after {          animation-duration: var(--duration-base), 1.8s;        }      }      /* The veil keeps its 160ms wait, as the spinner does: it fades in to its own opacity. */      @media (forced-colors: active) {        &::before {          background: Canvas;          opacity: .7;        }        &::after {          background: conic-gradient(from 90deg, GrayText 0 18%, CanvasText);          forced-color-adjust: none;        }      }    }  }}@keyframes busy-arrive {  from {    opacity: 0;  }}@keyframes busy-rotate {  to {    rotate: 360deg;  }}