feat: migrate ColorSwatch and public color boundary

This commit is contained in:
maoyingjie
2026-10-26 13:25:21 +08:00
parent 60af6ee40b
commit 270dea5cff
29 changed files with 1427 additions and 18 deletions
+5
View File
@@ -90,6 +90,11 @@ export const componentItems: DocsItem[] = [
url: "/components/close-button",
category: "buttons",
},
{
title: "ColorSwatch 颜色色块",
url: "/components/color-swatch",
category: "colors",
},
{
title: "ComboBox 组合框",
url: "/components/combo-box",
+4
View File
@@ -11,6 +11,10 @@ description: 浏览已迁移的 Vue 组件,更多组件正在路上。
<DocsComponents category="collections" />
## 颜色
<DocsComponents category="colors" />
## 控件
<DocsComponents category="controls" />
+153
View File
@@ -0,0 +1,153 @@
---
title: ColorSwatch 颜色色块
description: 颜色值的视觉预览,并提供无障碍支持
component: color-swatch
group: 组件
---
## 用法
```vue
<script setup lang="ts">
import { ColorSwatch } from "@heroui-vue/components";
</script>
<template>
<ColorSwatch aria-label="蓝色" color="#0485F7" />
</template>
```
<ComponentPreview name="color-swatch-basic" />
## 示例
### 尺寸
<ComponentPreview name="color-swatch-sizes" />
### 形状
<ComponentPreview name="color-swatch-shapes" />
### 透明度
<ComponentPreview name="color-swatch-transparency" />
### 渲染函数
上游 `render` 用于替换根元素或附加属性;Vue 版通过 `as` 更换元素类型,或通过 `as-child` 将色块属性合并到默认插槽中唯一的自定义根元素。`data-*`、`aria-*` 与事件直接透传。
<ComponentPreview name="color-swatch-render-function" />
### 无障碍
使用 `colorName` 为颜色提供自定义可访问名称,并使用 `aria-label` 补充颜色用途的上下文。
<ComponentPreview name="color-swatch-accessibility" />
## 自定义样式
### Tailwind CSS
<ComponentPreview name="color-swatch-custom-styles" />
### 全局 CSS
可使用 `@layer components` 指令自定义 ColorSwatch 组件类。
[了解更多](https://tailwindcss.com/docs/adding-custom-styles#adding-component-classes)。
```css
@layer components {
.hv-color-swatch {
@apply border-2 border-white;
}
}
```
## 样式参考
HeroUI 遵循 [BEM](https://getbem.com/) 方法论,确保组件变体与状态可复用且易于自定义。类名前缀为 `hv-`。
### CSS 类
ColorSwatch 组件使用以下 CSS 类([查看源码样式](https://github.com/heroui-inc/heroui/blob/v3/packages/styles/components/color-swatch.css)):
#### 基础类 [!toc]
- `.hv-color-swatch` - 基础 swatch 样式,透明区域使用棋盘格背景
#### 形状类 [!toc]
- `.hv-color-swatch--circle` - 圆形(默认)
- `.hv-color-swatch--square` - 圆角方形
#### 尺寸类 [!toc]
- `.hv-color-swatch--xs` - 特小(16px)
- `.hv-color-swatch--sm` - 小(24px)
- `.hv-color-swatch--md` - 中(32px,默认)
- `.hv-color-swatch--lg` - 大(36px)
- `.hv-color-swatch--xl` - 特大(40px)
## API 参考
### ColorSwatch
| Prop | 类型 | 默认值 | 描述 |
| -------------- | --------------------------------------------------- | ---------- | --------------------------------------------------------------------------------------------------------------------------------- |
| `color` | `string \| Color \| null` | - | 要展示的颜色值(hex、rgb、hsl 等),或本库 `Color` 实例。省略时继承最近的共享颜色 owner;显式 `null` 或空字符串清除继承并显示透明 |
| `colorName` | `string` | - | 颜色的可访问名称(会覆盖自动生成的描述) |
| `shape` | `"circle" \| "square"` | `"circle"` | swatch 形状 |
| `size` | `"xs" \| "sm" \| "md" \| "lg" \| "xl"` | `"md"` | swatch 尺寸 |
| `as` | `string` | `"div"` | 自定义根元素标签;对应上游 `render` 的元素替换 |
| `asChild` | `boolean` | `false` | 将色块属性合并到默认插槽中唯一的自定义根元素 |
| `style` | `CSSProperties \| ((renderProps) => CSSProperties)` | - | 行内样式或上游 style render prop;在组件基础样式之后合并 |
| `styleFn` | `(renderProps) => CSSProperties` | - | 模板中的 style 函数别名,接收解析后的 `Color`;在组件基础样式之后合并 |
| `aria-label` | `string` | - | swatch 的无障碍标签,与自动颜色名称合并为可访问名称 |
| `class` | `string \| Array \| Object` | - | 附加 CSS 类(Vue class 绑定) |
| 默认作用域插槽 | `({ color }) => VNodeChild` | - | 接收解析后的 `Color`;与 `asChild` 组合时可替换根元素 |
`class` 遵循 Vue 的 class 绑定,可接收字符串、数组和对象,并与组件基础类合并;不要使用 React 的 `className`。`color` 省略或为 `undefined` 时继承最近的共享颜色 owner;`null` 与空字符串是显式清空。无效颜色字符串按本库 `parseColor` 边界抛出 `RangeError`。
组件通过 `defineExpose` 暴露 `element`,指向实际根 DOM 元素;普通 `ref` 仍指向组件实例。`aria-roledescription` 与透明颜色的自动名称随 `ConfigProvider` 的 `locale` 切换;默认 `en-US`。
### Style Render
当使用函数形式时会获得包含颜色对象在内的 render props。Vue 模板保留了 `style` 绑定并会规整函数值,因此模板中通过 `styleFn` 传入;render 函数中可直接使用 `style`,普通对象仍使用标准 `style`:
```vue
<ColorSwatch
color="#0485F7"
:style-fn="
({ color }) => ({
boxShadow: `0 4px 14px ${color.toString('css')}80`,
})
"
/>
```
`color` 对象提供例如:
- `color.toString("css")` - 返回 CSS 颜色字符串
- `color.toString("hex")` - 返回十六进制颜色字符串
- `color.getChannelValue("alpha")` - 返回 alpha 通道数值
### Color 与 parseColor
`Color`、`parseColor`、`ColorValue`、`ColorChannel` 与 `ColorSpace` 从包根导出。当前公开子集实际支持:
| 公共接口 | 类型 / 说明 |
| --------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------- |
| `parseColor(value)` | `(value: string) => Color`;支持 hex3/4/6/8、RGB/HSL/HSB(含 alpha)字符串 |
| `ColorInput` | 本库结构化颜色输入:`RGBColor \| HSLColor \| HSBColor`,用于 `new Color(...)` |
| `getColorSpace()` | 返回 `"rgb" \| "hsl" \| "hsb"` |
| `getChannels()` / `getColorChannels()` | 返回当前颜色空间的通道列表 |
| `getChannelValue(channel)` / `withChannelValue(channel, value)` | 读取或返回带新通道值的不可变颜色 |
| `withAlpha(alpha)` / `getChannelRange(channel)` / `formatChannelValue(channel, locale)` | alpha、通道范围与本地化显示 |
| `toFormat(space)` / `toString(format?)` / `clone()` / `equals(other)` | 空间转换、序列化与比较 |
未实现 React Stately `Color` 的感知颜色名、颜色名表与全量通道名称等辅助 API;`ColorSwatch` 在未提供 `colorName` 时使用透明文案或 hex/hexa 作为自动描述,不伪造完整色名表。CSS 命名色、`var()`、Lab/OKLCH 与 CSS Color 4 语法不在当前解析范围。
## 相关组件
本批次仅公开 ColorSwatch。ColorArea、ColorField、ColorPicker、ColorSlider 与 ColorSwatchPicker 仍为私有颜色协议,尚未提供公共页面。
@@ -0,0 +1,11 @@
<script setup lang="ts">
import { ColorSwatch } from "@heroui-vue/components";
</script>
<template>
<div class="flex items-center gap-3">
<ColorSwatch aria-label="主品牌色" color="#0485F7" colorName="海洋蓝" />
<ColorSwatch aria-label="错误状态色" color="#EF4444" colorName="珊瑚红" />
<ColorSwatch aria-label="警告色" color="#F59E0B" colorName="落日橙" />
</div>
</template>
+13
View File
@@ -0,0 +1,13 @@
<script setup lang="ts">
import { ColorSwatch } from "@heroui-vue/components";
</script>
<template>
<div class="flex items-center gap-3">
<ColorSwatch aria-label="蓝色" color="#0485F7" />
<ColorSwatch aria-label="红色" color="#EF4444" />
<ColorSwatch aria-label="琥珀色" color="#F59E0B" />
<ColorSwatch aria-label="绿色" color="#10B981" />
<ColorSwatch aria-label="品红" color="#D946EF" />
</div>
</template>
@@ -0,0 +1,39 @@
<script setup lang="ts">
import { ColorSwatch } from "@heroui-vue/components";
const colors = ["#0485F7", "#EF4444", "#F59E0B", "#10B981", "#D946EF"];
</script>
<template>
<div class="flex flex-col gap-8">
<div class="flex flex-col gap-2">
<span class="text-sm text-muted">发光效果</span>
<div class="flex items-center gap-4">
<ColorSwatch
v-for="color in colors"
:key="color"
:color="color"
size="xl"
:style-fn="() => ({ boxShadow: `0 0 20px 2px ${color}` })"
/>
</div>
</div>
<div class="flex flex-col gap-2">
<span class="text-sm text-muted">渐变</span>
<div class="flex items-center gap-4">
<ColorSwatch
v-for="color in colors"
:key="color"
:color="color"
size="xl"
:style-fn="
({ color: current }) => ({
background: `linear-gradient(135deg, ${current.toString('css')}, white)`,
})
"
/>
</div>
</div>
</div>
</template>
@@ -0,0 +1,25 @@
<script setup lang="ts">
import { ColorSwatch } from "@heroui-vue/components";
const colors = [
{ color: "#0485F7", label: "蓝色", name: "blue" },
{ color: "#EF4444", label: "红色", name: "red" },
{ color: "#F59E0B", label: "琥珀色", name: "amber" },
{ color: "#10B981", label: "绿色", name: "green" },
{ color: "#D946EF", label: "品红", name: "fuchsia" },
];
</script>
<template>
<div class="flex items-center gap-3">
<ColorSwatch
v-for="item in colors"
:key="item.color"
:color="item.color"
:aria-label="item.label"
as-child
>
<div :data-custom="item.name" />
</ColorSwatch>
</div>
</template>
+10
View File
@@ -0,0 +1,10 @@
<script setup lang="ts">
import { ColorSwatch } from "@heroui-vue/components";
</script>
<template>
<div class="flex items-center gap-3">
<ColorSwatch color="#0485F7" shape="circle" />
<ColorSwatch color="#0485F7" shape="square" />
</div>
</template>
+13
View File
@@ -0,0 +1,13 @@
<script setup lang="ts">
import { ColorSwatch } from "@heroui-vue/components";
</script>
<template>
<div class="flex items-center gap-3">
<ColorSwatch color="#0485F7" size="xs" />
<ColorSwatch color="#EF4444" size="sm" />
<ColorSwatch color="#F59E0B" size="md" />
<ColorSwatch color="#10B981" size="lg" />
<ColorSwatch color="#D946EF" size="xl" />
</div>
</template>
@@ -0,0 +1,13 @@
<script setup lang="ts">
import { ColorSwatch } from "@heroui-vue/components";
</script>
<template>
<div class="flex items-center gap-3">
<ColorSwatch aria-label="100% 不透明度" color="rgba(4, 133, 247, 1)" />
<ColorSwatch aria-label="75% 不透明度" color="rgba(4, 133, 247, 0.75)" />
<ColorSwatch aria-label="50% 不透明度" color="rgba(4, 133, 247, 0.5)" />
<ColorSwatch aria-label="25% 不透明度" color="rgba(4, 133, 247, 0.25)" />
<ColorSwatch aria-label="0% 不透明度" color="rgba(4, 133, 247, 0)" />
</div>
</template>