You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feat: make the color picker a form-associated custom element
Make the color picker a form-associated custom element via the ElementInternals API. This allows the color picker to participate in forms as a form control and contribute a form value on form submission. Additionally, on form reset, the color picker's form value is reset to the value of the `value` content attribute. The `value` content attribute, `defaultValue` IDL attribute, and `value` IDL attribute are implemented to match the behavior of native HTML form controls.
Add the `value` IDL attribute. The getter returns a string formatted as a CSS RGB color (e.g. `'rgb(127.5 0 255 / 0.8)'`). This is also the value used in form submission. The setter accepts any valid CSS color string (including named colors) or any of the color picker's internal color object formats (e.g. `{ r: 127.5, g: 0, b: 255, a: 0.8 }`).
Add the `defaultValue` IDL and `value` content attribute. Sets the default value used on form reset. Also sets the `value` IDL attribute as long as the user hasn't change the color.
Add the `disabled` IDL and content attribute. Disabling the color picker will render it completely inert. All containing form controls and buttons will be disabled and the color picker thumb won't respond to interactions. An ancestor `fieldset` element which is disabled will also render the color picker disabled.
Add the `readOnly` IDL and `readonly` content attribute. Setting the color picker to read-only will prevent the user from changing the color.
Add the `required` IDL and content attribute.
Add the `input` and `change` events. They are fired whenever the user changes the color. Note, that there is currently no scenario in which only the `input` but not the `change` event is fired.
BREAKING CHANGE: Remove the content attribute `color`. **How to update**: Use the `value` content attribute or the `defaultValue` IDL attribute instead.
BREAKING CHANGE: Remove the IDL attribute `color`. **How to update**: Use the `value` IDL attribute instead.
BREAKING CHANGE: Remove the IDL attribute `colors`. **How to update**: Use the event data of the `color-change` event instead.
This package’s files are distributed in the ES module format and have not been transpiled. It uses [lit-html](https://www.npmjs.com/package/lit-html).
7
+
- Form-associated custom element (i.e. it works in forms via the [ElementInternals API](https://developer.mozilla.org/en-US/docs/Web/API/ElementInternals))
8
+
- Distributed is ES module format
9
+
- Not transpiled
10
+
- Uses [lit-html](https://www.npmjs.com/package/lit-html) (as a peer dependency)
8
11
9
12
Links:
10
13
@@ -21,17 +24,23 @@ Links:
21
24
- [As npm package](#as-npm-package)
22
25
- [As plain files directly in the browser (no build step)](#as-plain-files-directly-in-the-browser-no-build-step)
1. Define an [import map](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script/type/importmap) for lit-html.
@@ -190,19 +199,19 @@ Links:
190
199
191
200
## Documentation
192
201
193
-
### Properties
202
+
### Attributes
194
203
195
-
Most of the following properties can also be set via its corresponding attribute.
204
+
In the following sections, the web component's [IDL and content attibutes](https://developer.mozilla.org/en-US/docs/Glossary/IDL#content_versus_idl_attributes), are documented.
196
205
197
-
**Note**: Changing an attribute will be synced to its corresponding property; however, changing a property will *not* be synced to its corresponding attribute.
206
+
**Note**: Setting a content attribute will be reflected by its corresponding IDL attribute; however, setting an IDL attribute will *not* be reflected by its corresponding content attribute.
198
207
199
208
#### `alphaChannel`
200
209
201
-
-**Description**: Whether to show input controls for a color’s alpha channel. If set to `'hide'`, the alpha range input and the alpha channel input are hidden, the “Copy color” button will copy a CSS color value without alpha channel, and the object emitted in a `color-change` event will have a `cssColor` property value without alpha channel.
210
+
-**Description**: Whether to show controls for a color’s alpha channel. If set to `'hide'`, the alpha range input and the alpha channel input are hidden, the “Copy color” button will copy a CSS color value without alpha channel, and the object emitted in a `color-change` event will have a `cssColor` property value without alpha channel.
202
211
-**Type**: `'show' | 'hide'`
203
212
-**Required**: `false`
204
213
-**Default**: `'show'`
205
-
-**Attribute**: `alpha-channel`
214
+
-**Content attribute**: `alpha-channel`
206
215
-**Usage**:
207
216
208
217
JavaScript:
@@ -215,33 +224,47 @@ Most of the following properties can also be set via its corresponding attribute
@@ -250,7 +273,7 @@ Most of the following properties can also be set via its corresponding attribute
250
273
-**Type**: `VisibleColorFormat`
251
274
-**Required**: `false`
252
275
-**Default**: `'hsl'`
253
-
-**Attribute**: `format`
276
+
-**Content attribute**: `format`
254
277
-**Usage**:
255
278
256
279
JavaScript:
@@ -265,11 +288,14 @@ Most of the following properties can also be set via its corresponding attribute
265
288
266
289
#### `id`
267
290
268
-
-**Description**: The ID value will be used to prefix all `input` elements’ `id` and `label` elements’ `for` attribute values. Make sure to set this if you use multiple instances of the component on a page.
291
+
-**Description**: This value will be used to prefix any of the color picker’s form-associated elements’ `id` and `for` attribute values. Make sure to set this if you use multiple instances of the component on a page.
292
+
293
+
**Note**: The IDL attribute `id` of form-associated elements _is_ reflected by its content attribute.
294
+
269
295
-**Type**: `string`
270
296
-**Required**: `false`
271
297
-**Default**: `'color-picker'`
272
-
-**Attribute**: `id`
298
+
-**Content attribute**: `id`
273
299
-**Usage**:
274
300
275
301
JavaScript:
@@ -282,13 +308,80 @@ Most of the following properties can also be set via its corresponding attribute
282
308
<color-picker id="color-picker-1"></color-picker>
283
309
```
284
310
311
+
#### `name`
312
+
313
+
-**Description**: Name of the color picker (used when the color picker is part of a form).
314
+
315
+
**Note**: The IDL attribute `name` of form-associated elements _is_ reflected by its content attribute.
316
+
317
+
-**Type**: `string`
318
+
-**Required**: `false`
319
+
-**Default**: `''`
320
+
-**Content attribute**: `name`
321
+
-**Usage**:
322
+
323
+
JavaScript:
324
+
```js
325
+
colorPicker.name = 'color-picker'
326
+
```
327
+
328
+
HTML:
329
+
```html
330
+
<color-picker name="color-picker"></color-picker>
331
+
```
332
+
333
+
#### `readOnly`
334
+
335
+
-**Description**: Makes the color picker read-only.
336
+
-**Type**: `boolean`
337
+
-**Required**: `false`
338
+
-**Default**: `false`
339
+
-**Content attribute**: `readonly`
340
+
-**Usage**:
341
+
342
+
JavaScript:
343
+
```js
344
+
colorPicker.readonly = true
345
+
```
346
+
347
+
HTML:
348
+
```html
349
+
<color-picker readonly></color-picker>
350
+
```
351
+
352
+
#### `value`
353
+
354
+
-**Description**: The current color of the color picker.
355
+
356
+
The `value` getter will return the current color as a string (formatted as a CSS RGB color, e.g. `'rgb(127.5 0 255 / 0.8)'`). This is also the form value used in form submission.
357
+
358
+
The `value` setter accepts a `string` (any CSS color works, e.g. `'hsl(270 100% 50% / 0.8)'`) or an `object` (e.g. `{ h: 270, s: 100, l: 50, a: 0.8 }`).
359
+
360
+
-**Type**: `string | ColorHsl | ColorHwb | ColorRgb` (for `string`, any valid CSS color string will work)
361
+
-**Required**: `false`
362
+
-**Default**: `'rgb(255 255 255 / 1)'`
363
+
-**Content attribute**: None. The `value` IDL attribute doesn't directly reflect a content attribute. However, the `value` IDL attribute does reflect the `defaultValue` IDL attribute as long as the dirty flag isn't set.
-**Description**: A list of visible color formats. Controls for which formats the color `input` elements are shown and in which order the formats will be cycled through when activating the format switch button.
288
381
-**Type**: `VisibleColorFormat` (an array of `VisibleColorFormat`s)
289
382
-**Required**: `false`
290
383
-**Default**: `['hex', 'hsl', 'hwb', 'rgb']`
291
-
-**Attribute**: `visible-formats`
384
+
-**Content attribute**: `visible-formats`
292
385
-**Usage**:
293
386
294
387
JavaScript:
@@ -331,18 +424,22 @@ Most of the following properties can also be set via its corresponding attribute
331
424
332
425
### Events
333
426
427
+
#### `change`
428
+
429
+
-**Description**: The `change` event is fired when the color is changed.
430
+
-**Type**: `Event`
431
+
334
432
#### `color-change`
335
433
336
-
-**Description**: The custom event that is emitted each time the internal colors object is updated.
434
+
-**Description**: The `color-change` event is fired when the color is changed.
337
435
-**Type**: `CustomEvent<ColorChangeDetail>`
338
-
-**Data**: The custom event emits an object whose `detail` property contains both the internal colors object and a CSS color value as a string based on the currently active format. The `cssColor` property will respect `alpha-channel`.
436
+
-**Data**: Emits an object whose `detail` property contains both the internal `colors` object and a CSS color value as a string based on the currently active format. The `cssColor` property respects the `alphaChannel` IDL attribute.
339
437
340
438
```ts
341
439
{
342
440
colors: {
343
441
hex: string
344
442
hsl: ColorHsl
345
-
hsv: ColorHsv
346
443
hwb: ColorHwb
347
444
rgb: ColorRgb
348
445
}
@@ -367,6 +464,11 @@ Most of the following properties can also be set via its corresponding attribute
367
464
})
368
465
```
369
466
467
+
#### `input`
468
+
469
+
-**Description**: The `input` event is fired when the color is changed.
470
+
-**Type**: `Event`
471
+
370
472
## Theming
371
473
372
474
You can customize the GUI of the color picker using CSS custom properties:
0 commit comments