---
title: ColorPicker 颜色选择器
description: 可组合的颜色选择器,在多个颜色组件间同步颜色值
component: color-picker
group: 组件
---
## 用法
```vue
```
## 组件结构
ColorPicker 是组合多个颜色组件的可组合组件:固定源使用 `ColorPicker.Trigger` 对象组合;Vue 版提供同名具名组件,Root 别名 `ColorPickerRoot` 与 `ColorPicker` 共用实现。省略全部自身值属性的 [ColorArea](/components/color-area)、[ColorSlider](/components/color-slider)、[ColorField](/components/color-field)、[ColorSwatch](/components/color-swatch)、[ColorSwatchPicker](/components/color-swatch-picker) 会共享同一个颜色 owner。
```vue
```
## 示例
### 受控组件
### 带色板
### 带输入字段
使用 `ColorField` 允许用户编辑单个颜色通道值,配合 `Select` 切换颜色空间。
### 带滑块
使用多个 `ColorSlider` 组件调整颜色值的各通道。
## 自定义样式
### Tailwind CSS
### 全局 CSS
可使用 `@layer components` 指令自定义 ColorPicker 组件类。
[了解更多](https://tailwindcss.com/docs/adding-custom-styles#adding-component-classes)。
```css
@layer components {
.hv-color-picker {
@apply inline-flex;
}
.hv-color-picker__trigger {
@apply inline-flex items-center gap-4 rounded-lg;
}
.hv-color-picker__popover {
@apply p-4 rounded-xl;
}
}
```
## 样式参考
HeroUI 遵循 [BEM](https://getbem.com/) 方法论,确保组件变体与状态可复用且易于自定义。类名前缀为 `hv-`,`colorPickerVariants` 只有 `base`/`trigger`/`popover` 三个槽位,与固定源一致(无 size/variant 变体)。
### CSS 类
ColorPicker 组件使用以下 CSS 类([查看源码样式](https://github.com/heroui-inc/heroui/blob/v3/packages/styles/components/color-picker.css)):
#### 基础类 [!toc]
- `.hv-color-picker` - 基础容器(inline-flex)
- `.hv-color-picker__trigger` - 触发按钮(间距、圆角、focus/disabled 状态)
- `.hv-color-picker__popover` - 弹出层容器(`min-w-62`、纵向滚动、`overscroll-contain`、`bg-overlay`、圆角与阴影)
弹出层高度适配可用空间(`--reka-popover-content-available-height`),退出动画期间保持 `pointer-events: none`,定位原点使用 `--reka-popper-transform-origin`;`data-side` 与固定源的 `data-placement` 对应,供方向性进入动画选择滑入方向。
### 交互状态
组件同时支持 CSS 伪类与 data 属性:
- **Focus**:`:focus-visible` 或 `[data-focus-visible="true"]`
- **Disabled**:`:disabled` 或 `[data-disabled="true"]`(Trigger);根节点输出 `[data-disabled="true"]`
- **Read-only**:根节点输出 `[data-readonly="true"]`
- **Entering / Exiting**:`[data-entering="true"]` / `[data-exiting="true"]`(缩放淡入淡出,reduced-motion 停用)
## API 参考
### ColorPicker
对应固定源的 [React Aria ColorPicker](https://react-spectrum.adobe.com/react-aria/ColorPicker.html):共享颜色 owner + `DialogTrigger`/`Popover` 组合。普通颜色值使用 `v-model`,输出本库 `Color`。
| Prop | 类型 | 默认值 | 描述 |
| -------------- | --------------------------------------------- | ---------- | -------------------------------------------------------------------- |
| `modelValue` | `string \| Color` | - | 当前接受颜色(`v-model`;受控) |
| `value` | `string \| Color` | - | 低优先级受控别名;`modelValue` 存在时优先 |
| `defaultValue` | `string \| Color` | `"#000000"` | 非受控初值;未提供时使用固定源默认黑值 |
| `open` | `boolean` | - | 受控展开;拒绝打开时仍发出 `update:open`/`openChange` |
| `isOpen` | `boolean` | - | 展开别名;`open` 与 `isOpen` 同时存在时 `open` 优先,不会双输入竞争 |
| `defaultOpen` | `boolean` | `false` | 非受控初始展开 |
| `isDisabled` | `boolean` | `false` | 禁用整个组合(库扩展,作用于自身子组件) |
| `disabled` | `boolean` | `false` | `isDisabled` 别名 |
| `isReadOnly` | `boolean` | `false` | 只读:保留展示与程序化赋值,阻止子控件编辑(库扩展) |
| `name` | `string` | - | Vue 扩展:单个隐藏输入提交完整精度颜色 |
| `form` | `string` | - | Vue 扩展:关联外部表单 id |
| `as` | `string` | `"div"` | 自定义根元素标签 |
| `class` | `string \| Array \| Object` | - | 附加 CSS 类(Vue class 绑定) |
| 默认作用域插槽 | `({ color, isOpen, close, isDisabled, isReadOnly }) => VNodeChild` | - | 根渲染状态 |
事件:
| 事件 | 参数 | 时机 |
| --------------------- | ------- | -------------------------------------------------------------------- |
| `update:modelValue` / `change` | `Color` | 子控件提案被接受时一起触发(`v-model` 使用前者) |
| `changeEnd` | `Color` | 一次手势(拖动/按键/文本提交)结束并被接受时触发 |
| `update:open` / `update:isOpen` / `openChange` | `boolean` | 展开状态提案;受控拒绝时保持提案事件,不改变实际展开 |
受控拒绝、相同提案重试、外部替换与事务取消的行为与共享颜色 owner 一致:父组件不接受提案时坐标、滑块、字段文本、预览与表单值保持接受值,提案不会冒充已接受颜色,也不会经 hex 往返量化。
### ColorPickerTrigger
| Prop | 类型 | 默认值 | 描述 |
| --------------- | -------------------------------- | -------- | -------------------------------------------------------- |
| `as` | `string` | `"button"` | 自定义根元素标签 |
| `asChild` | `boolean` | `false` | 把触发器属性合并到默认插槽根节点 |
| `isDisabled` | `boolean` | `false` | 额外禁用;owner 禁用时同样禁用 |
| `class` | `string \| Array \| Object` | - | 附加 CSS 类 |
| 默认作用域插槽 | `({ color, isDisabled }) => VNodeChild` | - | 触发器内容状态 |
触发器渲染为真实 `button[type=button]`,不会意外提交外层表单;固定源的 Button render props(`isHovered`/`isPressed` 等)在 Vue 版收敛为 `color`/`isDisabled` 两个插槽状态,差异已记录。
### ColorPickerPopover
| Prop | 类型 | 默认值 | 描述 |
| --------------------------- | -------------------------- | --------------- | ---------------------------------------- |
| `placement` | `OverlayPlacement` | `"bottom left"` | 弹出层位置(RTL 下自动镜像) |
| `offset` | `number` | `8` | 与锚点的间距 |
| `crossOffset` | `number` | `0` | 交叉轴偏移 |
| `shouldFlip` | `boolean` | `true` | 视口碰撞时翻转 |
| `containerPadding` | `number` | `12` | 碰撞计算的容器留白 |
| `to` | `string \| HTMLElement` | - | 自定义 Teleport 目标 |
| `isNonModal` | `boolean` | `false` | 非模态:不锁定焦点与滚动 |
| `isDismissable` | `boolean` | `true` | 允许外部点击关闭 |
| `isKeyboardDismissDisabled` | `boolean` | `false` | 禁用 Esc 关闭 |
| `class` | `string \| Array \| Object` | - | 附加 CSS 类 |
| 默认作用域插槽 | `({ isOpen, close }) => VNodeChild` | - | 弹层内容状态 |
弹层内容通过 Teleport 渲染,Surface 默认上下文穿过 Teleport;嵌套的 `Select`/`ListBox` 等内层弹层各自独立处理 Esc、外部点击与焦点恢复,不会误关外层 ColorPicker。
### Render Props
| 插槽状态 | 类型 | 含义 |
| ------------------------------------------------------- | --------------- | ------------------------------------ |
| `color`(根) | `Color` | 当前接受颜色(组合不允许空值) |
| `isOpen` / `isDisabled` / `isReadOnly`(根) | `boolean` | 真实状态 |
| `close`(根 / 弹层) | `() => void` | 关闭弹层 |
| `color` / `isDisabled`(Trigger) | `Color \| null` / `boolean` | 触发器内容状态 |
| `isOpen`(Popover) | `boolean` | 弹层展开状态 |
Root 实例通过 `defineExpose` 提供 `element`(真实根 DOM)与 `color`(接受颜色)。不暴露颜色 owner、事务 token 或弹层内部状态。
### 展开与焦点
| 操作 | 行为 |
| ------------------- | ------------------------------------------------------------------------ |
| 触发器 click | 切换弹层;可取消的 `click`(`preventDefault`)会阻止打开 |
| `Esc` | 先恢复/丢弃子控件草稿,再关闭弹层并把焦点还给触发器 |
| 外部点击 | 关闭弹层;未结束的颜色事务立即取消,不补发虚假 `changeEnd` |
| `Tab` / `Shift+Tab` | 焦点在弹层内循环(模态);嵌套 Select 的内层选项可正常聚焦与选择 |
| 焦点恢复 | 关闭后焦点回到触发器;退出动画期间弹层保持不可交互 |
### 表单
`name`/`form` 是本库 Vue 扩展(固定源 ColorPicker 不提供表单提交 API):开启后渲染单个 `input[type=hidden]`,提交完整精度序列化(如 `hsba(270, 0%, 50%, 0.123456789)`)。`form.reset()` 恢复初值并同步真实 DOM;禁用时不参与提交;继承的子控件不重复提交完整颜色。未提供 `name` 时没有成功控件,FormData 保持为空。
### Color 与 parseColor
`Color`、`parseColor`、`ColorValue` 等类型从包根导出,与 [ColorSwatch](/components/color-swatch) 共用同一颜色模型;公开子集的方法表见该页。固定源 `Color` 的感知色名等接口不在公开子集内。取色区域轴名、滑块输出等本地化文案跟随 `ConfigProvider` 的 `locale`(默认 `en-US`),与 [ColorArea](/components/color-area) / [ColorSlider](/components/color-slider) 一致。
## 相关组件
已公开 [ColorField 颜色输入框](/components/color-field)(含 ColorInputGroup)、[ColorArea 颜色区域](/components/color-area)、[ColorSlider 颜色滑块](/components/color-slider)、[ColorSwatch 颜色色块](/components/color-swatch)、[ColorSwatchPicker 颜色色块选择器](/components/color-swatch-picker) 与 [ColorPicker 颜色选择器](/components/color-picker)。颜色家族共用同一颜色 owner,Picker 的 Trigger/Popover 组合可嵌套上述任意子组件。