@akhatmirzo/ui

ColorPicker

Pick a color: a saturation area, hue and alpha sliders, a hex input, an eyedropper and preset swatches.

Import

import { ColorPicker } from '@akhatmirzo/ui';

When to use

  • Users choose their own color — a brand color, a tag or category color, theme settings.
  • When the palette is limited, pass a preset palette via swatches; free picking stays available.
  • When users pick from only a handful of colors (5–6), a RadioGroup or a SegmentedControl with color samples is simpler.

Anatomy

The trigger is a button styled like an Input: a color sample on the left and the hex text next to it. Clicking it opens a Popover:

  1. Saturation area (2D) — the horizontal axis is saturation, the vertical axis is brightness.
  2. Hue slider — the hue (0–360°), built with the Slider component.
  3. Alpha slider — only with alpha.
  4. Eyedropper — picks a color from the screen when the browser supports the EyeDropper API (Chromium for now); otherwise the button is hidden.
  5. Hex input — a full 6 (or 8) digit hex applies right away; a short #abc applies on Enter or blur. Invalid text shows the error state and reverts to the previous color on blur.
  6. Preset swatches (swatches).

The value always comes back as lowercase #rrggbb (onValueChange('#0ea5e9')). Picking white, black or gray keeps the hue, so the slider doesn't jump.

With Field

Inside a Field the trigger is named by the label plus the current color ("Brend rangi #0EA5E9"), and the description and error go into aria-describedby. name adds a hidden <input> for forms.

Tugmalar va havolalarda ishlatiladi

Alpha

With alpha the panel shows an alpha slider and the value becomes #rrggbbaa (#10b98180 — 50%). Color samples sit on a checkerboard.

Qiymat: #10b98180

Swatches

The default swatches are 10 colors from the library palette (exported as defaultColorSwatches). Pass your own palette, or [] to hide them.

Sizes and states

size matches the Input height: sm 32px, md 40px (default), lg 48px. disabled doesn't open; invalid (or Field error) shows a red border.

Props

Other <button> attributes and ref go to the trigger.

PropTypeDefaultDescription
valuestring—Color `#rrggbb` (`#rrggbbaa` with alpha)
defaultValuestring'#4f46e5'Initial color
onValueChange(value: string) => void—Called when the color changes
swatchesstring[]defaultColorSwatchesPreset colors (10)
alphabooleanfalseAlpha slider and an `#rrggbbaa` value
eyeDropperbooleantrueEyedropper button when the EyeDropper API exists
size'sm' | 'md' | 'lg''md'Trigger height 32 / 40 / 48px
disabledboolean—Disabled state
invalidboolean—Error state (automatic inside Field)
namestring—Hidden input for forms
open / defaultOpenboolean—Panel state
onOpenChange(open: boolean) => void—Called when the panel opens or closes
labels{ trigger?; saturation?; saturationValue?; hue?; alpha?; hex?; eyeDropper?; swatches? }—Built-in texts (Uzbek by default) — see the table below
contentClassNamestring—Panel class

labels

PropTypeDefaultDescription
triggerstring'Rang tanlash'Trigger name (without Field)
saturationstring"To'yinganlik va yorqinlik"Name of the 2D area
saturationValue(s: number, v: number) => string"to'yinganlik 80%, yorqinlik 60%"aria-valuetext of the 2D area
huestring'Rang tusi'Hue slider
alphastring'Shaffoflik'Alpha slider
hexstring'HEX'Hex input
eyeDropperstring'Ekrandan rang olish'Eyedropper button
swatchesstring'Tayyor ranglar'Swatch group

Data attributes

AttributeWhereValue
data-statetriggeropen · closed
data-sizetriggersm · md · lg
data-invalidtriggerpresent in the error state
data-disabledtriggerpresent when disabled
data-stateswatchon · off
data-slotpartscolor-picker · color-picker-content · color-picker-area · color-picker-hue · color-picker-alpha

Keyboard

KeyWhereAction
Enter / SpacetriggerOpen the panel
EscpanelClose; focus returns to the trigger
← / →2D areaSaturation ±1% (±10% with Shift)
↑ / ↓2D areaBrightness ±1% (±10% with Shift)
PageUp / PageDown2D areaBrightness ±10%
Home / End2D areaSaturation 0% / 100%
arrowshue / alphaSlider keyboard
Enterhex inputApply the typed color

Accessibility

  • Trigger — a plain <button>; inside a Field it's named by the label plus the current hex, otherwise "Rang tanlash, #4F46E5" (change with aria-label or labels.trigger).
  • 2D area — role="slider" with aria-valuenow (saturation) and an aria-valuetext that covers both axes: "to'yinganlik 80%, yorqinlik 60%".
  • Hue and alpha — the Slider component: role="slider" with aria-valuetext ("210°", "50%").
  • Swatches — buttons inside a role="group", each named by its hex and using aria-pressed.
  • Eyedropper — shown only when the API exists (hidden on the server and in unsupported browsers), aria-label="Ekrandan rang olish".
  • Color isn't the only signal — the selected swatch gets an extra ring, and the trigger also shows the hex text.

On this page