網站導覽

v2.1.0
表單

漸層

製作中的漸層:色標排在一條橫條上,目前選取的色標以取色器調整,並可設定類型、角度與中心點。

本頁內容

漸層編輯器用來製作漸層:色標排在一條橫條上,目前選取的色標以取色器調整,並可設定漸層的類型、角度與中心點。其中每個控制項都是元件庫本身的元件:取色器、用來設定色標位置的滑桿和數字欄位、用來選擇類型的分段控制項、用來設定角度的旋鈕,以及用來設定中心點的數字欄位。漸層本身加上的是色標橫條、預覽,以及讓這一切保持同步的 gradient 行為。

漸層

<div class="gradient max-w(640) mx-auto" id="sunrise" role="group" aria-label="Background gradient" style="--gradient-ratio: 3 / 1">  <div class="gradient-preview" aria-hidden="true"></div>  <div class="gradient-bar"></div>  <div class="flex flex-wrap items-center gap-xs">    <button type="button" class="btn btn-sm gradient-add">      <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">        <path d="M3 8L13 8" /><path d="M8 3L8 13" />      </svg>Add a stop    </button>    <button type="button" class="btn btn-sm gradient-remove">      <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">        <path d="M3 8L13 8" />      </svg>Remove    </button>    <button type="button" class="btn btn-sm btn-quiet gradient-reverse">      <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">        <path d="M8 1.6667L8 14.3333" /><path d="M5.6667 6.3298L5.6667 11.45A0.55 0.55 0 0 1 5.1167 12L2.5566 12A0.55 0.55 0 0 1 2.0647 11.204L4.6248 6.0839A0.55 0.55 0 0 1 5.6667 6.3298Z" /><path d="M10.3333 6.3298L10.3333 11.45A0.55 0.55 0 0 0 10.8833 12L13.4434 12A0.55 0.55 0 0 0 13.9353 11.204L11.3752 6.0839A0.55 0.55 0 0 0 10.3333 6.3298Z" />      </svg>Reverse    </button>  </div>  <div class="gradient-fields">    <fieldset class="gradient-color">      <legend class="mb(6) text-xs font-medium fg-text-muted">Color of the stop</legend>      <div class="color-picker" style="--color-picker-lightness: .7; --color-picker-chroma: .19; --color-picker-hue: 50">        <div class="color-picker-area">          <div class="color-picker-thumb" role="slider" tabindex="0" aria-label="Lightness and chroma" aria-valuemin="0" aria-valuemax="100" aria-valuenow="70"></div>        </div>        <span class="swatch swatch-lg color-picker-swatch"></span>        <input type="range" class="slider color-picker-hue" min="0" max="360" value="50" aria-label="Hue">        <input type="range" class="slider color-picker-alpha" min="0" max="100" value="100" aria-label="Opacity">        <div class="color-picker-fields">          <select class="select select-sm color-picker-format" aria-label="Format">            <option value="hex" selected>Hex</option>            <option value="rgb">RGB</option>            <option value="oklch">OKLCH</option>          </select>          <input class="input input-sm color-picker-text" value="#ff6a00" aria-label="Color" spellcheck="false" autocomplete="off">        </div>      </div>    </fieldset>    <div class="flex flex-col gap-md min-w(0)">      <fieldset class="gradient-offset">        <legend class="mb(6) text-xs font-medium fg-text-muted">Position</legend>        <input type="range" class="slider" min="0" max="100" step="1" value="0" aria-label="Position of the stop">        <input type="number" class="input input-sm w(72)" min="0" max="100" step="1" value="0" aria-label="Position of the stop, in percent">      </fieldset>      <fieldset class="gradient-type">        <legend class="mb(6) text-xs font-medium fg-text-muted">Type</legend>        <div class="segmented segmented-sm" role="radiogroup" aria-label="Type">          <label class="segmented-item">            <input type="radio" class="segmented-input" name="sunrise-type" value="linear" checked>Linear          </label>          <label class="segmented-item">            <input type="radio" class="segmented-input" name="sunrise-type" value="radial">Radial          </label>          <label class="segmented-item">            <input type="radio" class="segmented-input" name="sunrise-type" value="conic">Conic          </label>        </div>      </fieldset>      <div class="flex flex-wrap items-start gap-lg">        <fieldset class="gradient-angle">          <legend class="mb(6) text-xs font-medium fg-text-muted">Angle</legend>          <div class="dial dial-sm dial-full" role="slider" tabindex="0" aria-label="Angle" aria-valuenow="135" aria-valuemin="0" aria-valuemax="360" aria-valuetext="135°" style="--dial-value: 135">            <span class="dial-value">135°</span>            <span class="dial-thumb"></span>          </div>        </fieldset>        <fieldset class="gradient-center">          <legend class="mb(6) text-xs font-medium fg-text-muted">Center</legend>          <input type="number" class="input input-sm w(72)" min="0" max="100" value="50" aria-label="Center across, in percent">          <input type="number" class="input input-sm w(72)" min="0" max="100" value="50" aria-label="Center down, in percent">        </fieldset>      </div>    </div>  </div>  <pre class="code-block gradient-css" role="region" tabindex="0" aria-label="The gradient as CSS"></pre></div><script>addEventListener('DOMContentLoaded', function () {MasterUI.gradient(document.getElementById('sunrise'), { value: { type: 'linear', angle: 135, center: [50, 50], stops: [  { id: 'dawn', offset: 0, color: '#ff6a00' },  { id: 'noon', offset: 55, color: '#ee0979' },  { id: 'dusk', offset: 100, color: '#4f46e5' }] } })})</script><script type="module">  import { attach } from '@master/ui/behaviors'  attach()</script>
import { useEffect, useRef } from 'react'import { attach, gradient } from '@master/ui/behaviors'import PlusIcon from '@master/icons-react/native/16/soft/plus'import MinusIcon from '@master/icons-react/native/16/soft/minus'import FlipHorizontalIcon from '@master/icons-react/native/16/soft/flip-horizontal'export function Example() {  const root = useRef<HTMLDivElement>(null)  useEffect(() => {    const detach = [      attach(root.current!),      gradient(document.getElementById('sunrise')!, { value: { type: 'linear', angle: 135, center: [50, 50], stops: [        { id: 'dawn', offset: 0, color: '#ff6a00' },        { id: 'noon', offset: 55, color: '#ee0979' },        { id: 'dusk', offset: 100, color: '#4f46e5' }      ] } }),    ]    return () => detach.forEach(stop => stop())  }, [])  return (    <div ref={root} className="gradient max-w(640) mx-auto" id="sunrise" role="group" aria-label="Background gradient" style={{ '--gradient-ratio': '3 / 1' } as React.CSSProperties}>      <div className="gradient-preview" aria-hidden="true"></div>      <div className="gradient-bar"></div>      <div className="flex flex-wrap items-center gap-xs">        <button type="button" className="btn btn-sm gradient-add">          <PlusIcon className="icon" />Add a stop        </button>        <button type="button" className="btn btn-sm gradient-remove">          <MinusIcon className="icon" />Remove        </button>        <button type="button" className="btn btn-sm btn-quiet gradient-reverse">          <FlipHorizontalIcon className="icon" />Reverse        </button>      </div>      <div className="gradient-fields">        <fieldset className="gradient-color">          <legend className="mb(6) text-xs font-medium fg-text-muted">Color of the stop</legend>          <div className="color-picker" style={{ '--color-picker-lightness': '.7', '--color-picker-chroma': '.19', '--color-picker-hue': '50' } as React.CSSProperties}>            <div className="color-picker-area">              <div className="color-picker-thumb" role="slider" tabIndex={0} aria-label="Lightness and chroma" aria-valuemin={0} aria-valuemax={100} aria-valuenow={70}></div>            </div>            <span className="swatch swatch-lg color-picker-swatch"></span>            <input type="range" className="slider color-picker-hue" min="0" max="360" defaultValue="50" aria-label="Hue" />            <input type="range" className="slider color-picker-alpha" min="0" max="100" defaultValue="100" aria-label="Opacity" />            <div className="color-picker-fields">              <select className="select select-sm color-picker-format" aria-label="Format" defaultValue="hex">                <option value="hex">Hex</option>                <option value="rgb">RGB</option>                <option value="oklch">OKLCH</option>              </select>              <input className="input input-sm color-picker-text" defaultValue="#ff6a00" aria-label="Color" spellCheck="false" autoComplete="off" />            </div>          </div>        </fieldset>        <div className="flex flex-col gap-md min-w(0)">          <fieldset className="gradient-offset">            <legend className="mb(6) text-xs font-medium fg-text-muted">Position</legend>            <input type="range" className="slider" min="0" max="100" step="1" defaultValue="0" aria-label="Position of the stop" />            <input type="number" className="input input-sm w(72)" min="0" max="100" step="1" defaultValue="0" aria-label="Position of the stop, in percent" />          </fieldset>          <fieldset className="gradient-type">            <legend className="mb(6) text-xs font-medium fg-text-muted">Type</legend>            <div className="segmented segmented-sm" role="radiogroup" aria-label="Type">              <label className="segmented-item">                <input type="radio" className="segmented-input" name="sunrise-type" value="linear" defaultChecked />Linear              </label>              <label className="segmented-item">                <input type="radio" className="segmented-input" name="sunrise-type" value="radial" />Radial              </label>              <label className="segmented-item">                <input type="radio" className="segmented-input" name="sunrise-type" value="conic" />Conic              </label>            </div>          </fieldset>          <div className="flex flex-wrap items-start gap-lg">            <fieldset className="gradient-angle">              <legend className="mb(6) text-xs font-medium fg-text-muted">Angle</legend>              <div className="dial dial-sm dial-full" role="slider" tabIndex={0} aria-label="Angle" aria-valuenow={135} aria-valuemin={0} aria-valuemax={360} aria-valuetext="135°" style={{ '--dial-value': '135' } as React.CSSProperties}>                <span className="dial-value">135°</span>                <span className="dial-thumb"></span>              </div>            </fieldset>            <fieldset className="gradient-center">              <legend className="mb(6) text-xs font-medium fg-text-muted">Center</legend>              <input type="number" className="input input-sm w(72)" min="0" max="100" defaultValue="50" aria-label="Center across, in percent" />              <input type="number" className="input input-sm w(72)" min="0" max="100" defaultValue="50" aria-label="Center down, in percent" />            </fieldset>          </div>        </div>      </div>      <pre className="code-block gradient-css" role="region" tabIndex={0} aria-label="The gradient as CSS">{``}</pre>    </div>  )}
<script>  import { attach, gradient } from '@master/ui/behaviors'  import PlusIcon from '@master/icons-svelte/native/16/soft/plus'  import MinusIcon from '@master/icons-svelte/native/16/soft/minus'  import FlipHorizontalIcon from '@master/icons-svelte/native/16/soft/flip-horizontal'  function mounted(root) {    const detach = [      attach(root),      gradient(document.getElementById('sunrise'), { value: { type: 'linear', angle: 135, center: [50, 50], stops: [        { id: 'dawn', offset: 0, color: '#ff6a00' },        { id: 'noon', offset: 55, color: '#ee0979' },        { id: 'dusk', offset: 100, color: '#4f46e5' }      ] } }),    ]    return () => detach.forEach(stop => stop())  }</script><div class="gradient max-w(640) mx-auto" id="sunrise" role="group" aria-label="Background gradient" style="--gradient-ratio: 3 / 1" {@attach mounted}>  <div class="gradient-preview" aria-hidden="true"></div>  <div class="gradient-bar"></div>  <div class="flex flex-wrap items-center gap-xs">    <button type="button" class="btn btn-sm gradient-add">      <PlusIcon class="icon" />Add a stop    </button>    <button type="button" class="btn btn-sm gradient-remove">      <MinusIcon class="icon" />Remove    </button>    <button type="button" class="btn btn-sm btn-quiet gradient-reverse">      <FlipHorizontalIcon class="icon" />Reverse    </button>  </div>  <div class="gradient-fields">    <fieldset class="gradient-color">      <legend class="mb(6) text-xs font-medium fg-text-muted">Color of the stop</legend>      <div class="color-picker" style="--color-picker-lightness: .7; --color-picker-chroma: .19; --color-picker-hue: 50">        <div class="color-picker-area">          <div class="color-picker-thumb" role="slider" tabindex="0" aria-label="Lightness and chroma" aria-valuemin="0" aria-valuemax="100" aria-valuenow="70"></div>        </div>        <span class="swatch swatch-lg color-picker-swatch"></span>        <input type="range" class="slider color-picker-hue" min="0" max="360" value="50" aria-label="Hue">        <input type="range" class="slider color-picker-alpha" min="0" max="100" value="100" aria-label="Opacity">        <div class="color-picker-fields">          <select class="select select-sm color-picker-format" aria-label="Format">            <option value="hex" selected>Hex</option>            <option value="rgb">RGB</option>            <option value="oklch">OKLCH</option>          </select>          <input class="input input-sm color-picker-text" value="#ff6a00" aria-label="Color" spellcheck="false" autocomplete="off">        </div>      </div>    </fieldset>    <div class="flex flex-col gap-md min-w(0)">      <fieldset class="gradient-offset">        <legend class="mb(6) text-xs font-medium fg-text-muted">Position</legend>        <input type="range" class="slider" min="0" max="100" step="1" value="0" aria-label="Position of the stop">        <input type="number" class="input input-sm w(72)" min="0" max="100" step="1" value="0" aria-label="Position of the stop, in percent">      </fieldset>      <fieldset class="gradient-type">        <legend class="mb(6) text-xs font-medium fg-text-muted">Type</legend>        <div class="segmented segmented-sm" role="radiogroup" aria-label="Type">          <label class="segmented-item">            <input type="radio" class="segmented-input" name="sunrise-type" value="linear" checked>Linear          </label>          <label class="segmented-item">            <input type="radio" class="segmented-input" name="sunrise-type" value="radial">Radial          </label>          <label class="segmented-item">            <input type="radio" class="segmented-input" name="sunrise-type" value="conic">Conic          </label>        </div>      </fieldset>      <div class="flex flex-wrap items-start gap-lg">        <fieldset class="gradient-angle">          <legend class="mb(6) text-xs font-medium fg-text-muted">Angle</legend>          <div class="dial dial-sm dial-full" role="slider" tabindex="0" aria-label="Angle" aria-valuenow="135" aria-valuemin="0" aria-valuemax="360" aria-valuetext="135°" style="--dial-value: 135">            <span class="dial-value">135°</span>            <span class="dial-thumb"></span>          </div>        </fieldset>        <fieldset class="gradient-center">          <legend class="mb(6) text-xs font-medium fg-text-muted">Center</legend>          <input type="number" class="input input-sm w(72)" min="0" max="100" value="50" aria-label="Center across, in percent">          <input type="number" class="input input-sm w(72)" min="0" max="100" value="50" aria-label="Center down, in percent">        </fieldset>      </div>    </div>  </div>  <pre class="code-block gradient-css" role="region" tabindex="0" aria-label="The gradient as CSS"></pre></div>
<script setup>import { onMounted, onUnmounted, ref } from 'vue'import { attach, gradient } from '@master/ui/behaviors'import PlusIcon from '@master/icons-vue/native/16/soft/plus'import MinusIcon from '@master/icons-vue/native/16/soft/minus'import FlipHorizontalIcon from '@master/icons-vue/native/16/soft/flip-horizontal'const root = ref(null)let detach = []onMounted(() => {  detach = [    attach(root.value),    gradient(document.getElementById('sunrise'), { value: { type: 'linear', angle: 135, center: [50, 50], stops: [      { id: 'dawn', offset: 0, color: '#ff6a00' },      { id: 'noon', offset: 55, color: '#ee0979' },      { id: 'dusk', offset: 100, color: '#4f46e5' }    ] } }),  ]})onUnmounted(() => detach.forEach(stop => stop()))</script><template>  <div ref="root" class="gradient max-w(640) mx-auto" id="sunrise" role="group" aria-label="Background gradient" style="--gradient-ratio: 3 / 1">    <div class="gradient-preview" aria-hidden="true"></div>    <div class="gradient-bar"></div>    <div class="flex flex-wrap items-center gap-xs">      <button type="button" class="btn btn-sm gradient-add">        <PlusIcon class="icon" />Add a stop      </button>      <button type="button" class="btn btn-sm gradient-remove">        <MinusIcon class="icon" />Remove      </button>      <button type="button" class="btn btn-sm btn-quiet gradient-reverse">        <FlipHorizontalIcon class="icon" />Reverse      </button>    </div>    <div class="gradient-fields">      <fieldset class="gradient-color">        <legend class="mb(6) text-xs font-medium fg-text-muted">Color of the stop</legend>        <div class="color-picker" style="--color-picker-lightness: .7; --color-picker-chroma: .19; --color-picker-hue: 50">          <div class="color-picker-area">            <div class="color-picker-thumb" role="slider" tabindex="0" aria-label="Lightness and chroma" aria-valuemin="0" aria-valuemax="100" aria-valuenow="70"></div>          </div>          <span class="swatch swatch-lg color-picker-swatch"></span>          <input type="range" class="slider color-picker-hue" min="0" max="360" value="50" aria-label="Hue">          <input type="range" class="slider color-picker-alpha" min="0" max="100" value="100" aria-label="Opacity">          <div class="color-picker-fields">            <select class="select select-sm color-picker-format" aria-label="Format">              <option value="hex" selected>Hex</option>              <option value="rgb">RGB</option>              <option value="oklch">OKLCH</option>            </select>            <input class="input input-sm color-picker-text" value="#ff6a00" aria-label="Color" spellcheck="false" autocomplete="off">          </div>        </div>      </fieldset>      <div class="flex flex-col gap-md min-w(0)">        <fieldset class="gradient-offset">          <legend class="mb(6) text-xs font-medium fg-text-muted">Position</legend>          <input type="range" class="slider" min="0" max="100" step="1" value="0" aria-label="Position of the stop">          <input type="number" class="input input-sm w(72)" min="0" max="100" step="1" value="0" aria-label="Position of the stop, in percent">        </fieldset>        <fieldset class="gradient-type">          <legend class="mb(6) text-xs font-medium fg-text-muted">Type</legend>          <div class="segmented segmented-sm" role="radiogroup" aria-label="Type">            <label class="segmented-item">              <input type="radio" class="segmented-input" name="sunrise-type" value="linear" checked>Linear            </label>            <label class="segmented-item">              <input type="radio" class="segmented-input" name="sunrise-type" value="radial">Radial            </label>            <label class="segmented-item">              <input type="radio" class="segmented-input" name="sunrise-type" value="conic">Conic            </label>          </div>        </fieldset>        <div class="flex flex-wrap items-start gap-lg">          <fieldset class="gradient-angle">            <legend class="mb(6) text-xs font-medium fg-text-muted">Angle</legend>            <div class="dial dial-sm dial-full" role="slider" tabindex="0" aria-label="Angle" aria-valuenow="135" aria-valuemin="0" aria-valuemax="360" aria-valuetext="135°" style="--dial-value: 135">              <span class="dial-value">135°</span>              <span class="dial-thumb"></span>            </div>          </fieldset>          <fieldset class="gradient-center">            <legend class="mb(6) text-xs font-medium fg-text-muted">Center</legend>            <input type="number" class="input input-sm w(72)" min="0" max="100" value="50" aria-label="Center across, in percent">            <input type="number" class="input input-sm w(72)" min="0" max="100" value="50" aria-label="Center down, in percent">          </fieldset>        </div>      </div>    </div>    <pre class="code-block gradient-css" role="region" tabindex="0" aria-label="The gradient as CSS"></pre>  </div></template>
拖曳色標,或選取色標後使用方向鍵;切換漸層類型,並轉動旋鈕調整角度。

#值

漸層是由四個部分組成的值:

部分內容
typelinear、radial 或 conic
angle角度(度),用於線性與圓錐漸層
center兩個百分比,用於放射與圓錐漸層
stops每個色標都有一個固定跟著它的 id、從 0 到 100 的 offset,以及一個 color

色彩的寫法與取色器相同,以十六進位的 sRGB 表示,並包含不透明度。gradientCss(value) 會將漸層寫成 CSS,預覽和複製的文字都來自它:在能指定插值色彩空間的瀏覽器中以 OKLab 插值,並在前面先寫一行供不支援的瀏覽器使用的備援。

copied.css
background-image: linear-gradient(90deg, #ff6a00 0%, #ee0979 100%);background-image: linear-gradient(in oklab 90deg, #ff6a00 0%, #ee0979 100%);

編輯器不會讀回 CSS:值由編輯器保存,應用程式也應儲存值,而不是文字。

#色標

  • 色標的控點是一個滑桿,代表它的位置,在橫條上以該色標顏色的圓盤呈現。按下即可選取色標,拖曳可移動它;方向鍵每次移動 1,按住 Shift 時每次移動 10,Home 和 End 則移到兩端。目前選取的色標帶有 aria-current="true",會略微突出,並加上以文字色繪製的外環。
  • 色標可以越過彼此。 將色標移過另一個色標時,移動的色標會保留原有的 id,控點也會保留焦點;漸層會依位置的順序繪製色標。兩個色標位於同一位置時會形成銳利的邊界,並維持原本的順序。
  • 新增會在目前色標之後 10 個單位處(最多到 100)加入一個與目前色標同色的色標。以按鈕或 Delete 移除色標時,焦點會交給橫條上的下一個色標,若沒有則交給前一個。至少需要兩個色標:只剩兩個時,無法再移除。
  • 反轉會將色標的順序顛倒:每個位置都變成 100 減去原本的值。

拖曳從來不是唯一的操作方式:位置有滑桿和旁邊的數字欄位,每個色標也都能以 Tab 抵達。

#狀態

在根元素上設定 aria-readonly="true" 表示漸層無法變更,設定 aria-disabled="true" 則表示漸層目前未使用:控點停留在原位,控制項則會停用。

#行為

app.js
import { gradient, gradientCss } from '@master/ui/behaviors/gradient.js'const editor = gradient(document.querySelector('.gradient'), {  value: { type: 'linear', angle: 90, center: [50, 50], stops: [    { id: 'from', offset: 0, color: '#ff6a00' },    { id: 'to', offset: 100, color: '#ee0979' },  ] },})editor.value                     // the value as abovegradientCss(editor.value).text   // the two lines to copy

使用者操作期間會持續觸發 input,操作結束時觸發 change;設定 value 會顯示另一個漸層,但不會觸發任何事件。行為會依值寫出控點,因此自行寫出控點的框架應提供 draw。它會在根元素上以 --gradient 和 --gradient-fallback 公開漸層,並在每個色標的控點上以 --gradient-offset 和 --gradient-color 公開該色標:編輯器繪製的是它的內容,樣式表則負責定位。

#鍵盤

按鍵動作
Tab移到橫條上的目前色標,再依序移過各個控制項;其他色標可以用指標選取,或從橫條在 Tab 順序中的位置抵達
ArrowRight, ArrowUp在色標上:朝終點移動 1 個單位;按住 Shift 時移動 10 個單位。頁面由右至左排列時,ArrowRight 會向左移動
ArrowLeft, ArrowDown在色標上:朝起點移動 1 個單位;按住 Shift 時移動 10 個單位
PageUp, PageDown在色標上:移動 10 個單位
Home, End在色標上:移到 0 或 100
Delete, Backspace在色標上:色標多於兩個時將它移除,並將焦點交給相鄰的色標

#無障礙

  • 每個控點都以它在色標中的順序命名,並在 aria-valuetext 中說明它的位置與色彩。
  • 預覽只是值的圖像:請將它從輔助技術中隱藏,因為色標和 CSS 已傳達相同的資訊。
  • 在強制色彩模式下,漸層會保留自己的色彩,因為這些色彩就是它的內容;控點和橫條則以系統色彩繪製輪廓。

#React、Svelte 和 Vue

gradient 元件依值寫出控點與控制項,並掛上行為。使用 npx master-ui add gradient 將它複製到專案中;React、Svelte 和 Vue 說明了這些元件的建構與使用方式。

ui/gradient.tsx
'use client'import { useId, useState, type ComponentProps, type CSSProperties } from 'react'import { colorPickerState } from '@master/ui/behaviors/color-picker.js'import { gradient, gradientCss, gradientStops, gradientValue, type GradientValue } from '@master/ui/behaviors/gradient.js'import { useBehavior } from './use-behavior'export type { GradientValue }/** What the controls are called, in the page's words. */export interface GradientLabels {  add?: string  remove?: string  reverse?: string  color?: string  position?: string  type?: string  linear?: string  radial?: string  conic?: string  angle?: string  center?: string  centerX?: string  centerY?: string  css?: string}export interface GradientProps extends Omit<ComponentProps<'div'>, 'defaultValue' | 'children' | 'ref'> {  value?: GradientValue  defaultValue?: GradientValue  onValueChange?: (value: GradientValue) => void  /** The gradient is shown and cannot be changed. */  readOnly?: boolean  disabled?: boolean  labels?: GradientLabels}const icon = (...paths: string[]) => <svg className="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="1" strokeLinecap="round" strokeLinejoin="round" aria-hidden="true">{paths.map(d => <path key={d} d={d} />)}</svg>/** * A gradient editor. React writes the controls and a handle for each stop from the value, so the first HTML * holds the gradient; the behavior moves every state and asks for the stops again when they change. */export function Gradient({ value, defaultValue, onValueChange, readOnly, disabled, labels = {}, className, style, ...root }: GradientProps) {  const id = useId()  const [first] = useState(() => gradientValue(value ?? defaultValue))  const [shown, setShown] = useState(first)  const words = { add: 'Add a stop', remove: 'Remove', reverse: 'Reverse', color: 'Color of the stop', position: 'Position', type: 'Type', linear: 'Linear', radial: 'Radial', conic: 'Conic', angle: 'Angle', center: 'Centre', centerX: 'Centre across, in percent', centerY: 'Centre down, in percent', css: 'The gradient as CSS', ...labels }  const ref = useBehavior(gradient, { value: first, draw: setShown }, { value, defaultValue, onValueChange })  const css = gradientCss(first)  const color = colorPickerState(first.stops[0]!.color)!  return (    <div className={className ? `gradient ${className}` : 'gradient'} ref={ref} aria-readonly={readOnly || undefined} aria-disabled={disabled || undefined} style={{ '--gradient': css.css, '--gradient-fallback': css.fallback, ...style } as CSSProperties} {...root}>      <div className="gradient-preview" aria-hidden="true" />      <div className="gradient-bar">        {gradientStops(shown.stops).map((stop, index, all) => (          <span className="gradient-stop" role="slider" data-id={stop.id} aria-label={`Stop ${index + 1} of ${all.length}`} aria-valuemin={0} aria-valuemax={100} aria-valuenow={stop.offset} style={{ '--gradient-offset': stop.offset, '--gradient-color': stop.color } as CSSProperties} key={stop.id} />        ))}      </div>      <div className="flex flex-wrap items-center gap-xs">        <button type="button" className="btn btn-sm gradient-add">{icon('M3 8L13 8', 'M8 3L8 13')}{words.add}</button>        <button type="button" className="btn btn-sm gradient-remove">{icon('M3 8L13 8')}{words.remove}</button>        <button type="button" className="btn btn-sm btn-quiet gradient-reverse">{icon('M8 1.6667L8 14.3333', 'M5.6667 6.3298L5.6667 11.45A0.55 0.55 0 0 1 5.1167 12L2.5566 12A0.55 0.55 0 0 1 2.0647 11.204L4.6248 6.0839A0.55 0.55 0 0 1 5.6667 6.3298Z', 'M10.3333 6.3298L10.3333 11.45A0.55 0.55 0 0 0 10.8833 12L13.4434 12A0.55 0.55 0 0 0 13.9353 11.204L11.3752 6.0839A0.55 0.55 0 0 0 10.3333 6.3298Z')}{words.reverse}</button>      </div>      <div className="gradient-fields">        <fieldset className="gradient-color">          <legend className="mb(6) text-xs font-medium fg-text-muted">{words.color}</legend>          <div className="color-picker" style={color.properties as CSSProperties}>            <div className="color-picker-area">              <div className="color-picker-thumb" role="slider" tabIndex={0} aria-label="Lightness and chroma" aria-valuemin={0} aria-valuemax={100} aria-valuenow={color.valueNow} aria-valuetext={color.valueText} />            </div>            <span className="swatch swatch-lg color-picker-swatch" />            <input type="range" className="slider color-picker-hue" min={0} max={360} defaultValue={color.hue} aria-label="Hue" />            <input type="range" className="slider color-picker-alpha" min={0} max={100} defaultValue={color.alpha * 100} aria-label="Opacity" />            <div className="color-picker-fields">              <select className="select select-sm color-picker-format" defaultValue="hex" aria-label="Format">                <option value="hex">Hex</option>                <option value="rgb">RGB</option>                <option value="oklch">OKLCH</option>              </select>              <input className="input input-sm color-picker-text" defaultValue={color.value} aria-label={words.color} spellCheck={false} autoComplete="off" />            </div>          </div>        </fieldset>        <div className="flex flex-col gap-md min-w(0)">          <fieldset className="gradient-offset">            <legend className="mb(6) text-xs font-medium fg-text-muted">{words.position}</legend>            <input type="range" className="slider" min={0} max={100} step={1} defaultValue={first.stops[0]!.offset} aria-label={words.position} />            <input type="number" className="input input-sm w(72)" min={0} max={100} step={0.1} defaultValue={first.stops[0]!.offset} aria-label={`${words.position}, %`} />          </fieldset>          <fieldset className="gradient-type">            <legend className="mb(6) text-xs font-medium fg-text-muted">{words.type}</legend>            <div className="segmented segmented-sm" role="radiogroup" aria-label={words.type}>              {(['linear', 'radial', 'conic'] as const).map(type => (                <label className="segmented-item" key={type}><input type="radio" className="segmented-input" name={`${id}type`} value={type} defaultChecked={first.type === type} />{words[type]}</label>              ))}            </div>          </fieldset>          <div className="flex flex-wrap items-start gap-lg">            <fieldset className="gradient-angle">              <legend className="mb(6) text-xs font-medium fg-text-muted">{words.angle}</legend>              <div className="dial dial-sm dial-full" role="slider" tabIndex={0} aria-label={words.angle} aria-valuenow={first.angle} aria-valuemin={0} aria-valuemax={360} aria-valuetext={`${first.angle}°`} style={{ '--dial-value': first.angle } as CSSProperties}>                <span className="dial-value">{`${first.angle}°`}</span>                <span className="dial-thumb" />              </div>            </fieldset>            <fieldset className="gradient-center">              <legend className="mb(6) text-xs font-medium fg-text-muted">{words.center}</legend>              <input type="number" className="input input-sm w(72)" min={0} max={100} defaultValue={first.center[0]} aria-label={words.centerX} />              <input type="number" className="input input-sm w(72)" min={0} max={100} defaultValue={first.center[1]} aria-label={words.centerY} />            </fieldset>          </div>        </div>      </div>      <pre className="code-block gradient-css" aria-label={words.css}>{css.text}</pre>    </div>  )}
ui/Gradient.svelte
<script lang="ts" module>  import type { HTMLAttributes } from 'svelte/elements'  import type { GradientValue } from '@master/ui/behaviors/gradient.js'  export type { GradientValue }  /** What the controls are called, in the page's words. */  export interface GradientLabels {    add?: string    remove?: string    reverse?: string    color?: string    position?: string    type?: string    linear?: string    radial?: string    conic?: string    angle?: string    center?: string    centerX?: string    centerY?: string    css?: string  }  export interface GradientProps extends Omit<HTMLAttributes<HTMLDivElement>, 'children'> {    /** The gradient. Bind it, or leave it out: two stops, black to white. */    value?: GradientValue    onValueChange?: (value: GradientValue) => void    /** The gradient is shown and cannot be changed. */    readOnly?: boolean    disabled?: boolean    labels?: GradientLabels  }</script><script lang="ts">  import { untrack } from 'svelte'  import { colorPickerState } from '@master/ui/behaviors/color-picker.js'  import { gradient, gradientCss, gradientStops, gradientValue } from '@master/ui/behaviors/gradient.js'  import { behavior } from './behavior.svelte.js'  let { value = $bindable(), onValueChange, readOnly, disabled, labels = {}, class: className, style, ...root }: GradientProps = $props()  const id = $props.id()  const first = untrack(() => gradientValue(value))  let shown = $state(first)  const words = $derived({ add: 'Add a stop', remove: 'Remove', reverse: 'Reverse', color: 'Color of the stop', position: 'Position', type: 'Type', linear: 'Linear', radial: 'Radial', conic: 'Conic', angle: 'Angle', center: 'Centre', centerX: 'Centre across, in percent', centerY: 'Centre down, in percent', css: 'The gradient as CSS', ...labels })  const css = gradientCss(first)  // Svelte writes a style again whenever its owner changes it, so it writes the gradient the behavior holds.  const painted = $derived(gradientCss(shown))  const color = colorPickerState(first.stops[0]!.color)!  const attach = behavior(gradient, () => ({ value: first, draw: (next: GradientValue) => { shown = next } }), {    value: () => value,    change: (next) => { value = next; onValueChange?.(next) },  })  const types = ['linear', 'radial', 'conic'] as const  const pickerStyle = Object.entries(color.properties).map(([name, text]) => `${name}: ${text}`).join('; ')</script><!--  @component  A gradient editor. Svelte writes the controls and a handle for each stop from the value, so the first HTML  holds the gradient; the behavior moves every state and asks for the stops again when they change.--><div class={['gradient', className]} aria-readonly={readOnly || undefined} aria-disabled={disabled || undefined} {style} style:--gradient={painted.css} style:--gradient-fallback={painted.fallback} {@attach attach} {...root}>  <div class="gradient-preview" aria-hidden="true"></div>  <div class="gradient-bar">    {#each gradientStops(shown.stops) as stop, index (stop.id)}      <span class="gradient-stop" role="slider" data-id={stop.id} aria-label="Stop {index + 1} of {shown.stops.length}" aria-valuemin={0} aria-valuemax={100} aria-valuenow={stop.offset} style="--gradient-offset: {stop.offset}; --gradient-color: {stop.color}"></span>    {/each}  </div>  <div class="flex flex-wrap items-center gap-xs">    <button type="button" class="btn btn-sm gradient-add"><svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M3 8L13 8" /><path d="M8 3L8 13" /></svg>{words.add}</button>    <button type="button" class="btn btn-sm gradient-remove"><svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M3 8L13 8" /></svg>{words.remove}</button>    <button type="button" class="btn btn-sm btn-quiet gradient-reverse"><svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M8 1.6667L8 14.3333" /><path d="M5.6667 6.3298L5.6667 11.45A0.55 0.55 0 0 1 5.1167 12L2.5566 12A0.55 0.55 0 0 1 2.0647 11.204L4.6248 6.0839A0.55 0.55 0 0 1 5.6667 6.3298Z" /><path d="M10.3333 6.3298L10.3333 11.45A0.55 0.55 0 0 0 10.8833 12L13.4434 12A0.55 0.55 0 0 0 13.9353 11.204L11.3752 6.0839A0.55 0.55 0 0 0 10.3333 6.3298Z" /></svg>{words.reverse}</button>  </div>  <div class="gradient-fields">    <fieldset class="gradient-color">      <legend class="mb(6) text-xs font-medium fg-text-muted">{words.color}</legend>      <div class="color-picker" style={pickerStyle}>        <div class="color-picker-area">          <div class="color-picker-thumb" role="slider" tabindex="0" aria-label="Lightness and chroma" aria-valuemin={0} aria-valuemax={100} aria-valuenow={color.valueNow} aria-valuetext={color.valueText}></div>        </div>        <span class="swatch swatch-lg color-picker-swatch"></span>        <input type="range" class="slider color-picker-hue" min="0" max="360" value={color.hue} aria-label="Hue" />        <input type="range" class="slider color-picker-alpha" min="0" max="100" value={color.alpha * 100} aria-label="Opacity" />        <div class="color-picker-fields">          <select class="select select-sm color-picker-format" aria-label="Format">            <option value="hex" selected>Hex</option>            <option value="rgb">RGB</option>            <option value="oklch">OKLCH</option>          </select>          <input class="input input-sm color-picker-text" value={color.value} aria-label={words.color} spellcheck="false" autocomplete="off" />        </div>      </div>    </fieldset>    <div class="flex flex-col gap-md min-w(0)">      <fieldset class="gradient-offset">        <legend class="mb(6) text-xs font-medium fg-text-muted">{words.position}</legend>        <input type="range" class="slider" min="0" max="100" step="1" value={first.stops[0]!.offset} aria-label={words.position} />        <input type="number" class="input input-sm w(72)" min="0" max="100" step="0.1" value={first.stops[0]!.offset} aria-label="{words.position}, %" />      </fieldset>      <fieldset class="gradient-type">        <legend class="mb(6) text-xs font-medium fg-text-muted">{words.type}</legend>        <div class="segmented segmented-sm" role="radiogroup" aria-label={words.type}>          {#each types as type (type)}            <label class="segmented-item"><input type="radio" class="segmented-input" name="{id}type" value={type} checked={first.type === type} />{words[type]}</label>          {/each}        </div>      </fieldset>      <div class="flex flex-wrap items-start gap-lg">        <fieldset class="gradient-angle">          <legend class="mb(6) text-xs font-medium fg-text-muted">{words.angle}</legend>          <div class="dial dial-sm dial-full" role="slider" tabindex="0" aria-label={words.angle} aria-valuenow={first.angle} aria-valuemin={0} aria-valuemax={360} aria-valuetext="{first.angle}°" style="--dial-value: {first.angle}">            <span class="dial-value">{first.angle}°</span>            <span class="dial-thumb"></span>          </div>        </fieldset>        <fieldset class="gradient-center">          <legend class="mb(6) text-xs font-medium fg-text-muted">{words.center}</legend>          <input type="number" class="input input-sm w(72)" min="0" max="100" value={first.center[0]} aria-label={words.centerX} />          <input type="number" class="input input-sm w(72)" min="0" max="100" value={first.center[1]} aria-label={words.centerY} />        </fieldset>      </div>    </div>  </div>  <pre class="code-block gradient-css" aria-label={words.css}>{css.text}</pre></div>
ui/Gradient.vue
<script lang="ts">import type { GradientValue } from '@master/ui/behaviors/gradient.js'export type { GradientValue }/** What the controls are called, in the page's words. */export interface GradientLabels {  add?: string  remove?: string  reverse?: string  color?: string  position?: string  type?: string  linear?: string  radial?: string  conic?: string  angle?: string  center?: string  centerX?: string  centerY?: string  css?: string}</script><script setup lang="ts">import { computed, shallowRef, useId, useTemplateRef } from 'vue'import { colorPickerState } from '@master/ui/behaviors/color-picker.js'import { gradient, gradientCss, gradientStops, gradientValue } from '@master/ui/behaviors/gradient.js'import { useBehavior } from './use-behavior'defineOptions({ inheritAttrs: false })const { readOnly, disabled, labels = {} } = defineProps<{  /** The gradient is shown and cannot be changed. */  readOnly?: boolean  disabled?: boolean  labels?: GradientLabels}>()/** The gradient: `v-model`. Two stops, black to white, by default. */const model = defineModel<GradientValue>()/* * A gradient editor. Vue writes the controls and a handle for each stop from the value, so the first HTML holds * the gradient; the behavior moves every state and asks for the stops again when they change. */const id = useId()const first = gradientValue(model.value)const shown = shallowRef<GradientValue>(first)const words = computed(() => ({ add: 'Add a stop', remove: 'Remove', reverse: 'Reverse', color: 'Color of the stop', position: 'Position', type: 'Type', linear: 'Linear', radial: 'Radial', conic: 'Conic', angle: 'Angle', center: 'Centre', centerX: 'Centre across, in percent', centerY: 'Centre down, in percent', css: 'The gradient as CSS', ...labels }))const css = gradientCss(first)// Vue writes a style again each time it renders, so it writes the gradient the behavior holds, not the first one.const painted = computed(() => gradientCss(shown.value))const color = colorPickerState(first.stops[0]!.color)!const types = ['linear', 'radial', 'conic'] as constconst root = useTemplateRef<HTMLDivElement>('root')useBehavior(root, gradient, () => ({ value: first, draw: (next: GradientValue) => { shown.value = next } }), model)</script><template>  <div class="gradient" ref="root" :aria-readonly="readOnly || undefined" :aria-disabled="disabled || undefined" :style="{ '--gradient': painted.css, '--gradient-fallback': painted.fallback }" v-bind="$attrs">    <div class="gradient-preview" aria-hidden="true"></div>    <div class="gradient-bar">      <span v-for="(stop, index) in gradientStops(shown.stops)" :key="stop.id" class="gradient-stop" role="slider" :data-id="stop.id" :aria-label="`Stop ${index + 1} of ${shown.stops.length}`" :aria-valuemin="0" :aria-valuemax="100" :aria-valuenow="stop.offset" :style="{ '--gradient-offset': stop.offset, '--gradient-color': stop.color }"></span>    </div>    <div class="flex flex-wrap items-center gap-xs">      <button type="button" class="btn btn-sm gradient-add"><svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M3 8L13 8" /><path d="M8 3L8 13" /></svg>{{ words.add }}</button>      <button type="button" class="btn btn-sm gradient-remove"><svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M3 8L13 8" /></svg>{{ words.remove }}</button>      <button type="button" class="btn btn-sm btn-quiet gradient-reverse"><svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M8 1.6667L8 14.3333" /><path d="M5.6667 6.3298L5.6667 11.45A0.55 0.55 0 0 1 5.1167 12L2.5566 12A0.55 0.55 0 0 1 2.0647 11.204L4.6248 6.0839A0.55 0.55 0 0 1 5.6667 6.3298Z" /><path d="M10.3333 6.3298L10.3333 11.45A0.55 0.55 0 0 0 10.8833 12L13.4434 12A0.55 0.55 0 0 0 13.9353 11.204L11.3752 6.0839A0.55 0.55 0 0 0 10.3333 6.3298Z" /></svg>{{ words.reverse }}</button>    </div>    <div class="gradient-fields">      <fieldset class="gradient-color">        <legend class="mb(6) text-xs font-medium fg-text-muted">{{ words.color }}</legend>        <div class="color-picker" :style="color.properties">          <div class="color-picker-area">            <div class="color-picker-thumb" role="slider" tabindex="0" aria-label="Lightness and chroma" :aria-valuemin="0" :aria-valuemax="100" :aria-valuenow="color.valueNow" :aria-valuetext="color.valueText"></div>          </div>          <span class="swatch swatch-lg color-picker-swatch"></span>          <input type="range" class="slider color-picker-hue" min="0" max="360" :value.attr="color.hue" aria-label="Hue" />          <input type="range" class="slider color-picker-alpha" min="0" max="100" :value.attr="color.alpha * 100" aria-label="Opacity" />          <div class="color-picker-fields">            <select class="select select-sm color-picker-format" aria-label="Format">              <option value="hex" selected>Hex</option>              <option value="rgb">RGB</option>              <option value="oklch">OKLCH</option>            </select>            <input class="input input-sm color-picker-text" :value.attr="color.value" :aria-label="words.color" spellcheck="false" autocomplete="off" />          </div>        </div>      </fieldset>      <div class="flex flex-col gap-md min-w(0)">        <fieldset class="gradient-offset">          <legend class="mb(6) text-xs font-medium fg-text-muted">{{ words.position }}</legend>          <input type="range" class="slider" min="0" max="100" step="1" :value.attr="first.stops[0]!.offset" :aria-label="words.position" />          <input type="number" class="input input-sm w(72)" min="0" max="100" step="0.1" :value.attr="first.stops[0]!.offset" :aria-label="`${words.position}, %`" />        </fieldset>        <fieldset class="gradient-type">          <legend class="mb(6) text-xs font-medium fg-text-muted">{{ words.type }}</legend>          <div class="segmented segmented-sm" role="radiogroup" :aria-label="words.type">            <label v-for="type in types" :key="type" class="segmented-item"><input type="radio" class="segmented-input" :name="`${id}-type`" :value="type" :checked="first.type === type" />{{ words[type] }}</label>          </div>        </fieldset>        <div class="flex flex-wrap items-start gap-lg">          <fieldset class="gradient-angle">            <legend class="mb(6) text-xs font-medium fg-text-muted">{{ words.angle }}</legend>            <div class="dial dial-sm dial-full" role="slider" tabindex="0" :aria-label="words.angle" :aria-valuenow="first.angle" :aria-valuemin="0" :aria-valuemax="360" :aria-valuetext="`${first.angle}°`" :style="{ '--dial-value': shown.angle }">              <span class="dial-value">{{ first.angle }}°</span>              <span class="dial-thumb"></span>            </div>          </fieldset>          <fieldset class="gradient-center">            <legend class="mb(6) text-xs font-medium fg-text-muted">{{ words.center }}</legend>            <input type="number" class="input input-sm w(72)" min="0" max="100" :value.attr="first.center[0]" :aria-label="words.centerX" />            <input type="number" class="input input-sm w(72)" min="0" max="100" :value.attr="first.center[1]" :aria-label="words.centerY" />          </fieldset>        </div>      </div>    </div>    <pre class="code-block gradient-css" :aria-label="words.css">{{ css.text }}</pre>  </div></template>

#類別

類別說明
gradient編輯器:預覽、色標橫條和控制項。gradient 行為會寫出色標的控點並公開漸層。在其上標記 aria-readonly="true" 或 aria-disabled="true" 可防止變更
gradient-preview棋盤格背景上的漸層,除非 --gradient-ratio 另有設定,否則比例為 16 : 9;請以 aria-hidden 隱藏它,因為色標和 CSS 已傳達相同的資訊
gradient-bar不論類型為何,都由起點到終點排列色標,呈現為一道讓控點立於其上的淺凹槽
gradient-stop由行為寫出的色標控點:沿橫條位於 --gradient-offset 處、帶珍珠色外環的色標色圓盤,也是代表位置的滑桿,並以它在色標中的順序命名
gradient-fields控制項,排成一欄;空間足夠時並排成兩欄
gradient-color容納目前色標的 color-picker;gradient 行為會掛上取色器
gradient-offset容納一個範圍滑桿和一個數字輸入欄位,範圍從 0 到 100,用於目前色標的位置
gradient-type容納選擇類型的 linear、radial 和 conic 單選按鈕;是一個分段控制項
gradient-angle容納設定角度的旋鈕,範圍從 0 到 360 度,用於線性與圓錐漸層
gradient-center容納兩個數字輸入欄位,即中心點的 x 和 y(百分比),用於放射與圓錐漸層
gradient-add在目前色標之後 10 個單位處加入一個同色的色標,最多到 100
gradient-remove移除目前色標;只剩兩個色標時停用
gradient-reverse反轉色標的順序
gradient-css由行為寫出、可供複製的 CSS 漸層:先是備援,再是以 OKLab 插值的那一行

#自訂屬性

屬性預設值說明
--gradientnone以 OKLab 插值的漸層,由行為寫出
--gradient-fallbacknone以 sRGB 插值的漸層,供無法指定插值方式的瀏覽器使用
--gradient-bar橫條的填色:不論漸層類型,都把每個色標從起點到終點畫成以 sRGB 插值的直線漸層,由行為寫出設定於 gradient
--gradient-ratio預覽的形狀;預設為 16 / 9設定於 gradient-preview
--gradient-offset控點在橫條上的位置,從 0 到 100,由行為寫出設定於 gradient-stop
--gradient-color控點所屬色標的色彩,由行為寫出設定於 gradient-stop

#原始碼

@master/ui/components/gradient.css
/* * Gradient: a gradient being made. A preview of it, a bar of its stops, and the controls of the stop in hand and * of the whole gradient, which are the library's own: a color picker, a range and a number, a segmented * control for the type, a dial for the angle, numbers for the centre. * * What the gradient looks like is its content, not a material: the gradient behavior publishes it as * --gradient (interpolated in OKLab) and --gradient-fallback on the root, and each stop's place and color as * --gradient-offset and --gradient-color on its handle. The stylesheet paints the preview and the bar from * them over a checkerboard, so a stop's opacity shows, and places each handle along the bar. * * The bar is a short pressed track as wide as the gradient; a handle is a disc of its stop's color with a * pearl ring, standing on the bar where its stop is, its middle on the stop. The stop in hand rises and takes an * ink ring; a handle the keys are on draws the focus ring as well. Handles keep their place in the DOM as they * move, so the one in hand never loses focus when it passes another. */@layer components.ui {  .gradient {    --gradient: none;    --gradient-fallback: none;    --gradient-handle: 1.25rem;    --gradient-check: color-mix(in oklab, var(--color-text-strong) 9%, transparent);    container: gradient / inline-size;    display: grid;    gap: 1rem;    min-width: 0;    &[aria-disabled="true"] {      opacity: .6;    }  }  /*   * The preview: the gradient on a face with a large control's corner, over a checkerboard, so transparency   * reads as such. Its size is the page's to give; it keeps a 16 : 9 shape by default.   */  .gradient-preview {    aspect-ratio: var(--gradient-ratio, 16 / 9);    min-height: 0;    border-radius: calc(var(--radius-control-lg) * var(--corner-scale));    corner-shape: var(--corner-shape);    background:      var(--gradient-fallback),      repeating-conic-gradient(var(--gradient-check) 0 25%, transparent 0 50%) 0 0 / 16px 16px,      var(--color-surface-raised);    box-shadow: inset 0 0 0 .5px var(--color-line-image-strong);    @supports (background-image: linear-gradient(in oklab, white, black)) {      background:        var(--gradient),        repeating-conic-gradient(var(--gradient-check) 0 25%, transparent 0 50%) 0 0 / 16px 16px,        var(--color-surface-raised);    }    @media (forced-colors: active) {      forced-color-adjust: none;      outline: 1px solid CanvasText;    }  }  /*   * The bar: the stops from start to end, whatever the type, as a short well. The handles stand on it, half   * a handle in from its ends at 0 and 100, so they never hang outside the gradient. Only a handle takes the   * press, so the bar leaves a finger to scroll the page; each handle reaches 44px on touch itself.   */  .gradient-bar {    position: relative;    /* The stop in hand comes forward within the bar, never over the page around it. */    isolation: isolate;    height: .75rem;    margin-inline: calc(var(--gradient-handle) / 2);    border-radius: 999px;    corner-shape: round;    background:      var(--gradient-bar, var(--gradient-fallback)),      repeating-conic-gradient(var(--gradient-check) 0 25%, transparent 0 50%) 0 0 / 8px 8px,      var(--color-surface-raised);    box-shadow: var(--shadow-pressed);    @media (forced-colors: active) {      forced-color-adjust: none;      outline: 1px solid CanvasText;    }  }  /* A stop's handle: a disc of its color in a pearl ring, its middle on the stop. */  .gradient-stop {    position: absolute;    top: 50%;    inset-inline-start: calc(var(--gradient-offset, 0) * 1%);    width: var(--gradient-handle);    height: var(--gradient-handle);    translate: -50% -50%;    border-radius: 999px;    corner-shape: round;    background: linear-gradient(var(--gradient-color, transparent), var(--gradient-color, transparent)), repeating-conic-gradient(var(--gradient-check) 0 25%, transparent 0 50%) 0 0 / 6px 6px, var(--color-surface-raised);    box-shadow: 0 0 0 2px var(--color-thumb-high), 0 0 0 2.5px var(--color-line-strong), 0 1px 3px 1px var(--color-shade-1), 0 2px 6px var(--color-shade-2);    cursor: grab;    touch-action: none;    -webkit-tap-highlight-color: transparent;    transition-property: box-shadow, scale;    transition-duration: var(--duration-quick);    transition-timing-function: var(--easing-glide);    &:dir(rtl) {      translate: 50% -50%;    }    /* The stop in hand stands a little forward, ringed in ink. */    &[aria-current="true"] {      z-index: 1;      scale: 1.12;      box-shadow: 0 0 0 2px var(--color-thumb-high), 0 0 0 3.5px var(--color-text-strong), 0 2px 6px 1px var(--color-shade-1), 0 4px 10px var(--color-shade-2);    }    &:active {      cursor: grabbing;    }    &:focus-visible {      @apply --focus-ring;      outline-offset: 4px;    }    /* Locked: as heavy as :active, and later, so a press shows no grip either. */    &[aria-disabled="true"] {      cursor: not-allowed;    }    /* On touch the target reaches 44px around the handle, which keeps its size. */    @media (pointer: coarse) {      &::after {        @apply --touch-target;      }    }    @media (prefers-reduced-motion: reduce) {      transition-property: box-shadow;      &[aria-current="true"] {        scale: none;      }    }    @media (forced-colors: active) {      forced-color-adjust: none;      outline: 2px solid ButtonText;      &[aria-current="true"] {        outline-color: Highlight;        outline-width: 3px;      }      /* Opted out of forced colors, the focus ring would keep the theme's ink: it takes the system's own. */      &:focus-visible {        outline-color: Highlight;      }      &[aria-disabled="true"] {        outline-color: GrayText;      }    }  }  /*   * The controls: the stop in hand (its color, its offset, remove and add) and the gradient (its type, angle and   * centre, reverse), in a column that becomes two side by side when the room allows.   */  .gradient-fields {    display: grid;    gap: .75rem 1.25rem;    align-items: start;  }  @container gradient (width >= 34rem) {    .gradient-fields {      grid-template-columns: auto minmax(0, 1fr);    }  }  /* A labelled group of the gradient's controls: a row of a label over its control. */  .gradient-color, .gradient-offset, .gradient-type, .gradient-angle, .gradient-center {    display: grid;    gap: .375rem;    min-width: 0;    margin: 0;    padding: 0;    border: 0;  }  .gradient-offset, .gradient-center {    grid-template-columns: minmax(0, 1fr) auto;    align-items: center;  }  .gradient-add, .gradient-remove, .gradient-reverse {    flex: none;  }  /* The CSS of the gradient, as it is copied: the fallback, then the OKLab line. A code block scrolls it. */  .gradient-css {    min-width: 0;    margin: 0;  }}