網站導覽

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