Fragiola

API: components

ColorPicker

One color shared by the color controls inside it, a format switch, the browser's eyedropper and its form value, in each framework.

Edit one color with several controls: a Root holds the color and shares it with every ColorArea, ColorSlider, ColorWheel, ColorField, ColorSwatchPicker and ColorSwatch placed inside it. Inline it renders in the page; in a popover, add the opt-in ColorPickerPopover: an inline picker bundles no overlay code.

Theme color picker
Gallery

Behaviour

  • Sharing: a control inside the Root with neither value nor defaultValue is bound to it: it shows the picker's color and every change it makes is the picker's (its own onValueChange fires too). A control given its own value or defaultValue keeps its own state.
  • Inside the picker, every control is disabled with it and reads no Field (the picker is the field's control); a bound control submits nothing of its own.
  • Value: reported as the control that changed it reports it (each in the space it edits); onValueCommit once a change is complete (a drag ended, a key, a ColorField's commit, a swatch chosen, an eyedropper pick).
  • Format: hex, rgb, hsl or hsb: which channel fields you show. The FormatSelect sets it and converts the value to the format's space, so those fields edit it in their space.
  • Eyedropper: the EyeDropperTrigger renders only where the browser has EyeDropper (Chromium), once mounted; a pick keeps the value's space and alpha.
  • Forms: a hidden input submits the color as hex; a form reset returns the picker, and every bound control with it, to the default.

Anatomy

partelementdata-primitives-partrole
Rootdivcolor-picker-roota group holding the color and the format
FormatSelectselectcolor-picker-format-selectthe format; its options are yours
EyeDropperTriggerbuttoncolor-picker-eye-dropper-triggerpicks a color on screen
(hidden input)input type="hidden"color-picker-inputthe color as hex

Usage

import { ColorArea } from "@fragiola/primitives-react/color-area";
import { ColorPicker } from "@fragiola/primitives-react/color-picker";
import { ColorSlider } from "@fragiola/primitives-react/color-slider";

<ColorPicker.Root aria-label="Theme color" name="theme" defaultValue="#7c3aed">
    <ColorArea.Root xChannel="saturation" yChannel="brightness">
        <ColorArea.Thumb>
            <ColorArea.Input axis="x" aria-label="Saturation" />
            <ColorArea.Input axis="y" aria-label="Brightness" />
        </ColorArea.Thumb>
    </ColorArea.Root>
    <ColorSlider.Root channel="hue">
        <ColorSlider.Track>
            <ColorSlider.Thumb aria-label="Hue" />
        </ColorSlider.Track>
    </ColorSlider.Root>
    <ColorPicker.EyeDropperTrigger>Pick from the screen</ColorPicker.EyeDropperTrigger>
</ColorPicker.Root>;

Props

Every part also takes what the primitive contract gives: render (Vue: as and the render slot), a class and a style, a ref, and any attribute, which it forwards. The names below are React's; Vue binds them the same way (v-model:value, v-model:format, @value-commit), and Angular as inputs and outputs ([(value)], [(format)], (valueCommit)).

Root

proptypedefaultdescription
valueColor | stringthe color, controlled
defaultValueColor | string"#000000"the initial color, uncontrolled
onValueChange(value: Color, details: { reason }) => voida bound control's change, the format's conversion (input-change), a pick (trigger-press), a reset (none)
onValueCommit(value: Color, details: { reason }) => voida change is complete
format"hex" | "rgb" | "hsl" | "hsb"the format, controlled
defaultFormat"hex" | "rgb" | "hsl" | "hsb""hex"the initial format
onFormatChange(format, details: { reason }) => voidthe FormatSelect changed it (input-change)
disabledbooleanfalseevery control inside is disabled
namestringthe hidden input's form name (a Field's wins)

FormatSelect

No props of its own; its children are your <option value="hex"> (and rgb, hsl, hsb).

EyeDropperTrigger

No props of its own; its content and name are yours.

State and part attributes

partattributes
Rootrole="group", data-format, data-disabled, --color-picker-color; in a Field, its ids and states
FormatSelectdisabled, data-format, data-disabled
EyeDropperTriggertype="button", disabled, data-disabled
hidden inputtype="hidden", name, value (the hex), disabled

Keyboard

The Root adds no key of its own: each control inside keeps its own (the slider keys of a ColorSlider, a ColorArea and a ColorWheel, a ColorField's Enter and arrows, a ColorSwatchPicker's listbox keys); the FormatSelect is a native select and the EyeDropperTrigger a native button.

Exports

entryexports
@fragiola/primitives-react/color-pickerChangeDetails, ChangeReason, Color, ColorPicker, ColorPickerEyeDropperTriggerProps, ColorPickerFormat, ColorPickerFormatSelectProps, ColorPickerRootProps, ColorPickerState, ColorSpace, HsbColor, HslColor, RenderProp, RgbColor
@fragiola/primitives-vue/color-pickerChangeDetails, ChangeReason, Color, ColorPicker, ColorPickerEyeDropperTriggerProps, ColorPickerFormat, ColorPickerFormatSelectProps, ColorPickerRootEmits, ColorPickerRootProps, ColorPickerState, ColorSpace, HsbColor, HslColor, RenderSlotProps, RgbColor
@fragiola/primitives-angular/color-pickerChangeDetails, ChangeReason, Color, ColorPickerEyeDropperTrigger, ColorPickerFormat, ColorPickerFormatSelect, ColorPickerRoot, ColorPickerState, ColorSpace, HsbColor, HslColor, RgbColor, ValueChangeEvent