Skip to Content
文档

颜色选择器

用于从颜色区域或一组定义的色板中选择颜色

SourceStorybookRecipeArk

用法

¥Usage

import { ColorPicker } from "@chakra-ui/react"
<ColorPicker.Root>
  <ColorPicker.HiddenInput />
  <ColorPicker.Label />
  <ColorPicker.Control>
    <ColorPicker.Input />
    <ColorPicker.Trigger />
  </ColorPicker.Control>
  <ColorPicker.Positioner>
    <ColorPicker.Content>
      <ColorPicker.Area />
      <ColorPicker.EyeDropper />
      <ColorPicker.Sliders />
      <ColorPicker.SwatchGroup>
        <ColorPicker.SwatchTrigger>
          <ColorPicker.Swatch />
        </ColorPicker.SwatchTrigger>
      </ColorPicker.SwatchGroup>
    </ColorPicker.Content>
  </ColorPicker.Positioner>
</ColorPicker.Root>

快捷键

¥Shortcuts

ColorPicker.ChannelSlider

此组件渲染滑块轨道、缩略图和透明网格。

¥This component renders the slider track, thumb and transparency grid.

<ColorPicker.ChannelSlider />

等同于:

¥is the same as:

<ColorPicker.ChannelSlider>
  <ColorPickerTransparencyGrid />
  <ColorPickerChannelSliderTrack />
  <ColorPickerChannelSliderThumb />
</ColorPicker.ChannelSlider>

ColorPicker.Sliders

这是色调和 alpha 滑块的快捷方式组件:

¥This is a shortcut component for the hue and alpha sliders:

<Stack>
  <ColorPickerChannelSlider channel="hue" />
  <ColorPickerChannelSlider channel="alpha" />
</Stack>

ColorPicker.Area

此组件渲染颜色区域缩略图和背景。

¥This component renders the color area thumb and background.

<ColorPicker.Area>
  <ColorPicker.AreaThumb />
  <ColorPicker.AreaBackground />
</ColorPicker.Area>

等同于:

¥is the same as:

<ColorPicker.Area />

ColorPicker.EyeDropper

这是以下组件的快捷方式组件:

¥This is a shortcut component for:

<ColorPicker.EyeDropperTrigger asChild>
  <IconButton>
    <LuPipette />
  </IconButton>
</ColorPicker.EyeDropperTrigger>

示例

¥Examples

尺寸

¥Sizes

使用 size 属性更改颜色选择器的大小。

¥Use the size prop to change the size of the color picker.

变量

¥Variants

使用 variant 属性更改颜色选择器的视觉样式。值可以是 outlinesubtle

¥Use the variant prop to change the visual style of the color picker. Values can be either outline or subtle.

仅输入框

¥Input Only

组合 InputGroup 上的 ColorPicker.ValueSwatchColorPicker.EyeDropper 以渲染仅包含输入的颜色选择器。

¥Combine the ColorPicker.ValueSwatch and the ColorPicker.EyeDropper on the InputGroup to render a color picker that contains only an input.

仅色板

¥Swatch Only

使用 ColorPicker.SwatchGroupColorPicker.SwatchTrigger 仅渲染颜色样本。

¥Use the ColorPicker.SwatchGroup and ColorPicker.SwatchTrigger to render only the color swatches.

触发器仅限

¥Trigger Only

使用 ColorPicker.ValueSwatchColorPicker.ValueText 组件编写颜色选择器,使其最初仅显示触发器。

¥Compose the color picker to initially show only a trigger using the ColorPicker.ValueSwatch and ColorPicker.ValueText.

触发器内部输入

¥Trigger Inside Input

使用 InputGroupColorPickerInput 组件编写颜色选择器,使其在输入中触发。

¥Compose the color picker to trigger in input using the InputGroup and ColorPickerInput.

受控

¥Controlled

使用 valueonValueChange 属性控制颜色选择器的状态。

¥Use the value and onValueChange props to control the state of the color picker.

商店

¥Store

控制颜色选择器的另一种方法是使用 RootProvider 组件和 useColorPicker 存储钩子。

¥An alternative way to control the color picker is to use the RootProvider component and the useColorPicker store hook.

这样,你就可以从颜色选择器外部访问颜色选择器的状态和方法。

¥This way you can access the color picker state and methods from outside the color picker.

更改结束

¥Change End

使用 onValueChangeEnd 监听用户完成颜色选择的时间,而不是在用户拖动或拖拽颜色区域时。

¥Use the onValueChangeEnd to listen to when the user finishes selecting a color, rather than while they are scrubbing or dragging through the color area.

onChangeEnd: #EB5E41

通道滑块

¥Channel Slider

组合 ColorPickerChannelSlidersformat 属性,为颜色选择器添加不同的颜色通道。

¥Combine the ColorPickerChannelSliders and the format prop to add the different color channels to the color picker.

钩子表单

¥Hook Form

以下是如何将颜色选择器与 react-hook-form 集成的示例。

¥Here's an example of how to integrate the color picker with react-hook-form.

内联

¥Inline

以下是如何显示颜色选择器的内联版本的示例。

¥Here's an example of how to display an inline version of the color picker.

已禁用

¥Disabled

传递 disabled 属性以禁用颜色选择器。

¥Pass the disabled prop to disable the color picker.

通道输入

¥Channel Input

使用 ChannelFormat.SelectColorPicker.ChannelInput 创建一个颜色选择器,允许用户选择他们喜欢的通道。

¥Use the ChannelFormat.Select and ColorPicker.ChannelInput to create a color picker that allows users to select their preferred channel.

适合内容

¥Fit Content

data-fit-content 属性传递给 ColorPicker.Trigger 组件,使其与内容相适应。

¥Pass the data-fit-content attribute to the ColorPicker.Trigger to make it fit the content.

ReadOnly

使用 readOnly 属性使颜色选择器组件变为只读。

¥Use the readOnly prop to make the color picker component read-only.

保存样本

¥Save Swatch

以下是如何将选定颜色保存为色板的示例。

¥Here's an example of how to save a selected color as a swatch.

色板

¥Swatches

以下是如何将颜色选择器与预定义色板组合的示例。

¥Here's an example of how to combine the color picker with pre-defined swatches.

色板和输入

¥Swatch and Input

以下是如何使用输入组合色板。

¥Here's how to compose a swatch with an input.

色板和触发器

¥Swatch and Trigger

以下是如何使用触发器组合色板。

¥Here's how to compose a swatch with a trigger.

属性

¥Props

根元素

¥Root

PropDefaultType
closeOnSelect false
boolean

Whether to close the color picker when a swatch is selected

defaultFormat '\'rgba\''
ColorFormat

The initial color format when rendered. Use when you don't need to control the color format of the color picker.

defaultValue '#000000'
Color

The initial color value when rendered. Use when you don't need to control the color value of the color picker.

lazyMount false
boolean

Whether to enable lazy mounting

openAutoFocus true
boolean

Whether to auto focus the color picker when it is opened

skipAnimationOnMount false
boolean

Whether to allow the initial presence animation.

unmountOnExit false
boolean

Whether to unmount on exit.

colorPalette 'gray'
'gray' | 'red' | 'orange' | 'yellow' | 'green' | 'teal' | 'blue' | 'cyan' | 'purple' | 'pink'

The color palette of the component

size 'md'
'2xs' | 'xs' | 'sm' | 'md' | 'lg' | 'xl' | '2xl'

The size of the component

variant 'outline'
'outline' | 'subtle'

The variant of the component

as
React.ElementType

The underlying element to render.

asChild
boolean

Use the provided child element as the default rendered element, combining their props and behavior.

For more details, read our Composition guide.
unstyled
boolean

Whether to remove the component's style.

defaultOpen
boolean

The initial open state of the color picker when rendered. Use when you don't need to control the open state of the color picker.

disabled
boolean

Whether the color picker is disabled

format
ColorFormat

The controlled color format to use

id
string

The unique identifier of the machine.

ids
Partial<{ root: string; control: string; trigger: string; label: string; input: string; hiddenInput: string; content: string; area: string; areaGradient: string; positioner: string; formatSelect: string; areaThumb: string; channelInput: (id: string) => string; channelSliderTrack: (id: ColorChannel) => string; channe...

The ids of the elements in the color picker. Useful for composition.

immediate
boolean

Whether to synchronize the present change immediately or defer it to the next frame

initialFocusEl
() => HTMLElement | null

The initial focus element when the color picker is opened.

inline
boolean

Whether to render the color picker inline

invalid
boolean

Whether the color picker is invalid

name
string

The name for the form input

onExitComplete
VoidFunction

Function called when the animation ends in the closed state

onFocusOutside
(event: FocusOutsideEvent) => void

Function called when the focus is moved outside the component

onFormatChange
(details: FormatChangeDetails) => void

Function called when the color format changes

onInteractOutside
(event: InteractOutsideEvent) => void

Function called when an interaction happens outside the component

onOpenChange
(details: OpenChangeDetails) => void

Handler that is called when the user opens or closes the color picker.

onPointerDownOutside
(event: PointerDownOutsideEvent) => void

Function called when the pointer is pressed down outside the component

onValueChange
(details: ValueChangeDetails) => void

Handler that is called when the value changes, as the user drags.

onValueChangeEnd
(details: ValueChangeDetails) => void

Handler that is called when the user stops dragging.

open
boolean

The controlled open state of the color picker

positioning
PositioningOptions

The positioning options for the color picker

present
boolean

Whether the node is present (controlled by the user)

readOnly
boolean

Whether the color picker is read-only

required
boolean

Whether the color picker is required

value
Color

The controlled color value of the color picker

Previous

复选框卡片

Next

颜色样本