網站導覽

v2.1.0
表單

滾輪選擇器

轉動欄位,讓要選取的值停在標記下方。

本頁內容

滾輪選擇器透過轉動欄位來選取值,讓小時、分鐘或單位停在標記下方。它利用平台原有的功能做出手機上常見的選擇器。每個欄位都是捲動容器,會讓一個選項貼齊中央。滾輪的選項區向內凹,中央有一道橫跨各欄的標記,用來指出目前選取的是哪一列。

滾輪選擇器

<div class="wheel" role="group" aria-label="Reminder time">  <div class="wheel-column" role="listbox" aria-label="Hour" tabindex="0">    <div class="wheel-option" role="option" aria-selected="false">01</div>    <div class="wheel-option" role="option" aria-selected="false">02</div>    <div class="wheel-option" role="option" aria-selected="false">03</div>    <div class="wheel-option" role="option" aria-selected="false">04</div>    <div class="wheel-option" role="option" aria-selected="false">05</div>    <div class="wheel-option" role="option" aria-selected="false">06</div>    <div class="wheel-option" role="option" aria-selected="false">07</div>    <div class="wheel-option" role="option" aria-selected="false">08</div>    <div class="wheel-option" role="option" aria-selected="true">09</div>    <div class="wheel-option" role="option" aria-selected="false">10</div>    <div class="wheel-option" role="option" aria-selected="false">11</div>    <div class="wheel-option" role="option" aria-selected="false">12</div>  </div>  <span class="wheel-separator" aria-hidden="true">:</span>  <div class="wheel-column" role="listbox" aria-label="Minute" tabindex="0">    <div class="wheel-option" role="option" aria-selected="false">00</div>    <div class="wheel-option" role="option" aria-selected="false">05</div>    <div class="wheel-option" role="option" aria-selected="false">10</div>    <div class="wheel-option" role="option" aria-selected="false">15</div>    <div class="wheel-option" role="option" aria-selected="false">20</div>    <div class="wheel-option" role="option" aria-selected="false">25</div>    <div class="wheel-option" role="option" aria-selected="true">30</div>    <div class="wheel-option" role="option" aria-selected="false">35</div>    <div class="wheel-option" role="option" aria-selected="false">40</div>    <div class="wheel-option" role="option" aria-selected="false">45</div>    <div class="wheel-option" role="option" aria-selected="false">50</div>    <div class="wheel-option" role="option" aria-selected="false">55</div>  </div>  <div class="wheel-column" role="listbox" aria-label="Period" tabindex="0">    <div class="wheel-option" role="option" aria-selected="true">AM</div>    <div class="wheel-option" role="option" aria-selected="false">PM</div>  </div></div><script type="module">  import { attach } from '@master/ui/behaviors'  attach()</script>
import { useEffect, useRef } from 'react'import { attach } from '@master/ui/behaviors'export function Example() {  const root = useRef<HTMLDivElement>(null)  useEffect(() => attach(root.current!), [])  return (    <div ref={root} className="wheel" role="group" aria-label="Reminder time">      <div className="wheel-column" role="listbox" aria-label="Hour" tabIndex={0}>        <div className="wheel-option" role="option" aria-selected="false">01</div>        <div className="wheel-option" role="option" aria-selected="false">02</div>        <div className="wheel-option" role="option" aria-selected="false">03</div>        <div className="wheel-option" role="option" aria-selected="false">04</div>        <div className="wheel-option" role="option" aria-selected="false">05</div>        <div className="wheel-option" role="option" aria-selected="false">06</div>        <div className="wheel-option" role="option" aria-selected="false">07</div>        <div className="wheel-option" role="option" aria-selected="false">08</div>        <div className="wheel-option" role="option" aria-selected="true">09</div>        <div className="wheel-option" role="option" aria-selected="false">10</div>        <div className="wheel-option" role="option" aria-selected="false">11</div>        <div className="wheel-option" role="option" aria-selected="false">12</div>      </div>      <span className="wheel-separator" aria-hidden="true">:</span>      <div className="wheel-column" role="listbox" aria-label="Minute" tabIndex={0}>        <div className="wheel-option" role="option" aria-selected="false">00</div>        <div className="wheel-option" role="option" aria-selected="false">05</div>        <div className="wheel-option" role="option" aria-selected="false">10</div>        <div className="wheel-option" role="option" aria-selected="false">15</div>        <div className="wheel-option" role="option" aria-selected="false">20</div>        <div className="wheel-option" role="option" aria-selected="false">25</div>        <div className="wheel-option" role="option" aria-selected="true">30</div>        <div className="wheel-option" role="option" aria-selected="false">35</div>        <div className="wheel-option" role="option" aria-selected="false">40</div>        <div className="wheel-option" role="option" aria-selected="false">45</div>        <div className="wheel-option" role="option" aria-selected="false">50</div>        <div className="wheel-option" role="option" aria-selected="false">55</div>      </div>      <div className="wheel-column" role="listbox" aria-label="Period" tabIndex={0}>        <div className="wheel-option" role="option" aria-selected="true">AM</div>        <div className="wheel-option" role="option" aria-selected="false">PM</div>      </div>    </div>  )}
<script>  import { attach } from '@master/ui/behaviors'</script><div class="wheel" role="group" aria-label="Reminder time" {@attach attach}>  <div class="wheel-column" role="listbox" aria-label="Hour" tabindex="0">    <div class="wheel-option" role="option" aria-selected="false">01</div>    <div class="wheel-option" role="option" aria-selected="false">02</div>    <div class="wheel-option" role="option" aria-selected="false">03</div>    <div class="wheel-option" role="option" aria-selected="false">04</div>    <div class="wheel-option" role="option" aria-selected="false">05</div>    <div class="wheel-option" role="option" aria-selected="false">06</div>    <div class="wheel-option" role="option" aria-selected="false">07</div>    <div class="wheel-option" role="option" aria-selected="false">08</div>    <div class="wheel-option" role="option" aria-selected="true">09</div>    <div class="wheel-option" role="option" aria-selected="false">10</div>    <div class="wheel-option" role="option" aria-selected="false">11</div>    <div class="wheel-option" role="option" aria-selected="false">12</div>  </div>  <span class="wheel-separator" aria-hidden="true">:</span>  <div class="wheel-column" role="listbox" aria-label="Minute" tabindex="0">    <div class="wheel-option" role="option" aria-selected="false">00</div>    <div class="wheel-option" role="option" aria-selected="false">05</div>    <div class="wheel-option" role="option" aria-selected="false">10</div>    <div class="wheel-option" role="option" aria-selected="false">15</div>    <div class="wheel-option" role="option" aria-selected="false">20</div>    <div class="wheel-option" role="option" aria-selected="false">25</div>    <div class="wheel-option" role="option" aria-selected="true">30</div>    <div class="wheel-option" role="option" aria-selected="false">35</div>    <div class="wheel-option" role="option" aria-selected="false">40</div>    <div class="wheel-option" role="option" aria-selected="false">45</div>    <div class="wheel-option" role="option" aria-selected="false">50</div>    <div class="wheel-option" role="option" aria-selected="false">55</div>  </div>  <div class="wheel-column" role="listbox" aria-label="Period" tabindex="0">    <div class="wheel-option" role="option" aria-selected="true">AM</div>    <div class="wheel-option" role="option" aria-selected="false">PM</div>  </div></div>
<script setup>import { onMounted, onUnmounted, ref } from 'vue'import { attach } from '@master/ui/behaviors'const root = ref(null)let detachonMounted(() => { detach = attach(root.value) })onUnmounted(() => detach?.())</script><template>  <div ref="root" class="wheel" role="group" aria-label="Reminder time">    <div class="wheel-column" role="listbox" aria-label="Hour" tabindex="0">      <div class="wheel-option" role="option" aria-selected="false">01</div>      <div class="wheel-option" role="option" aria-selected="false">02</div>      <div class="wheel-option" role="option" aria-selected="false">03</div>      <div class="wheel-option" role="option" aria-selected="false">04</div>      <div class="wheel-option" role="option" aria-selected="false">05</div>      <div class="wheel-option" role="option" aria-selected="false">06</div>      <div class="wheel-option" role="option" aria-selected="false">07</div>      <div class="wheel-option" role="option" aria-selected="false">08</div>      <div class="wheel-option" role="option" aria-selected="true">09</div>      <div class="wheel-option" role="option" aria-selected="false">10</div>      <div class="wheel-option" role="option" aria-selected="false">11</div>      <div class="wheel-option" role="option" aria-selected="false">12</div>    </div>    <span class="wheel-separator" aria-hidden="true">:</span>    <div class="wheel-column" role="listbox" aria-label="Minute" tabindex="0">      <div class="wheel-option" role="option" aria-selected="false">00</div>      <div class="wheel-option" role="option" aria-selected="false">05</div>      <div class="wheel-option" role="option" aria-selected="false">10</div>      <div class="wheel-option" role="option" aria-selected="false">15</div>      <div class="wheel-option" role="option" aria-selected="false">20</div>      <div class="wheel-option" role="option" aria-selected="false">25</div>      <div class="wheel-option" role="option" aria-selected="true">30</div>      <div class="wheel-option" role="option" aria-selected="false">35</div>      <div class="wheel-option" role="option" aria-selected="false">40</div>      <div class="wheel-option" role="option" aria-selected="false">45</div>      <div class="wheel-option" role="option" aria-selected="false">50</div>      <div class="wheel-option" role="option" aria-selected="false">55</div>    </div>    <div class="wheel-column" role="listbox" aria-label="Period" tabindex="0">      <div class="wheel-option" role="option" aria-selected="true">AM</div>      <div class="wheel-option" role="option" aria-selected="false">PM</div>    </div>  </div></template>
捲動欄位、在觸控螢幕上拖曳欄位、點選選項,或將焦點移到欄位後使用方向鍵。

#平台提供的功能

  • 轉動就是捲動。滑鼠滾輪、手指和觸控板的操作方式與其他捲動容器相同。
  • 初始選取項目在支援 scroll-initial-target 時,第一次繪製就會對齊標記。其他瀏覽器會在行為掛載時對齊。
  • 停靠由捲動貼齊實現。在欄位上設定 scroll-snap-type: y mandatory,並在每個選項上設定 scroll-snap-align: center。欄位不會停在兩個值之間。欄位兩端都留有空間,讓第一個和最後一個選項也能移到標記下方。
  • 滾輪的外觀由捲動驅動的動畫呈現。每個選項都有自己的檢視時間軸,以它在欄位中的位置為準。選項經過中央時顏色最深,離中央越遠就越淡,並略微向外轉。這個效果不需要透過腳本讀取捲動位置。

如果瀏覽器不支援檢視時間軸,所有選項都會平面顯示,其中標有 aria-selected="true" 的選項會以較深的顏色呈現。

#行為

樣式表無法告訴表單哪個值位於標記下方。wheel 行為會在欄位停止捲動時讀取這個值。它只接受帶有 wheel 類別的根元素,沒有設定選項,並傳回用來解除此行為的函式。

app.js
import { wheel } from '@master/ui/behaviors/wheel.js'const root = document.querySelector('.wheel')wheel(root)root.addEventListener('change', (event) => {  // event.target is the column that came to rest.  console.log(event.target.getAttribute('aria-label'), event.detail.value)})
  • 停靠。 欄位停止捲動後,行為會將 aria-selected="true" 移到標記下方的選項。 支援 scrollsnapchange 時,它採用瀏覽器回報的貼齊選項,其他情況則讀取捲動位置。
  • 事件。 如果選項已變更,欄位就會依序派送可冒泡的 input 和 change 事件。兩者的 detail.value 都取自選項的 data-value,未設定時則取其文字;detail.option 是選項元素本身。
  • 初始化。 行為掛載時,以及欄位重新進行版面配置時(例如彈出面板開啟時),都會將各欄位目前選取的選項移到標記下方。
  • 點選選項會將該選項移到標記下方。
  • 按鍵。 欄位取得焦點後,按鍵操作方式與清單方塊相同,每次移動一個選項。
  • 停用的選項。 若欄位停在帶有 aria-disabled="true" 的選項上,就會繼續轉到最近的可用選項。

若要隨表單送出選取值,請在 change 事件監聽器中將 detail.value 複製到隱藏的輸入欄位。 沒有行為時,欄位仍會捲動與貼齊。支援 scroll-initial-target 時從選取的項目開始,其他瀏覽器從第一個項目開始。aria-selected 維持標記原本的設定。

#單欄

滾輪選擇器可以只有一欄。wheel-sm 會把顯示列數從五列縮為三列,適合放在彈出面板或一列設定項目中。

單欄

<div class="wheel" role="group" aria-label="Repeat every">  <div class="wheel-column" role="listbox" aria-label="Interval" tabindex="0">    <div class="wheel-option" role="option" aria-selected="false">Hour</div>    <div class="wheel-option" role="option" aria-selected="false">Day</div>    <div class="wheel-option" role="option" aria-selected="true">Week</div>    <div class="wheel-option" role="option" aria-selected="false">Fortnight</div>    <div class="wheel-option" role="option" aria-selected="false">Month</div>    <div class="wheel-option" role="option" aria-selected="false">Quarter</div>    <div class="wheel-option" role="option" aria-selected="false">Year</div>  </div></div><div class="wheel wheel-sm" role="group" aria-label="Font size">  <div class="wheel-column" role="listbox" aria-label="Size" tabindex="0">    <div class="wheel-option" role="option" aria-selected="false">12</div>    <div class="wheel-option" role="option" aria-selected="false">13</div>    <div class="wheel-option" role="option" aria-selected="true">14</div>    <div class="wheel-option" role="option" aria-selected="false">15</div>    <div class="wheel-option" role="option" aria-selected="false">16</div>    <div class="wheel-option" role="option" aria-selected="false">18</div>    <div class="wheel-option" role="option" aria-selected="false">20</div>    <div class="wheel-option" role="option" aria-selected="false">24</div>  </div>  <span class="wheel-separator" aria-hidden="true">px</span></div><script type="module">  import { attach } from '@master/ui/behaviors'  attach()</script>
import { useEffect, useRef } from 'react'import { attach } from '@master/ui/behaviors'export function Example() {  const root = useRef<HTMLDivElement>(null)  useEffect(() => attach(root.current!), [])  return (    <div ref={root}>      <div className="wheel" role="group" aria-label="Repeat every">        <div className="wheel-column" role="listbox" aria-label="Interval" tabIndex={0}>          <div className="wheel-option" role="option" aria-selected="false">Hour</div>          <div className="wheel-option" role="option" aria-selected="false">Day</div>          <div className="wheel-option" role="option" aria-selected="true">Week</div>          <div className="wheel-option" role="option" aria-selected="false">Fortnight</div>          <div className="wheel-option" role="option" aria-selected="false">Month</div>          <div className="wheel-option" role="option" aria-selected="false">Quarter</div>          <div className="wheel-option" role="option" aria-selected="false">Year</div>        </div>      </div>      <div className="wheel wheel-sm" role="group" aria-label="Font size">        <div className="wheel-column" role="listbox" aria-label="Size" tabIndex={0}>          <div className="wheel-option" role="option" aria-selected="false">12</div>          <div className="wheel-option" role="option" aria-selected="false">13</div>          <div className="wheel-option" role="option" aria-selected="true">14</div>          <div className="wheel-option" role="option" aria-selected="false">15</div>          <div className="wheel-option" role="option" aria-selected="false">16</div>          <div className="wheel-option" role="option" aria-selected="false">18</div>          <div className="wheel-option" role="option" aria-selected="false">20</div>          <div className="wheel-option" role="option" aria-selected="false">24</div>        </div>        <span className="wheel-separator" aria-hidden="true">px</span>      </div>    </div>  )}
<script>  import { attach } from '@master/ui/behaviors'</script><div {@attach attach}>  <div class="wheel" role="group" aria-label="Repeat every">    <div class="wheel-column" role="listbox" aria-label="Interval" tabindex="0">      <div class="wheel-option" role="option" aria-selected="false">Hour</div>      <div class="wheel-option" role="option" aria-selected="false">Day</div>      <div class="wheel-option" role="option" aria-selected="true">Week</div>      <div class="wheel-option" role="option" aria-selected="false">Fortnight</div>      <div class="wheel-option" role="option" aria-selected="false">Month</div>      <div class="wheel-option" role="option" aria-selected="false">Quarter</div>      <div class="wheel-option" role="option" aria-selected="false">Year</div>    </div>  </div>  <div class="wheel wheel-sm" role="group" aria-label="Font size">    <div class="wheel-column" role="listbox" aria-label="Size" tabindex="0">      <div class="wheel-option" role="option" aria-selected="false">12</div>      <div class="wheel-option" role="option" aria-selected="false">13</div>      <div class="wheel-option" role="option" aria-selected="true">14</div>      <div class="wheel-option" role="option" aria-selected="false">15</div>      <div class="wheel-option" role="option" aria-selected="false">16</div>      <div class="wheel-option" role="option" aria-selected="false">18</div>      <div class="wheel-option" role="option" aria-selected="false">20</div>      <div class="wheel-option" role="option" aria-selected="false">24</div>    </div>    <span class="wheel-separator" aria-hidden="true">px</span>  </div></div>
<script setup>import { onMounted, onUnmounted, ref } from 'vue'import { attach } from '@master/ui/behaviors'const root = ref(null)let detachonMounted(() => { detach = attach(root.value) })onUnmounted(() => detach?.())</script><template>  <div ref="root">    <div class="wheel" role="group" aria-label="Repeat every">      <div class="wheel-column" role="listbox" aria-label="Interval" tabindex="0">        <div class="wheel-option" role="option" aria-selected="false">Hour</div>        <div class="wheel-option" role="option" aria-selected="false">Day</div>        <div class="wheel-option" role="option" aria-selected="true">Week</div>        <div class="wheel-option" role="option" aria-selected="false">Fortnight</div>        <div class="wheel-option" role="option" aria-selected="false">Month</div>        <div class="wheel-option" role="option" aria-selected="false">Quarter</div>        <div class="wheel-option" role="option" aria-selected="false">Year</div>      </div>    </div>    <div class="wheel wheel-sm" role="group" aria-label="Font size">      <div class="wheel-column" role="listbox" aria-label="Size" tabindex="0">        <div class="wheel-option" role="option" aria-selected="false">12</div>        <div class="wheel-option" role="option" aria-selected="false">13</div>        <div class="wheel-option" role="option" aria-selected="true">14</div>        <div class="wheel-option" role="option" aria-selected="false">15</div>        <div class="wheel-option" role="option" aria-selected="false">16</div>        <div class="wheel-option" role="option" aria-selected="false">18</div>        <div class="wheel-option" role="option" aria-selected="false">20</div>        <div class="wheel-option" role="option" aria-selected="false">24</div>      </div>      <span class="wheel-separator" aria-hidden="true">px</span>    </div>  </div></template>

#標記

橫跨中央的標記屬於整個滾輪,因此同一個時間的小時和分鐘欄位會共用它。標記從凹槽邊緣內縮 4px,圓角與外框同心。wheel-separator 是固定顯示在標記上、位於兩欄之間的文字,例如時間中的冒號或接在數字後的單位。

每列在任何指標下都高 36px。將 --wheel-rows 設為奇數,就能顯示欄位中的更多列。

#何時改用其他元件

滾輪適合用在觸控裝置上,讓使用者從少量、順序固定且熟悉的選項中挑選。使用鍵盤和指標時,下拉選單或數字步進器操作更快。若要選日期,月曆能讓使用者在挑選日期時看到該日期所在的一週。原生 <input type="time"> 在手機上就會開啟平台自帶的滾輪。需要把選擇器直接放在頁面中時,再使用這個元件。

#鍵盤操作

掛載行為後,對取得焦點的欄位按下按鍵,會以整個選項為單位移動,並跳過停用的選項。未掛載行為時,相同的按鍵會像操作其他捲動容器一樣捲動欄位,再由捲動貼齊完成定位。

按鍵動作
ArrowUp, ArrowDown捲動取得焦點的欄,讓上一個或下一個可用選項對準標記,並跳過停用的選項
PageUp, PageDown將取得焦點的欄捲動一頁,每頁的選項數等於該欄目前可見的選項數
Home, End將取得焦點的欄捲動到第一個或最後一個選項

#無障礙

  • 每個欄位都使用 role="listbox",並設有 aria-label 和 tabindex="0"。每個選項都使用 role="option"。行為會讓 aria-selected 始終標示位於標記下方的選項,並在欄位上以 aria-activedescendant 指出這個選項;欄位在轉動時一直保有焦點,因此螢幕閱讀器會讀出按鍵轉到的每個選項。沒有 id 的選項會由行為補上。
  • 轉動和淡化效果會隨使用者的捲動而變化。啟用減少動態效果的偏好設定時,選項會保留淡化效果,並取消轉動。

#React、Svelte 和 Vue

wheel 元件會根據資料呈現欄位、掛載行為,並支援傳入與傳出每個欄位的值。使用 npx master-ui add wheel 將元件複製到專案。React、Svelte 和 Vue 說明了這些元件的實作與用法。

ui/wheel.tsx
'use client'import { Fragment, useState, type ComponentProps } from 'react'import { wheel } from '@master/ui/behaviors/wheel.js'import { useBehavior } from './use-behavior'export interface WheelColumn {  /** The name of the column, for assistive technology. */  label: string  options: (string | { value: string, label: string, disabled?: boolean })[]  /** Fixed text after the column that rests on the mark: the colon of a time, a unit. */  separator?: string}export interface WheelProps extends Omit<ComponentProps<'div'>, 'defaultValue' | 'role' | 'ref'> {  columns: WheelColumn[]  /** The value of each column, in the order of the columns. */  value?: string[]  defaultValue?: string[]  onValueChange?: (value: string[]) => void}/** Columns that turn. Name the group with `aria-label` or `aria-labelledby`. */export function Wheel({ columns, value, defaultValue, onValueChange, className, ...root }: WheelProps) {  const ref = useBehavior(wheel, undefined, { value, defaultValue, onValueChange })  const [first] = useState(value ?? defaultValue ?? [])  return (    <div className={className ? `wheel ${className}` : 'wheel'} role="group" ref={ref} {...root}>      {columns.map((column, at) => (        <Fragment key={column.label}>          <div className="wheel-column" role="listbox" aria-label={column.label} tabIndex={0}>            {column.options.map((option) => {              const { value: own, label, disabled = false } = typeof option === 'string' ? { value: option, label: option } : option              return <div className="wheel-option" role="option" key={own} data-value={own} aria-selected={own === first[at]} aria-disabled={disabled || undefined}>{label}</div>            })}          </div>          {column.separator !== undefined && <span className="wheel-separator" aria-hidden="true">{column.separator}</span>}        </Fragment>      ))}    </div>  )}
ui/Wheel.svelte
<script lang="ts" module>  import type { HTMLAttributes } from 'svelte/elements'  export interface WheelColumn {    /** The name of the column, for assistive technology. */    label: string    options: (string | { value: string, label: string, disabled?: boolean })[]    /** Fixed text after the column that rests on the mark: the colon of a time, a unit. */    separator?: string  }  export interface WheelProps extends Omit<HTMLAttributes<HTMLDivElement>, 'role' | 'children'> {    columns: WheelColumn[]    /** The value of each column, in the order of the columns. Bind it, or leave it out. */    value?: string[]    onValueChange?: (value: string[]) => void  }</script><script lang="ts">  import { untrack } from 'svelte'  import { wheel } from '@master/ui/behaviors/wheel.js'  import { behavior } from './behavior.svelte.js'  let { columns, value = $bindable(), onValueChange, class: className, ...root }: WheelProps = $props()  // The first value is drawn into the first render; after that the behavior marks what is selected. An option  // written later, from new columns, is drawn with the value as it is then, which is not read as state.  const chosen = { values: untrack(() => value ?? []) }  const attach = behavior(wheel, undefined, {    value: () => value,    change: (next) => { chosen.values = next; value = next; onValueChange?.(next) },    settled: (held) => { chosen.values = held },  })  const plain = (option: WheelColumn['options'][number]) => typeof option === 'string' ? { value: option, label: option, disabled: false } : option</script><!--  @component  Columns that turn. Name the group with `aria-label` or `aria-labelledby`.--><div class={['wheel', className]} role="group" {@attach attach} {...root}>  {#each columns as column, at (column.label)}    <div class="wheel-column" role="listbox" aria-label={column.label} tabindex={0}>      {#each column.options.map(plain) as option (option.value)}        <div class="wheel-option" role="option" data-value={option.value} aria-selected={option.value === chosen.values[at]} aria-disabled={option.disabled || undefined}>{option.label}</div>      {/each}    </div>    {#if column.separator !== undefined}<span class="wheel-separator" aria-hidden="true">{column.separator}</span>{/if}  {/each}</div>
ui/Wheel.vue
<script lang="ts">export interface WheelColumn {  /** The name of the column, for assistive technology. */  label: string  options: (string | { value: string, label: string, disabled?: boolean })[]  /** Fixed text after the column that rests on the mark: the colon of a time, a unit. */  separator?: string}</script><script setup lang="ts">import { useTemplateRef } from 'vue'import { wheel } from '@master/ui/behaviors/wheel.js'import { useBehavior } from './use-behavior'defineOptions({ inheritAttrs: false })defineProps<{  columns: WheelColumn[]}>()/** The value of each column, in the order of the columns: `v-model`. */const model = defineModel<string[]>()/* Columns that turn. Name the group with `aria-label` or `aria-labelledby`. */const first = model.value ?? []const root = useTemplateRef<HTMLDivElement>('root')useBehavior(root, wheel, undefined, model)const row = (option: WheelColumn['options'][number]) => typeof option === 'string' ? { value: option, label: option, disabled: false } : option</script><template>  <div class="wheel" role="group" ref="root" v-bind="$attrs">    <template v-for="(column, at) in columns" :key="column.label">      <div class="wheel-column" role="listbox" :aria-label="column.label" tabindex="0">        <div v-for="option in column.options.map(row)" :key="option.value" class="wheel-option" role="option" :data-value="option.value" :aria-selected="option.value === first[at]" :aria-disabled="option.disabled || undefined">{{ option.label }}</div>      </div>      <span v-if="column.separator !== undefined" class="wheel-separator" aria-hidden="true">{{ column.separator }}</span>    </template>  </div></template>

#類別

類別說明
wheel各欄共用的凹槽,以及橫跨凹槽中央的標記
wheel-sm三列,每列 32px。預設為五列,每列 36px。搭配 wheel 使用
wheel-column可捲動的直欄,每個選項都能吸附到中央,包含第一個與最後一個。直欄使用 role="listbox",並提供可存取名稱與 tabindex="0"
wheel-option欄中的單一選項值,由設有 role="option" 的元素呈現
wheel-separator置於欄與欄之間、與標記對齊的固定文字,例如時間中的冒號或數字後的單位

#自訂屬性

屬性預設值說明
--wheel-row2.25rem列與標記的高度
--wheel-rows5可見列數,請使用奇數
--wheel-radiusvar(--radius-control-lg)凹槽圓角

#原始碼

@master/ui/components/wheel.css
/* * Wheel: a value picked by turning a column until it rests under a mark: an hour, a minute, a unit. * The wheel is a well. Each column in it is a scroll container that snaps one option to its center, and one * small sheet lies across the middle of the well, under the options, to say which row is the value; several * columns share it, as the hours and the minutes of one time do. A column fades into the well at both ends. * Scrolling and snapping are the platform's; the behavior moves a focused column by one option with the arrow * keys, by a page with Page Up and Page Down, and to its ends with Home and End. Where the browser has view timelines, each * option reads its own place in the column: it is full ink under the mark, and quieter and slightly turned * away the further it has gone. Elsewhere, and for assistive technology, the chosen option is the one a * script marks aria-selected, which it reads from how far the column has scrolled. */@layer components.ui {  .wheel {    --wheel-row: 2.25rem;    --wheel-rows: 5;    --wheel-radius: var(--radius-control-lg);    --wheel-inset: 4px;    position: relative;    display: inline-flex;    align-items: stretch;    justify-content: center;    max-width: 100%;    height: calc(var(--wheel-row) * var(--wheel-rows));    padding: 0 var(--wheel-inset);    border-radius: calc(var(--wheel-radius) * var(--corner-scale));    corner-shape: var(--corner-shape);    background: var(--color-surface-inset);    color: var(--color-text-strong);    box-shadow: var(--shadow-pressed);    font: var(--font-weight-control) .9375rem/1 var(--font-family-sans);    font-feature-settings: inherit;    font-variant-numeric: tabular-nums;    vertical-align: middle;    isolation: isolate;    /* The script listens on the whole wheel for a tap on an option: it must not flash the wheel grey on iOS. */    -webkit-tap-highlight-color: transparent;    /* The mark: a sheet as tall as a row, 4px inside the well, with the concentric corner. */    &::before {      content: '';      position: absolute;      inset: calc(50% - var(--wheel-row) / 2) var(--wheel-inset) auto;      z-index: -1;      height: var(--wheel-row);      border-radius: calc(var(--wheel-radius) * var(--corner-scale) - var(--wheel-inset));      corner-shape: var(--corner-shape);      @apply --surface-sheet;      box-shadow: var(--shadow-raised);      pointer-events: none;    }    &.wheel-sm {      --wheel-row: 2rem;      --wheel-rows: 3;      --wheel-radius: var(--radius-control);      font-size: .875rem;    }    @media (forced-colors: active) {      outline: 1px solid CanvasText;      outline-offset: -1px;      &::before {        outline: 1px solid Highlight;        outline-offset: -1px;      }    }  }  /*   * A column: a listbox that scrolls. Start padding and an end spacer leave room for the first and last   * options to rest under the mark. The spacer participates in scrollable overflow even when a browser   * excludes trailing padding from a perspective container's scroll extent.   */  .wheel-column {    display: block;    flex: 1 1 auto;    min-width: 3.25rem;    margin: 0;    padding: calc((var(--wheel-rows) - 1) / 2 * var(--wheel-row)) 0 0;    overflow: hidden auto;    overscroll-behavior: contain;    list-style: none;    scroll-snap-type: y mandatory;    scrollbar-width: none;    mask-image: linear-gradient(to bottom, transparent, black 38%, black 62%, transparent);    perspective: 20rem;    &::after {      content: '';      display: block;      height: calc((var(--wheel-rows) - 1) / 2 * var(--wheel-row));    }    @media (forced-colors: active) {      mask-image: none;    }    /*     * Focused, the column's fade would cut its own ring at both ends. The mask keeps the fade and lets through     * a 2px frame around the box, where the ring is drawn inside it, so the whole ring shows on the column the     * keys are on.     */    &:focus-visible {      outline: 2px solid var(--color-focus);      outline-offset: -2px;      border-radius: calc(var(--wheel-radius) * var(--corner-scale) - var(--wheel-inset));      corner-shape: var(--corner-shape);      mask-image:        linear-gradient(to bottom, transparent, black 38%, black 62%, transparent),        linear-gradient(black, black), linear-gradient(black, black), linear-gradient(black, black), linear-gradient(black, black);      mask-position: 0 0, top, bottom, left, right;      mask-size: 100% 100%, 100% 2px, 100% 2px, 2px 100%, 2px 100%;      mask-repeat: no-repeat;      @media (forced-colors: active) {        mask-image: none;      }    }  }  .wheel-option {    display: flex;    align-items: center;    justify-content: center;    height: var(--wheel-row);    margin: 0;    padding: 0 .875rem;    color: var(--color-text-muted);    white-space: nowrap;    scroll-snap-align: center;    scroll-snap-stop: normal;    user-select: none;    -webkit-user-select: none;    &[aria-selected="true"] {      color: var(--color-text-strong);    }    /* Where supported, the first render starts on the selection. The behavior aligns it elsewhere. */    @supports (scroll-initial-target: nearest) {      &[aria-selected="true"]:not([aria-disabled="true"], [hidden]) {        scroll-initial-target: nearest;      }    }    &[aria-disabled="true"] {      color: var(--color-text-disabled);      /* Forced colors would give it the page's text color back: it keeps the system's disabled ink. */      @media (forced-colors: active) {        color: GrayText;      }    }    /*     * Each option is its own view timeline over the column. At the middle of its pass it is under the mark;     * a row away it has already let go of the ink.     */    @supports (animation-timeline: view()) {      &:not([aria-disabled="true"]) {        color: var(--color-text-strong);        animation-name: wheel-turn;        animation-timing-function: linear;        animation-fill-mode: both;        animation-timeline: view(block);        animation-range: cover;      }      @media (prefers-reduced-motion: reduce) {        &:not([aria-disabled="true"]) {          animation-name: wheel-fade;        }      }      /* In forced colors a faded value could not be read: every option is plain text, the chosen one marked by the sheet. */      @media (forced-colors: active) {        &:not([aria-disabled="true"]) {          animation-name: none;        }      }    }  }  /* Fixed text between columns: the colon of a time, a unit after a number. It rests on the mark. */  .wheel-separator {    display: flex;    flex: none;    align-items: center;    align-self: center;    height: var(--wheel-row);    color: var(--color-text-muted);    user-select: none;    -webkit-user-select: none;  }}@keyframes wheel-turn {  0% {    opacity: .2;    rotate: x -52deg;    scale: .86;  }  38% {    opacity: .42;    rotate: x -18deg;    scale: .96;  }  50% {    opacity: 1;    rotate: x 0deg;    scale: 1;  }  62% {    opacity: .42;    rotate: x 18deg;    scale: .96;  }  100% {    opacity: .2;    rotate: x 52deg;    scale: .86;  }}@keyframes wheel-fade {  0%, 100% {    opacity: .2;  }  38%, 62% {    opacity: .42;  }  50% {    opacity: 1;  }}