导航

v2.1.0
反馈

流光文字

任务进行时,高光从提示文字上掠过。

本页内容

流光文字用一行字说明正在进行的工作,例如“正在思考”“正在搜索 14 个文件”“正在部署”。光带未覆盖的部分以基础颜色显示,颜色强度略高于周围文字的一半。同色光带以周围文字的完整颜色强度扫过字形,让经过的部分变亮,色相保持不变。将 shimmer 添加到承载这行文字的元素上。

流光文字

<p class="shimmer text-sm" role="status">Thinking…</p><p class="shimmer text-sm" role="status">Searching 14 files for “corner-shape”</p>
<>  <p className="shimmer text-sm" role="status">Thinking…</p>  <p className="shimmer text-sm" role="status">Searching 14 files for “corner-shape”</p></>
<p class="shimmer text-sm" role="status">Thinking…</p><p class="shimmer text-sm" role="status">Searching 14 files for “corner-shape”</p>
<template>  <p class="shimmer text-sm" role="status">Thinking…</p>  <p class="shimmer text-sm" role="status">Searching 14 files for “corner-shape”</p></template>

#用文字说明任务状态

骨架屏用于内容轮廓已知、具体文字尚未确定的情况。加载指示器只表示操作正在进行。流光文字适用于两者之间的情形:可以用文字说明当前正在做什么,这行文字本身就是状态信息。

光带是裁剪到字形内的背景。文字仍然可以选中、复制、翻译和朗读。容器收紧到文字的宽度,让光带只扫过文字。

#搭配其他内容

组件类沿用所在位置的字号和字重,可用来提示对话中尚未到达的回复、正在执行的步骤,或在页面加载期间显示标题。

用于对话和列表

<div class="flex flex-col">  <div class="message message-sent">    <div class="message-bubble">What changed in the last release?</div>  </div>  <div class="message">    <div class="message-bubble">      <span class="shimmer" role="status">Reading the changelog…</span>    </div>  </div></div><ol class="steps steps-vertical">  <li class="steps-item">    <div class="steps-body">      <p class="steps-title">Install dependencies</p>    </div>  </li>  <li class="steps-item" aria-current="step">    <div class="steps-body">      <p class="steps-title shimmer">Building 212 pages</p>    </div>  </li>  <li class="steps-item">    <div class="steps-body">      <p class="steps-title">Deploy</p>    </div>  </li></ol>
<>  <div className="flex flex-col">    <div className="message message-sent">      <div className="message-bubble">What changed in the last release?</div>    </div>    <div className="message">      <div className="message-bubble">        <span className="shimmer" role="status">Reading the changelog…</span>      </div>    </div>  </div>  <ol className="steps steps-vertical">    <li className="steps-item">      <div className="steps-body">        <p className="steps-title">Install dependencies</p>      </div>    </li>    <li className="steps-item" aria-current="step">      <div className="steps-body">        <p className="steps-title shimmer">Building 212 pages</p>      </div>    </li>    <li className="steps-item">      <div className="steps-body">        <p className="steps-title">Deploy</p>      </div>    </li>  </ol></>
<div class="flex flex-col">  <div class="message message-sent">    <div class="message-bubble">What changed in the last release?</div>  </div>  <div class="message">    <div class="message-bubble">      <span class="shimmer" role="status">Reading the changelog…</span>    </div>  </div></div><ol class="steps steps-vertical">  <li class="steps-item">    <div class="steps-body">      <p class="steps-title">Install dependencies</p>    </div>  </li>  <li class="steps-item" aria-current="step">    <div class="steps-body">      <p class="steps-title shimmer">Building 212 pages</p>    </div>  </li>  <li class="steps-item">    <div class="steps-body">      <p class="steps-title">Deploy</p>    </div>  </li></ol>
<template>  <div class="flex flex-col">    <div class="message message-sent">      <div class="message-bubble">What changed in the last release?</div>    </div>    <div class="message">      <div class="message-bubble">        <span class="shimmer" role="status">Reading the changelog…</span>      </div>    </div>  </div>  <ol class="steps steps-vertical">    <li class="steps-item">      <div class="steps-body">        <p class="steps-title">Install dependencies</p>      </div>    </li>    <li class="steps-item" aria-current="step">      <div class="steps-body">        <p class="steps-title shimmer">Building 212 pages</p>      </div>    </li>    <li class="steps-item">      <div class="steps-body">        <p class="steps-title">Deploy</p>      </div>    </li>  </ol></template>

#调整效果

--shimmer-duration 控制光带扫过一次的时长,默认为两秒,较长的句子可以设为三秒。--shimmer-color 控制文字的基础颜色,--shimmer-light 控制光带的颜色。两者都由当前文字颜色混合得出,因此无论放在页面、卡片还是已发送的消息中,都能沿用各处的文字颜色并保持清晰。

启用 prefers-reduced-motion: reduce 后,光带消失,文字保持较淡的基础颜色,仍会说明正在进行的工作。

#无障碍

  • 如果这行文字在读者浏览页面期间出现或更新,请为它设置 role="status",以便辅助技术播报。每项任务设置一个状态区域即可。
  • 结果就绪后,用结果替换这行状态文字,避免它在已显示的内容旁继续播放动画。
  • 流光效果仅作装饰,完整的含义由文字传达。

#类名

类名说明
shimmer用于承载文字的元素,以较淡的墨色显示文字,并让光带从文字上扫过

#自定义属性

属性默认值说明
--shimmer-duration2s光带扫过文字一次所需的时间。
--shimmer-colorcolor-mix(in oklab, currentColor 56%, transparent)未被光带照亮时的文字颜色
--shimmer-lightcurrentColor光带的颜色

#源码

@master/ui/components/shimmer.css
/* * Shimmer: a line of words that says work is under way, with a band of light passing over the letters. * The words rest at a little over half the ink of the text around them and the band is that ink in full, so * the line brightens where the light is, never changes hue and reads on any surface it is put on. It is drawn * as a background clipped to the text, which leaves the words selectable and real. * With reduced motion the line rests in its quieter ink, --shimmer-color, without the band; in forced colors * it is plain text. */@layer components.ui {  .shimmer {    --shimmer-color: color-mix(in oklab, currentColor 56%, transparent);    --shimmer-light: currentColor;    --shimmer-duration: 2s;    /* The box is as wide as the words, so the light crosses the words and not the room beside them. */    width: fit-content;    max-width: 100%;    background:      linear-gradient(100deg, transparent 38%, var(--shimmer-light) 50%, transparent 62%) 150% 0 / 300% 100% no-repeat,      linear-gradient(var(--shimmer-color), var(--shimmer-color));    /* The prefixed clip first: an engine that knows only it would otherwise clear the fill and lose the words. */    -webkit-background-clip: text;    background-clip: text;    /* The fill is cleared, not the color: the ink above is still the color of the text around the line. */    -webkit-text-fill-color: transparent;    /* Linear: the light crosses at one speed and the loop has no seam to rest at. */    animation: shimmer-sweep var(--shimmer-duration) linear infinite;    /* The light crosses the words the way they are read: from the right where they run from the right. */    &:dir(rtl) {      animation-direction: reverse;    }    @media (prefers-reduced-motion: reduce) {      background: none;      -webkit-text-fill-color: var(--shimmer-color);      animation: none;    }    @media (forced-colors: active) {      background: none;      -webkit-text-fill-color: currentColor;      animation: none;    }  }}@keyframes shimmer-sweep {  to {    background-position: -50% 0, 0 0;  }}