@ui-sdk/react 0.0.0-stage → 0.1.0-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +202 -0
- package/README.md +10 -2
- package/dist/accordion.d.ts +331 -0
- package/dist/accordion.js +25 -0
- package/dist/agent.d.ts +2 -0
- package/dist/agent.js +8 -0
- package/dist/alert-dialog.d.ts +56 -0
- package/dist/alert-dialog.js +45 -0
- package/dist/async-options.d.ts +22 -0
- package/dist/async-options.js +56 -0
- package/dist/autocomplete.d.ts +102 -0
- package/dist/autocomplete.js +195 -0
- package/dist/avatar.d.ts +301 -0
- package/dist/avatar.js +17 -0
- package/dist/button.d.ts +21 -0
- package/dist/button.js +45 -0
- package/dist/calendar-context.d.ts +10 -0
- package/dist/calendar-context.js +9 -0
- package/dist/calendar.d.ts +80 -0
- package/dist/calendar.js +257 -0
- package/dist/carousel.d.ts +52 -0
- package/dist/carousel.js +151 -0
- package/dist/checkbox.d.ts +24 -0
- package/dist/checkbox.js +98 -0
- package/dist/children.d.ts +38 -0
- package/dist/children.js +63 -0
- package/dist/collapsible.d.ts +302 -0
- package/dist/collapsible.js +19 -0
- package/dist/color-picker.d.ts +72 -0
- package/dist/color-picker.js +133 -0
- package/dist/color.d.ts +2 -0
- package/dist/color.js +22 -0
- package/dist/combobox.d.ts +152 -0
- package/dist/combobox.js +338 -0
- package/dist/command.d.ts +88 -0
- package/dist/command.js +233 -0
- package/dist/controller-target.d.ts +3 -0
- package/dist/controller-target.js +59 -0
- package/dist/date-input.d.ts +19 -0
- package/dist/date-input.js +58 -0
- package/dist/date-picker.d.ts +47 -0
- package/dist/date-picker.js +310 -0
- package/dist/date.d.ts +2 -0
- package/dist/date.js +60 -0
- package/dist/dialog-parts.d.ts +23 -0
- package/dist/dialog-parts.js +142 -0
- package/dist/dialog.d.ts +57 -0
- package/dist/dialog.js +32 -0
- package/dist/drawer.d.ts +70 -0
- package/dist/drawer.js +158 -0
- package/dist/editor-context.d.ts +4 -0
- package/dist/editor-context.js +9 -0
- package/dist/field-context.d.ts +47 -0
- package/dist/field-context.js +87 -0
- package/dist/field.d.ts +49 -0
- package/dist/field.js +169 -0
- package/dist/file-input.d.ts +46 -0
- package/dist/file-input.js +170 -0
- package/dist/form-inputs.d.ts +5 -0
- package/dist/form-inputs.js +22 -0
- package/dist/gesture.d.ts +1 -0
- package/dist/gesture.js +16 -0
- package/dist/history.d.ts +2 -0
- package/dist/history.js +12 -0
- package/dist/hover-card.d.ts +33 -0
- package/dist/hover-card.js +84 -0
- package/dist/image-cropper.d.ts +333 -0
- package/dist/image-cropper.js +37 -0
- package/dist/image.d.ts +2 -0
- package/dist/image.js +6 -0
- package/dist/input.d.ts +22 -0
- package/dist/input.js +51 -0
- package/dist/label.d.ts +294 -0
- package/dist/label.js +9 -0
- package/dist/list-items.d.ts +15 -0
- package/dist/list-items.js +39 -0
- package/dist/list-text.d.ts +4 -0
- package/dist/list-text.js +15 -0
- package/dist/listbox.d.ts +56 -0
- package/dist/listbox.js +122 -0
- package/dist/menu.d.ts +116 -0
- package/dist/menu.js +426 -0
- package/dist/menubar-context.d.ts +4 -0
- package/dist/menubar-context.js +7 -0
- package/dist/menubar.d.ts +11 -0
- package/dist/menubar.js +32 -0
- package/dist/meter.d.ts +312 -0
- package/dist/meter.js +30 -0
- package/dist/navigation-menu.d.ts +61 -0
- package/dist/navigation-menu.js +169 -0
- package/dist/number-input.d.ts +44 -0
- package/dist/number-input.js +185 -0
- package/dist/number.d.ts +2 -0
- package/dist/number.js +40 -0
- package/dist/observe.d.ts +2 -0
- package/dist/observe.js +8 -0
- package/dist/otp-input.d.ts +29 -0
- package/dist/otp-input.js +94 -0
- package/dist/overlays.d.ts +26 -0
- package/dist/overlays.js +82 -0
- package/dist/pagination.d.ts +336 -0
- package/dist/pagination.js +71 -0
- package/dist/password-input.d.ts +23 -0
- package/dist/password-input.js +65 -0
- package/dist/popover.d.ts +54 -0
- package/dist/popover.js +151 -0
- package/dist/popup.d.ts +41 -0
- package/dist/popup.js +180 -0
- package/dist/preferences.d.ts +35 -0
- package/dist/preferences.js +93 -0
- package/dist/primitive.d.ts +2 -0
- package/dist/primitive.js +14 -0
- package/dist/progress.d.ts +312 -0
- package/dist/progress.js +30 -0
- package/dist/radio.d.ts +22 -0
- package/dist/radio.js +86 -0
- package/dist/rating.d.ts +20 -0
- package/dist/rating.js +94 -0
- package/dist/render-element.d.ts +35 -0
- package/dist/render-element.js +103 -0
- package/dist/resizable.d.ts +302 -0
- package/dist/resizable.js +23 -0
- package/dist/scroll-area.d.ts +324 -0
- package/dist/scroll-area.js +49 -0
- package/dist/segmented-control.d.ts +24 -0
- package/dist/segmented-control.js +78 -0
- package/dist/segmented-input.d.ts +15 -0
- package/dist/segmented-input.js +48 -0
- package/dist/select.d.ts +125 -0
- package/dist/select.js +330 -0
- package/dist/separator.d.ts +290 -0
- package/dist/separator.js +9 -0
- package/dist/shortcuts.d.ts +32 -0
- package/dist/shortcuts.js +61 -0
- package/dist/slider.d.ts +38 -0
- package/dist/slider.js +163 -0
- package/dist/switch.d.ts +15 -0
- package/dist/switch.js +39 -0
- package/dist/tabs.d.ts +331 -0
- package/dist/tabs.js +28 -0
- package/dist/text-control.d.ts +22 -0
- package/dist/text-control.js +24 -0
- package/dist/text-match.d.ts +2 -0
- package/dist/text-match.js +8 -0
- package/dist/textarea.d.ts +10 -0
- package/dist/textarea.js +48 -0
- package/dist/time-input.d.ts +19 -0
- package/dist/time-input.js +63 -0
- package/dist/timeline.d.ts +327 -0
- package/dist/timeline.js +31 -0
- package/dist/toast-parts.d.ts +63 -0
- package/dist/toast-parts.js +144 -0
- package/dist/toast.d.ts +2 -0
- package/dist/toast.js +22 -0
- package/dist/toggle.d.ts +23 -0
- package/dist/toggle.js +76 -0
- package/dist/token-input.d.ts +35 -0
- package/dist/token-input.js +100 -0
- package/dist/toolbar-context.d.ts +6 -0
- package/dist/toolbar-context.js +16 -0
- package/dist/toolbar.d.ts +38 -0
- package/dist/toolbar.js +63 -0
- package/dist/tooltip.d.ts +39 -0
- package/dist/tooltip.js +108 -0
- package/dist/tree.d.ts +51 -0
- package/dist/tree.js +150 -0
- package/dist/trigger-menu.d.ts +83 -0
- package/dist/trigger-menu.js +167 -0
- package/dist/use-primitive.d.ts +178 -0
- package/dist/use-primitive.js +674 -0
- package/dist/waterfall.d.ts +80 -0
- package/dist/waterfall.js +108 -0
- package/docs/README.md +89 -0
- package/docs/agents.md +1523 -0
- package/docs/components/accordion.md +1391 -0
- package/docs/components/alert-dialog.md +850 -0
- package/docs/components/autocomplete.md +1963 -0
- package/docs/components/avatar.md +252 -0
- package/docs/components/button.md +444 -0
- package/docs/components/calendar.md +1687 -0
- package/docs/components/carousel.md +1244 -0
- package/docs/components/checkbox.md +1069 -0
- package/docs/components/collapsible.md +567 -0
- package/docs/components/color-picker.md +736 -0
- package/docs/components/combobox.md +3647 -0
- package/docs/components/command.md +1573 -0
- package/docs/components/date-input.md +608 -0
- package/docs/components/date-picker.md +1266 -0
- package/docs/components/dialog.md +1469 -0
- package/docs/components/drawer.md +1197 -0
- package/docs/components/field.md +549 -0
- package/docs/components/file-input.md +609 -0
- package/docs/components/hover-card.md +514 -0
- package/docs/components/image-cropper.md +579 -0
- package/docs/components/input.md +301 -0
- package/docs/components/label.md +217 -0
- package/docs/components/listbox.md +1130 -0
- package/docs/components/menu.md +1286 -0
- package/docs/components/menubar.md +455 -0
- package/docs/components/meter.md +304 -0
- package/docs/components/navigation-menu.md +1581 -0
- package/docs/components/number-input.md +1086 -0
- package/docs/components/otp-input.md +565 -0
- package/docs/components/pagination.md +648 -0
- package/docs/components/password-input.md +737 -0
- package/docs/components/popover.md +1552 -0
- package/docs/components/preferences.md +220 -0
- package/docs/components/progress.md +443 -0
- package/docs/components/radio.md +846 -0
- package/docs/components/rating.md +458 -0
- package/docs/components/resizable.md +982 -0
- package/docs/components/scroll-area.md +980 -0
- package/docs/components/segmented-control.md +465 -0
- package/docs/components/select.md +2133 -0
- package/docs/components/separator.md +243 -0
- package/docs/components/slider.md +1169 -0
- package/docs/components/switch.md +613 -0
- package/docs/components/tabs.md +1014 -0
- package/docs/components/textarea.md +318 -0
- package/docs/components/time-input.md +231 -0
- package/docs/components/timeline.md +677 -0
- package/docs/components/toast.md +1395 -0
- package/docs/components/toggle.md +541 -0
- package/docs/components/token-input.md +1007 -0
- package/docs/components/toolbar.md +787 -0
- package/docs/components/tooltip.md +884 -0
- package/docs/components/tree.md +1346 -0
- package/docs/components/trigger-menu.md +1257 -0
- package/docs/components/waterfall.md +754 -0
- package/docs/controllers.md +89 -0
- package/docs/core.md +8027 -0
- package/docs/custom-parts.md +120 -0
- package/docs/first-component.md +83 -0
- package/docs/forms.md +148 -0
- package/docs/hooks/async-options.md +366 -0
- package/docs/hooks/shortcuts.md +221 -0
- package/docs/installation.md +45 -0
- package/docs/introduction.md +32 -0
- package/docs/lists-and-menus.md +170 -0
- package/docs/llms.txt +1844 -0
- package/docs/parts-and-composition.md +93 -0
- package/docs/popups.md +117 -0
- package/docs/rendering.md +121 -0
- package/docs/server-rendering.md +253 -0
- package/docs/state-and-events.md +105 -0
- package/docs/styling-and-animation.md +176 -0
- package/package.json +333 -3
|
@@ -0,0 +1,736 @@
|
|
|
1
|
+
# Color Picker
|
|
2
|
+
|
|
3
|
+
Choose a color by dragging across an area and along sliders, typing any CSS color, or picking a preset.
|
|
4
|
+
|
|
5
|
+
Use a color picker to choose a color, like a brand or theme color. The value is a CSS color string, and every part edits it: an area for two channels at once, a slider per channel, a text input that reads any CSS color, and presets.
|
|
6
|
+
|
|
7
|
+
<ComponentExample
|
|
8
|
+
name="color-picker/basic"
|
|
9
|
+
inspect
|
|
10
|
+
caption="Drag across the area or along the sliders, or choose a preset. Choose HSL to write the color another way, or type one like rebeccapurple and press Enter."
|
|
11
|
+
/>
|
|
12
|
+
|
|
13
|
+
## Installation
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
npm install @ui-sdk/react @ui-sdk/core
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Composition
|
|
20
|
+
|
|
21
|
+
Use the following composition to build a `ColorPicker`:
|
|
22
|
+
|
|
23
|
+
```text
|
|
24
|
+
ColorPicker
|
|
25
|
+
├── ColorPickerArea
|
|
26
|
+
│ ├── ColorPickerAreaCanvas
|
|
27
|
+
│ └── ColorPickerAreaThumb
|
|
28
|
+
├── ColorPickerSlider
|
|
29
|
+
│ └── ColorPickerSliderThumb
|
|
30
|
+
├── ColorPickerSlider
|
|
31
|
+
│ └── ColorPickerSliderThumb
|
|
32
|
+
├── ColorPickerSwatch
|
|
33
|
+
├── ColorPickerInput
|
|
34
|
+
├── ColorPickerChannelInput
|
|
35
|
+
├── ColorPickerEyeDropper
|
|
36
|
+
└── ColorPickerSwatches
|
|
37
|
+
├── ColorPickerSwatchItem
|
|
38
|
+
└── ColorPickerSwatchItem
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
Every part is optional, so use the ones your picker needs.
|
|
42
|
+
|
|
43
|
+
- **ColorPicker** holds the value and is a group around the parts.
|
|
44
|
+
- **ColorPickerArea** edits two channels, like saturation across and brightness up. A press moves the thumb there and starts a drag. Its background is `--color-area-background`, so give it a size and that background.
|
|
45
|
+
- **ColorPickerAreaCanvas** paints the area pixel by pixel, for channels no gradient can draw, like OKLCH's chroma and lightness.
|
|
46
|
+
- **ColorPickerAreaThumb** shows where the color sits in the area. It's a slider that screen readers hear as both channels.
|
|
47
|
+
- **ColorPickerSlider** edits one `channel`, like `hue` or `alpha`, drawn by `--color-slider-background`.
|
|
48
|
+
- **ColorPickerSliderThumb** shows where the channel sits along its slider.
|
|
49
|
+
- **ColorPickerSwatch** shows the color, named for screen readers, like "vivid blue".
|
|
50
|
+
- **ColorPickerInput** reads any CSS color when you press Enter or leave it. Text that isn't a color goes back to the value.
|
|
51
|
+
- **ColorPickerChannelInput** edits one `channel` as a number, in the units people read it in, like 0 to 255 for red.
|
|
52
|
+
- **ColorPickerEyeDropper** picks a color from anywhere on the screen, where the browser can.
|
|
53
|
+
- **ColorPickerSwatches** and **ColorPickerSwatchItem** are a list of preset colors.
|
|
54
|
+
|
|
55
|
+
Each part writes the color as `--color-picker-color`, so a thumb or a swatch fills itself with `background: var(--color-picker-color)`. The picker also renders a hidden input that the form submits.
|
|
56
|
+
|
|
57
|
+
## Usage
|
|
58
|
+
|
|
59
|
+
Import the parts and put them together:
|
|
60
|
+
|
|
61
|
+
```tsx
|
|
62
|
+
"use client"
|
|
63
|
+
|
|
64
|
+
import {
|
|
65
|
+
ColorPicker,
|
|
66
|
+
ColorPickerArea,
|
|
67
|
+
ColorPickerAreaThumb,
|
|
68
|
+
ColorPickerInput,
|
|
69
|
+
ColorPickerSlider,
|
|
70
|
+
ColorPickerSliderThumb,
|
|
71
|
+
} from "@ui-sdk/react/color-picker"
|
|
72
|
+
|
|
73
|
+
function BrandColor() {
|
|
74
|
+
return (
|
|
75
|
+
<ColorPicker aria-label="Brand color" defaultValue="#0ea5e9">
|
|
76
|
+
<ColorPickerArea>
|
|
77
|
+
<ColorPickerAreaThumb />
|
|
78
|
+
</ColorPickerArea>
|
|
79
|
+
<ColorPickerSlider channel="hue">
|
|
80
|
+
<ColorPickerSliderThumb />
|
|
81
|
+
</ColorPickerSlider>
|
|
82
|
+
<ColorPickerInput aria-label="Hex" />
|
|
83
|
+
</ColorPicker>
|
|
84
|
+
)
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export { BrandColor }
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
Name the picker with `aria-label`, or put it in a [Field](./field.md) whose label names it. Without `defaultValue`, it starts at black.
|
|
91
|
+
|
|
92
|
+
## Spaces and Channels
|
|
93
|
+
|
|
94
|
+
`space` decides which channels the parts edit, and what the value is written in:
|
|
95
|
+
|
|
96
|
+
| `space` | Area, across and up | Slider | Value written as |
|
|
97
|
+
| ------- | ---------------------- | ------ | ----------------------------- |
|
|
98
|
+
| `hsv` | saturation, brightness | hue | hex, like `#0ea5e9` |
|
|
99
|
+
| `hsl` | saturation, lightness | hue | `hsl(198.63 88.66% 48.43%)` |
|
|
100
|
+
| `srgb` | red, green | blue | `rgb(14 165 233)` |
|
|
101
|
+
| `oklch` | chroma, lightness | hue | `oklch(0.6847 0.1479 237.32)` |
|
|
102
|
+
|
|
103
|
+
Set `format` to write the value another way, like `format="hex"` in any space. Every space has an `alpha` channel too. Set `x` and `y` on the area, or `channel` on a slider, to edit other channels:
|
|
104
|
+
|
|
105
|
+
```tsx
|
|
106
|
+
<ColorPicker
|
|
107
|
+
aria-label="Background"
|
|
108
|
+
space="hsl"
|
|
109
|
+
defaultValue="hsl(220 60% 50%)"
|
|
110
|
+
>
|
|
111
|
+
<ColorPickerArea x="hue" y="lightness">
|
|
112
|
+
<ColorPickerAreaCanvas />
|
|
113
|
+
<ColorPickerAreaThumb />
|
|
114
|
+
</ColorPickerArea>
|
|
115
|
+
<ColorPickerSlider channel="saturation" orientation="vertical">
|
|
116
|
+
<ColorPickerSliderThumb />
|
|
117
|
+
</ColorPickerSlider>
|
|
118
|
+
</ColorPicker>
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
The parts keep a hue while the color turns gray or black, so dragging back out of a corner returns to the same hue.
|
|
122
|
+
|
|
123
|
+
## OKLCH
|
|
124
|
+
|
|
125
|
+
OKLCH spaces its colors evenly for the eye, so two colors with the same lightness look equally bright. Its edge with the colors a screen can show is a curve, which no gradient can draw. Add `ColorPickerAreaCanvas` to the area, and it paints the colors inside that edge and leaves the rest clear.
|
|
126
|
+
|
|
127
|
+
```tsx title="oklch.tsx"
|
|
128
|
+
"use client"
|
|
129
|
+
|
|
130
|
+
import {
|
|
131
|
+
ColorPicker,
|
|
132
|
+
ColorPickerArea,
|
|
133
|
+
ColorPickerAreaCanvas,
|
|
134
|
+
ColorPickerAreaThumb,
|
|
135
|
+
ColorPickerInput,
|
|
136
|
+
ColorPickerSlider,
|
|
137
|
+
ColorPickerSliderThumb,
|
|
138
|
+
ColorPickerSwatch,
|
|
139
|
+
} from "@ui-sdk/react/color-picker"
|
|
140
|
+
|
|
141
|
+
function Example() {
|
|
142
|
+
return (
|
|
143
|
+
<ColorPicker
|
|
144
|
+
aria-label="Accent"
|
|
145
|
+
space="oklch"
|
|
146
|
+
defaultValue="oklch(0.62 0.19 259)"
|
|
147
|
+
>
|
|
148
|
+
<ColorPickerArea>
|
|
149
|
+
<ColorPickerAreaCanvas />
|
|
150
|
+
<ColorPickerAreaThumb />
|
|
151
|
+
</ColorPickerArea>
|
|
152
|
+
<ColorPickerSlider channel="hue">
|
|
153
|
+
<ColorPickerSliderThumb />
|
|
154
|
+
</ColorPickerSlider>
|
|
155
|
+
<ColorPickerSlider channel="alpha">
|
|
156
|
+
<ColorPickerSliderThumb />
|
|
157
|
+
</ColorPickerSlider>
|
|
158
|
+
<div data-slot="row">
|
|
159
|
+
<ColorPickerSwatch />
|
|
160
|
+
<ColorPickerInput aria-label="OKLCH" />
|
|
161
|
+
</div>
|
|
162
|
+
</ColorPicker>
|
|
163
|
+
)
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
export { Example }
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
Drag the thumb right, past the painted edge, and let go. The thumb moves back to the closest color inside.
|
|
170
|
+
|
|
171
|
+
While a drag holds a color past the edge, the value is already the closest color inside it, and every part has `data-out-of-gamut`. Once the drag ends, the thumb moves to that color. Colors stay inside sRGB by default. Set `gamut="p3"` for wide-gamut screens, or `gamut={false}` to allow any color:
|
|
172
|
+
|
|
173
|
+
```tsx
|
|
174
|
+
<ColorPicker
|
|
175
|
+
aria-label="Accent"
|
|
176
|
+
space="oklch"
|
|
177
|
+
gamut="p3"
|
|
178
|
+
defaultValue="oklch(0.7 0.25 150)"
|
|
179
|
+
>
|
|
180
|
+
{/* … */}
|
|
181
|
+
</ColorPicker>
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
## Presets and Channel Inputs
|
|
185
|
+
|
|
186
|
+
Put `ColorPickerSwatchItem`s inside `ColorPickerSwatches` for preset colors. Each one is selected while the value is its color, however either is written, so `#0000ff` selects `rgb(0 0 255)`. The list is one tab stop, and the arrow keys move between the presets.
|
|
187
|
+
|
|
188
|
+
`ColorPickerChannelInput` edits one channel as a number and sets it on Enter, when you leave it, or with its arrows. `ColorPickerEyeDropper` picks from the screen in browsers that can. Elsewhere it's disabled, with `data-unsupported`, so you can hide it.
|
|
189
|
+
|
|
190
|
+
```tsx title="swatches.tsx"
|
|
191
|
+
"use client"
|
|
192
|
+
|
|
193
|
+
import {
|
|
194
|
+
ColorPicker,
|
|
195
|
+
ColorPickerChannelInput,
|
|
196
|
+
ColorPickerEyeDropper,
|
|
197
|
+
ColorPickerSwatches,
|
|
198
|
+
ColorPickerSwatchItem,
|
|
199
|
+
} from "@ui-sdk/react/color-picker"
|
|
200
|
+
import { Pipette } from "lucide-react"
|
|
201
|
+
|
|
202
|
+
const presets = [
|
|
203
|
+
"#ef4444",
|
|
204
|
+
"#f97316",
|
|
205
|
+
"#eab308",
|
|
206
|
+
"#22c55e",
|
|
207
|
+
"#0ea5e9",
|
|
208
|
+
"#6366f1",
|
|
209
|
+
"#a855f7",
|
|
210
|
+
"#ec4899",
|
|
211
|
+
]
|
|
212
|
+
|
|
213
|
+
function Example() {
|
|
214
|
+
return (
|
|
215
|
+
<ColorPicker aria-label="Label color" space="srgb" format="hex" defaultValue="#0ea5e9">
|
|
216
|
+
<ColorPickerSwatches aria-label="Presets">
|
|
217
|
+
{presets.map((preset) => (
|
|
218
|
+
<ColorPickerSwatchItem key={preset} value={preset} />
|
|
219
|
+
))}
|
|
220
|
+
</ColorPickerSwatches>
|
|
221
|
+
<div data-slot="row">
|
|
222
|
+
<ColorPickerChannelInput channel="red" />
|
|
223
|
+
<ColorPickerChannelInput channel="green" />
|
|
224
|
+
<ColorPickerChannelInput channel="blue" />
|
|
225
|
+
<ColorPickerEyeDropper aria-label="Pick from screen">
|
|
226
|
+
<Pipette aria-hidden="true" />
|
|
227
|
+
</ColorPickerEyeDropper>
|
|
228
|
+
</div>
|
|
229
|
+
</ColorPicker>
|
|
230
|
+
)
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
export { Example }
|
|
234
|
+
```
|
|
235
|
+
|
|
236
|
+
Choose a preset, or use the arrow keys between them. Type 255 into the first number to set red.
|
|
237
|
+
|
|
238
|
+
## Popover
|
|
239
|
+
|
|
240
|
+
Put a `Popover` inside the picker to open it from a swatch. The parts inside the popup edit the same color, and the swatch in the trigger shows it.
|
|
241
|
+
|
|
242
|
+
```tsx title="popover.tsx"
|
|
243
|
+
"use client"
|
|
244
|
+
|
|
245
|
+
import {
|
|
246
|
+
ColorPicker,
|
|
247
|
+
ColorPickerArea,
|
|
248
|
+
ColorPickerAreaThumb,
|
|
249
|
+
ColorPickerInput,
|
|
250
|
+
ColorPickerSlider,
|
|
251
|
+
ColorPickerSliderThumb,
|
|
252
|
+
ColorPickerSwatch,
|
|
253
|
+
} from "@ui-sdk/react/color-picker"
|
|
254
|
+
import { Popover, PopoverContent, PopoverTrigger } from "@ui-sdk/react/popover"
|
|
255
|
+
|
|
256
|
+
function Example() {
|
|
257
|
+
return (
|
|
258
|
+
<ColorPicker aria-label="Background" defaultValue="#22c55e">
|
|
259
|
+
<Popover>
|
|
260
|
+
<PopoverTrigger aria-label="Background">
|
|
261
|
+
<ColorPickerSwatch />
|
|
262
|
+
</PopoverTrigger>
|
|
263
|
+
<PopoverContent>
|
|
264
|
+
<ColorPickerArea>
|
|
265
|
+
<ColorPickerAreaThumb />
|
|
266
|
+
</ColorPickerArea>
|
|
267
|
+
<ColorPickerSlider channel="hue">
|
|
268
|
+
<ColorPickerSliderThumb />
|
|
269
|
+
</ColorPickerSlider>
|
|
270
|
+
<ColorPickerInput aria-label="Hex" />
|
|
271
|
+
</PopoverContent>
|
|
272
|
+
</Popover>
|
|
273
|
+
</ColorPicker>
|
|
274
|
+
)
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
export { Example }
|
|
278
|
+
```
|
|
279
|
+
|
|
280
|
+
Press the Background swatch to open the picker, then drag to change the color.
|
|
281
|
+
|
|
282
|
+
## Disabled
|
|
283
|
+
|
|
284
|
+
Set `disabled`. A disabled picker can't be focused or changed, and isn't submitted. A disabled `<fieldset>` around it disables it too.
|
|
285
|
+
|
|
286
|
+
```tsx title="disabled.tsx"
|
|
287
|
+
"use client"
|
|
288
|
+
|
|
289
|
+
import {
|
|
290
|
+
ColorPicker,
|
|
291
|
+
ColorPickerArea,
|
|
292
|
+
ColorPickerAreaThumb,
|
|
293
|
+
ColorPickerInput,
|
|
294
|
+
ColorPickerSlider,
|
|
295
|
+
ColorPickerSliderThumb,
|
|
296
|
+
} from "@ui-sdk/react/color-picker"
|
|
297
|
+
|
|
298
|
+
function Example() {
|
|
299
|
+
return (
|
|
300
|
+
<ColorPicker aria-label="Brand color" defaultValue="#0ea5e9" disabled>
|
|
301
|
+
<ColorPickerArea>
|
|
302
|
+
<ColorPickerAreaThumb />
|
|
303
|
+
</ColorPickerArea>
|
|
304
|
+
<ColorPickerSlider channel="hue">
|
|
305
|
+
<ColorPickerSliderThumb />
|
|
306
|
+
</ColorPickerSlider>
|
|
307
|
+
<ColorPickerInput aria-label="Hex" />
|
|
308
|
+
</ColorPicker>
|
|
309
|
+
)
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
export { Example }
|
|
313
|
+
```
|
|
314
|
+
|
|
315
|
+
The thumbs can't be focused or dragged.
|
|
316
|
+
|
|
317
|
+
Set `readOnly` to keep the parts focusable and the value submitted, while drags, keys and typing leave the color alone:
|
|
318
|
+
|
|
319
|
+
```tsx
|
|
320
|
+
<ColorPicker aria-label="Theme color" defaultValue="#0ea5e9" readOnly>
|
|
321
|
+
<ColorPickerSwatch />
|
|
322
|
+
<ColorPickerInput aria-label="Hex" />
|
|
323
|
+
</ColorPicker>
|
|
324
|
+
```
|
|
325
|
+
|
|
326
|
+
## Controlled
|
|
327
|
+
|
|
328
|
+
Pass `value` and update it in `onValueChange`. Each change carries a [reason](#reasons), like `drag`, `step-key` or `input-commit`, and `details.color`, the color the parts hold, which keeps a hue while the value turns gray. Call `details.cancel()` in the callback to keep the current value.
|
|
329
|
+
|
|
330
|
+
`onValueChange` runs on every step of a drag. `onValueCommit` runs once, when the drag, key press or typed color ends, so use it to save. A controller from `createColorPickerController()` sets the color from anywhere with `setValue()`, like the Use Purple button below.
|
|
331
|
+
|
|
332
|
+
```tsx title="controlled.tsx"
|
|
333
|
+
"use client"
|
|
334
|
+
|
|
335
|
+
import {
|
|
336
|
+
ColorPicker,
|
|
337
|
+
ColorPickerArea,
|
|
338
|
+
ColorPickerAreaThumb,
|
|
339
|
+
ColorPickerInput,
|
|
340
|
+
ColorPickerSlider,
|
|
341
|
+
ColorPickerSliderThumb,
|
|
342
|
+
createColorPickerController,
|
|
343
|
+
} from "@ui-sdk/react/color-picker"
|
|
344
|
+
import * as React from "react"
|
|
345
|
+
import { useLog } from "./log"
|
|
346
|
+
|
|
347
|
+
function Example() {
|
|
348
|
+
const log = useLog()
|
|
349
|
+
const [picker] = React.useState(() => createColorPickerController())
|
|
350
|
+
const [value, setValue] = React.useState("#0ea5e9")
|
|
351
|
+
|
|
352
|
+
return (
|
|
353
|
+
<div>
|
|
354
|
+
<div data-slot="controls">
|
|
355
|
+
<button type="button" onClick={() => picker.setValue("#663399")}>
|
|
356
|
+
Use Purple
|
|
357
|
+
</button>
|
|
358
|
+
</div>
|
|
359
|
+
<ColorPicker
|
|
360
|
+
aria-label="Theme color"
|
|
361
|
+
value={value}
|
|
362
|
+
onValueChange={(next) => setValue(next)}
|
|
363
|
+
onValueCommit={(next, details) =>
|
|
364
|
+
log(`committed: ${next} (${details.reason})`)
|
|
365
|
+
}
|
|
366
|
+
controller={picker}
|
|
367
|
+
>
|
|
368
|
+
<ColorPickerArea>
|
|
369
|
+
<ColorPickerAreaThumb />
|
|
370
|
+
</ColorPickerArea>
|
|
371
|
+
<ColorPickerSlider channel="hue">
|
|
372
|
+
<ColorPickerSliderThumb />
|
|
373
|
+
</ColorPickerSlider>
|
|
374
|
+
<ColorPickerInput aria-label="Hex" />
|
|
375
|
+
</ColorPicker>
|
|
376
|
+
</div>
|
|
377
|
+
)
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
export { Example }
|
|
381
|
+
```
|
|
382
|
+
|
|
383
|
+
Drag across the area and let go, or press Use Purple. The console logs each committed color and its reason.
|
|
384
|
+
|
|
385
|
+
See [State and Events](../state-and-events.md) for controlled state, cancellation and debugging.
|
|
386
|
+
|
|
387
|
+
## Form
|
|
388
|
+
|
|
389
|
+
The hidden input submits the value under `name`, like `#f97316`, and a disabled picker submits nothing. A form reset returns it to `defaultValue`. Set `form` to join a form by its id.
|
|
390
|
+
|
|
391
|
+
```tsx title="form.tsx"
|
|
392
|
+
"use client"
|
|
393
|
+
|
|
394
|
+
import {
|
|
395
|
+
ColorPicker,
|
|
396
|
+
ColorPickerArea,
|
|
397
|
+
ColorPickerAreaThumb,
|
|
398
|
+
ColorPickerSlider,
|
|
399
|
+
ColorPickerSliderThumb,
|
|
400
|
+
ColorPickerSwatch,
|
|
401
|
+
} from "@ui-sdk/react/color-picker"
|
|
402
|
+
import { Field, FieldLabel } from "@ui-sdk/react/field"
|
|
403
|
+
import { useLog } from "./log"
|
|
404
|
+
|
|
405
|
+
function Example() {
|
|
406
|
+
const log = useLog()
|
|
407
|
+
|
|
408
|
+
return (
|
|
409
|
+
<form
|
|
410
|
+
data-slot="stack"
|
|
411
|
+
onSubmit={(event) => {
|
|
412
|
+
event.preventDefault()
|
|
413
|
+
log(`Submitted brand=${new FormData(event.currentTarget).get("brand")}`)
|
|
414
|
+
}}
|
|
415
|
+
>
|
|
416
|
+
<Field>
|
|
417
|
+
<FieldLabel>Brand color</FieldLabel>
|
|
418
|
+
<ColorPicker name="brand" defaultValue="#f97316">
|
|
419
|
+
<ColorPickerArea>
|
|
420
|
+
<ColorPickerAreaThumb />
|
|
421
|
+
</ColorPickerArea>
|
|
422
|
+
<div data-slot="row">
|
|
423
|
+
<ColorPickerSwatch />
|
|
424
|
+
<ColorPickerSlider channel="hue">
|
|
425
|
+
<ColorPickerSliderThumb />
|
|
426
|
+
</ColorPickerSlider>
|
|
427
|
+
</div>
|
|
428
|
+
</ColorPicker>
|
|
429
|
+
</Field>
|
|
430
|
+
<div data-slot="row">
|
|
431
|
+
<button type="submit" data-variant="solid">
|
|
432
|
+
Save
|
|
433
|
+
</button>
|
|
434
|
+
<button type="reset">Reset</button>
|
|
435
|
+
</div>
|
|
436
|
+
</form>
|
|
437
|
+
)
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
export { Example }
|
|
441
|
+
```
|
|
442
|
+
|
|
443
|
+
Change the color and press Save to log the submitted value. Reset restores the orange.
|
|
444
|
+
|
|
445
|
+
A color picker always has a value, so `required` only adds `data-required`.
|
|
446
|
+
|
|
447
|
+
## Custom Rendering
|
|
448
|
+
|
|
449
|
+
Every part takes `render`, to render a different element or component in its place. `render`, `className` and `children` can be functions of the part's state. A slider thumb's state has its `channelValue`, a preset's has whether it's `selected`, and every part has the `color` and whether it's `outOfGamut`:
|
|
450
|
+
|
|
451
|
+
```tsx
|
|
452
|
+
<ColorPickerSlider channel="alpha">
|
|
453
|
+
<ColorPickerSliderThumb className={({ outOfGamut }) => (outOfGamut ? "dashed" : undefined)}>
|
|
454
|
+
{({ channelValue }) => <span>{Math.round(channelValue)}%</span>}
|
|
455
|
+
</ColorPickerSliderThumb>
|
|
456
|
+
</ColorPickerSlider>
|
|
457
|
+
<ColorPickerSwatch>{({ color }) => (color.alpha < 1 ? <span>Transparent</span> : null)}</ColorPickerSwatch>
|
|
458
|
+
```
|
|
459
|
+
|
|
460
|
+
See [Parts and Composition](../parts-and-composition.md) for the rendering contract and [State](#state) for each part's fields.
|
|
461
|
+
|
|
462
|
+
## Styling
|
|
463
|
+
|
|
464
|
+
Parts are unstyled, apart from the positions they need: thumbs get `position: absolute` and their place in percent, as inline styles. Give the area and sliders a size and their background variable, and center each thumb on its point with `translate: -50% -50%`.
|
|
465
|
+
|
|
466
|
+
Each part has a `data-slot`, such as `color-picker-area`, and its state attributes are listed under [Data Attributes](#data-attributes).
|
|
467
|
+
|
|
468
|
+
### ColorPicker
|
|
469
|
+
- `--color-picker-color`: The color as CSS, inside the gamut. On a swatch item, its own color. Use it to fill a thumb or a swatch.
|
|
470
|
+
|
|
471
|
+
### ColorPickerArea
|
|
472
|
+
- `--color-area-background`: Gradients that draw the area, when its channels are saturation and brightness or lightness. Unset for others, which a canvas paints.
|
|
473
|
+
|
|
474
|
+
### ColorPickerAreaThumb
|
|
475
|
+
- `--area-x`: Where the thumb is across the area, from 0 to 1.
|
|
476
|
+
- `--area-y`: Where the thumb is up the area, from 0 at the bottom to 1 at the top.
|
|
477
|
+
|
|
478
|
+
### ColorPickerSlider
|
|
479
|
+
- `--color-slider-background`: A gradient through the slider's channel, with the other channels as they are.
|
|
480
|
+
|
|
481
|
+
### ColorPickerSliderThumb
|
|
482
|
+
- `--color-slider-thumb-position`: Where the thumb is along the slider, as a percentage.
|
|
483
|
+
|
|
484
|
+
### ColorPickerSwatch
|
|
485
|
+
- `--color-picker-color`: The color as CSS, inside the gamut. On a swatch item, its own color. Use it to fill a thumb or a swatch.
|
|
486
|
+
|
|
487
|
+
### ColorPickerSwatchItem
|
|
488
|
+
- `--color-picker-color`: The color as CSS, inside the gamut. On a swatch item, its own color. Use it to fill a thumb or a swatch.
|
|
489
|
+
|
|
490
|
+
## Data Attributes
|
|
491
|
+
|
|
492
|
+
### ColorPicker
|
|
493
|
+
`[data-slot="color-picker"]`
|
|
494
|
+
|
|
495
|
+
- `data-disabled`: Present when the picker is disabled, by its own prop or a `<fieldset>`.
|
|
496
|
+
- `data-readonly`: Present when the picker shows its color but can't change it.
|
|
497
|
+
- `data-required`: Present when the picker is required.
|
|
498
|
+
- `data-out-of-gamut`: Present while a drag or a key holds a color outside the gamut. The value is the closest color inside, and the parts move there once the change commits.
|
|
499
|
+
|
|
500
|
+
### ColorPickerArea
|
|
501
|
+
`[data-slot="color-picker-area"]`
|
|
502
|
+
|
|
503
|
+
- `data-disabled`: Present when the picker is disabled, by its own prop or a `<fieldset>`.
|
|
504
|
+
- `data-readonly`: Present when the picker shows its color but can't change it.
|
|
505
|
+
- `data-required`: Present when the picker is required.
|
|
506
|
+
- `data-out-of-gamut`: Present while a drag or a key holds a color outside the gamut. The value is the closest color inside, and the parts move there once the change commits.
|
|
507
|
+
|
|
508
|
+
### ColorPickerAreaThumb
|
|
509
|
+
`[data-slot="color-picker-area-thumb"]`
|
|
510
|
+
|
|
511
|
+
- `data-area-thumb`: Present on the area's thumb, which a press on the area moves and focuses.
|
|
512
|
+
- `data-disabled`: Present when the picker is disabled, by its own prop or a `<fieldset>`.
|
|
513
|
+
- `data-readonly`: Present when the picker shows its color but can't change it.
|
|
514
|
+
- `data-required`: Present when the picker is required.
|
|
515
|
+
- `data-out-of-gamut`: Present while a drag or a key holds a color outside the gamut. The value is the closest color inside, and the parts move there once the change commits.
|
|
516
|
+
|
|
517
|
+
### ColorPickerSlider
|
|
518
|
+
`[data-slot="color-picker-slider"]`
|
|
519
|
+
|
|
520
|
+
- `data-disabled`: Present when the picker is disabled, by its own prop or a `<fieldset>`.
|
|
521
|
+
- `data-readonly`: Present when the picker shows its color but can't change it.
|
|
522
|
+
- `data-required`: Present when the picker is required.
|
|
523
|
+
- `data-orientation` (`horizontal`, `vertical`): The slider's axis.
|
|
524
|
+
- `data-channel` (`hue`, `saturation`, `brightness`, `lightness`, `chroma`, `red`, `green`, `blue`, `alpha`): The channel the part edits.
|
|
525
|
+
- `data-out-of-gamut`: Present while a drag or a key holds a color outside the gamut. The value is the closest color inside, and the parts move there once the change commits.
|
|
526
|
+
|
|
527
|
+
### ColorPickerSliderThumb
|
|
528
|
+
`[data-slot="color-picker-slider-thumb"]`
|
|
529
|
+
|
|
530
|
+
- `data-disabled`: Present when the picker is disabled, by its own prop or a `<fieldset>`.
|
|
531
|
+
- `data-readonly`: Present when the picker shows its color but can't change it.
|
|
532
|
+
- `data-required`: Present when the picker is required.
|
|
533
|
+
- `data-orientation` (`horizontal`, `vertical`): The slider's axis.
|
|
534
|
+
- `data-channel` (`hue`, `saturation`, `brightness`, `lightness`, `chroma`, `red`, `green`, `blue`, `alpha`): The channel the part edits.
|
|
535
|
+
- `data-out-of-gamut`: Present while a drag or a key holds a color outside the gamut. The value is the closest color inside, and the parts move there once the change commits.
|
|
536
|
+
|
|
537
|
+
### ColorPickerInput
|
|
538
|
+
`[data-slot="color-picker-input"]`
|
|
539
|
+
|
|
540
|
+
- `data-disabled`: Present when the picker is disabled, by its own prop or a `<fieldset>`.
|
|
541
|
+
- `data-readonly`: Present when the picker shows its color but can't change it.
|
|
542
|
+
- `data-required`: Present when the picker is required.
|
|
543
|
+
- `data-out-of-gamut`: Present while a drag or a key holds a color outside the gamut. The value is the closest color inside, and the parts move there once the change commits.
|
|
544
|
+
|
|
545
|
+
### ColorPickerChannelInput
|
|
546
|
+
`[data-slot="color-picker-channel-input"]`
|
|
547
|
+
|
|
548
|
+
- `data-disabled`: Present when the picker is disabled, by its own prop or a `<fieldset>`.
|
|
549
|
+
- `data-readonly`: Present when the picker shows its color but can't change it.
|
|
550
|
+
- `data-required`: Present when the picker is required.
|
|
551
|
+
- `data-channel` (`hue`, `saturation`, `brightness`, `lightness`, `chroma`, `red`, `green`, `blue`, `alpha`): The channel the part edits.
|
|
552
|
+
- `data-out-of-gamut`: Present while a drag or a key holds a color outside the gamut. The value is the closest color inside, and the parts move there once the change commits.
|
|
553
|
+
|
|
554
|
+
### ColorPickerSwatch
|
|
555
|
+
`[data-slot="color-picker-swatch"]`
|
|
556
|
+
|
|
557
|
+
- `data-disabled`: Present when the picker is disabled, by its own prop or a `<fieldset>`.
|
|
558
|
+
- `data-readonly`: Present when the picker shows its color but can't change it.
|
|
559
|
+
- `data-required`: Present when the picker is required.
|
|
560
|
+
- `data-out-of-gamut`: Present while a drag or a key holds a color outside the gamut. The value is the closest color inside, and the parts move there once the change commits.
|
|
561
|
+
|
|
562
|
+
### ColorPickerEyeDropper
|
|
563
|
+
`[data-slot="color-picker-eye-dropper"]`
|
|
564
|
+
|
|
565
|
+
- `data-disabled`: Present when the picker is disabled, by its own prop or a `<fieldset>`.
|
|
566
|
+
- `data-readonly`: Present when the picker shows its color but can't change it.
|
|
567
|
+
- `data-required`: Present when the picker is required.
|
|
568
|
+
- `data-out-of-gamut`: Present while a drag or a key holds a color outside the gamut. The value is the closest color inside, and the parts move there once the change commits.
|
|
569
|
+
- `data-unsupported`: Present when the browser can't pick a color from the screen.
|
|
570
|
+
|
|
571
|
+
### ColorPickerSwatches
|
|
572
|
+
`[data-slot="color-picker-swatches"]`
|
|
573
|
+
|
|
574
|
+
- `data-disabled`: Present when the picker is disabled, by its own prop or a `<fieldset>`.
|
|
575
|
+
- `data-readonly`: Present when the picker shows its color but can't change it.
|
|
576
|
+
- `data-required`: Present when the picker is required.
|
|
577
|
+
- `data-out-of-gamut`: Present while a drag or a key holds a color outside the gamut. The value is the closest color inside, and the parts move there once the change commits.
|
|
578
|
+
|
|
579
|
+
### ColorPickerSwatchItem
|
|
580
|
+
`[data-slot="color-picker-swatch-item"]`
|
|
581
|
+
|
|
582
|
+
- `data-selected`: Present on the swatch item whose color is the value.
|
|
583
|
+
- `data-disabled`: Present when the picker is disabled, by its own prop or a `<fieldset>`.
|
|
584
|
+
- `data-readonly`: Present when the picker shows its color but can't change it.
|
|
585
|
+
|
|
586
|
+
## API Reference
|
|
587
|
+
|
|
588
|
+
Props and callbacks are the same as below, as React props.
|
|
589
|
+
|
|
590
|
+
### ColorPicker
|
|
591
|
+
|
|
592
|
+
- `value?` (`string`): The value, as a CSS color, like `"#0ea5e9"` or `"oklch(0.62 0.19 259)"`. Pass it to control the picker.
|
|
593
|
+
- `defaultValue?` (`string`): The value at first, when uncontrolled. A form reset returns to it.
|
|
594
|
+
- `onValueChange?` (`((value: string, details: ColorPickerValueChangeDetails) => void)`): Runs while the value changes: on every step of a drag or a key. `details.color` is the color, and `details.cancel()` stops it.
|
|
595
|
+
- `onValueCommit?` (`((value: string, details: ColorPickerValueCommitDetails) => void)`): Runs once an interaction that changed the value ends: the pointer lifts, a key is pressed, a swatch is picked. For saving.
|
|
596
|
+
- `space?` (`ColorPickerSpace`): The space the parts edit in, which decides their channels: `"hsv"` has hue, saturation and brightness, `"hsl"` hue, saturation and lightness, `"srgb"` red, green and blue, `"oklch"` lightness, chroma and hue.
|
|
597
|
+
- `format?` (`ColorFormat`): How the value is written: `"hex"`, or a space in its CSS syntax, like `"oklch"`.
|
|
598
|
+
- `gamut?` (`false | ColorGamut`): The colors the value can hold. A drag can go past the edge, and the value is the closest color inside it; once the drag ends, the picker moves there too. `"p3"` allows wide-gamut screens' colors, and `false` any color.
|
|
599
|
+
- `disabled?` (`boolean`): Disables every part: they can't be focused or changed, and the value isn't submitted.
|
|
600
|
+
- `readOnly?` (`boolean`): Keeps the parts focusable and the value submitted, but stops changes.
|
|
601
|
+
- `required?` (`boolean`): Marks it required, with `data-required`. A color picker always has a value, so it never blocks a form.
|
|
602
|
+
- `name?` (`string`): The name the value submits under. Without one, the picker submits nothing.
|
|
603
|
+
- `form?` (`string`): The id of a form to belong to, when it isn't inside that form.
|
|
604
|
+
- `controller?` (`{ getValue: () => string; setValue: (value: string, options?: ColorPickerChangeOptions) => boolean; focus: () => void; }`): Sets the value from anywhere. Create one with `createColorPickerController()`.
|
|
605
|
+
- `debug?` (`boolean`): Logs every state change with its reason to the console, in development builds.
|
|
606
|
+
|
|
607
|
+
### ColorPickerArea
|
|
608
|
+
|
|
609
|
+
- `x?` (`ColorChannel`): The channel across, left to right.
|
|
610
|
+
- `y?` (`ColorChannel`): The channel up, bottom to top.
|
|
611
|
+
|
|
612
|
+
### ColorPickerSlider
|
|
613
|
+
|
|
614
|
+
- `channel?` (`ColorChannel`): The channel the slider edits, like `"hue"` or `"alpha"`.
|
|
615
|
+
- `orientation?` (`"horizontal" | "vertical"`): The slider's axis. Vertical sliders grow upward.
|
|
616
|
+
|
|
617
|
+
### ColorPickerChannelInput
|
|
618
|
+
|
|
619
|
+
- `channel?` (`ColorChannel`): The channel the input edits, like `"hue"` or `"alpha"`.
|
|
620
|
+
|
|
621
|
+
### ColorPickerSwatchItem
|
|
622
|
+
|
|
623
|
+
- `value` (`string`): The color it picks, as any CSS color.
|
|
624
|
+
|
|
625
|
+
### State
|
|
626
|
+
|
|
627
|
+
What render functions, state-based class names and slots receive. Every part gets the picker's state.
|
|
628
|
+
|
|
629
|
+
- `value` (`string`): The value, as a CSS color.
|
|
630
|
+
- `color` (`Color`): The color the parts show, in the picker's space. During a drag it can be outside the gamut.
|
|
631
|
+
- `space` (`ColorPickerSpace`): The space the parts edit in.
|
|
632
|
+
- `disabled` (`boolean`): Whether it's disabled, by itself or by a `<fieldset>`.
|
|
633
|
+
- `readOnly` (`boolean`): Whether it's read-only: the parts stay focusable, but nothing changes the value.
|
|
634
|
+
- `required` (`boolean`): Whether it's marked required.
|
|
635
|
+
- `outOfGamut` (`boolean`): Whether the color the parts show is outside the gamut, so the value is the closest one inside.
|
|
636
|
+
|
|
637
|
+
Sliders and their thumbs add their channel:
|
|
638
|
+
|
|
639
|
+
- `value` (`string`): The value, as a CSS color.
|
|
640
|
+
- `color` (`Color`): The color the parts show, in the picker's space. During a drag it can be outside the gamut.
|
|
641
|
+
- `space` (`ColorPickerSpace`): The space the parts edit in.
|
|
642
|
+
- `disabled` (`boolean`): Whether it's disabled, by itself or by a `<fieldset>`.
|
|
643
|
+
- `readOnly` (`boolean`): Whether it's read-only: the parts stay focusable, but nothing changes the value.
|
|
644
|
+
- `required` (`boolean`): Whether it's marked required.
|
|
645
|
+
- `outOfGamut` (`boolean`): Whether the color the parts show is outside the gamut, so the value is the closest one inside.
|
|
646
|
+
- `channel` (`ColorChannel`): The channel the slider edits.
|
|
647
|
+
- `channelValue` (`number`): The channel's value, in the units people read it in: degrees, percentages, 0 to 255 for red.
|
|
648
|
+
- `orientation` (`"horizontal" | "vertical"`): The slider's axis.
|
|
649
|
+
|
|
650
|
+
Preset items have their own:
|
|
651
|
+
|
|
652
|
+
- `selected` (`boolean`): Whether the picker's color looks the same as this swatch's, whatever each is written in.
|
|
653
|
+
- `disabled` (`boolean`): Whether pressing it changes nothing, since the picker is disabled or read-only.
|
|
654
|
+
|
|
655
|
+
### Reasons
|
|
656
|
+
|
|
657
|
+
`details.reason` says why the value changed.
|
|
658
|
+
|
|
659
|
+
- `track-press` (`PointerEvent`): A press on a slider's track or an area's surface moved the nearest thumb there.
|
|
660
|
+
- `drag` (`PointerEvent`): A slider thumb or a resize handle was dragged, or an item was dragged to a new place in a list.
|
|
661
|
+
- `step-key` (`KeyboardEvent`): Arrow, Page Up, Page Down, Home or End stepped a slider or a number input.
|
|
662
|
+
- `input-commit` (`KeyboardEvent | FocusEvent`): Enter, or leaving a number input, committed what was typed: a number input parses, clamps and snaps it to the step, and an autocomplete closes its suggestions on it.
|
|
663
|
+
- `item-press` (`MouseEvent | KeyboardEvent`): A member of a collection was chosen: a menu item, a tab, a radio or a checkbox item.
|
|
664
|
+
- `eye-dropper-pick`
|
|
665
|
+
- `reset` (`none`): A form reset.
|
|
666
|
+
- `restore` (`none`): The browser restored state, like after going back, or a saved value was read back from storage.
|
|
667
|
+
- `imperative` (`none`): A controller or `api` call, or a property set on a web component.
|
|
668
|
+
|
|
669
|
+
### Controller
|
|
670
|
+
|
|
671
|
+
Create one with `createColorPickerController()` and pass it as `controller`.
|
|
672
|
+
|
|
673
|
+
`setValue()` takes any CSS color and an optional `{ reason, event }`, runs the change callback like a drag, and returns `false` when the color can't be read or the change is canceled.
|
|
674
|
+
|
|
675
|
+
- `getValue()` (`() => string`): The value, as a CSS color.
|
|
676
|
+
- `setValue(value, options?)` (`(value: string, options?: ColorPickerChangeOptions | undefined) => boolean`): Sets the value from any CSS color, as a user would. Returns false for text that isn't one.
|
|
677
|
+
- `focus()` (`() => void`): Moves focus to the area's thumb, or the first slider's.
|
|
678
|
+
|
|
679
|
+
### Events
|
|
680
|
+
|
|
681
|
+
Callbacks get the new value first, then the details.
|
|
682
|
+
|
|
683
|
+
| Prop | On | Receives |
|
|
684
|
+
| --------------- | ------------- | --------------------------------------------------------------------------------------- |
|
|
685
|
+
| `onValueChange` | `ColorPicker` | `(value, details)`: the new value, the [reason](#reasons) and `details.color` |
|
|
686
|
+
| `onValueCommit` | `ColorPicker` | `(value, details)`: the value once the interaction ends, its reason and `details.color` |
|
|
687
|
+
|
|
688
|
+
## Accessibility
|
|
689
|
+
|
|
690
|
+
No WAI-ARIA pattern covers a color picker, so it's built from ones that do. The picker is a group. The area's thumb and each slider's thumb follow the [WAI-ARIA slider pattern](https://www.w3.org/WAI/ARIA/apg/patterns/slider/), and the area's thumb names both of its channels, like "Saturation 76%, Brightness 96%". The presets follow the [listbox pattern](https://www.w3.org/WAI/ARIA/apg/patterns/listbox/), and each one is named by its color, like "vivid red", as is `ColorPickerSwatch`.
|
|
691
|
+
|
|
692
|
+
The thumbs, inputs and presets are named in English by default. Pass `aria-label` to name them in another language.
|
|
693
|
+
|
|
694
|
+
### Keyboard
|
|
695
|
+
|
|
696
|
+
| Key | Does |
|
|
697
|
+
| -------------------- | --------------------------------------------------------------------------------------------- |
|
|
698
|
+
| Arrow keys | Move a thumb one step. On the area, Left and Right move across, Up and Down move up and down. |
|
|
699
|
+
| Shift with an arrow | Moves ten steps |
|
|
700
|
+
| Page Up or Page Down | Moves a tenth of the range, up the area or along a slider |
|
|
701
|
+
| Home or End | Moves to the start or end, across the area or along a slider |
|
|
702
|
+
| Enter | Reads the text in the input, or picks the focused preset |
|
|
703
|
+
| Escape | Puts the input's text back to the value |
|
|
704
|
+
|
|
705
|
+
Each key press commits, like the end of a drag.
|
|
706
|
+
|
|
707
|
+
### Tested Behavior
|
|
708
|
+
|
|
709
|
+
Every line below is an automated test.
|
|
710
|
+
|
|
711
|
+
**Value**
|
|
712
|
+
|
|
713
|
+
- The parts show the default value.
|
|
714
|
+
- A press on the area moves the color there, and commits when it lifts.
|
|
715
|
+
- A drag along a slider follows the pointer and commits once.
|
|
716
|
+
- The text input reads any CSS color on Enter, and text that isn't one goes back.
|
|
717
|
+
- A channel input sets its channel in its own units.
|
|
718
|
+
- A controller sets the value, and a canceled change keeps it.
|
|
719
|
+
|
|
720
|
+
**Keyboard**
|
|
721
|
+
|
|
722
|
+
- Arrows step a slider, Shift ten steps, and each press commits.
|
|
723
|
+
- Arrows move the area's thumb on both axes.
|
|
724
|
+
- Swatches are one tab stop, arrows move between them, and a press picks one.
|
|
725
|
+
|
|
726
|
+
**Forms**
|
|
727
|
+
|
|
728
|
+
- The value submits under its name, and a reset returns to the default.
|
|
729
|
+
- A disabled picker can't be focused or changed, and submits nothing.
|
|
730
|
+
|
|
731
|
+
**Gamut**
|
|
732
|
+
|
|
733
|
+
- In OKLCH, a color past the sRGB edge writes the closest one inside, and the thumb moves there once it lifts.
|
|
734
|
+
- The canvas paints the area, with the colors outside the gamut left clear.
|
|
735
|
+
|
|
736
|
+
Runs in React, Vue and web components.
|