@commmerce/ui 1.15.0 → 1.17.0

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.
@@ -1,22 +1,25 @@
1
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
2
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
3
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
4
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
5
+ };
1
6
  var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
2
7
  if (kind === "m") throw new TypeError("Private method is not writable");
3
8
  if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
4
9
  if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
5
10
  return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
6
11
  };
7
- var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
8
- if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
9
- if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
10
- return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
11
- };
12
- var _CmColorField_instances, _CmColorField_internals, _CmColorField_v, _CmColorField_id, _CmColorField_defaultValue, _CmColorField_typed, _CmColorField_emit, _CmColorField_pick, _CmColorField_onSwatchKey, _CmColorField_onHexInput, _CmColorField_commitHex, _CmColorField_onHexChange, _CmColorField_onHexKey, _CmColorField_onPickerInput, _CmColorField_onPickerChange;
12
+ var _CmColorField_instances, _CmColorField_internals, _CmColorField_layer, _CmColorField_v, _CmColorField_id, _CmColorField_defaultValue, _CmColorField_typed, _CmColorField_wide_get, _CmColorField_normalize, _CmColorField_onOutside, _CmColorField_emit, _CmColorField_pick, _CmColorField_onSwatchKey, _CmColorField_onHexInput, _CmColorField_commitHex, _CmColorField_onHexChange, _CmColorField_onHexKey, _CmColorField_onPickerInput, _CmColorField_onPickerChange, _CmColorField_onPanelInput, _CmColorField_onPanelChange, _CmColorField_onPopKey, _CmColorField_formatOf;
13
13
  import { LitElement, html, css, nothing } from 'lit';
14
14
  import { live } from 'lit/directives/live.js';
15
15
  import { fieldStyles } from '../field.js';
16
16
  import { base, define, nextId, logicalKey } from '../base.js';
17
17
  import { CmLocalizedElement, defineStrings } from '../localize.js';
18
18
  import { FieldValidity } from '../validation.js';
19
+ import { TopLayer, topLayerStyles } from '../top-layer.js';
20
+ import { normalizeHex, isColorValue, parseColor, parseGradient } from '../color.js';
19
21
  import './icon.js';
22
+ import './color-picker.js';
20
23
  const strings = defineStrings('colorField', {
21
24
  /** Accessible name of each swatch with a name: "Blue, #2e6faf". */
22
25
  swatch: '{name}, {hex}',
@@ -32,6 +35,11 @@ const strings = defineStrings('colorField', {
32
35
  required: 'Please choose a colour.',
33
36
  /** Validation message for text that isn't a colour. */
34
37
  invalid: 'Enter a colour as #RRGGBB, for example #2e6faf.',
38
+ /** Validation message when the field takes RGB, HSL, opacity or gradients (`formats`, `alpha`, `gradient`). */
39
+ invalidAny: 'Enter a colour such as #2e6faf or rgb(46, 111, 175).',
40
+ /** Accessible name of the colour box when it takes more than hex. `valueNamed` is used with a label. */
41
+ value: 'Colour value',
42
+ valueNamed: '{label}, colour value',
35
43
  /** Names of the default swatches. */
36
44
  black: 'Black',
37
45
  grey: 'Grey',
@@ -46,6 +54,7 @@ const strings = defineStrings('colorField', {
46
54
  blue: 'Blue',
47
55
  indigo: 'Indigo',
48
56
  });
57
+ export { normalizeHex } from '../color.js';
49
58
  /**
50
59
  * The default palette: the hues of Commmerce's light theme tokens (text-*, bg-*, icon-rating, nav-badge, gray-*), so
51
60
  * colours picked for pick-list values sit well next to the admin. Names are built-in strings (translatable).
@@ -64,14 +73,11 @@ const DEFAULT_SWATCHES = [
64
73
  { value: '#2e6faf', key: 'blue' },
65
74
  { value: '#446beb', key: 'indigo' },
66
75
  ];
67
- /** `#RGB`, `RRGGBB`, `#RRGGBB` (any case, spaces around) -> `#rrggbb`; anything else -> null. */
68
- export function normalizeHex(text) {
69
- const m = /^#?([0-9a-f]{3}|[0-9a-f]{6})$/i.exec(text.trim());
70
- if (!m)
71
- return null;
72
- const h = m[1].length === 3 ? [...m[1]].map((c) => c + c).join('') : m[1];
73
- return `#${h.toLowerCase()}`;
74
- }
76
+ /** `picker` attribute: absent = no picker, `native` = the browser's own, anything else = the Commmerce picker panel. */
77
+ const pickerConverter = {
78
+ fromAttribute: (v) => (v === null ? false : v === 'native' ? 'native' : true),
79
+ toAttribute: (v) => (v === 'native' ? 'native' : v ? '' : null),
80
+ };
75
81
  const luminance = (hex) => {
76
82
  const n = parseInt(hex.slice(1), 16);
77
83
  return [(n >> 16) & 255, (n >> 8) & 255, n & 255]
@@ -88,12 +94,15 @@ export function checkColor(hex) {
88
94
  return (l + 0.05) / 0.05 >= 1.05 / (l + 0.05) ? '#000000' : '#ffffff';
89
95
  }
90
96
  /**
91
- * Colour field: a palette of swatches, a hex box and, optionally, the browser's colour picker. For colours that
92
- * mean something to the merchant, such as the colour of each lead status. The value is lowercase `#rrggbb`.
97
+ * Colour field: a palette of swatches, a hex box and, optionally, a colour picker (`picker`): the cm-color-picker
98
+ * panel with the store's brand colours, recent colours, opacity, formats and gradients, or the browser's own
99
+ * (`picker="native"`). For colours that mean something to the merchant, such as the colour of each lead status.
100
+ * The value is lowercase `#rrggbb` unless you allow more with `formats`, `alpha` or `gradient`.
93
101
  * Participates in native forms.
94
102
  * @element cm-color-field
95
103
  * @fires input - When the colour changes: a swatch, a complete hex code typed, or the picker moving.
96
104
  * @fires change - When the colour is committed: a swatch picked, the hex box left or Enter pressed, the picker closed.
105
+ * @fires cm-recent-change - From the picker panel: `detail.colors`, the new recent list.
97
106
  * @csspart label - The label.
98
107
  * @csspart swatches - The swatch radio group.
99
108
  * @csspart swatch - Each swatch.
@@ -115,8 +124,23 @@ export class CmColorField extends CmLocalizedElement {
115
124
  this.name = '';
116
125
  /** The colour as lowercase `#rrggbb`, or empty for none. `#ABC` and `ABCDEF` are accepted and normalised. */
117
126
  this.value = '';
118
- /** Adds a button that opens the browser's own colour picker, for colours outside the palette. */
127
+ /**
128
+ * Adds a button that opens a colour picker for colours outside the palette: the Commmerce picker panel (since
129
+ * 1.17.0), or the browser's own with `picker="native"`.
130
+ */
119
131
  this.picker = false;
132
+ /** Allows see-through colours: the picker shows opacity; the value may be `#rrggbbaa`, `rgba()` or `hsla()`. */
133
+ this.alpha = false;
134
+ /** Allows linear and radial gradients from the picker; the value may then be a CSS gradient. */
135
+ this.gradient = false;
136
+ /**
137
+ * Formats the merchant may pick in the picker, space separated: `hex`, `rgb`, `hsl`. Default `hex` only, so the
138
+ * value stays `#rrggbb`. With more, the value is written in the format they chose.
139
+ */
140
+ this.formats = 'hex';
141
+ /** Keeps recent colours in this browser under this key when there is no `source`. */
142
+ this.recentKey = '';
143
+ this._open = false;
120
144
  /** Shows a red asterisk and blocks form submission until a colour is chosen. */
121
145
  this.required = false;
122
146
  /** Blocks interaction. Also set by a disabled `<fieldset>`. */
@@ -124,16 +148,31 @@ export class CmColorField extends CmLocalizedElement {
124
148
  /** The hex box holds text that isn't a colour (after it was committed). */
125
149
  this._invalid = false;
126
150
  _CmColorField_internals.set(this, this.attachInternals());
151
+ _CmColorField_layer.set(this, new TopLayer());
127
152
  _CmColorField_v.set(this, new FieldValidity(this, __classPrivateFieldGet(this, _CmColorField_internals, "f")));
128
153
  _CmColorField_id.set(this, nextId('cm-cf'));
129
154
  _CmColorField_defaultValue.set(this, '');
130
155
  /** The value this element set itself from the hex box; any other new value replaces the box's text. */
131
156
  _CmColorField_typed.set(this, void 0);
157
+ _CmColorField_onOutside.set(this, (e) => {
158
+ if (this._open && !e.composedPath().includes(this))
159
+ this._open = false;
160
+ });
132
161
  }
133
162
  connectedCallback() {
134
163
  super.connectedCallback();
135
- __classPrivateFieldSet(this, _CmColorField_defaultValue, normalizeHex(this.value) ?? this.value, "f");
164
+ __classPrivateFieldSet(this, _CmColorField_defaultValue, __classPrivateFieldGet(this, _CmColorField_instances, "m", _CmColorField_normalize).call(this, this.value) ?? this.value, "f");
165
+ document.addEventListener('pointerdown', __classPrivateFieldGet(this, _CmColorField_onOutside, "f"), true);
136
166
  }
167
+ disconnectedCallback() {
168
+ super.disconnectedCallback();
169
+ document.removeEventListener('pointerdown', __classPrivateFieldGet(this, _CmColorField_onOutside, "f"), true);
170
+ __classPrivateFieldGet(this, _CmColorField_layer, "f").close();
171
+ }
172
+ /** Opens or closes the picker panel. */
173
+ show() { if (!this.disabled && this.picker && this.picker !== 'native')
174
+ this._open = true; }
175
+ close() { this._open = false; }
137
176
  get form() { return __classPrivateFieldGet(this, _CmColorField_internals, "f").form; }
138
177
  get validity() { return __classPrivateFieldGet(this, _CmColorField_internals, "f").validity; }
139
178
  get validationMessage() { return __classPrivateFieldGet(this, _CmColorField_internals, "f").validationMessage; }
@@ -153,13 +192,13 @@ export class CmColorField extends CmLocalizedElement {
153
192
  return DEFAULT_SWATCHES.map((s) => ({ value: s.value, name: this.t(s.key) }));
154
193
  return this.swatches.flatMap((s) => {
155
194
  const o = typeof s === 'string' ? { value: s } : s;
156
- const value = normalizeHex(o?.value ?? '');
195
+ const value = normalizeHex(o?.value ?? '') ?? (__classPrivateFieldGet(this, _CmColorField_instances, "a", _CmColorField_wide_get) && o?.value && isColorValue(o.value) ? o.value.trim() : null);
157
196
  return value ? [{ value, name: o.name }] : [];
158
197
  });
159
198
  }
160
199
  willUpdate(changed) {
161
200
  if (changed.has('value')) {
162
- const n = normalizeHex(this.value ?? '');
201
+ const n = __classPrivateFieldGet(this, _CmColorField_instances, "m", _CmColorField_normalize).call(this, this.value ?? '');
163
202
  if (n && n !== this.value)
164
203
  this.value = n;
165
204
  if (this.hasUpdated && this.value !== __classPrivateFieldGet(this, _CmColorField_typed, "f")) {
@@ -169,14 +208,30 @@ export class CmColorField extends CmLocalizedElement {
169
208
  __classPrivateFieldSet(this, _CmColorField_typed, undefined, "f");
170
209
  }
171
210
  }
172
- updated() {
173
- const ok = !this.value || normalizeHex(this.value) !== null;
211
+ updated(changed) {
212
+ if (changed.has('_open')) {
213
+ const pop = this.renderRoot.querySelector('.pop');
214
+ if (this._open) {
215
+ if (!__classPrivateFieldGet(this, _CmColorField_layer, "f").open(pop, this.renderRoot.querySelector('.anchor'), { align: 'start', gap: 8 }))
216
+ pop?.setAttribute('data-open', '');
217
+ this.renderRoot.querySelector('cm-color-picker')?.updateComplete.then(() => {
218
+ this.renderRoot.querySelector('cm-color-picker')?.shadowRoot?.querySelector('.area')?.focus();
219
+ });
220
+ }
221
+ else if (changed.get('_open')) {
222
+ __classPrivateFieldGet(this, _CmColorField_layer, "f").close();
223
+ pop?.removeAttribute('data-open');
224
+ }
225
+ }
226
+ else if (this._open)
227
+ __classPrivateFieldGet(this, _CmColorField_layer, "f").place();
228
+ const ok = !this.value || __classPrivateFieldGet(this, _CmColorField_instances, "m", _CmColorField_normalize).call(this, this.value) !== null;
174
229
  __classPrivateFieldGet(this, _CmColorField_internals, "f").setFormValue(this.value && ok ? this.value : null, this.value);
175
230
  const anchor = this.renderRoot.querySelector('.input') ?? undefined;
176
231
  if (this.error)
177
232
  __classPrivateFieldGet(this, _CmColorField_internals, "f").setValidity({ customError: true }, this.error, anchor);
178
233
  else if (this._invalid || !ok)
179
- __classPrivateFieldGet(this, _CmColorField_internals, "f").setValidity({ patternMismatch: true }, this.t('invalid'), anchor);
234
+ __classPrivateFieldGet(this, _CmColorField_internals, "f").setValidity({ patternMismatch: true }, this.t(__classPrivateFieldGet(this, _CmColorField_instances, "a", _CmColorField_wide_get) ? 'invalidAny' : 'invalid'), anchor);
180
235
  else if (this.required && !this.value)
181
236
  __classPrivateFieldGet(this, _CmColorField_internals, "f").setValidity({ valueMissing: true }, this.t('required'), anchor);
182
237
  else
@@ -185,9 +240,12 @@ export class CmColorField extends CmLocalizedElement {
185
240
  }
186
241
  render() {
187
242
  const items = this.items;
188
- const current = normalizeHex(this.value ?? '');
243
+ const current = __classPrivateFieldGet(this, _CmColorField_instances, "m", _CmColorField_normalize).call(this, this.value ?? '');
189
244
  const sel = items.findIndex((s) => s.value === current);
190
- const message = this.error || (this._invalid || (this.value && !current) ? this.t('invalid') : '') || __classPrivateFieldGet(this, _CmColorField_v, "f").message;
245
+ const wide = __classPrivateFieldGet(this, _CmColorField_instances, "a", _CmColorField_wide_get);
246
+ const message = this.error || (this._invalid || (this.value && !current) ? this.t(wide ? 'invalidAny' : 'invalid') : '') || __classPrivateFieldGet(this, _CmColorField_v, "f").message;
247
+ const hex6 = current ? normalizeHex(current) : null;
248
+ const fill = current ? `background:${current}` : nothing;
191
249
  const describedBy = [message && `${__classPrivateFieldGet(this, _CmColorField_id, "f")}-err`, this.helpText && `${__classPrivateFieldGet(this, _CmColorField_id, "f")}-help`].filter(Boolean).join(' ') || nothing;
192
250
  const hostLabel = this.getAttribute('aria-label');
193
251
  const named = this.label || hostLabel || '';
@@ -209,17 +267,34 @@ export class CmColorField extends CmLocalizedElement {
209
267
  </div>`
210
268
  : nothing}
211
269
  <div class="row">
212
- ${this.picker
213
- ? html `<label class="pick" part="picker" style=${current ? `background-color:${current}` : nothing}>
214
- <input type="color" .value=${live(current ?? '#000000')} ?disabled=${this.disabled}
270
+ ${this.picker && this.picker !== 'native'
271
+ ? html `<span class="anchor"><button type="button" class="pick checker-bg" part="picker" ?disabled=${this.disabled}
272
+ aria-haspopup="dialog" aria-expanded=${this._open ? 'true' : 'false'} aria-controls="${__classPrivateFieldGet(this, _CmColorField_id, "f")}-pop"
273
+ aria-label=${named ? this.t('pickerNamed', { label: named }) : this.t('picker')}
274
+ @click=${() => { this._open = !this._open; }}>
275
+ <span class="fill" style=${fill}></span>
276
+ <cm-icon name="edit" size="12" aria-hidden="true"></cm-icon>
277
+ </button>
278
+ <div class="pop" id="${__classPrivateFieldGet(this, _CmColorField_id, "f")}-pop" role="dialog" aria-label=${named ? this.t('pickerNamed', { label: named }) : this.t('picker')}
279
+ @keydown=${__classPrivateFieldGet(this, _CmColorField_instances, "m", _CmColorField_onPopKey)}>
280
+ ${this._open
281
+ ? html `<cm-color-picker .value=${this.value} .format=${__classPrivateFieldGet(this, _CmColorField_instances, "m", _CmColorField_formatOf).call(this)} .formats=${this.formats} .alpha=${this.alpha}
282
+ .gradient=${this.gradient} .swatches=${this.swatches ? this.items : undefined} .brandColors=${this.brandColors}
283
+ .recentColors=${this.recentColors} .source=${this.source} .recentKey=${this.recentKey}
284
+ @input=${__classPrivateFieldGet(this, _CmColorField_instances, "m", _CmColorField_onPanelInput)} @change=${__classPrivateFieldGet(this, _CmColorField_instances, "m", _CmColorField_onPanelChange)}></cm-color-picker>`
285
+ : nothing}
286
+ </div></span>`
287
+ : this.picker
288
+ ? html `<label class="pick" part="picker" style=${hex6 ? `background-color:${hex6}` : nothing}>
289
+ <input type="color" .value=${live(hex6 ?? '#000000')} ?disabled=${this.disabled}
215
290
  aria-label=${named ? this.t('pickerNamed', { label: named }) : this.t('picker')}
216
291
  @input=${__classPrivateFieldGet(this, _CmColorField_instances, "m", _CmColorField_onPickerInput)} @change=${__classPrivateFieldGet(this, _CmColorField_instances, "m", _CmColorField_onPickerChange)} />
217
292
  <cm-icon name="edit" size="12" aria-hidden="true"></cm-icon>
218
293
  </label>`
219
- : html `<span class="preview" part="preview" aria-hidden="true" style=${current ? `background-color:${current}` : nothing}></span>`}
220
- <input class="input" part="input" id=${__classPrivateFieldGet(this, _CmColorField_id, "f")} type="text" inputmode="text" autocomplete="off" spellcheck="false"
221
- maxlength="7" .value=${live(this._draft ?? this.value ?? '')} placeholder=${this.t('placeholder')}
222
- aria-label=${named ? this.t('hexNamed', { label: named }) : this.t('hex')}
294
+ : html `<span class="preview" part="preview" aria-hidden="true" style=${fill}></span>`}
295
+ <input part="input" id=${__classPrivateFieldGet(this, _CmColorField_id, "f")} type="text" inputmode="text" autocomplete="off" spellcheck="false"
296
+ maxlength=${wide ? 200 : 7} class="input ${wide ? 'wide' : ''}" .value=${live(this._draft ?? this.value ?? '')} placeholder=${this.t('placeholder')}
297
+ aria-label=${wide ? (named ? this.t('valueNamed', { label: named }) : this.t('value')) : named ? this.t('hexNamed', { label: named }) : this.t('hex')}
223
298
  ?required=${this.required} ?disabled=${this.disabled}
224
299
  aria-invalid=${message ? 'true' : nothing} aria-describedby=${describedBy}
225
300
  @input=${__classPrivateFieldGet(this, _CmColorField_instances, "m", _CmColorField_onHexInput)} @change=${__classPrivateFieldGet(this, _CmColorField_instances, "m", _CmColorField_onHexChange)} @keydown=${__classPrivateFieldGet(this, _CmColorField_instances, "m", _CmColorField_onHexKey)} />
@@ -229,7 +304,20 @@ export class CmColorField extends CmLocalizedElement {
229
304
  </div>`;
230
305
  }
231
306
  }
232
- _CmColorField_internals = new WeakMap(), _CmColorField_v = new WeakMap(), _CmColorField_id = new WeakMap(), _CmColorField_defaultValue = new WeakMap(), _CmColorField_typed = new WeakMap(), _CmColorField_instances = new WeakSet(), _CmColorField_emit = function _CmColorField_emit(...types) {
307
+ _CmColorField_internals = new WeakMap(), _CmColorField_layer = new WeakMap(), _CmColorField_v = new WeakMap(), _CmColorField_id = new WeakMap(), _CmColorField_defaultValue = new WeakMap(), _CmColorField_typed = new WeakMap(), _CmColorField_onOutside = new WeakMap(), _CmColorField_instances = new WeakSet(), _CmColorField_wide_get = function _CmColorField_wide_get() { return this.alpha || this.gradient || /rgb|hsl/.test(this.formats ?? ''); }, _CmColorField_normalize = function _CmColorField_normalize(text) {
308
+ const t = (text ?? '').trim();
309
+ const hex = normalizeHex(t);
310
+ if (hex)
311
+ return hex;
312
+ if (!__classPrivateFieldGet(this, _CmColorField_instances, "a", _CmColorField_wide_get))
313
+ return null;
314
+ if (/^#[0-9a-f]{4}$|^#[0-9a-f]{8}$/i.test(t))
315
+ return this.alpha ? t.toLowerCase() : null;
316
+ if (parseGradient(t))
317
+ return this.gradient ? t : null;
318
+ const c = parseColor(t);
319
+ return c && (this.alpha || c.a === 1) ? t : null;
320
+ }, _CmColorField_emit = function _CmColorField_emit(...types) {
233
321
  for (const type of types)
234
322
  this.dispatchEvent(new Event(type, { bubbles: true, composed: true }));
235
323
  }, _CmColorField_pick = function _CmColorField_pick(i) {
@@ -265,7 +353,7 @@ _CmColorField_internals = new WeakMap(), _CmColorField_v = new WeakMap(), _CmCol
265
353
  e.stopPropagation();
266
354
  const text = e.target.value;
267
355
  this._draft = text;
268
- const n = /^#?[0-9a-f]{6}$/i.test(text.trim()) ? normalizeHex(text) : null; // live only for full codes
356
+ const n = /^#?[0-9a-f]{6}$/i.test(text.trim()) ? normalizeHex(text) : __classPrivateFieldGet(this, _CmColorField_instances, "a", _CmColorField_wide_get) && /\)\s*$|^#[0-9a-f]{8}$/i.test(text.trim()) ? __classPrivateFieldGet(this, _CmColorField_instances, "m", _CmColorField_normalize).call(this, text) : null; // live only for complete values
269
357
  if (n && n !== this.value) {
270
358
  __classPrivateFieldSet(this, _CmColorField_typed, n, "f");
271
359
  this.value = n;
@@ -277,7 +365,7 @@ _CmColorField_internals = new WeakMap(), _CmColorField_v = new WeakMap(), _CmCol
277
365
  if (this._draft === undefined)
278
366
  return;
279
367
  const text = this._draft.trim();
280
- const n = text ? normalizeHex(text) : '';
368
+ const n = text ? __classPrivateFieldGet(this, _CmColorField_instances, "m", _CmColorField_normalize).call(this, text) : '';
281
369
  if (n === null) {
282
370
  this._invalid = true;
283
371
  return;
@@ -307,6 +395,29 @@ _CmColorField_internals = new WeakMap(), _CmColorField_v = new WeakMap(), _CmCol
307
395
  }, _CmColorField_onPickerChange = function _CmColorField_onPickerChange(e) {
308
396
  e.stopPropagation();
309
397
  __classPrivateFieldGet(this, _CmColorField_instances, "m", _CmColorField_emit).call(this, 'change');
398
+ }, _CmColorField_onPanelInput = function _CmColorField_onPanelInput(e) {
399
+ e.stopPropagation();
400
+ const v = e.target.value;
401
+ if (v === this.value)
402
+ return;
403
+ __classPrivateFieldSet(this, _CmColorField_typed, v, "f");
404
+ this.value = v;
405
+ this._draft = undefined;
406
+ this._invalid = false;
407
+ __classPrivateFieldGet(this, _CmColorField_instances, "m", _CmColorField_emit).call(this, 'input');
408
+ }, _CmColorField_onPanelChange = function _CmColorField_onPanelChange(e) {
409
+ e.stopPropagation();
410
+ __classPrivateFieldGet(this, _CmColorField_instances, "m", _CmColorField_emit).call(this, 'change');
411
+ }, _CmColorField_onPopKey = function _CmColorField_onPopKey(e) {
412
+ if (e.key !== 'Escape')
413
+ return;
414
+ e.preventDefault();
415
+ e.stopPropagation();
416
+ this._open = false;
417
+ this.updateComplete.then(() => this.renderRoot.querySelector('button.pick')?.focus());
418
+ }, _CmColorField_formatOf = function _CmColorField_formatOf() {
419
+ const v = this.value ?? '';
420
+ return /^rgba?\(|gradient\(.*rgba?\(/i.test(v) ? 'rgb' : /^hsla?\(|gradient\(.*hsla?\(/i.test(v) ? 'hsl' : 'hex';
310
421
  };
311
422
  CmColorField.formAssociated = true;
312
423
  CmColorField.shadowRootOptions = { ...LitElement.shadowRootOptions, delegatesFocus: true };
@@ -318,7 +429,15 @@ CmColorField.properties = {
318
429
  name: { reflect: true },
319
430
  value: {},
320
431
  swatches: { type: Array },
321
- picker: { type: Boolean },
432
+ picker: { converter: pickerConverter, reflect: true },
433
+ alpha: { type: Boolean },
434
+ gradient: { type: Boolean },
435
+ formats: {},
436
+ brandColors: { type: Array, attribute: 'brand-colors' },
437
+ recentColors: { type: Array, attribute: 'recent-colors' },
438
+ source: { attribute: false },
439
+ recentKey: { attribute: 'recent-key' },
440
+ _open: { state: true },
322
441
  required: { type: Boolean, reflect: true },
323
442
  disabled: { type: Boolean, reflect: true },
324
443
  _draft: { state: true },
@@ -327,6 +446,7 @@ CmColorField.properties = {
327
446
  CmColorField.styles = [
328
447
  base,
329
448
  fieldStyles,
449
+ topLayerStyles,
330
450
  css `
331
451
  :host { display: block; }
332
452
  .swatches { display: flex; flex-wrap: wrap; gap: var(--cm-space-8); padding: 4px; margin: -4px; }
@@ -365,6 +485,20 @@ CmColorField.styles = [
365
485
  .input[aria-invalid='true'] { border-color: var(--cm-border-critical); }
366
486
  .input:disabled { background: var(--cm-bg-input-filled); color: var(--cm-text-secondary); border-color: var(--cm-border-subtle); cursor: not-allowed; }
367
487
  :host([disabled]) .pick { cursor: not-allowed; opacity: 0.4; }
488
+ button.pick { padding: 0; overflow: visible; }
489
+ .checker-bg {
490
+ background-color: var(--cm-bg-surface);
491
+ background-image: repeating-conic-gradient(var(--cm-border-subtle) 0 25%, transparent 0 50%); background-size: 8px 8px;
492
+ }
493
+ .pick .fill { border-radius: inherit; }
494
+ .pick .fill { position: absolute; inset: 0; }
495
+ .input.wide { width: 28ch; flex: 1 1 auto; max-width: 100%; }
496
+ .pop {
497
+ position: absolute; z-index: 20; padding: var(--cm-space-12); border-radius: var(--cm-radius-md);
498
+ background: var(--cm-bg-surface); border: var(--cm-border-1) solid var(--cm-border-subtle); box-shadow: var(--cm-shadow-md);
499
+ }
500
+ .pop:not(:popover-open):not([data-open]) { display: none; }
501
+ .anchor { position: relative; }
368
502
  :host([disabled]) .preview { opacity: 0.4; }
369
503
  `,
370
504
  ];
@@ -0,0 +1,179 @@
1
+ import { type PropertyValues } from 'lit';
2
+ import { CmLocalizedElement } from '../localize.js';
3
+ import { type ColorFormat, type Gradient, type Hsva } from '../color.js';
4
+ import { type ColorSource, type ColorSwatch } from '../color-source.js';
5
+ import './icon.js';
6
+ declare const strings: {
7
+ /** Accessible name of the whole picker when it has no `aria-label`. */
8
+ label: string;
9
+ solid: string;
10
+ gradient: string;
11
+ /** Accessible name of the saturation and brightness square, and how its value is read out. */
12
+ area: string;
13
+ areaValue: string;
14
+ hue: string;
15
+ opacity: string;
16
+ /** The colour format menu and its options. */
17
+ format: string;
18
+ hex: string;
19
+ rgb: string;
20
+ hsl: string;
21
+ red: string;
22
+ green: string;
23
+ blue: string;
24
+ saturation: string;
25
+ lightness: string;
26
+ /** The browser's eyedropper button (shown only where the browser has one). */
27
+ eyedropper: string;
28
+ /** Gradient controls. */
29
+ gradientType: string;
30
+ linear: string;
31
+ radial: string;
32
+ angle: string;
33
+ stops: string;
34
+ stop: string;
35
+ addStop: string;
36
+ removeStop: string;
37
+ /** Headings of the swatch groups. */
38
+ brand: string;
39
+ recent: string;
40
+ palette: string;
41
+ /** Accessible name of a swatch: its name and value, or the value alone. */
42
+ swatch: string;
43
+ };
44
+ declare module '../localize.js' {
45
+ interface CmStrings {
46
+ colorPicker: typeof strings;
47
+ }
48
+ }
49
+ /** The default palette (the hues of the Commmerce light theme), shared with cm-color-field. */
50
+ export declare const DEFAULT_PALETTE: ColorSwatch[];
51
+ /**
52
+ * Colour picker: a saturation and brightness square, hue and opacity sliders, the value as hex, RGB or HSL, the
53
+ * browser's eyedropper, gradients, and swatches for the store's brand colours, the merchant's recent colours and a
54
+ * palette. Use it inline in a panel, or let cm-color-field open it (`picker`).
55
+ *
56
+ * Brand and recent colours come from `brand-colors` / `recent-colors`, or from a `source`: `app.colors` (App
57
+ * Bridge), `httpColorSource()` (your server, Colour API) or `localColorSource()`. Each committed colour that isn't
58
+ * a brand or palette colour is added to the recents: through `source.addRecentColor`, or kept locally when
59
+ * `recent-key` is set, and announced with `cm-recent-change` either way.
60
+ * @element cm-color-picker
61
+ * @fires input - While the colour changes (dragging, typing a complete value).
62
+ * @fires change - When a colour is committed: a drag ends, a swatch is picked, a box is left or Enter pressed.
63
+ * @fires cm-recent-change - `detail.colors`: the new recent list (newest first) after a commit added to it.
64
+ * @csspart area - The saturation and brightness square.
65
+ * @csspart swatch - Each swatch button.
66
+ */
67
+ export declare class CmColorPicker extends CmLocalizedElement {
68
+ #private;
69
+ static properties: {
70
+ value: {};
71
+ format: {
72
+ reflect: boolean;
73
+ };
74
+ formats: {};
75
+ alpha: {
76
+ type: BooleanConstructor;
77
+ };
78
+ gradient: {
79
+ type: BooleanConstructor;
80
+ };
81
+ swatches: {
82
+ type: ArrayConstructor;
83
+ };
84
+ brandColors: {
85
+ type: ArrayConstructor;
86
+ attribute: string;
87
+ };
88
+ recentColors: {
89
+ type: ArrayConstructor;
90
+ attribute: string;
91
+ };
92
+ source: {
93
+ attribute: boolean;
94
+ };
95
+ recentKey: {
96
+ attribute: string;
97
+ };
98
+ maxRecent: {
99
+ type: NumberConstructor;
100
+ attribute: string;
101
+ };
102
+ eyedropper: {
103
+ type: BooleanConstructor;
104
+ };
105
+ disabled: {
106
+ type: BooleanConstructor;
107
+ reflect: boolean;
108
+ };
109
+ _hsva: {
110
+ state: boolean;
111
+ };
112
+ _grad: {
113
+ state: boolean;
114
+ };
115
+ _stop: {
116
+ state: boolean;
117
+ };
118
+ _brand: {
119
+ state: boolean;
120
+ };
121
+ _recent: {
122
+ state: boolean;
123
+ };
124
+ _drafts: {
125
+ state: boolean;
126
+ };
127
+ };
128
+ /** The colour: hex, `rgb()`, `hsl()` or, with `gradient`, a `linear-gradient()` / `radial-gradient()`. Empty for none. */
129
+ value: string;
130
+ /** How the value is written: `hex` (default), `rgb` or `hsl`. Changes when the merchant picks another format. */
131
+ format: ColorFormat;
132
+ /** Formats the merchant may switch between, space separated. Default `hex rgb hsl`; one format hides the menu. */
133
+ formats: string;
134
+ /** Shows the opacity slider. A see-through colour is written `#rrggbbaa`, `rgba()` or `hsla()`. */
135
+ alpha: boolean;
136
+ /** Adds Solid / Gradient tabs: linear or radial gradients with two or more stops. */
137
+ gradient: boolean;
138
+ /** The palette, as colours or `{ value, name }`. Default: 12 colours from the Commmerce tokens. `[]` hides it. */
139
+ swatches?: Array<string | ColorSwatch>;
140
+ /** The store's brand colours. Wins over `source`. In HTML, pass JSON. */
141
+ brandColors?: Array<string | ColorSwatch>;
142
+ /** Recent colours, newest first. Wins over `source`; update it from `cm-recent-change`. */
143
+ recentColors?: string[];
144
+ /** Where brand and recent colours come from (see the colour picker docs). */
145
+ source?: ColorSource;
146
+ /** Keeps recents in this browser under this key when there is no `source` (a `localColorSource`). */
147
+ recentKey: string;
148
+ /** How many recent colours to show. Default 16. */
149
+ maxRecent: number;
150
+ /** Shows the eyedropper where the browser has one (Chrome and Edge on desktop). Default on. */
151
+ eyedropper: boolean;
152
+ /** Blocks interaction. */
153
+ disabled: boolean;
154
+ _hsva: Hsva;
155
+ _grad: Gradient | null;
156
+ _stop: number;
157
+ _brand: ColorSwatch[];
158
+ _recent: string[];
159
+ /** Text in a box while it differs from the colour, keyed by box. */
160
+ _drafts: Record<string, string>;
161
+ static styles: import("lit").CSSResult[];
162
+ connectedCallback(): void;
163
+ /** Formats the merchant may choose. */
164
+ get allowedFormats(): ColorFormat[];
165
+ /** The palette in use. */
166
+ get palette(): ColorSwatch[];
167
+ /** Brand colours in use (`brand-colors`, else from `source`). */
168
+ get brand(): ColorSwatch[];
169
+ /** Recent colours in use (`recent-colors`, else from `source` / `recent-key`), without gradients unless `gradient`. */
170
+ get recent(): string[];
171
+ willUpdate(changed: PropertyValues<this>): void;
172
+ render(): import("lit-html").TemplateResult<1>;
173
+ }
174
+ declare global {
175
+ interface HTMLElementTagNameMap {
176
+ 'cm-color-picker': CmColorPicker;
177
+ }
178
+ }
179
+ export {};