HD-04 scope, against heroui ce90ea3 cn sources: - stale/fictional content: input.md no longer claims TextField is unmigrated; color-field.md no longer claims ColorPicker is private; header.md drops the fictional Header.Root subcomponent and the stale 'menus not migrated' wording (MenuSection label registration is now documented from HeaderRoot source); radio-group.md drops three copied CheckboxGroup wordings (variant override is Radio's, scalar emits, native single-value submit); input-otp.md drops the fictional data-complete state and lists the real missing upstream props - toggle-button disabled row now matches the real CSS hooks (:disabled / [aria-disabled], with data-disabled as a marker only) - part API / CSS classes / interactive states restored per pinned sources: button (full class lists + real variable-based global CSS), accordion (per-part tables + variant/state lists), card (per-part tables + variant list), switch (SwitchGroup snippet + Switch/ SwitchGroup class lists + states), color-field Group (class list + states), input-otp (variant notes, global CSS, class/state lists); input-group gains the two upstream usage chapters - RelatedComponents restored from the pinned componentRelationships (first-3, filtered against componentsMap): 64 pages rewritten, list-box/slider sections added, 7 empty-mapping pages' fabricated entries removed; RelatedShowcases left unmigrated with its real data inventory recorded (2 cases, showcase-site cards) - defaults unified on implementation evidence: granularity (inferred day/minute) and visibleYears (full min-max span, no "or 20" fallback); HD-03 hand-offs resolved (trigger focus-visible noted for HD-06, width guards kept) - new demos: header-collection-grouping (MenuSection/ListBoxSection label wiring), empty-state-collection (collection empty slot); empty-state page deduplicated with a boundary compatibility note - ComponentPreview minHeight/isBgSolid restored per the 30 upstream marks; tabs-overflow max-w-full kept after measuring internal scroll - tests: +3 header registration/demo cases, +1 empty-state collection case; full suite 110 files / 1630 tests, typecheck and build:docs pass
15 KiB
title, description, component, group
| title | description | component | group |
|---|---|---|---|
| ColorSwatchPicker 颜色色块选择器 | 允许用户从预置调色板中选择颜色的 swatch 列表 | color-swatch-picker | 组件 |
用法
<script setup lang="ts">
import {
ColorSwatchPicker,
ColorSwatchPickerIndicator,
ColorSwatchPickerItem,
ColorSwatchPickerSwatch,
} from "@heroui-vue/components";
</script>
<template>
<ColorSwatchPicker>
<ColorSwatchPickerItem color="#F43F5E">
<ColorSwatchPickerSwatch />
<ColorSwatchPickerIndicator />
</ColorSwatchPickerItem>
</ColorSwatchPicker>
</template>
组件结构
固定源使用 ColorSwatchPicker.Item 对象组合;Vue 版提供同名具名组件,Root 别名 ColorSwatchPickerRoot 与 ColorSwatchPicker 共用实现。
<script setup lang="ts">
import {
ColorSwatchPicker,
ColorSwatchPickerIndicator,
ColorSwatchPickerItem,
ColorSwatchPickerSwatch,
} from "@heroui-vue/components";
</script>
<template>
<ColorSwatchPicker>
<ColorSwatchPickerItem color="#F43F5E">
<ColorSwatchPickerSwatch />
<ColorSwatchPickerIndicator />
</ColorSwatchPickerItem>
<ColorSwatchPickerItem color="#D946EF">
<ColorSwatchPickerSwatch />
<ColorSwatchPickerIndicator />
</ColorSwatchPickerItem>
<ColorSwatchPickerItem color="#8B5CF6">
<ColorSwatchPickerSwatch />
<ColorSwatchPickerIndicator />
</ColorSwatchPickerItem>
</ColorSwatchPicker>
</template>
示例
变体
尺寸
禁用
堆叠布局
默认值
受控组件
自定义指示器
渲染函数
上游 render 用于替换根元素或附加属性;Vue 版通过 as 更换元素类型,data-*、aria-* 与事件直接透传,并使用作用域插槽透出真实状态(根与项两级)。
自定义样式
Tailwind CSS
全局 CSS
可使用 @layer components 指令自定义 ColorSwatchPicker 组件类。
了解更多。
@layer components {
.hv-color-swatch-picker {
@apply gap-4;
}
.hv-color-swatch-picker__item {
@apply shadow-md;
}
.hv-color-swatch-picker__swatch {
@apply border-2 border-white;
}
}
样式参考
HeroUI 遵循 BEM 方法论,确保组件变体与状态可复用且易于自定义。类名前缀为 hv-。
CSS 类
ColorSwatchPicker 组件使用以下 CSS 类(查看源码样式):
基础与结构 [!toc]
.hv-color-swatch-picker- 基础容器(flex 布局).hv-color-swatch-picker__item- 单个 swatch 包裹层.hv-color-swatch-picker__swatch- swatch 视觉元素.hv-color-swatch-picker__indicator- 选中指示器
尺寸类 [!toc]
.hv-color-swatch-picker--xs- 特小(16px).hv-color-swatch-picker--sm- 小(24px).hv-color-swatch-picker--md- 中(32px,默认).hv-color-swatch-picker--lg- 大(36px).hv-color-swatch-picker--xl- 特大(40px)
形状变体 [!toc]
.hv-color-swatch-picker--circle- 圆形(默认).hv-color-swatch-picker--square- 圆角方形
布局类 [!toc]
.hv-color-swatch-picker--grid- 横向换行网格(默认).hv-color-swatch-picker--stack- 纵向堆叠
交互状态
组件同时支持 CSS 伪类与 data 属性:
- Hover:
:hover或[data-hovered="true"]- 缩放至 1.1(选中状态以选中缩放为准) - Focus:
:focus-visible或[data-focus-visible="true"]- 焦点环 - Selected:
[data-selected="true"]- 与 swatch 同色的内边框,swatch 缩至 0.77 显出间隔 - Disabled:
[data-disabled="true"]- 降低透明度并禁用交互
透明颜色保留 ColorSwatch 的棋盘格底纹;--color-swatch-current 写在每个项与色块节点上,供选中边框与棋盘底色使用。
API 参考
ColorSwatchPicker
对应固定源的 React Aria ColorSwatchPicker(listbox / option 语义,单项选择且不允许清空)。
| Prop | 类型 | 默认值 | 描述 |
|---|---|---|---|
modelValue |
string | Color | null |
- | 当前接受颜色(v-model;受控) |
value |
string | Color | null |
- | 低优先级受控别名,与共享颜色模型一致 |
defaultValue |
string | Color | null |
"#000000" |
非受控初值;未提供时使用固定源默认黑值,不会自动选中第一个候选 |
layout |
"grid" | "stack" |
"grid" |
布局方向(视觉 flex 布局,ARIA 方向固定为 vertical) |
size |
"xs" | "sm" | "md" | "lg" | "xl" |
"md" |
swatch 尺寸 |
variant |
"circle" | "square" |
"circle" |
swatch 形状 |
isDisabled |
boolean |
false |
禁用整个选择器(库扩展) |
disabled |
boolean |
false |
isDisabled 别名 |
isReadOnly |
boolean |
false |
只读:保留选择与焦点,阻止新的选择(库扩展) |
dir |
"ltr" | "rtl" |
- | 方向;影响水平方向键映射 |
as |
string |
"div" |
自定义根元素标签;对应上游 render 的元素替换 |
aria-label |
string |
- | 列表名称;省略且没有 aria-labelledby 时使用本地化默认名称 |
class |
string | Array | Object |
- | 附加 CSS 类(Vue class 绑定) |
| 默认作用域插槽 | ({ color, isEmpty, isDisabled, isReadOnly, isFocused, layout }) => VNodeChild |
- | 根渲染状态 |
change 事件与 update:modelValue 一起在选择被接受时触发,参数为本库 Color,保留候选色自身的颜色空间与精度。选择比较使用固定源的 hexa 键(8 位 RGB + alpha):等价格式(hex/rgb/hsl)视为同一颜色,alpha 按 1/255 量化比较,灰色的色相在比较中折叠;接受值本身不会被反写或量化。
省略全部自身值属性时继承最近的颜色 owner(例如 ColorField 家族共享的私有 ColorPicker 组合);显式 value/modelValue 覆盖继承,动态增删属性可切换归属。显式 null 表示受控空值(不选中任何项);固定源把 null 视为非受控,此项差异已记录。
ColorSwatchPicker.Item
| Prop | 类型 | 默认值 | 描述 |
|---|---|---|---|
color |
string | Color | null |
- | 候选颜色;省略或空字符串渲染透明色块(对应固定源 #0000) |
isDisabled |
boolean |
false |
禁用该项(不可聚焦、不可选择、方向键跳过) |
as |
string |
"div" |
自定义根元素标签 |
aria-label |
string |
- | 覆盖自动生成的项名称 |
| 默认作用域插槽 | (state) => VNodeChild |
- | 项状态,见下方 Render Props |
ColorSwatchPicker.Swatch
| Prop | 类型 | 默认值 | 描述 |
|---|---|---|---|
color |
string | Color | null |
- | 覆盖颜色;省略时使用所属项的颜色 |
colorName |
string |
- | 颜色的可访问名称(覆盖自动生成的描述) |
as |
string |
"div" |
自定义根元素标签 |
ColorSwatchPicker.Indicator
| Prop | 类型 | 默认值 | 描述 |
|---|---|---|---|
as |
string |
"span" |
自定义根元素标签 |
| 默认作用域插槽 | (state) => VNodeChild |
- | 替换默认勾选图标,接收项状态 |
默认图标是固定源的勾选 SVG;[data-light-color="true"] 由色块相对亮度(> 0.5)决定,浅色背景使用黑色勾选。指示器始终渲染在 DOM 中,由 CSS 在选中时缩放显示。
Render Props
| 插槽状态 | 类型 | 含义 |
|---|---|---|
color(项) |
Color |
该项的候选颜色 |
isSelected / isDisabled / isFocused / isFocusVisible / isPressed / isHovered |
boolean |
项的真实状态 |
color(根) |
Color | null |
当前接受颜色 |
isEmpty / isDisabled / isReadOnly / isFocused |
boolean |
根的真实状态 |
layout(根) |
"grid" | "stack" |
当前布局 |
Root 实例通过 defineExpose 提供 element(真实根 DOM)与 focus()(按入口规则聚焦项);Item 提供 element。不暴露颜色 owner、事务 token 或内部集合键。
选择与焦点
| 操作 | 行为 |
|---|---|
| 鼠标主键按下 | 选择该项;仅主键(button === 0)激活,右键/中键保留原生行为;可取消的 mousedown(preventDefault)会阻止选择 |
Enter / Space |
选择当前聚焦项(按下即选中) |
Tab / Shift+Tab |
整个列表是一个 tab 停靠点;进入时聚焦已选项或首/末可用项 |
↑ / ↓ |
只移动焦点不选择;grid 按几何在换行行间移动,stack 按顺序移动 |
← / → |
grid 按顺序移动(RTL 反向);stack 交给浏览器滚动 |
Home / End |
聚焦首/末可用项 |
PageUp / PageDown |
容器不可滚动时跳到首/末项,可滚动时按页移动 |
click(detail=0) |
辅助技术/程序化的 click-only 激活选择当前项;正常鼠标/键盘的兼容 click 不重复提案 |
| 输入字符 | 按项名称前缀匹配并聚焦(名称差异见下) |
| 边界 | 不循环;到边界后保持当前项 |
焦点与选择分离:方向键只移动焦点,aria-selected 与指示器只反映接受颜色。父级拒绝提案后,选择样式、aria-selected 与 DOM 保持接受状态,同一手势可重试。窗口失焦或指针取消会清除按压状态与点击去重归属,恢复后可以立即重试。
可访问名称差异
固定源使用 React Aria 的感知色名(getColorName)作为项与色块的自动名称。本库未实现感知色名表:自动名称使用 hex/hexa 或“透明”文案,并接受显式 aria-label / colorName 覆盖。列表本身在省略 aria-label 时使用本地化默认名称“颜色色块选择器”。
表单
固定源 ColorSwatchPicker 不提供 name/form 提交、表单重置或 required/validate 字段 API,本库同样不新增这些字段 API(明确差异)。需要表单提交时请与共享颜色 owner 组合:由 owner 的隐藏值提交完整精度颜色,选择器不重复提交或接管重置,组合中的 reset 会恢复真实 DOM 状态。
Color 与 parseColor
Color、parseColor、ColorValue 等类型从包根导出,与 ColorSwatch 共用同一颜色模型;详见该页的公共接口表。
相关组件
ColorSwatch 颜色色块 · ColorField 颜色输入框 · ColorArea 颜色区域
已公开 ColorField 颜色输入框(含 ColorInputGroup)、ColorArea 颜色区域、ColorSlider 颜色滑块、ColorSwatch 颜色色块、ColorSwatchPicker 颜色色块选择器 与 ColorPicker 颜色选择器。颜色家族共用同一颜色 owner,Picker 的 Trigger/Popover 组合可嵌套上述任意子组件。