"use client"
import { ColorPicker, HStack, Portal, parseColor } from "@chakra-ui/react"
export const ColorPickerBasic = () => {
return (
<ColorPicker.Root defaultValue={parseColor("#eb5e41")} maxW="200px">
<ColorPicker.HiddenInput />
<ColorPicker.Label>Color</ColorPicker.Label>
<ColorPicker.Control>
<ColorPicker.Input />
<ColorPicker.Trigger />
</ColorPicker.Control>
<Portal>
<ColorPicker.Positioner>
<ColorPicker.Content>
<ColorPicker.Area />
<HStack>
<ColorPicker.EyeDropper size="xs" variant="outline" />
<ColorPicker.Sliders />
</HStack>
</ColorPicker.Content>
</ColorPicker.Positioner>
</Portal>
</ColorPicker.Root>
)
}
Anatomy
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
Some parts are shortcuts that render a group of parts for you.
ColorPicker.Area renders the area background and thumb:
<ColorPicker.Area>
<ColorPicker.AreaThumb />
<ColorPicker.AreaBackground />
</ColorPicker.Area>ColorPicker.ChannelSlider renders the transparency grid, track and thumb:
<ColorPicker.ChannelSlider>
<ColorPicker.TransparencyGrid />
<ColorPicker.ChannelSliderTrack />
<ColorPicker.ChannelSliderThumb />
</ColorPicker.ChannelSlider>ColorPicker.Sliders renders both the hue and alpha channel sliders:
<Stack>
<ColorPicker.ChannelSlider channel="hue" />
<ColorPicker.ChannelSlider channel="alpha" />
</Stack>ColorPicker.EyeDropper renders an icon button wired to the eye dropper
trigger:
<ColorPicker.EyeDropperTrigger asChild>
<IconButton>
<LuPipette />
</IconButton>
</ColorPicker.EyeDropperTrigger>Examples
Basic
Use parseColor to create the initial color value, and compose the input and
trigger inside ColorPicker.Control.
"use client"
import { ColorPicker, HStack, Portal, parseColor } from "@chakra-ui/react"
export const ColorPickerBasic = () => {
return (
<ColorPicker.Root defaultValue={parseColor("#eb5e41")} maxW="200px">
<ColorPicker.HiddenInput />
<ColorPicker.Label>Color</ColorPicker.Label>
<ColorPicker.Control>
<ColorPicker.Input />
<ColorPicker.Trigger />
</ColorPicker.Control>
<Portal>
<ColorPicker.Positioner>
<ColorPicker.Content>
<ColorPicker.Area />
<HStack>
<ColorPicker.EyeDropper size="xs" variant="outline" />
<ColorPicker.Sliders />
</HStack>
</ColorPicker.Content>
</ColorPicker.Positioner>
</Portal>
</ColorPicker.Root>
)
}
Sizes
Use the size prop to change the size of the color picker.
"use client"
import {
ColorPicker,
For,
HStack,
Portal,
Stack,
parseColor,
} from "@chakra-ui/react"
import { LuCheck } from "react-icons/lu"
export const ColorPickerWithSizes = () => {
return (
<Stack gap="8" maxW="sm">
<For each={["2xs", "xs", "sm", "md", "lg", "xl", "2xl"]}>
{(size) => (
<ColorPicker.Root
key={size}
defaultValue={parseColor("#eb5e41")}
size={size}
>
<ColorPicker.HiddenInput />
<ColorPicker.Label>Color ({size})</ColorPicker.Label>
<ColorPicker.Control>
<ColorPicker.Input />
<ColorPicker.Trigger />
</ColorPicker.Control>
<Portal>
<ColorPicker.Positioner>
<ColorPicker.Content>
<ColorPicker.Area />
<HStack>
<ColorPicker.EyeDropper size="sm" variant="outline" />
<ColorPicker.Sliders />
</HStack>
<ColorPicker.SwatchGroup>
{swatches.map((item) => (
<ColorPicker.SwatchTrigger key={item} value={item}>
<ColorPicker.Swatch value={item} boxSize="4.5">
<ColorPicker.SwatchIndicator>
<LuCheck />
</ColorPicker.SwatchIndicator>
</ColorPicker.Swatch>
</ColorPicker.SwatchTrigger>
))}
</ColorPicker.SwatchGroup>
</ColorPicker.Content>
</ColorPicker.Positioner>
</Portal>
</ColorPicker.Root>
)}
</For>
</Stack>
)
}
const swatches = ["red", "blue", "green"]
Variants
Use the variant prop to change the visual style of the color picker. Values
can be either outline or subtle.
"use client"
import {
ColorPicker,
For,
HStack,
Portal,
Stack,
parseColor,
} from "@chakra-ui/react"
export const ColorPickerWithVariants = () => {
return (
<Stack gap="8">
<For each={["outline", "subtle"]}>
{(variant) => (
<ColorPicker.Root
key={variant}
defaultValue={parseColor("#eb5e41")}
maxW="200px"
variant={variant}
>
<ColorPicker.HiddenInput />
<ColorPicker.Label>Color ({variant})</ColorPicker.Label>
<ColorPicker.Control>
<ColorPicker.Input />
<ColorPicker.Trigger />
</ColorPicker.Control>
<Portal>
<ColorPicker.Positioner>
<ColorPicker.Content>
<ColorPicker.Area />
<HStack>
<ColorPicker.EyeDropper size="xs" variant="outline" />
<ColorPicker.Sliders />
</HStack>
</ColorPicker.Content>
</ColorPicker.Positioner>
</Portal>
</ColorPicker.Root>
)}
</For>
</Stack>
)
}
Controlled
Use the value and onValueChange props to control the state of the color
picker.
"use client"
import { ColorPicker, HStack, Portal, parseColor } from "@chakra-ui/react"
import { useState } from "react"
export const ColorPickerControlled = () => {
const [color, setColor] = useState(parseColor("#eb5e41"))
return (
<ColorPicker.Root
value={color}
format="hsla"
onValueChange={(e) => setColor(e.value)}
maxW="200px"
>
<ColorPicker.HiddenInput />
<ColorPicker.Label>Color</ColorPicker.Label>
<ColorPicker.Control>
<ColorPicker.Input />
<ColorPicker.Trigger />
</ColorPicker.Control>
<Portal>
<ColorPicker.Positioner>
<ColorPicker.Content>
<ColorPicker.Area />
<HStack>
<ColorPicker.EyeDropper size="xs" variant="outline" />
<ColorPicker.Sliders />
</HStack>
</ColorPicker.Content>
</ColorPicker.Positioner>
</Portal>
</ColorPicker.Root>
)
}
Swatches
Render a ColorPicker.SwatchGroup inside the content to offer a set of
predefined colors alongside the color area.
"use client"
import { ColorPicker, HStack, Portal, parseColor } from "@chakra-ui/react"
import { LuCheck } from "react-icons/lu"
export const ColorPickerWithSwatches = () => {
return (
<ColorPicker.Root defaultValue={parseColor("#eb5e41")} maxW="200px">
<ColorPicker.HiddenInput />
<ColorPicker.Label>Color</ColorPicker.Label>
<ColorPicker.Control>
<ColorPicker.Input />
<ColorPicker.Trigger />
</ColorPicker.Control>
<Portal>
<ColorPicker.Positioner>
<ColorPicker.Content>
<ColorPicker.Area />
<HStack>
<ColorPicker.EyeDropper size="xs" variant="outline" />
<ColorPicker.Sliders />
</HStack>
<ColorPicker.SwatchGroup>
{swatches.map((item) => (
<ColorPicker.SwatchTrigger key={item} value={item}>
<ColorPicker.Swatch boxSize="4.5" value={item}>
<ColorPicker.SwatchIndicator>
<LuCheck />
</ColorPicker.SwatchIndicator>
</ColorPicker.Swatch>
</ColorPicker.SwatchTrigger>
))}
</ColorPicker.SwatchGroup>
</ColorPicker.Content>
</ColorPicker.Positioner>
</Portal>
</ColorPicker.Root>
)
}
// prettier-ignore
const swatches = ["#000000", "#4A5568", "#F56565", "#ED64A6", "#9F7AEA", "#6B46C1", "#4299E1", "#0BC5EA", "#00B5D8", "#38B2AC", "#48BB78", "#68D391", "#ECC94B", "#DD6B20"]
Input Only
Combine ColorPicker.ValueSwatch and ColorPicker.EyeDropper with an
InputGroup to render a color picker that consists of only an input.
"use client"
import { ColorPicker, InputGroup, parseColor } from "@chakra-ui/react"
export const ColorPickerInputOnly = () => {
return (
<ColorPicker.Root defaultValue={parseColor("#eb5e41")} maxW="200px">
<ColorPicker.HiddenInput />
<ColorPicker.Label>Color</ColorPicker.Label>
<ColorPicker.Control>
<InputGroup
startElement={<ColorPicker.ValueSwatch boxSize="4.5" />}
endElementProps={{ px: "1" }}
endElement={<ColorPicker.EyeDropper size="xs" variant="ghost" />}
>
<ColorPicker.Input />
</InputGroup>
</ColorPicker.Control>
</ColorPicker.Root>
)
}
Inline
Pass the open prop to render the color picker inline, without a popover.
"use client"
import { ColorPicker, HStack, parseColor } from "@chakra-ui/react"
export const ColorPickerInline = () => {
return (
<ColorPicker.Root open defaultValue={parseColor("#000")}>
<ColorPicker.HiddenInput />
<ColorPicker.Content animation="none" shadow="none" padding="0">
<ColorPicker.Area />
<HStack>
<ColorPicker.EyeDropper size="xs" variant="outline" />
<ColorPicker.Sliders />
<ColorPicker.ValueSwatch />
</HStack>
</ColorPicker.Content>
</ColorPicker.Root>
)
}
Guide
Getting the hex code
Use the onValueChange callback to get the color value. The value object has
a toString() method that accepts different format options.
<ColorPicker.Root
onValueChange={(details) => {
console.log(details.value.toString('hex')) // "#ff0000"
console.log(details.value.toString('hexa')) // "#ff0000ff" (with alpha)
console.log(details.value.toString('rgb')) // "rgb(255, 0, 0)"
console.log(details.value.toString('css')) // CSS color string
}}
>
{/* ... */}
</ColorPicker.Root>You can also access it from the store:
const picker = useColorPicker() // or useColorPickerContext()
const hexValue = picker.value.toString('hex') // "#ff0000"The same toString() method is available when using parseColor:
import { parseColor } from '@chakra-ui/react'
const color = parseColor('#ff0000')
console.log(color.toString('hex')) // "#ff0000"
console.log(color.toString('rgba')) // "rgba(255, 0, 0, 1)"Props
Root
| Prop | Default | Type |
|---|---|---|
closeOnSelect | false | booleanWhether to close the color picker when a swatch is selected |
defaultFormat | '\'rgba\'' | ColorFormatThe initial color format when rendered. Use when you don't need to control the color format of the color picker. |
defaultValue | '#000000' | ColorThe initial color value when rendered. Use when you don't need to control the color value of the color picker. |
hideMode | ''display-none'' | HideModeHow to hide content when mounted but not present. - `'display-none'`: HTML `hidden` attribute. Effects stay alive. - `'activity'`: React 19 `<Activity mode="hidden">`. Effects pause. Requires React 19+. |
lazyMount | false | booleanWhether to enable lazy mounting |
openAutoFocus | true | booleanWhether to auto focus the color picker when it is opened |
skipAnimationOnMount | false | booleanWhether to allow the initial presence animation. |
unmountOnExit | false | booleanWhether to unmount on exit. |
colorPalette | 'gray' | 'base' | 'gray' | 'zinc' | 'neutral' | 'stone' | 'red' | 'orange' | 'amber' | 'yellow' | 'lime' | 'green' | 'emerald' | 'teal' | 'cyan' | 'sky' | 'blue' | 'indigo' | 'violet' | 'purple' | 'fuchsia' | 'pink' | 'rose' | 'sidebar' | 'sidebar.accent' | 'interaction' | 'accent' | 'presence' | 'status' | 'slate'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 |
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. For more details, read our Composition guide. | |
defaultOpen | booleanThe 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 | booleanWhether the color picker is disabled | |
format | ColorFormatThe controlled color format to use | |
id | stringThe 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 | booleanWhether to synchronize the present change immediately or defer it to the next frame | |
initialFocusEl | () => HTMLElement | nullThe initial focus element when the color picker is opened. | |
inline | booleanWhether to render the color picker inline | |
invalid | booleanWhether the color picker is invalid | |
name | stringThe name for the form input | |
onExitComplete | VoidFunctionFunction called when the animation ends in the closed state | |
onFocusOutside | (event: FocusOutsideEvent) => voidFunction called when the focus is moved outside the component | |
onFormatChange | (details: FormatChangeDetails) => voidFunction called when the color format changes | |
onInteractOutside | (event: InteractOutsideEvent) => voidFunction called when an interaction happens outside the component | |
onOpenChange | (details: OpenChangeDetails) => voidHandler that is called when the user opens or closes the color picker. | |
onPointerDownOutside | (event: PointerDownOutsideEvent) => voidFunction called when the pointer is pressed down outside the component | |
onValueChange | (details: ValueChangeDetails) => voidHandler that is called when the value changes, as the user drags. | |
onValueChangeEnd | (details: ValueChangeDetails) => voidHandler that is called when the user stops dragging. | |
open | booleanThe controlled open state of the color picker | |
positioning | PositioningOptionsThe positioning options for the color picker | |
present | booleanWhether the node is present (controlled by the user) | |
readOnly | booleanWhether the color picker is read-only | |
required | booleanWhether the color picker is required | |
value | ColorThe controlled color value of the color picker |