导航

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可滚动的列,每个选项都能吸附到列中央,包括首尾选项。用于具有标签和 tabindex="0" 的 role="listbox" 元素。
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;  }}