ColorPicker
ColorPicker is a component used by the user to select a color or enter an arbitrary color value.
<ColorPicker placeholder="#4387f4" />
Usage
import { ColorPicker } from "@yamada-ui/react"
import { ColorPicker } from "@/components/ui"
import { ColorPicker } from "@workspaces/ui"
<ColorPicker />
Change Variant
<VStack>
<For each={["outline", "filled", "flushed", "plain"]}>
{(variant, index) => (
<ColorPicker
key={index}
variant={variant}
placeholder={toTitleCase(variant)}
/>
)}
</For>
</VStack>
Change Size
<VStack>
<For each={["xs", "sm", "md", "lg", "xl"]}>
{(size, index) => (
<ColorPicker key={index} size={size} placeholder={`Size (${size})`} />
)}
</For>
</VStack>
Change Color Scheme
<VStack>
<For each={["success", "warning"]}>
{(colorScheme, index) => (
<ColorPicker
key={index}
colorScheme={colorScheme}
placeholder="#4387f4"
/>
)}
</For>
</VStack>
Set Default Value
To set a default value, set defaultValue to a value.
<ColorPicker defaultValue="#4387f4" placeholder="#4387f4" />
Set Alpha Value
To set an alpha value, set defaultValue to a value containing an alpha value or set format to "hexa" or "hsla" etc.
<ColorPicker defaultValue="#775999A0" placeholder="#775999A0" />
Change Format
To change the format, set format to a value. The default is to determine the format from the string of value or defaultValue.
<ColorPicker placeholder="hsl(0, 100%, 50%)" format="hsl" />
Limit Input Value
To limit the input value, set pattern to a regular expression.
<ColorPicker placeholder="#4387f4" pattern={/[^a-fA-F0-9#]/g} />
Format Input Value
To format the input value, set formatInput to a function.
<ColorPicker
placeholder="#4387f4"
pattern={/[^a-fA-F0-9#]/g}
formatInput={(value) => value.toUpperCase()}
/>
"use client" to the top of the file.Show Color Swatches
To show color swatches, set colorSwatches to an array.
<ColorPicker
placeholder="#4387f4"
colorSwatches={[
"hsl(0, 100%, 50%)",
"hsl(45, 100%, 50%)",
"hsl(90, 100%, 50%)",
"hsl(135, 100%, 50%)",
"hsl(180, 100%, 50%)",
"hsl(225, 100%, 50%)",
"hsl(270, 100%, 50%)",
"hsl(315, 100%, 50%)",
]}
colorSwatchGroupLabel="Pick a color"
/>
Change Color Swatch Columns
To change the number of columns in the color swatch group, set colorSwatchGroupColumns to a number.
<ColorPicker
placeholder="#4387f4"
colorSwatches={[
"hsl(0, 100%, 50%)",
"hsl(36, 100%, 50%)",
"hsl(72, 100%, 50%)",
"hsl(108, 100%, 50%)",
"hsl(144, 100%, 50%)",
"hsl(180, 100%, 50%)",
"hsl(216, 100%, 50%)",
"hsl(252, 100%, 50%)",
"hsl(288, 100%, 50%)",
"hsl(324, 100%, 50%)",
]}
colorSwatchGroupLabel="Pick a color"
colorSwatchGroupColumns={10}
/>
Change Offset
To change the offset, set gutter or offset to a value.
<ColorPicker placeholder="#4387f4" gutter={16} />
Change Animation Scheme
To change the animation scheme, set animationScheme to "block-start" or "inline-end" etc. The default is "scale".
<ColorPicker placeholder="#4387f4" animationScheme="inline-start" />
Change Placement
To change the placement, set placement to "end" or "start-center" etc. The default is "end-start".
<ColorPicker
placeholder="#4387f4"
animationScheme="inline-start"
placement="center-end"
rootProps={{ w: "xs" }}
/>
Blocking Scroll
To block scrolling, set blockScrollOnMount to true.
<ColorPicker placeholder="#4387f4" blockScrollOnMount />
Close Dropdown On Scroll
To close the dropdown on scroll, set closeOnScroll to true.
<ColorPicker placeholder="#4387f4" closeOnScroll />
Handle Opening Dropdown On Change
To handle the event of opening the dropdown on change, set a function to openOnChange.
<ColorPicker
placeholder="#4387f4"
openOnFocus={false}
openOnChange={(ev) => ev.target.value.length > 1}
/>
"use client" to the top of the file.Handle Closing Dropdown On Change
To handle the event of closing the dropdown on change, set a function to closeOnChange.
<ColorPicker
placeholder="#4387f4"
openOnFocus={false}
closeOnChange={(ev) => !ev.target.value.length}
/>
"use client" to the top of the file.Disable Open Dropdown On Focus
To disable opening the dropdown on focus, set openOnFocus to false.
<ColorPicker placeholder="#4387f4" openOnFocus={false} />
Disable Open Dropdown On Click
To disable opening the dropdown on click, set openOnClick to false.
<ColorPicker placeholder="#4387f4" openOnClick={false} />
Disable Close On Outside Click
To disable closing the dropdown on outside click, set closeOnBlur to false.
<ColorPicker placeholder="#4387f4" closeOnBlur={false} />
Disable Close On Esc
To disable closing the dropdown on ESC key, set closeOnEsc to false.
<ColorPicker placeholder="#4387f4" closeOnEsc={false} />
Disable Allow Input
To disable allowing input, set allowInput to false.
<ColorPicker placeholder="#4387f4" allowInput={false} />
Change Shape
<VStack>
<For each={["rounded", "circle", "square"]}>
{(shape, index) => (
<ColorPicker
key={index}
placeholder="#4387f4"
selectorProps={{ shape }}
/>
)}
</For>
</VStack>
Disable
To disable, set disabled to true.
<VStack>
<For each={["outline", "filled", "flushed"]}>
{(variant, index) => (
<ColorPicker
key={index}
variant={variant}
disabled
placeholder={toTitleCase(variant)}
/>
)}
</For>
</VStack>
Make Read Only
To make read only, set readOnly to true.
<VStack>
<For each={["outline", "filled", "flushed"]}>
{(variant, index) => (
<ColorPicker
key={index}
variant={variant}
readOnly
placeholder={toTitleCase(variant)}
/>
)}
</For>
</VStack>
Make Invalid
To make invalid, set invalid to true.
<VStack>
<For each={["outline", "filled", "flushed"]}>
{(variant, index) => (
<ColorPicker
key={index}
variant={variant}
invalid
placeholder={toTitleCase(variant)}
/>
)}
</For>
</VStack>
Change Border Color
To change the border color, set focusBorderColor or errorBorderColor to a value.
<VStack>
<ColorPicker focusBorderColor="green.500" placeholder="custom border color" />
<ColorPicker
invalid
errorBorderColor="orange.500"
placeholder="custom border color"
/>
</VStack>
Control
const [value, onChange] = useState<string>("#4387f4")
return <ColorPicker value={value} onChange={onChange} />
Props
Accessibility
ColorPicker follows the WAI-ARIA - Combobox Pattern for accessibility.
If you do not use Field.Root, set aria-label or aria-labelledby on ColorPicker.
<ColorPicker placeholder="#4387f4" aria-label="Pick color" />
<VStack gap="sm">
<Text as="h3" id="label">
Pick Color
</Text>
<ColorPicker placeholder="#4387f4" aria-labelledby="label" />
</VStack>
Keyboard Navigation
| Key | Description | State |
|---|---|---|
Tab | Opens the dialog when focus moves to the color picker. | openOnFocus={true} |
Escape | Closes the dialog. | closeOnEsc={true} |
ArrowRight, ArrowUp | If within a slider, increases the value based on step. | - |
ArrowLeft, ArrowDown | If within a slider, decreases the value based on step. | - |
Home | If within HueSlider or AlphaSlider, sets the value to min. | - |
End | If within HueSlider or AlphaSlider, sets the value to max. | - |
PageUp | If within HueSlider or AlphaSlider, increases the value based on min and max. | - |
PageDown | If within HueSlider or AlphaSlider, decreases the value based on min and max. | - |
ARIA Roles and Attributes
| Component | Roles and Attributes | Usage |
|---|---|---|
ColorPickerField | role="combobox" | Indicates that this is a combobox. |
aria-controls | If the dialog is open, sets the id of the related ColorPickerContent; when closed, sets undefined. | |
aria-describedby | If ColorPicker is within a Field.Root and Field.Root has an errorMessage, helperMessage, or a Field.ErrorMessage, Field.HelperMessage, sets its id. | |
aria-disabled | Sets to "true" if disabled or readOnly is set. | |
aria-expanded | Sets to "true" when the dialog is open, "false" when closed. | |
aria-haspopup="dialog" | Indicates that a dialog exists. | |
aria-invalid | Sets to "true" if invalid is set. | |
aria-readonly | Sets to "true" if readOnly is set. | |
aria-required | Sets to "true" if required is set. | |
ColorPickerEyeDropper | role="button" | Indicates that this is a button. |
aria-disabled | Sets to "true" if disabled or readOnly is set. | |
aria-label | Sets to "Pick a color". | |
ColorPickerContent | role="dialog" | Indicates that this is a dialog. |
ColorPickerColorSwatch | role="img" | Indicates that this is an image. |
aria-label | Sets the current value. | |
aria-roledescription | Sets to "color swatch". | |
SaturationSlider.Thumb | role="slider" | Indicates that this is a slider. |
aria-label | Sets to "Saturation and brightness thumb". | |
aria-roledescription | Sets to "2D slider". | |
aria-valuemin | Sets the value of min. The default is 0. | |
aria-valuemax | Sets the value of max. The default is 100. | |
aria-valuenow | Sets the current value. | |
aria-valuetext | Sets the current value like "Saturation 18%, Brightness 18%". | |
HueSlider.Thumb | role="slider" | Indicates that this is a slider. |
aria-label | Sets to "Slider thumb". | |
aria-orientation | Sets to "horizontal" or "vertical" based on orientation. The default is "horizontal". | |
aria-valuemin | Sets the value of min. The default is 0. | |
aria-valuemax | Sets the value of max. The default is 360. | |
aria-valuenow | Sets the current value. | |
aria-valuetext | Sets the current value like "18°, Red". | |
AlphaSlider.Thumb | role="slider" | Indicates that this is a slider. |
aria-label | Sets to "Slider thumb". | |
aria-orientation | Sets to "horizontal" or "vertical" based on orientation. The default is "horizontal". | |
aria-valuemin | Sets the value of min. The default is 0. | |
aria-valuemax | Sets the value of max. The default is 1. | |
aria-valuenow | Sets the current value. | |
aria-valuetext | Sets the current value like "18%". |