2023-05-12 14:43:48 +08:00
---
category: Components
title: ColorPicker
2024-03-22 14:22:42 +08:00
description: Used for color selection.
2023-05-12 14:43:48 +08:00
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
2024-04-02 14:05:03 +08:00
tag: 5.5.0
2023-05-12 14:43:48 +08:00
demo:
cols: 2
group:
title: Data Entry
---
## When To Use
2024-06-12 14:27:15 +08:00
Used when the user needs to make a customized color selection.
2023-05-12 14:43:48 +08:00
## Examples
<!-- prettier - ignore -->
< code src = "./demo/base.tsx" > Basic Usage< / code >
2023-06-21 11:30:09 +08:00
< code src = "./demo/size.tsx" > Trigger size< / code >
2023-05-16 13:49:59 +08:00
< code src = "./demo/controlled.tsx" > controlled mode< / code >
2023-07-10 13:37:43 +08:00
< code src = "./demo/change-completed.tsx" > Color change completed< / code >
2023-06-19 19:29:39 +08:00
< code src = "./demo/text-render.tsx" > Rendering Trigger Text< / code >
< code src = "./demo/disabled.tsx" > Disable< / code >
2023-07-12 19:42:33 +08:00
< code src = "./demo/disabled-alpha.tsx" > Disabled Alpha< / code >
2023-05-12 14:43:48 +08:00
< 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 >
2023-06-29 11:46:11 +08:00
< code src = "./demo/panel-render.tsx" > Custom Render Panel< / code >
2023-05-12 14:43:48 +08:00
< code src = "./demo/pure-panel.tsx" debug > Pure Render< / code >
## API
2023-08-08 18:27:48 +08:00
Common props ref: [Common props](/docs/react/common-props)
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 -->
2023-06-19 19:29:39 +08:00
| Property | Description | Type | Default | Version |
| :-- | :-- | :-- | :-- | :-- |
| allowClear | Allow clearing color selected | boolean | false | |
2023-06-29 11:46:11 +08:00
| arrow | Configuration for popup arrow | `boolean \| { pointAtCenter: boolean }` | true | |
2023-06-19 19:29:39 +08:00
| children | Trigger of ColorPicker | React.ReactNode | - | |
2023-06-29 11:46:11 +08:00
| defaultValue | Default value of color | string \| `Color` | - | |
2023-09-05 18:42:40 +08:00
| defaultFormat | Default format of color | `rgb` \| `hex` \| `hsb` | - | 5.9.0 |
2023-06-19 19:29:39 +08:00
| disabled | Disable ColorPicker | boolean | - | |
2023-07-12 19:42:33 +08:00
| disabledAlpha | Disable Alpha | boolean | - | 5.8.0 |
2023-06-19 19:29:39 +08:00
| destroyTooltipOnHide | Whether destroy popover when hidden | `boolean` | false | 5.7.0 |
2023-06-29 11:46:11 +08:00
| format | Format of color | `rgb` \| `hex` \| `hsb` | `hex` | |
| open | Whether to show popup | boolean | - | |
2023-11-03 16:49:22 +08:00
| presets | Preset colors | `{ label: ReactNode, colors: Array<string \| Color>, defaultOpen?: boolean }[]` | - | `defaultOpen: 5.11.0` |
2024-07-19 11:04:46 +08:00
| placement | Placement of popup | The design of the [placement ](/components/tooltip/#api ) parameter is the same as the `Tooltips` component. | `bottomLeft` | |
2023-06-29 11:46:11 +08:00
| panelRender | Custom Render Panel | `(panel: React.ReactNode, extra: { components: { Picker: FC; Presets: FC } }) => React.ReactNode` | - | 5.7.0 |
| showText | Show color text | boolean \| `(color: Color) => React.ReactNode` | - | 5.7.0 |
2023-06-21 11:30:09 +08:00
| size | Setting the trigger size | `large` \| `middle` \| `small` | `middle` | 5.7.0 |
2023-06-29 11:46:11 +08:00
| trigger | ColorPicker trigger mode | `hover` \| `click` | `click` | |
| value | Value of color | string \| `Color` | - | |
2023-06-19 19:29:39 +08:00
| onChange | Callback when `value` is changed | `(value: Color, hex: string) => void` | - | |
2023-07-10 13:37:43 +08:00
| onChangeComplete | Called when color pick ends | `(value: Color) => void` | - | 5.7.0 |
2023-06-19 19:29:39 +08:00
| 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` | - | 5.6.0 |
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.