# ColorPicker

Crate: `libero`
Import: `use libero::components::{ColorPicker, ColorCode};`
Feature: `color` (off by default, turns on `slider`, `action-icon`, `use-media-query`, `use-swipe`, `use-timers`; [features.md](features.md))
Source: <https://github.com/tdymel/libero/tree/main/libero/src/components/form/color>
Index: [index.md](index.md) lists every other page
Description: A saturation panel and a hue slider, with optional alpha and preset swatches, over one `ColorCode`. Also documents `HueSlider`, `AlphaSlider` and `ColorSwatch`.

A saturation panel and a hue slider, with an optional alpha slider and preset
swatches. The value is a `ColorCode`, which parses from hex, `rgb()` or `hsl()`
and converts back to any of them. It reads commas, spaces and `/` alike and
clamps a value out of range, as CSS does, but knows no named colors: `"red"` is
an error. `ColorCode::hex(0x228be6)` and its siblings build one, and a theme `HexColor` converts into one. It prints as `#rrggbb`, or
`#rrggbbaa` when translucent, which a `style` accepts.

To open a picker from a text field or from a swatch button, use
[ColorField](color_field.md): it brings the dropdown, the focus return and
Escape, and `variant: ColorFieldVariant::Swatch` leaves only the swatch.

## Usage

```rust
use dioxus::prelude::*;
use libero::components::{ColorCode, ColorPicker, Flex, SliderChangeEvent, Text};
use libero::sx::sx;

#[component]
fn Demo() -> Element {
    let mut color = use_signal(|| ColorCode::hex(0x228be6));

    rsx! {
        Flex {
            direction: "column",
            gap: "sm",
            sx: sx().width("100%"),
            ColorPicker {
                value: color(),
                oninput: move |event: SliderChangeEvent<ColorCode>| color.set(event.value()),
            }
            Text { size: "sm", "{color().to_hexa()} · {color().to_rgba()} · {color().to_hsla()}" }
        }
    }
}
```

## Props

### `ColorPicker`

| Prop | Type | Default | Description |
|---|---|---|---|
| `size` | `Responsive<Size>` | `md` | Width, panel height, thumbs, preview and swatches. Swatches keep their size when `full_width` stretches the picker. Or one per breakpoint, `size: responsive(Size::Sm).md(Size::Lg)`. |
| `radius` | `ThemeAwareValue` | `xxl` | Corner radius of the swatches and the preview, or any CSS, e.g. `radius: "0"`. `xs` makes them square. |
| `value` | `ColorCode` | required | The color. Pair it with `oninput`. |
| `oninput` | `EventHandler<SliderChangeEvent<ColorCode>>` | - | `Start` and `End` bracket a drag on the panel or a slider. A key press or a swatch click sends `Change`, then `End`, so saving on `End` is enough. |
| `with_alpha` | `bool` | `false` | Shows the alpha slider and a preview swatch beside it. Without it, the color is always opaque. |
| `swatches` | `Swatches` | - | Preset colors under the panel, as `ColorCode`s or CSS strings. A string that is no color is skipped with a warning. The swatch equal to `value` shows as picked. |
| `swatches_per_row` | `usize` | - | Caps how many swatches share a row. Unset, they wrap to fill the width, seven per row at the picker's own width. |
| `with_picker` | `bool` | `true` | `false` leaves only the swatches, a palette. Without `swatches` it draws nothing and warns. Its swatches are buttons: without `oninput` or `onswatchclick` they do nothing, and it warns once. |
| `onswatchclick` | `EventHandler<ColorCode>` | - | A swatch was clicked. `oninput` fires with the same color first. |
| `full_width` | `bool` | `false` | Takes the container's width instead of the size step's. |
| `name` | `String` | - | Posts the color in a hidden input of that name. |
| `format` | `ColorFormat` | `hex, or hexa with alpha` | How the hidden input writes the color. `hex`, `hexa`, `rgb`, `rgba`, `hsl` or `hsla`. |
| `focusable` | `bool` | `true` | `false` keeps the thumbs and swatches out of the tab order, for a picker in a dropdown whose input must keep focus. |
| `disabled` | `bool` | `false` | Dims the panel, the sliders and the swatches and takes them out of the tab order. The hidden input no longer posts. A disabled `Fieldset` does the same. |
| `readonly` | `bool` | `false` | Shows the color but ignores drags, keys and swatch clicks, `oninput` included. The thumbs stay tab stops and read as read-only; the swatches stay focusable buttons marked `aria-disabled`. |
| `saturation_label` | `String` | `color.saturation` | Names the saturation panel's thumb. Unset, the localization's `color.saturation`. |
| `hue_label` | `String` | `color.hue` | Names the hue slider's thumb. Unset, the localization's `color.hue`. |
| `alpha_label` | `String` | `color.alpha` | Names the alpha slider's thumb. Unset, the localization's `color.alpha`. |

### `HueSlider`

| Prop | Type | Default | Description |
|---|---|---|---|
| `value` | `f64` | required | The hue in degrees, 0 to 360. Pair it with `oninput`. A `ColorPicker`'s own hue slider stops at 359: 360 is the same hue as 0. |
| `oninput` | `EventHandler<SliderChangeEvent>` | - | Every new hue. |
| `size` | `Responsive<Size>` | `md` | Track height and thumb. Or one per breakpoint. |
| `disabled` | `bool` | `false` | Dims the slider and stops it moving. |
| `focusable` | `bool` | `true` | `false` keeps the thumb out of the tab order. |
| `aria_label` | `String` | - | Names the thumb. Unset, the thumb has no name: unlike `ColorPicker`, the standalone slider falls back to no localization. |

### `AlphaSlider`

| Prop | Type | Default | Description |
|---|---|---|---|
| `value` | `f64` | required | The alpha, 0.0 to 1.0. Pair it with `oninput`. |
| `color` | `ColorCode` | required | The color the track fades in. Its own alpha is ignored. |
| `oninput` | `EventHandler<SliderChangeEvent>` | - | Every new alpha. |
| `size` | `Responsive<Size>` | `md` | Track height and thumb. Or one per breakpoint. |
| `disabled` | `bool` | `false` | Dims the slider and stops it moving. |
| `focusable` | `bool` | `true` | `false` keeps the thumb out of the tab order. |
| `aria_label` | `String` | - | Names the thumb. Unset, the thumb has no name: unlike `ColorPicker`, the standalone slider falls back to no localization. |

### `ColorSwatch`

| Prop | Type | Default | Description |
|---|---|---|---|
| `color` | `ColorCode` | required | The color. A translucent one shows a checkerboard through. |
| `size` | `Size` | `md` | Diameter. |
| `radius` | `ThemeAwareValue` | `xxl` | Corner radius, or any CSS, e.g. `radius: "0"`. Round by default. |
| `background` | `String` | - | Any CSS `background`, such as a conic gradient for a hue wheel, painted in place of `color`. `color` still decides whether the mark is black or white, so set it to what shows. |
| `with_shadow` | `bool` | `true` | A faint inner ring, so a color close to the background keeps an edge. |
| `onclick` | `EventHandler<MouseEvent>` | - | Makes the swatch a `<button>`. |
| `children` | `Element` | - | Drawn on the color, such as a check mark, in black or white, whichever reads. |

Like every component, each of them also takes the shared props `sx`, `class`,
`style`, `states`, `hidden_from`, `visible_from`, and any extra HTML attributes. The attributes land on the
root.

## Style API

Style a part with the `parts` prop, or address it as `[data-slot='…']` in your
own CSS. The names are stable. [Style API in Styling](styling.md#style-api)
explains how parts work. `ColorSwatch` is one element and has no parts.

### `ColorPicker`

| Part | `data-slot` | Description |
|---|---|---|
| `ColorPickerPart::Saturation` | `saturation` | The saturation and brightness panel. |
| `ColorPickerPart::Body` | `body` | The row under the panel: the sliders and the preview. |
| `ColorPickerPart::Sliders` | `sliders` | The column of the hue and alpha sliders. |
| `ColorPickerPart::Hue` | `hue` | The hue slider. |
| `ColorPickerPart::Alpha` | `alpha` | The alpha slider, with `with_alpha`. |
| `ColorPickerPart::Track` | `track` | Both sliders' gradient tracks. |
| `ColorPickerPart::Thumb` | `thumb` | Every handle: the panel's and the sliders'. |
| `ColorPickerPart::Preview` | `preview` | The current color beside the sliders, with `with_alpha`. |
| `ColorPickerPart::Swatches` | `swatches` | The row of preset swatches. |
| `ColorPickerPart::Swatch` | `swatch` | One preset swatch. |

### `HueSlider` and `AlphaSlider`

| Part | `data-slot` | Description |
|---|---|---|
| `ColorSliderPart::Track` | `track` | The gradient track. |
| `ColorSliderPart::Thumb` | `thumb` | The handle, filled with the color it points at. |

## Accessibility

### Keyboard

| Key | Action |
|---|---|
| `ArrowRight`, `ArrowDown` | In the swatch row: the next swatch, wrapping to the first. The row is one list even where it wraps onto several lines, so Down is the next swatch, not the one below it. |
| `ArrowLeft`, `ArrowUp` | In the swatch row: the previous swatch, wrapping to the last. Up is the previous swatch, not the one above it. |
| `Home`, `End` | In the swatch row: the first or the last swatch. |

### Libero handles

- Each thumb is a slider with the usual keys. In a `ColorPicker` the hue runs
  from 0 to 359 degrees, since 360 is the same hue as 0.
- The saturation panel and the hue and alpha thumbs meet the 24px target size of
  WCAG 2.5.8 at every size. At `xs` and `sm` the two sliders sit further apart
  than `spacing`, so their 24px squares do not overlap.
- The swatch equal to the value is pressed and checked.
- The preset row is a group named "Color swatches" (localized,
  `ColorLabels::swatches`), unless you name the picker.

### You must

- Give a picker you name `role: "group"` beside its `aria-label`: the root is a
  plain `div` that takes no name, and the swatch row then drops its own.
- Name swatches with `Swatches::labelled`. By default they are named by their
  hex, which a screen reader spells out.
- Give a `ColorSwatch` with `onclick` an `aria-label`; without one it is just
  "button", and it warns.
- Name the color in text beside a plain `ColorSwatch`, or give it `role: "img"`
  and an `aria-label`: on its own it says nothing.

### Example

A brand color picker with `swatches: Swatches::labelled(..)` naming each
swatch "Ocean", "Forest" and so on: a screen reader reads "Ocean", not a hex
code spelled out, and the swatch equal to the value reads as pressed.

## Theme defaults

`theme.color_picker` is a `ColorPickerDefaults` with `size`, `swatches_per_row`,
`radius`, and per size step `width`, `saturation_height`, `thumb_size`,
`preview_size`, `spacing` and `swatch_size`. `theme.color_swatch` is a
`ColorSwatchDefaults` with `size`, `radius` and `sizes`.
