DocumentationHooks

Hooks

useColorState holds the color and returns every setter the primitives need. usePointerDrag handles dragging on the color area and sliders. useDebounce is deprecated.

useColorState

The core state hook. Pass an initial color and an optional options object. It returns the current color in every format, plus the setters that ColorArea, the sliders, and the inputs expect.

const {
  hsva,
  colorValue,
  updateColor,
  setFromString,
  startDrag,
  endDrag,
} = useColorState('#6366F1', {
  onChange: (color) => console.log(color.hex),
});

// colorValue.hex, colorValue.oklch, … are all live
useColorState + primitives
#6366f1

Returns

KeyTypeDescription
hsvaHSVAThe color as HSVA. Pass it to ColorArea and the sliders.
colorValueColorValueThe color in every format.
updateColor(hsva: HSVA) => voidSet the color from an HSVA value (used by sliders/area).
setFromString(color: string) => ColorValue | nullParse and set from any color string.
startDrag() => voidMark a drag as begun (gates onChangeComplete).
endDrag() => voidEnd a drag; fires onChangeComplete with the final color.

Options

OptionTypeDescription
valuestringControlled color. When set, the hook follows it.
onChange(color: ColorValue) => voidFires on every change (drag, typing).
onChangeComplete(color: ColorValue) => voidFires once a drag ends, with the final color.

usePointerDrag

The drag primitive behind the color area and sliders. It reports coordinates from 0 to 1 and manages the document-level pointer listeners.

const { containerRef, handlePointerDown } = usePointerDrag(
  ({ x, y }) => {
    // x, y are 0..1 within the element
  },
  onStart,
  onEnd,
);

<div ref={containerRef} onPointerDown={handlePointerDown} />

useDebounce (deprecated)

Deprecated. It will be removed in a future minor release. Debounce in your app instead; the pattern below works with any debounce helper.

const debounced = useDebounce(color, 200);

useEffect(() => {
  save(debounced); // runs 200ms after the last change
}, [debounced]);