ColorSelector
ColorSelector is a component used by the user to select a color.
<ColorSelector.Root maxW="md" />
Usage
import { ColorPicker } from "@yamada-ui/react"
import { ColorPicker } from "@/components/ui"
import { ColorPicker } from "@workspaces/ui"
<ColorSelector.Root>
<ColorSelector.SaturationSlider />
<ColorSelector.HueSlider />
<ColorSelector.AlphaSlider />
<ColorSelector.ColorSwatchGroupLabel />
<ColorSelector.ColorSwatchGroup>
<ColorSelector.ColorSwatchItem />
</ColorSelector.ColorSwatchGroup>
</ColorSelector.Root>
Change Size
<VStack>
<For each={["sm", "md", "lg"]}>
{(size, index) => <ColorSelector.Root maxW="md" key={index} size={size} />}
</For>
</VStack>
Change Default Value
To change the default value, set defaultValue to a value.
<ColorSelector.Root defaultValue="#775999" maxW="md" />
Change Alpha Value
To change the alpha value, set defaultValue to a value containing the alpha value, or set format to "hexa" or "hsla" etc.
<ColorSelector.Root defaultValue="#775999A0" maxW="md" />
Change Color Swatches
To change the color swatches, set colorSwatches to an array.
<ColorSelector.Root
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"
maxW="md"
/>
Change Color Swatch Group Columns
To change the number of columns in the color swatch group, set colorSwatchGroupColumns to a number.
<ColorSelector.Root
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%)",
]}
colorSwatchGroupColumns={10}
colorSwatchGroupLabel="Pick a color"
maxW="md"
/>
Change Shape
<VStack>
<For each={["rounded", "circle", "square"]}>
{(shape, index) => (
<ColorSelector.Root key={index} format="hexa" maxW="md" shape={shape} />
)}
</For>
</VStack>
Make Disabled
<ColorSelector.Root disabled maxW="md" />
Make Read Only
<ColorSelector.Root maxW="md" readOnly />
Customize Component
const [value, setValue] = useState("#FF0000FF")
return (
<Popover.Root placement="end-start">
<Popover.Trigger>
<Center rounded="full" w="fit-content">
<ColorSwatch color={value} variant="circle" />
</Center>
</Popover.Trigger>
<Popover.Content w="xs" p="1">
<Popover.Body>
<ColorSelector.Root value={value} onChange={setValue} />
</Popover.Body>
</Popover.Content>
</Popover.Root>
)
"use client" to the top of the file.Control
const [value, setValue] = useState("#FF0000A0")
return <ColorSelector.Root maxW="md" value={value} onChange={setValue} />
"use client" to the top of the file.Props
Accessibility
ColorSelector uses appropriate ARIA attributes to ensure accessibility for color selection.
Keyboard Navigation
| Key | Description | State |
|---|---|---|
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 hue slider or alpha slider, sets the value to min. | - |
End | If within hue slider or alpha slider, sets the value to max. | - |
PageUp | If within hue slider or alpha slider, increases the value based on min and max. | - |
PageDown | If within hue slider or alpha slider, decreases the value based on min and max. | - |
ARIA Roles and Attributes
| Component and Element | Role and Attribute | Usage |
|---|---|---|
ColorSelector.EyeDropper | 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". | |
aria-disabled | Set to "true" if disabled is set. | |
ColorSelector.SaturationSlider > input | aria-hidden | Excludes the element from the accessibility tree. |
aria-describedby | If this is within a Field.Root and Field.Root has an errorMessage, helperMessage, or a Field.ErrorMessage, Field.HelperMessage, sets its id. | |
ColorSelector.SaturationSlider > div | role="slider" | Indicates that this is a slider. |
aria-label | Sets to "Saturation and brightness thumb". | |
aria-describedby | If this is within a Field.Root and Field.Root has an errorMessage, helperMessage, or a Field.ErrorMessage, Field.HelperMessage, sets its id. | |
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%". | |
aria-readonly | Set to "true" if readOnly is set. | |
aria-disabled | Set to "true" if disabled is set. | |
ColorSelector.HueSlider > input | aria-hidden | Excludes the element from the accessibility tree. |
aria-describedby | If this is within a Field.Root and Field.Root has an errorMessage, helperMessage, or a Field.ErrorMessage, Field.HelperMessage, sets its id. | |
ColorSelector.HueSlider > div | role="slider" | Indicates that this is a slider. |
aria-label | Sets to "Slider thumb". | |
aria-describedby | If this is within a Field.Root and Field.Root has an errorMessage, helperMessage, or a Field.ErrorMessage, Field.HelperMessage, sets its id. | |
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". | |
aria-readonly | Set to "true" if readOnly is set. | |
aria-disabled | Set to "true" if disabled is set. | |
ColorSelector.AlphaSlider > input | aria-hidden | Excludes the element from the accessibility tree. |
aria-describedby | If this is within a Field.Root and Field.Root has an errorMessage, helperMessage, or a Field.ErrorMessage, Field.HelperMessage, sets its id. | |
ColorSelector.AlphaSlider > div | role="slider" | Indicates that this is a slider. |
aria-label | Sets to "Slider thumb". | |
aria-describedby | If this is within a Field.Root and Field.Root has an errorMessage, helperMessage, or a Field.ErrorMessage, Field.HelperMessage, sets its id. | |
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%". | |
aria-readonly | Set to "true" if readOnly is set. | |
aria-disabled | Set to "true" if disabled is set. | |
ColorSelector.ColorSwatchGroupLabel | id | Used to associate with ColorSelector.ColorSwatchGroup. |
ColorSelector.ColorSwatchGroup | role="listbox" | Indicates that this is a listbox. |
aria-labelledby | Sets the id of the associated ColorSelector.ColorSwatchGroupLabel. | |
ColorSelector.ColorSwatchItem | role="option" | Indicates that this is an option. |
aria-label | Sets the current value. | |
aria-roledescription | Sets to "color swatch". | |
aria-selected | Sets to "true" if value is the same as the current value. | |
aria-disabled | Sets to "true" if disabled or readOnly is set. |
Similar Components
AlphaSlider
AlphaSlider is a component used to allow the user to select color transparency.
HueSlider
HueSlider is a component used to allow the user to select a color hue.
SaturationSlider
SaturationSlider is a component used to allow the user to select a color saturation.
ColorPicker
ColorPicker is a component used by the user to select a color or enter an arbitrary color value.
Checkbox
Checkbox is a component used for allowing users to select multiple values from multiple options.
Dropzone
Dropzone is a component used for uploading files via drag and drop.
Editable
Editable is a component used to obtain inline editable text input.
Field
Field is a component used to group form elements with label, helper message, error message, etc.
Uses Components & Hooks
NativeSelect
NativeSelect is a component used for allowing users to select one value from a list of options. It displays a native dropdown list provided by the browser (user agent).
ColorSwatch
ColorSwatch is a component that displays color samples.
AlphaSlider
AlphaSlider is a component used to allow the user to select color transparency.
HueSlider
HueSlider is a component used to allow the user to select a color hue.
Icon
Icon is a general icon component that can be used in your projects.
SaturationSlider
SaturationSlider is a component used to allow the user to select a color saturation.
Slider
Slider is a component used for allowing users to select a value from a range.