Fragiola

API: components

ColorSwatchPicker

A palette of swatches for choosing one color, the APG listbox laid out in rows, as a form control, in each framework.

Choose one color from a palette: the APG listbox in single selection, its options laid out in wrapping rows by your styles, the vertical arrows moving between rows. It works in a native <form> and a Field, and inside a ColorPicker it shows and sets the picker's color.

Theme color picker
Gallery

Behaviour

  • Value: a Color or null; an option is selected when its color is the value's (compared as #rrggbbaa, so an hsb value selects the #ff0000 swatch of the same color).
  • Keys: ArrowRight / ArrowLeft along the list (swapped in RTL, wrapping), ArrowDown / ArrowUp to the nearest option of the row below or above (measured on the rendered layout), Home / End; a focus move never selects, Space and Enter do (the APG's recommended model).
  • Pointer: a press focuses and selects; a disabled option takes focus, never the selection.
  • Forms: a hidden input submits the selected color as hex; a form reset returns to the default.
  • In a ColorPicker, with neither value nor defaultValue, it is bound to the picker's color (and submits nothing of its own).

Anatomy

partelementdata-primitives-partrole
Rootdivcolor-swatch-picker-rootthe listbox
Itemdivcolor-swatch-picker-iteman option, its content a ColorSwatch
(hidden input)input type="hidden"color-swatch-picker-inputwhat the form submits

Usage

import { ColorSwatch } from "@fragiola/primitives-react/color-swatch";
import { ColorSwatchPicker } from "@fragiola/primitives-react/color-swatch-picker";

<ColorSwatchPicker.Root aria-label="Accent" name="accent" defaultValue="#2563eb">
    <ColorSwatchPicker.Item value="#2563eb">
        <ColorSwatch color="#2563eb" aria-label="Blue" />
    </ColorSwatchPicker.Item>
    <ColorSwatchPicker.Item value="#16a34a">
        <ColorSwatch color="#16a34a" aria-label="Green" />
    </ColorSwatchPicker.Item>
</ColorSwatchPicker.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), and Angular as inputs and outputs ([(value)], (valueChangeDetails)).

Root

proptypedefaultdescription
valueColor | string | nullthe selected color, controlled
defaultValueColor | string | nullnullthe initial color, uncontrolled
onValueChange(value: Color | null, details: { reason }) => voida choice (item-press, keyboard) or a form reset (none)
disabledbooleanfalseevery option is disabled and none takes focus
namestringthe hidden input's form name (a Field's wins)

Item

proptypedefaultdescription
valueColor | stringthe color it selects (required)
disabledbooleanfalsetakes focus, never the selection

State and part attributes

partattributes
Rootrole="listbox", aria-orientation="horizontal", aria-disabled, data-disabled; in a Field, its ids and states
Itemrole="option", tabindex (roving), aria-selected, aria-disabled, data-selected, data-highlighted, data-disabled, --color-swatch-picker-item-color
hidden inputtype="hidden", name, value (the hex, or ""), disabled

Keyboard

keyaction
ArrowRight / ArrowLeftthe next / previous option (swapped in RTL), wrapping
ArrowDown / ArrowUpthe option in the row below / above
Home / Endthe first / last option
Space, Enterselects the focused option
Tableaves; the next Tab in lands on the selected option

Exports

entryexports
@fragiola/primitives-react/color-swatch-pickerChangeDetails, ChangeReason, Color, ColorSpace, ColorSwatchPicker, ColorSwatchPickerItemProps, ColorSwatchPickerItemState, ColorSwatchPickerRootProps, ColorSwatchPickerRootState, HsbColor, HslColor, RenderProp, RgbColor
@fragiola/primitives-vue/color-swatch-pickerChangeDetails, ChangeReason, Color, ColorSpace, ColorSwatchPicker, ColorSwatchPickerItemProps, ColorSwatchPickerItemState, ColorSwatchPickerRootEmits, ColorSwatchPickerRootProps, ColorSwatchPickerRootState, HsbColor, HslColor, RenderSlotProps, RgbColor
@fragiola/primitives-angular/color-swatch-pickerChangeDetails, ChangeReason, Color, ColorSpace, ColorSwatchPickerItem, ColorSwatchPickerItemState, ColorSwatchPickerRoot, ColorSwatchPickerRootState, HsbColor, HslColor, RgbColor, ValueChangeEvent