导航

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