Files
heroui-vue/apps/docs/components/color-swatch.md
T
maoyingjie 145a208687 docs: close documentation facts, structure and related-component mapping
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
2026-10-28 22:32:10 +08:00

9.3 KiB
Raw Blame History

title, description, component, group
title description component group
ColorSwatch 颜色色块 颜色值的视觉预览,并提供无障碍支持 color-swatch 组件

用法

<script setup lang="ts">
import { ColorSwatch } from "@heroui-vue/components";
</script>

<template>
  <ColorSwatch aria-label="蓝色" color="#0485F7" />
</template>

示例

尺寸

形状

透明度

渲染函数

上游 render 用于替换根元素或附加属性;Vue 版通过 as 更换元素类型,或通过 as-child 将色块属性合并到默认插槽中唯一的自定义根元素。data-*、aria-* 与事件直接透传。

无障碍

使用 colorName 为颜色提供自定义可访问名称,并使用 aria-label 补充颜色用途的上下文。

自定义样式

Tailwind CSS

全局 CSS

可使用 @layer components 指令自定义 ColorSwatch 组件类。 了解更多。

@layer components {
  .hv-color-swatch {
    @apply border-2 border-white;
  }
}

样式参考

HeroUI 遵循 BEM 方法论,确保组件变体与状态可复用且易于自定义。类名前缀为 hv-。

CSS 类

ColorSwatch 组件使用以下 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。

style 与 styleFn 可同时使用,合并顺序为组件基础样式 → style → styleFn;冲突属性由 styleFn 覆盖,普通样式不会因函数样式存在而丢失。

组件通过 defineExpose 暴露 element,指向实际根 DOM 元素;普通 ref 仍指向组件实例。aria-roledescription 与透明颜色的自动名称随 ConfigProvider 的 locale 切换;默认 en-US。

Style Render

当使用函数形式时会获得包含颜色对象在内的 render props。Vue 模板保留了 style 绑定并会规整函数值,因此模板中通过 styleFn 传入;render 函数中可直接使用 style,普通对象仍使用标准 style:

<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 语法不在当前解析范围。

相关组件

ColorSwatchPicker 颜色色块选择器 · ColorField 颜色输入框 · ColorArea 颜色区域

已公开 ColorField 颜色输入框(含 ColorInputGroup)、ColorArea 颜色区域、ColorSlider 颜色滑块、ColorSwatch 颜色色块、ColorSwatchPicker 颜色色块选择器 与 ColorPicker 颜色选择器。颜色家族共用同一颜色 owner,Picker 的 Trigger/Popover 组合可嵌套上述任意子组件。