2023-05-12 14:43:48 +08:00
---
category: Components
title: ColorPicker
cover: https://mdn.alipayobjects.com/huamei_7uahnr/afts/img/A*PpY4RYNM8UcAAAAAAAAAAAAADrJ8AQ/original
coverDark: https://mdn.alipayobjects.com/huamei_7uahnr/afts/img/A*EHL-QYJofZsAAAAAAAAAAAAADrJ8AQ/original
2023-05-12 17:53:37 +08:00
tag: New
2023-05-12 14:43:48 +08:00
demo:
cols: 2
group:
title: Data Entry
---
2023-05-14 11:47:01 +08:00
Components providing color selection, Available since `5.5.0` .
2023-05-12 14:43:48 +08:00
## When To Use
2023-05-14 11:47:01 +08:00
Used when the user needs to customize the color selection.
2023-05-12 14:43:48 +08:00
## Examples
<!-- prettier - ignore -->
< code src = "./demo/base.tsx" > Basic Usage< / code >
2023-05-16 13:49:59 +08:00
< code src = "./demo/controlled.tsx" > controlled mode< / code >
2023-05-12 14:43:48 +08:00
< code src = "./demo/disabled.tsx" debug > Disable< / code >
< code src = "./demo/allowClear.tsx" > Clear Color< / code >
< code src = "./demo/trigger.tsx" > Custom Trigger< / code >
2023-05-26 11:23:48 +08:00
< code src = "./demo/trigger-event.tsx" > Custom Trigger Event< / code >
2023-05-12 14:43:48 +08:00
< code src = "./demo/format.tsx" > Color Format< / code >
< code src = "./demo/presets.tsx" > Preset Colors< / code >
< code src = "./demo/pure-panel.tsx" debug > Pure Render< / code >
## API
2023-05-14 11:47:01 +08:00
> This component is available since `antd@5.5.0`.
2023-05-12 14:43:48 +08:00
<!-- prettier - ignore -->
| Property | Description | Type | Default |
| :-- | :-- | :-- | :-- |
| format | Format of color | `rgb` \| `hex` \| `hsb` | `hex` |
| value | Value of color | string \| `Color` | - |
| defaultValue | Default value of color | string \| `Color` | - |
| allowClear | Allow clearing color selected | boolean | false |
| presets | Preset colors | `{ label: ReactNode, colors: Array<string \| Color> }[]` | - |
2023-05-13 17:34:09 +08:00
| children | Trigger of ColorPicker | React.ReactNode | - |
2023-05-12 14:43:48 +08:00
| trigger | ColorPicker trigger mode | `hover` \| `click` | `click` |
| open | Whether to show popup | boolean | - |
| disabled | Disable ColorPicker | boolean | - |
| placement | Placement of popup | `top` \| `topLeft` \| `topRight` \| `bottom` \| `bottomLeft` \| `bottomRight` | `bottomLeft` |
2023-05-15 18:44:47 +08:00
| arrow | Configuration for popup arrow | `boolean \| { pointAtCenter: boolean }` | `true` | - |
2023-05-26 15:19:34 +08:00
| onChange | Callback when `value` is changed | `(value: Color, hex: string) => void` | - |
| onFormatChange | Callback when `format` is changed | `(format: 'hex' \| 'rgb' \| 'hsb') => void` | - |
| onOpenChange | Callback when `open` is changed | `(open: boolean) => void` | - |
| onClear | Called when clear | `() => void` | - |
2023-05-12 14:43:48 +08:00
### Color
<!-- prettier - ignore -->
| Property | Description | Type | Default |
| :-- | :-- | :-- | :-- |
2023-06-01 14:36:58 +08:00
| toHex | Convert to `hex` format characters, the return type like: `1677ff` | `() => string` | - |
| toHexString | Convert to `hex` format color string, the return type like: `#1677ff` | `() => string` | - |
2023-05-12 14:43:48 +08:00
| toHsb | Convert to `hsb` object | `() => ({ h: number, s: number, b: number, a number })` | - |
2023-06-01 14:36:58 +08:00
| toHsbString | Convert to `hsb` format color string, the return type like: `hsb(215, 91%, 100%)` | `() => string` | - |
2023-05-12 14:43:48 +08:00
| toRgb | Convert to `rgb` object | `() => ({ r: number, g: number, b: number, a number })` | - |
2023-06-01 14:36:58 +08:00
| toRgbString | Convert to `rgb` format color string, the return type like: `rgb(22, 119, 255)` | `() => string` | - |
2023-05-12 14:43:48 +08:00
## FAQ
### Questions about color assignment
The value of the color selector supports both string color values and selector-generated `Color` objects. However, since there is a precision error when converting color strings of different formats to each other, it is recommended to use selector-generated `Color` objects for assignment operations in controlled scenarios, so that the precision problem can be avoided and the values are guaranteed to be accurate and the selector can work as expected.