# Color picker

A color field: type a hex, rgb() or hsl() color, or pick one from a saturation and brightness area, a hue slider, an optional opacity slider and preset swatches.

- Package: [@vigdx/ui](https://www.npmjs.com/package/@vigdx/ui)
- Selectors: `vig-color-picker`
- Source: [libs/ui/src/lib/molecules/color-picker/color-picker.ts](https://github.com/bakyurekk/vigdx/blob/main/libs/ui/src/lib/molecules/color-picker/color-picker.ts)

```typescript title="Import"
import { ColorPicker } from '@vigdx/ui/color-picker';
```

## Basic

Bind `[(value)]` to a `string | null` signal. The swatch button opens the picker in a panel; the text field next to it takes a typed color, updating the value as soon as it reads as one.

```typescript title="color-picker-basic.ts"
import { ChangeDetectionStrategy, Component, signal } from '@angular/core';
import { ColorPicker } from '@vigdx/ui/color-picker';

@Component({
  selector: 'app-color-picker-basic',
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [ColorPicker],
  template: `
    <div class="max-w-xs">
      <vig-color-picker label="Brand color" [(value)]="color" />
    </div>
    <p class="text-sm text-muted-foreground">Value: {{ color() ?? 'none' }}</p>
  `,
})
export class ColorPickerBasicDemo {
  protected readonly color = signal<string | null>('#2b5ce6');
}
```

## Inline, opacity and presets

`mode="inline"` shows the picker in place. `alpha` adds an opacity slider and writes translucent colors with an alpha part (`#rrggbbaa`). `presets` adds one-click swatches; give them a `label` for screen readers. Where the browser has the EyeDropper API (Chromium), a button samples a color from the screen.

```typescript title="color-picker-inline.ts"
import { ChangeDetectionStrategy, Component, signal } from '@angular/core';
import { ColorPicker, type ColorPreset } from '@vigdx/ui/color-picker';

@Component({
  selector: 'app-color-picker-inline',
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [ColorPicker],
  template: `
    <vig-color-picker label="Highlight" mode="inline" alpha [presets]="presets" [(value)]="color" />
    <p class="text-sm text-muted-foreground">Value: {{ color() ?? 'none' }}</p>
  `,
})
export class ColorPickerInlineDemo {
  protected readonly color = signal<string | null>('#2b5ce6cc');
  // Name presets for screen readers; a bare string is announced as is.
  protected readonly presets: ColorPreset[] = [
    { value: '#2b5ce6', label: 'Brand blue' },
    { value: '#11754f', label: 'Forest green' },
    { value: '#be3037', label: 'Signal red' },
    { value: '#945806', label: 'Amber' },
    { value: '#7c3aed', label: 'Violet' },
  ];
}
```

## Formats

`format` is the format the value is written in: `hex` (default), `rgb` or `hsl`. The text field accepts all three syntaxes, comma or space separated, whatever the format. Typing writes the color at once; leaving the field never rewrites a value, so `#F00` set from code stays `#F00` until the user changes it. Without `alpha` the colors the user picks or types are written opaque.

```typescript title="color-picker-formats.ts"
import { ChangeDetectionStrategy, Component, signal } from '@angular/core';
import { ColorPicker } from '@vigdx/ui/color-picker';

@Component({
  selector: 'app-color-picker-formats',
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [ColorPicker],
  template: `
    <div class="grid gap-6 sm:grid-cols-2">
      <vig-color-picker label="RGB" format="rgb" [(value)]="rgb" />
      <vig-color-picker label="HSL with opacity" format="hsl" alpha [(value)]="hsl" />
    </div>
    <p class="text-sm text-muted-foreground">{{ rgb() }} · {{ hsl() }}</p>
  `,
})
export class ColorPickerFormatsDemo {
  protected readonly rgb = signal<string | null>('rgb(17, 117, 79)');
  protected readonly hsl = signal<string | null>('hsla(262, 83%, 58%, 0.8)');
}
```

## Sizes and states

`size` sets the field height (`sm`, `md`, `lg`). `readonly` keeps the text selectable but the picker closed; `disabled` turns everything off.

```typescript title="color-picker-states.ts"
import { ChangeDetectionStrategy, Component } from '@angular/core';
import { ColorPicker } from '@vigdx/ui/color-picker';

@Component({
  selector: 'app-color-picker-states',
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [ColorPicker],
  template: `
    <div class="grid gap-6 sm:grid-cols-3">
      <vig-color-picker label="Small" size="sm" value="#11754f" />
      <vig-color-picker label="Read-only" readonly value="#945806" />
      <vig-color-picker label="Disabled" disabled value="#7c3aed" />
    </div>
  `,
})
export class ColorPickerStatesDemo {}
```

## Signal Forms

`vig-color-picker` implements `FormValueControl<string | null>`. Text that is not a color shows its own error at once and never reaches the model.

```typescript title="color-picker-signal-forms.ts"
import { ChangeDetectionStrategy, Component, signal } from '@angular/core';
import { FormField, form, required } from '@angular/forms/signals';
import { ColorPicker } from '@vigdx/ui/color-picker';

@Component({
  selector: 'app-color-picker-signal-forms',
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [FormField, ColorPicker],
  template: `
    <div class="max-w-xs">
      <vig-color-picker label="Label color" hint="Required" [formField]="theme.label" />
    </div>
    <p class="text-sm text-muted-foreground">
      Valid: {{ theme().valid() ? 'yes' : 'no' }} · {{ model().label ?? 'none' }}
    </p>
  `,
})
export class ColorPickerSignalFormsDemo {
  protected readonly model = signal<{ label: string | null }>({ label: null });
  protected readonly theme = form(this.model, (path) => {
    required(path.label, { message: 'Pick a label color.' });
  });
}
```

## Reactive Forms

Import `VigValueAccessor` next to the component to use `formControlName`, `[formControl]` or `ngModel`.

```typescript title="color-picker-reactive-forms.ts"
import { ChangeDetectionStrategy, Component } from '@angular/core';
import { FormControl, ReactiveFormsModule } from '@angular/forms';
import { ColorPicker } from '@vigdx/ui/color-picker';
import { VigValueAccessor } from '@vigdx/ui/core';

@Component({
  selector: 'app-color-picker-reactive-forms',
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [ReactiveFormsModule, ColorPicker, VigValueAccessor],
  template: `
    <div class="max-w-xs">
      <vig-color-picker label="Background" [formControl]="background" />
    </div>
    <p class="text-sm text-muted-foreground">
      {{ background.value ?? 'none' }} · {{ background.dirty ? 'dirty' : 'pristine' }}
    </p>
  `,
})
export class ColorPickerReactiveFormsDemo {
  protected readonly background = new FormControl<string | null>('#f7f8fa');
}
```

## Native alternative

When an opaque hex color is all you need and the browser's own picker is acceptable, a labelled `<input type="color">` is the lighter choice: it is keyboard and screen-reader accessible by default and opens the platform's picker. It has no opacity, no presets and no typed entry on most platforms, and its look varies by browser, which is what `vig-color-picker` adds.

## Styling hooks

Internal elements carry a stable `data-part` and the component's `data-scope`; both are public API, so removing one is a breaking change. Select a part by both, e.g. `[data-scope="color-picker"][data-part="label"] { … }`: the pair stays within this component when kit components nest, and also reaches parts in overlay panels, which render outside the host. State is exposed through `aria-*` and, where listed, a `data-state`; literal values are checked by the API gate, values computed from the component's state follow its documented types. Other `data-*` attributes listed below (such as `data-opening` while an overlay animates) are API too. See [Customizing](https://vigdx.com/docs/theming.md#customizing).

Scope: `[data-scope="color-picker"]`

| Part | Element | data-state |
| --- | --- | --- |
| `label` | The visible label. |   |
| `field` | The bordered row of the swatch (button) and the text field. |   |
| `trigger` | The swatch button that opens the panel (`mode="popup"`). | `open, closed` |
| `swatch` | The current color over a checkerboard; `empty` without a value. | `empty` |
| `input` | The text field: hex, `rgb()` or `hsl()`. |   |
| `panel` | The popup dialog (in the overlay container), or the inline group. |   |
| `area` | The saturation and brightness area (`role="slider"`). |   |
| `area-thumb` | The marker in the area. |   |
| `hue` | The native hue range. |   |
| `alpha` | The native opacity range (`alpha`). |   |
| `eye-dropper` | The button that samples the screen (EyeDropper API browsers only). |   |
| `presets` | The group of preset swatches. |   |
| `preset` | One preset swatch button (`aria-pressed`). |   |
| `preset-selected` | The ring around the selected preset. |   |
| `hint` | The `vig-hint` (a `display: contents` host; style its `text` part). |   |
| `disabled-reason` | The `vig-hint` with the disabled reason (`display: contents`). |   |
| `error` | The `vig-field-error` (a `display: contents` host; style its `message` part). |   |

| Attribute | Meaning |
| --- | --- |
| `data-opening` | On the overlay element (panel or `<dialog>`) while its entrance transition plays; removed once it settles (2s at most). Style the entrance with `data-opening:` utilities. |
| `data-closing` | On the overlay element while its exit transition plays, before it closes; removed once it settles (2s at most). Style the closed look with `data-closing:` utilities. |

## Accessibility

- The text field is the field's main control: it is named by the label, carries `aria-required`, `aria-invalid` and the hint and error, and always shows the value as text, so the color is never conveyed by the swatch alone.
- The swatch button (`aria-haspopup="dialog"`) opens a non-modal dialog and moves focus to the area; Escape (also from the text field) closes it and returns to the button, Tab past the last control leaves it. Leaving the whole field marks it touched, also in `inline` mode.
- The area is a `role="slider"`: Left and Right change the saturation, Up and Down the brightness (Shift for steps of 10), Page Up and Page Down the brightness by 10, Home and End the saturation to 0 and 100. Its `aria-valuetext` always says both ("Saturation 40%, brightness 80%"); `aria-valuenow` follows the axis changed last. One slider rather than two keeps the area a single tab stop, as the arrow keys already cover both axes. Hue and opacity are native ranges with readable `aria-valuetext` ("210 degrees", "50%").
- Drags use pointer capture, and only the area sets `touch-action: none`.
- In forced-colors mode the swatches, the area and the slider tracks keep their colors (they are the information) inside system-colored borders; the selected preset is marked by a ring, not a tint.
- The EyeDropper button renders only where `window.EyeDropper` exists. The picker works in sRGB (the area is an HSV model), so it uses no `color-mix()` or `oklch()`; wide-gamut colors typed in are not read.

## API

### ColorPicker

Selector: `vig-color-picker` · Kind: component

A color field: a saturation/brightness area, a hue slider, an optional opacity slider (`alpha`), preset swatches and a text field for typing a color, either in place (`mode="inline"`) or behind a swatch button (`mode="popup"`, the default). Where the browser has the EyeDropper API a button samples a color from the screen.

**Example :**

```html
<vig-color-picker label="Brand color" [(value)]="color" />
```

The value is a string in `format` (`#1e90ff` by default, or `rgb()` / `hsl()`), `null` while empty. The text field takes any of those syntaxes; what it cannot read shows an error and leaves the value alone. Every part works from the keyboard: the area is a slider (arrows, Page Up/Down, Home and End) announced as "Saturation 40%, brightness 80%", the hue and opacity sliders are native ranges. Works with Signal Forms and, through `VigValueAccessor`, with Reactive Forms and `ngModel`. While it plays its entrance or exit transition, the animating element carries `data-opening` or `data-closing` (stable hooks for styling and for waiting in tests; see the theming guide). data-opening data-closing

#### Inputs

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `alpha` | `boolean` | `false` | Adds an opacity slider; translucent values get an alpha part (`#rrggbbaa`, `rgba()`, `hsla()`). Without it the picker works with opaque colors: what the user picks or types is written without alpha. A translucent value set from outside is kept as it is until the user changes the color. |
| `dirty` | `boolean` | `false` | — |
| `disabled` | `boolean` | `false` | — |
| `disabledReasons` | `readonly WithOptionalFieldTree<DisabledReason>[]` | `[]` | — |
| `errorMessages` | `Record<string, string>` | — | — |
| `errors` | `readonly ValidationError.WithOptionalFieldTree[]` | `[]` | — |
| `format` | `ColorFormat` | `'hex'` | The format the value is written in: `hex` (default), `rgb` or `hsl`. |
| `hidden` | `boolean` | `false` | — |
| `hideLabel` | `boolean` | `false` | Keeps the label for assistive tech only. |
| `hint` | `string \| undefined` | `undefined` | — |
| `invalid` | `boolean` | `false` | — |
| `label` (required) | `string` | — | — |
| `labels` | `Partial<ColorPickerLabels>` | — | Texts (i18n); locale defaults. |
| `mode` | `ColorPickerMode` | `'popup'` | `popup` (default): a swatch button opens the picker; `inline`: shown in place. |
| `name` | `string` | `''` | — |
| `pending` | `boolean` | `false` | — |
| `presets` | `readonly ColorPreset[]` | `[]` | Swatches to pick from with one click; name them for screen readers with `{ value, label }`. |
| `readonly` | `boolean` | `false` | — |
| `required` | `boolean` | `false` | — |
| `size` | `FieldSize` | `'md'` | — |
| `touched` | `boolean` | `false` | — |
| `value` (model) | `string \| null` | `null` | The color in `format`, `null` while empty. |

#### Outputs

| Name | Payload | Description |
| --- | --- | --- |
| `touch` | `void` | — |
| `valueChange` | `string \| null` | The color in `format`, `null` while empty. |

#### Methods

| Signature | Description |
| --- | --- |
| `focus(options?: FocusOptions): void` | Moves focus to the control's interactive element (the input, or the trigger of a popup control). Signal Forms calls it through `field().focusBoundControl()`; the host element itself cannot take focus. |
| `reset(): void` | Clears a typing error and closes the popup; see `VigFormField.reset`. |
