HextaUI

Color picker (UI SDK): Controlled

onValueChange runs on every step of a drag, and onValueCommit once when it ends, so save there. A controller from createColorPickerController() sets the color from anywhere.

Saved #0ea5e9

"use client"

import * as React from "react"

import { Button } from "@/components/ui/button"
import {
  ColorPicker,
  ColorPickerArea,
  ColorPickerInput,
  ColorPickerSlider,
  createColorPickerController,
} from "@/components/ui/ui-sdk-color-picker"

function UiSdkColorPickerControlled() {
  const [picker] = React.useState(() => createColorPickerController())
  const [value, setValue] = React.useState("#0ea5e9")
  const [saved, setSaved] = React.useState("#0ea5e9")

  return (
    <div className="flex w-full max-w-64 flex-col gap-3">
      <ColorPicker
        aria-label="Theme color"
        value={value}
        onValueChange={setValue}
        onValueCommit={setSaved}
        controller={picker}
      >
        <ColorPickerArea />
        <ColorPickerSlider channel="hue" />
        <ColorPickerInput aria-label="Hex" />
      </ColorPicker>
      <div className="flex items-center justify-between gap-2 text-sm">
        <p className="text-muted-foreground">
          Saved <span className="font-mono text-foreground">{saved}</span>
        </p>
        <Button
          variant="outline"
          size="sm"
          onClick={() => picker.setValue("#663399")}
        >
          Use purple
        </Button>
      </div>
    </div>
  )
}

export { UiSdkColorPickerControlled }

This adds the example as components/examples/ui-sdk-color-picker/controlled.tsx, together with the Color picker component and the HextaUI theme tokens it needs.

pnpm dlx shadcn@latest add https://hextaui.com/r/ui-sdk-color-picker-controlled.json

To add only the component, follow the Color picker installation.

Props in this example

The Color picker props this example sets, from the API reference.

PropTypeDefault
value
string–
onValueChangeRuns on every step of a drag, a key press, a typed color or a preset.
(value: string, details: ChangeDetails) => void–
onValueCommitRuns once when a drag, key or typed color ends.
(value: string, details: ChangeDetails) => void–
controllerFrom createColorPickerController().
ColorPickerController–
PropTypeDefault
channelHue, or blue in sRGB, by default.
ColorChannel–

Every prop is in the Color picker API reference.

See them all, with keyboard support and accessibility notes, on the Color picker page.