@joralvmel/cosmos-elements 0.3.0 → 0.4.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.
Files changed (70) hide show
  1. package/README.md +9 -4
  2. package/dist/components/badge/cosm-badge.d.ts +64 -0
  3. package/dist/components/badge/cosm-badge.d.ts.map +1 -0
  4. package/dist/components/badge/cosm-badge.js +162 -0
  5. package/dist/components/badge/cosm-badge.js.map +1 -0
  6. package/dist/components/button/cosm-button.d.ts.map +1 -1
  7. package/dist/components/button/cosm-button.js +2 -0
  8. package/dist/components/button/cosm-button.js.map +1 -1
  9. package/dist/components/calendar/cosm-calendar.d.ts +77 -0
  10. package/dist/components/calendar/cosm-calendar.d.ts.map +1 -0
  11. package/dist/components/calendar/cosm-calendar.js +696 -0
  12. package/dist/components/calendar/cosm-calendar.js.map +1 -0
  13. package/dist/components/checkbox/cosm-checkbox.d.ts +122 -0
  14. package/dist/components/checkbox/cosm-checkbox.d.ts.map +1 -0
  15. package/dist/components/checkbox/cosm-checkbox.js +433 -0
  16. package/dist/components/checkbox/cosm-checkbox.js.map +1 -0
  17. package/dist/components/dialog/cosm-dialog.d.ts +103 -0
  18. package/dist/components/dialog/cosm-dialog.d.ts.map +1 -0
  19. package/dist/components/dialog/cosm-dialog.js +497 -0
  20. package/dist/components/dialog/cosm-dialog.js.map +1 -0
  21. package/dist/components/icon/cosm-icon.d.ts +7 -15
  22. package/dist/components/icon/cosm-icon.d.ts.map +1 -1
  23. package/dist/components/icon/cosm-icon.js +29 -30
  24. package/dist/components/icon/cosm-icon.js.map +1 -1
  25. package/dist/components/icon-button/cosm-icon-button.d.ts.map +1 -1
  26. package/dist/components/icon-button/cosm-icon-button.js +6 -0
  27. package/dist/components/icon-button/cosm-icon-button.js.map +1 -1
  28. package/dist/components/listbox/cosm-listbox.d.ts +1 -5
  29. package/dist/components/listbox/cosm-listbox.d.ts.map +1 -1
  30. package/dist/components/listbox/cosm-listbox.js +3 -18
  31. package/dist/components/listbox/cosm-listbox.js.map +1 -1
  32. package/dist/components/pagination/cosm-pagination.d.ts +118 -0
  33. package/dist/components/pagination/cosm-pagination.d.ts.map +1 -0
  34. package/dist/components/pagination/cosm-pagination.js +315 -0
  35. package/dist/components/pagination/cosm-pagination.js.map +1 -0
  36. package/dist/components/popover/cosm-popover.d.ts +0 -22
  37. package/dist/components/popover/cosm-popover.d.ts.map +1 -1
  38. package/dist/components/popover/cosm-popover.js +21 -54
  39. package/dist/components/popover/cosm-popover.js.map +1 -1
  40. package/dist/components/radio-group/cosm-radio-group.d.ts +111 -0
  41. package/dist/components/radio-group/cosm-radio-group.d.ts.map +1 -0
  42. package/dist/components/radio-group/cosm-radio-group.js +478 -0
  43. package/dist/components/radio-group/cosm-radio-group.js.map +1 -0
  44. package/dist/components/select/cosm-select.d.ts +126 -0
  45. package/dist/components/select/cosm-select.d.ts.map +1 -0
  46. package/dist/components/select/cosm-select.js +682 -0
  47. package/dist/components/select/cosm-select.js.map +1 -0
  48. package/dist/components/switch/cosm-switch.d.ts +103 -0
  49. package/dist/components/switch/cosm-switch.d.ts.map +1 -0
  50. package/dist/components/switch/cosm-switch.js +359 -0
  51. package/dist/components/switch/cosm-switch.js.map +1 -0
  52. package/dist/components/text-field/cosm-text-field.d.ts +8 -18
  53. package/dist/components/text-field/cosm-text-field.d.ts.map +1 -1
  54. package/dist/components/text-field/cosm-text-field.js +30 -67
  55. package/dist/components/text-field/cosm-text-field.js.map +1 -1
  56. package/dist/components/textarea/cosm-textarea.d.ts +131 -0
  57. package/dist/components/textarea/cosm-textarea.d.ts.map +1 -0
  58. package/dist/components/textarea/cosm-textarea.js +445 -0
  59. package/dist/components/textarea/cosm-textarea.js.map +1 -0
  60. package/dist/custom-elements.json +5915 -1330
  61. package/dist/index.d.ts +10 -1
  62. package/dist/index.d.ts.map +1 -1
  63. package/dist/index.js +9 -0
  64. package/dist/index.js.map +1 -1
  65. package/dist/internal/current-page.d.ts +7 -0
  66. package/dist/internal/current-page.d.ts.map +1 -0
  67. package/dist/internal/current-page.js +18 -0
  68. package/dist/internal/current-page.js.map +1 -0
  69. package/llms.md +306 -20
  70. package/package.json +39 -3
@@ -0,0 +1,682 @@
1
+ import { findListboxMatch, getListboxInitialActive, getListboxOptions, moveListboxActive, selectContract, validateSelectValue, } from '@joralvmel/cosmos-core';
2
+ import { check } from '@joralvmel/cosmos-icons/check';
3
+ import { chevronDown } from '@joralvmel/cosmos-icons/chevron-down';
4
+ import { css, html, LitElement, nothing } from 'lit';
5
+ import '../icon/cosm-icon.js';
6
+ import { CosmElement } from '../../internal/cosm-element.js';
7
+ import { define } from '../../internal/define.js';
8
+ import { boxSizing, focusRing } from '../../internal/styles.js';
9
+ let instance = 0;
10
+ const typeaheadInterval = 700;
11
+ const styles = css `
12
+ :host {
13
+ display: block;
14
+ color: var(--cosm-fg, oklch(0.24 0.01 268));
15
+ font-family: var(--cosm-font-sans, system-ui, sans-serif);
16
+ }
17
+ .base {
18
+ display: grid;
19
+ gap: var(--cosm-space-xs, 0.25rem);
20
+ }
21
+ .label {
22
+ color: var(--cosm-fg, oklch(0.24 0.01 268));
23
+ font-size: var(--cosm-text-sm, 0.875rem);
24
+ font-weight: 500;
25
+ }
26
+ :host([size='sm']) {
27
+ --_control-height: var(--cosm-control-sm, 2rem);
28
+ --_option-height: 2.25rem;
29
+ --_font-size: var(--cosm-text-sm, 0.875rem);
30
+ }
31
+ :host([size='md']) {
32
+ --_control-height: var(--cosm-control-md, 2.5rem);
33
+ --_option-height: 2.5rem;
34
+ --_font-size: var(--cosm-text-md, 1rem);
35
+ }
36
+ :host([size='lg']) {
37
+ --_control-height: var(--cosm-control-lg, 3rem);
38
+ --_option-height: 3rem;
39
+ --_font-size: var(--cosm-text-lg, 1.125rem);
40
+ }
41
+ .control {
42
+ position: relative;
43
+ display: flex;
44
+ align-items: stretch;
45
+ min-width: 0;
46
+ }
47
+ .trigger {
48
+ box-sizing: border-box;
49
+ display: inline-flex;
50
+ width: 100%;
51
+ min-height: max(
52
+ var(--_control-height, var(--cosm-control-md, 2.5rem)),
53
+ var(--cosm-select-trigger-min-height, 0px)
54
+ );
55
+ align-items: center;
56
+ justify-content: space-between;
57
+ gap: var(--cosm-space-sm, 0.5rem);
58
+ padding: var(--cosm-space-xs, 0.25rem) var(--cosm-space-md, 0.75rem);
59
+ border: 1px solid var(--cosm-select-border, var(--cosm-border-strong, oklch(0.64 0.01 268)));
60
+ border-radius: var(--cosm-select-radius, var(--cosm-radius-md, 0.5rem));
61
+ background: var(--cosm-select-background, var(--cosm-bg, oklch(0.985 0.007 268)));
62
+ color: var(--cosm-select-color, var(--cosm-fg, oklch(0.24 0.01 268)));
63
+ font: inherit;
64
+ font-size: var(--_font-size, var(--cosm-text-md, 1rem));
65
+ text-align: start;
66
+ cursor: pointer;
67
+ transition:
68
+ border-color var(--cosm-duration-fast, 120ms) var(--cosm-easing-standard, ease),
69
+ background-color var(--cosm-duration-fast, 120ms) var(--cosm-easing-standard, ease);
70
+ }
71
+ :host([variant='filled']) .trigger {
72
+ border-color: var(--cosm-select-border, var(--cosm-border-strong, oklch(0.64 0.01 268)));
73
+ background: var(--cosm-select-background, var(--cosm-neutral-2, oklch(0.96 0.008 268)));
74
+ }
75
+ @media (hover: hover) {
76
+ .trigger:hover:not(:disabled):not([aria-invalid='true']) {
77
+ border-color: var(--cosm-select-border, var(--cosm-primary, oklch(0.55 0.2 268)));
78
+ }
79
+ }
80
+ .trigger:focus-visible {
81
+ outline: 2px solid var(--cosm-select-focus, var(--cosm-focus-ring, oklch(0.55 0.2 268)));
82
+ outline-offset: 2px;
83
+ }
84
+ .trigger[aria-invalid='true'] {
85
+ border-color: var(--cosm-danger, oklch(0.55 0.2 25));
86
+ }
87
+ .trigger[aria-expanded='true'] {
88
+ background: var(--cosm-select-background, var(--cosm-primary-subtle, oklch(0.92 0.04 265)));
89
+ box-shadow: inset 0 0 0 1px
90
+ var(--cosm-select-focus, var(--cosm-focus-ring, oklch(0.55 0.2 268)));
91
+ }
92
+ .trigger[aria-expanded='true']:not([aria-invalid='true']) {
93
+ border-color: var(--cosm-select-focus, var(--cosm-focus-ring, oklch(0.55 0.2 268)));
94
+ }
95
+ .trigger:disabled {
96
+ cursor: not-allowed;
97
+ opacity: 0.55;
98
+ }
99
+ .value {
100
+ min-width: 0;
101
+ overflow: hidden;
102
+ text-overflow: ellipsis;
103
+ white-space: nowrap;
104
+ }
105
+ .placeholder { color: var(--cosm-select-placeholder, var(--cosm-fg-muted, oklch(0.48 0.01 268))); }
106
+ .trigger-icon {
107
+ flex: none;
108
+ transition: transform var(--cosm-duration-fast, 120ms) var(--cosm-easing-standard, ease);
109
+ }
110
+ .trigger[aria-expanded='true'] .trigger-icon { transform: rotate(180deg); }
111
+ .popup {
112
+ position: fixed;
113
+ inset: auto;
114
+ z-index: var(--cosm-popover-z-index, 1000);
115
+ display: none;
116
+ max-width: calc(100vw - 1rem);
117
+ max-height: calc(100vh - 1rem);
118
+ overflow: auto;
119
+ box-sizing: border-box;
120
+ margin: 0;
121
+ padding: var(--cosm-space-xs, 0.25rem);
122
+ border: 1px solid var(--cosm-border-strong, oklch(0.64 0.01 268));
123
+ border-radius: var(--cosm-select-radius, var(--cosm-radius-md, 0.5rem));
124
+ background: var(--cosm-bg, oklch(0.985 0.007 268));
125
+ color: var(--cosm-fg, oklch(0.24 0.01 268));
126
+ box-shadow: var(--cosm-shadow-lg, 0 8px 24px oklch(0 0 0 / 16%));
127
+ }
128
+ .popup[data-open='true'],
129
+ .popup:popover-open { display: block; }
130
+ .listbox { display: grid; gap: var(--cosm-space-2xs, 0.125rem); }
131
+ .option {
132
+ display: flex;
133
+ min-height: var(--_option-height, 2.5rem);
134
+ align-items: center;
135
+ gap: var(--cosm-space-sm, 0.5rem);
136
+ padding: var(--cosm-space-xs, 0.25rem) var(--cosm-space-sm, 0.5rem);
137
+ border-radius: var(--cosm-radius-sm, 0.25rem);
138
+ color: var(--cosm-fg, oklch(0.24 0.01 268));
139
+ cursor: pointer;
140
+ }
141
+ .option[data-selected='true'] {
142
+ background: var(--cosm-select-option-selected-background, var(--cosm-primary-subtle, oklch(0.92 0.04 265)));
143
+ color: var(--cosm-select-option-selected-color, var(--cosm-primary-text, oklch(0.32 0.12 265)));
144
+ font-weight: 600;
145
+ }
146
+ .option[data-active='true']:not([data-disabled='true']):not([data-selected='true']) {
147
+ background: var(--cosm-neutral-2, oklch(0.96 0.008 268));
148
+ }
149
+ @media (hover: hover) {
150
+ .option:hover:not([data-disabled='true']):not([data-selected='true']) {
151
+ background: var(--cosm-neutral-2, oklch(0.96 0.008 268));
152
+ }
153
+ }
154
+ .option[data-active='true']:focus-visible {
155
+ outline: 2px solid var(--cosm-select-focus, var(--cosm-focus-ring, oklch(0.55 0.2 268)));
156
+ outline-offset: -2px;
157
+ }
158
+ .option[data-disabled='true'] {
159
+ color: var(--cosm-fg-muted, oklch(0.48 0.01 268));
160
+ cursor: not-allowed;
161
+ opacity: 0.55;
162
+ }
163
+ .option-content { display: grid; min-width: 0; }
164
+ .option-label { font-size: var(--_font-size, var(--cosm-text-md, 1rem)); }
165
+ .check { margin-inline-start: auto; flex: none; }
166
+ .help, .error { font-size: var(--cosm-text-sm, 0.875rem); }
167
+ .help { color: var(--cosm-muted, oklch(0.48 0.01 268)); }
168
+ .error { color: var(--cosm-danger, oklch(0.55 0.2 25)); }
169
+ .native-validation {
170
+ position: absolute;
171
+ width: 1px;
172
+ height: 1px;
173
+ overflow: hidden;
174
+ clip-path: inset(50%);
175
+ white-space: nowrap;
176
+ }
177
+ @media (prefers-reduced-motion: reduce) {
178
+ .trigger, .trigger-icon, .option { transition: none; }
179
+ }
180
+ @media (forced-colors: active) {
181
+ .trigger:focus-visible, .option[data-active='true']:focus-visible {
182
+ outline-color: Highlight;
183
+ }
184
+ .trigger[aria-expanded='true'] { border-color: Highlight; }
185
+ .option[data-selected='true'] { border: 1px solid Highlight; }
186
+ }
187
+ `;
188
+ /**
189
+ * Labeled, single-selection combobox with a flat popup and native Web form behavior.
190
+ *
191
+ * @tag cosm-select
192
+ * @csspart base - Field wrapper.
193
+ * @csspart label - Visible accessible label.
194
+ * @csspart trigger - Select-only combobox trigger.
195
+ * @csspart value - Selected option text.
196
+ * @csspart placeholder - Text displayed when no option is selected.
197
+ * @csspart popup - Anchored option surface.
198
+ * @csspart listbox - Single-selection option collection.
199
+ * @csspart option - Selectable option row.
200
+ * @csspart content - Option text wrapper.
201
+ * @csspart option-label - Primary option text.
202
+ * @csspart help - Supporting text.
203
+ * @csspart error - Error description.
204
+ * @fires {CustomEvent<SelectChangeDetail>} cosm-change - Proposes a selection.
205
+ * @cssprop --cosm-select-background - Control background.
206
+ * @cssprop --cosm-select-color - Control and value text color.
207
+ * @cssprop --cosm-select-placeholder - Placeholder text color.
208
+ * @cssprop --cosm-select-border - Control boundary color.
209
+ * @cssprop --cosm-select-focus - Visible keyboard focus indicator color.
210
+ * @cssprop --cosm-select-radius - Control corner radius.
211
+ * @cssprop --cosm-select-trigger-min-height - Custom minimum trigger height.
212
+ * @cssprop --cosm-select-option-selected-background - Selected option background.
213
+ * @cssprop --cosm-select-option-selected-color - Selected option text color.
214
+ */
215
+ export class CosmSelect extends CosmElement {
216
+ static formAssociated = true;
217
+ static shadowRootOptions = {
218
+ ...LitElement.shadowRootOptions,
219
+ delegatesFocus: true,
220
+ };
221
+ static properties = {
222
+ label: { type: String },
223
+ items: { attribute: false },
224
+ value: { attribute: false, noAccessor: true },
225
+ defaultValue: { attribute: false },
226
+ placeholder: { type: String },
227
+ variant: { reflect: true },
228
+ size: { reflect: true },
229
+ disabled: { type: Boolean, reflect: true },
230
+ required: { type: Boolean, reflect: true },
231
+ invalid: { type: Boolean, reflect: true },
232
+ helperText: { type: String, attribute: 'helper-text' },
233
+ errorText: { type: String, attribute: 'error-text' },
234
+ name: { type: String, reflect: true },
235
+ };
236
+ static styles = [boxSizing, focusRing, styles];
237
+ /** Set by adapters to distinguish a bound value, including null, from autonomous state. */
238
+ valueControlled = false;
239
+ #internals;
240
+ #id = `cosm-select-${++instance}`;
241
+ #value = null;
242
+ #formDisabled = false;
243
+ #initialized = false;
244
+ #open = false;
245
+ #activeValue = null;
246
+ #nativePopoverOpen = false;
247
+ #typeahead = '';
248
+ #typeaheadAt = 0;
249
+ #showNativeError = false;
250
+ #resizeObserver;
251
+ get value() {
252
+ return this.#value;
253
+ }
254
+ set value(value) {
255
+ this.valueControlled = value !== undefined;
256
+ this.#setValue(value ?? null);
257
+ }
258
+ constructor() {
259
+ super();
260
+ const props = selectContract.props;
261
+ this.label = '';
262
+ this.items = [];
263
+ this.defaultValue = undefined;
264
+ this.placeholder = '';
265
+ this.variant = props.variant.default;
266
+ this.size = props.size.default;
267
+ this.disabled = props.disabled.default;
268
+ this.required = props.required.default;
269
+ this.invalid = props.invalid.default;
270
+ this.helperText = '';
271
+ this.errorText = '';
272
+ this.name = undefined;
273
+ this.#internals =
274
+ typeof this.attachInternals === 'function' ? this.attachInternals() : undefined;
275
+ }
276
+ connectedCallback() {
277
+ super.connectedCallback();
278
+ this.addEventListener('invalid', this.#onInvalid);
279
+ this.addEventListener('focusout', this.#onFocusOut);
280
+ }
281
+ disconnectedCallback() {
282
+ this.removeEventListener('invalid', this.#onInvalid);
283
+ this.removeEventListener('focusout', this.#onFocusOut);
284
+ this.#closePopup(false);
285
+ super.disconnectedCallback();
286
+ }
287
+ get form() {
288
+ return this.#internals?.form ?? this.closest('form');
289
+ }
290
+ focus(options) {
291
+ this.#trigger()?.focus(options);
292
+ }
293
+ checkValidity() {
294
+ return this.#internals?.checkValidity() ?? true;
295
+ }
296
+ reportValidity() {
297
+ this.#showNativeError = true;
298
+ this.requestUpdate();
299
+ this.#syncFormState();
300
+ return this.#internals?.reportValidity() ?? true;
301
+ }
302
+ formDisabledCallback(disabled) {
303
+ this.#formDisabled = disabled;
304
+ this.requestUpdate();
305
+ }
306
+ formResetCallback() {
307
+ this.#showNativeError = false;
308
+ const resetValue = this.defaultValue ?? null;
309
+ if (this.valueControlled) {
310
+ this.emit('cosm-change', { value: resetValue });
311
+ return;
312
+ }
313
+ this.#setValue(resetValue);
314
+ }
315
+ formStateRestoreCallback(state) {
316
+ if (typeof state !== 'string')
317
+ return;
318
+ const restoredValue = state || null;
319
+ const options = getListboxOptions(this.items);
320
+ validateSelectValue(restoredValue, options, 'value');
321
+ this.#showNativeError = false;
322
+ if (this.valueControlled) {
323
+ this.emit('cosm-change', { value: restoredValue });
324
+ return;
325
+ }
326
+ this.#setValue(restoredValue);
327
+ }
328
+ willUpdate() {
329
+ if (typeof this.label !== 'string' || !this.label.trim()) {
330
+ throw new TypeError('Select requires a non-empty label.');
331
+ }
332
+ if (typeof this.placeholder !== 'string' || !this.placeholder.trim()) {
333
+ throw new TypeError('Select requires a non-empty placeholder.');
334
+ }
335
+ if (!['outlined', 'filled'].includes(this.variant)) {
336
+ throw new TypeError('Select variant must be outlined or filled.');
337
+ }
338
+ if (!['sm', 'md', 'lg'].includes(this.size)) {
339
+ throw new TypeError('Select size must be sm, md, or lg.');
340
+ }
341
+ for (const [name, value] of [
342
+ ['disabled', this.disabled],
343
+ ['required', this.required],
344
+ ['invalid', this.invalid],
345
+ ]) {
346
+ if (typeof value !== 'boolean')
347
+ throw new TypeError(`Select ${name} must be a boolean.`);
348
+ }
349
+ if (typeof this.helperText !== 'string' ||
350
+ typeof this.errorText !== 'string' ||
351
+ (typeof this.name !== 'string' && this.name !== undefined)) {
352
+ throw new TypeError('Select helperText, errorText and name must be strings.');
353
+ }
354
+ const options = getListboxOptions(this.items);
355
+ if (options.length === 0)
356
+ throw new TypeError('Select requires at least one option.');
357
+ validateSelectValue(this.value, options, 'value');
358
+ validateSelectValue(this.defaultValue ?? null, options, 'defaultValue');
359
+ if (!this.#initialized) {
360
+ if (!this.valueControlled)
361
+ this.#setValue(this.defaultValue ?? null);
362
+ this.#initialized = true;
363
+ }
364
+ if (!this.#activeValue ||
365
+ !options.some(option => option.value === this.#activeValue && !option.disabled)) {
366
+ this.#activeValue = getListboxInitialActive(options, this.value ? [this.value] : []);
367
+ }
368
+ if (this.#open && (this.disabled || this.#formDisabled)) {
369
+ this.#closePopup(false);
370
+ }
371
+ }
372
+ updated(changed) {
373
+ this.#syncFormState();
374
+ if (this.#open && [...changed.keys()].some(key => key === 'items' || key === 'size')) {
375
+ this.#positionPopup();
376
+ }
377
+ }
378
+ #trigger = () => this.renderRoot.querySelector('[part="trigger"]');
379
+ #popup = () => this.renderRoot.querySelector('[part="popup"]');
380
+ #setValue(value) {
381
+ const previous = this.#value;
382
+ if (previous === value)
383
+ return;
384
+ this.#value = value;
385
+ this.requestUpdate('value', previous);
386
+ }
387
+ #syncFormState() {
388
+ if (!this.#internals)
389
+ return;
390
+ const disabled = this.disabled || this.#formDisabled;
391
+ const options = getListboxOptions(this.items);
392
+ const selected = options.find(option => option.value === this.value);
393
+ const selectedDisabled = selected?.disabled === true;
394
+ this.#internals.setFormValue(disabled || selectedDisabled ? null : (this.value ?? null), this.value ?? '');
395
+ if (disabled || !this.required || (this.value !== null && !selectedDisabled)) {
396
+ this.#internals.setValidity({});
397
+ return;
398
+ }
399
+ const validation = this.renderRoot.querySelector('.native-validation');
400
+ const trigger = this.#trigger();
401
+ this.#internals.setValidity({ valueMissing: true }, validation?.validationMessage ||
402
+ (this.errorText?.trim() ? this.errorText : '') ||
403
+ 'Please select an option.', trigger ?? undefined);
404
+ }
405
+ #onInvalid = () => {
406
+ this.#showNativeError = true;
407
+ this.requestUpdate();
408
+ };
409
+ #onFocusOut = (event) => {
410
+ if (event.relatedTarget === this ||
411
+ (event.relatedTarget instanceof Node &&
412
+ (this.contains(event.relatedTarget) || this.renderRoot.contains(event.relatedTarget)))) {
413
+ return;
414
+ }
415
+ this.#closePopup(false);
416
+ };
417
+ #proposeValue(value) {
418
+ if (value === this.value)
419
+ return;
420
+ this.#showNativeError = false;
421
+ if (!this.valueControlled)
422
+ this.#setValue(value);
423
+ this.emit('cosm-change', { value });
424
+ if (this.valueControlled)
425
+ this.#syncFormState();
426
+ }
427
+ #onTriggerKeyDown = (event) => {
428
+ if (this.disabled || this.#formDisabled || event.altKey || event.ctrlKey || event.metaKey) {
429
+ return;
430
+ }
431
+ const options = getListboxOptions(this.items);
432
+ if (!this.#open) {
433
+ if (['ArrowDown', 'ArrowUp', 'Enter', ' '].includes(event.key)) {
434
+ event.preventDefault();
435
+ this.#activeValue = getListboxInitialActive(options, this.value ? [this.value] : []);
436
+ this.#openPopup();
437
+ return;
438
+ }
439
+ if (event.key.length === 1 && event.key.trim()) {
440
+ const match = this.#findTypeaheadMatch(event.key, options);
441
+ if (match) {
442
+ event.preventDefault();
443
+ this.#activeValue = match;
444
+ this.#openPopup();
445
+ }
446
+ }
447
+ return;
448
+ }
449
+ if (['ArrowDown', 'ArrowUp', 'Home', 'End'].includes(event.key)) {
450
+ event.preventDefault();
451
+ this.#setActive(moveListboxActive(options, this.#activeValue, event.key));
452
+ return;
453
+ }
454
+ if (event.key === 'Enter' || event.key === ' ') {
455
+ event.preventDefault();
456
+ if (this.#activeValue)
457
+ this.#select(this.#activeValue);
458
+ return;
459
+ }
460
+ if (event.key === 'Escape') {
461
+ event.preventDefault();
462
+ this.#closePopup(true);
463
+ return;
464
+ }
465
+ if (event.key.length === 1 && event.key.trim() && event.key !== ' ') {
466
+ const match = this.#findTypeaheadMatch(event.key, options);
467
+ if (match)
468
+ this.#setActive(match);
469
+ event.preventDefault();
470
+ }
471
+ };
472
+ #findTypeaheadMatch(key, options) {
473
+ const now = Date.now();
474
+ this.#typeahead =
475
+ now - this.#typeaheadAt > typeaheadInterval ? key : `${this.#typeahead}${key}`;
476
+ this.#typeaheadAt = now;
477
+ return findListboxMatch(options, this.#activeValue, this.#typeahead, this.closest('[lang]')?.getAttribute('lang') || undefined);
478
+ }
479
+ #setActive(value) {
480
+ this.#activeValue = value;
481
+ this.requestUpdate();
482
+ }
483
+ #select(value) {
484
+ const option = getListboxOptions(this.items).find(candidate => candidate.value === value);
485
+ if (!option || option.disabled || this.disabled || this.#formDisabled)
486
+ return;
487
+ if (this.value !== value)
488
+ this.#proposeValue(value);
489
+ this.#closePopup(true);
490
+ }
491
+ #openPopup = () => {
492
+ if (this.#open || this.disabled || this.#formDisabled)
493
+ return;
494
+ this.#open = true;
495
+ this.requestUpdate();
496
+ document.addEventListener('pointerdown', this.#onDocumentPointerDown, true);
497
+ window.addEventListener('resize', this.#positionPopup);
498
+ window.addEventListener('scroll', this.#positionPopup, true);
499
+ void this.updateComplete.then(() => {
500
+ const popup = this.#popup();
501
+ if (!popup || !this.isConnected || !this.#open)
502
+ return;
503
+ if (typeof popup.showPopover === 'function' && !this.#nativePopoverOpen) {
504
+ popup.showPopover();
505
+ this.#nativePopoverOpen = true;
506
+ }
507
+ this.#positionPopup();
508
+ if (typeof ResizeObserver !== 'undefined') {
509
+ this.#resizeObserver = new ResizeObserver(this.#positionPopup);
510
+ this.#resizeObserver.observe(popup);
511
+ const trigger = this.#trigger();
512
+ if (trigger)
513
+ this.#resizeObserver.observe(trigger);
514
+ }
515
+ });
516
+ };
517
+ #closePopup = (restoreFocus) => {
518
+ if (!this.#open)
519
+ return;
520
+ this.#open = false;
521
+ this.#resizeObserver?.disconnect();
522
+ this.#resizeObserver = undefined;
523
+ document.removeEventListener('pointerdown', this.#onDocumentPointerDown, true);
524
+ window.removeEventListener('resize', this.#positionPopup);
525
+ window.removeEventListener('scroll', this.#positionPopup, true);
526
+ const popup = this.#popup();
527
+ if (this.#nativePopoverOpen && popup && typeof popup.hidePopover === 'function') {
528
+ popup.hidePopover();
529
+ this.#nativePopoverOpen = false;
530
+ }
531
+ this.requestUpdate();
532
+ if (restoreFocus)
533
+ void this.updateComplete.then(() => this.#trigger()?.focus());
534
+ };
535
+ #onDocumentPointerDown = (event) => {
536
+ if (event.composedPath().includes(this))
537
+ return;
538
+ this.#closePopup(false);
539
+ };
540
+ #positionPopup = () => {
541
+ if (!this.#open)
542
+ return;
543
+ const trigger = this.#trigger();
544
+ const popup = this.#popup();
545
+ if (!trigger || !popup)
546
+ return;
547
+ const rect = trigger.getBoundingClientRect();
548
+ const viewportWidth = document.documentElement.clientWidth;
549
+ const viewportHeight = window.innerHeight;
550
+ const margin = 8;
551
+ const gap = 4;
552
+ const below = viewportHeight - rect.bottom - margin - gap;
553
+ const above = rect.top - margin - gap;
554
+ const maxHeight = Math.max(0, Math.min(320, Math.max(below, above)));
555
+ popup.style.maxHeight = `${maxHeight}px`;
556
+ popup.style.minWidth = `${Math.min(rect.width, viewportWidth - margin * 2)}px`;
557
+ const popupHeight = popup.getBoundingClientRect().height;
558
+ const placeBelow = below >= Math.min(popupHeight, maxHeight) || below >= above;
559
+ const top = placeBelow
560
+ ? rect.bottom + gap
561
+ : Math.max(margin, rect.top - Math.min(popupHeight, maxHeight) - gap);
562
+ const left = Math.max(margin, Math.min(rect.left, viewportWidth - rect.width - margin));
563
+ popup.style.top = `${top}px`;
564
+ popup.style.left = `${left}px`;
565
+ };
566
+ #renderOption(option, index) {
567
+ const selected = option.value === this.value;
568
+ const active = option.value === this.#activeValue;
569
+ const disabled = Boolean(option.disabled);
570
+ return html `
571
+ <div
572
+ part="option"
573
+ class="option"
574
+ role="option"
575
+ id="${this.#id}-option-${index}"
576
+ data-value=${option.value}
577
+ data-selected=${String(selected)}
578
+ data-active=${String(active)}
579
+ data-disabled=${String(disabled)}
580
+ aria-selected=${String(selected)}
581
+ aria-disabled=${disabled ? 'true' : nothing}
582
+ @click=${() => this.#select(option.value)}
583
+ >
584
+ <span part="content" class="option-content">
585
+ <span part="option-label" class="option-label">${option.label}</span>
586
+ </span>
587
+ ${selected
588
+ ? html `<cosm-icon class="check" aria-hidden="true" .icon=${check}></cosm-icon>`
589
+ : nothing}
590
+ </div>
591
+ `;
592
+ }
593
+ render() {
594
+ const effectiveDisabled = this.disabled || this.#formDisabled;
595
+ const errorText = this.errorText?.trim() ? this.errorText : '';
596
+ const invalid = this.invalid || Boolean(errorText) || this.#showNativeError;
597
+ const options = getListboxOptions(this.items);
598
+ const selected = options.find(option => option.value === this.value);
599
+ const activeIndex = options.findIndex(option => option.value === this.#activeValue && !option.disabled);
600
+ const describedBy = [
601
+ this.helperText ? `${this.#id}-help` : '',
602
+ errorText || this.#showNativeError ? `${this.#id}-error` : '',
603
+ ]
604
+ .filter(Boolean)
605
+ .join(' ');
606
+ const items = options.map((option, index) => this.#renderOption(option, index));
607
+ return html `
608
+ <div part="base" class="base" data-disabled=${String(effectiveDisabled)}>
609
+ <label part="label" class="label" id="${this.#id}-label" for="${this.#id}-trigger">
610
+ ${this.label}
611
+ </label>
612
+ <div class="control">
613
+ <button
614
+ part="trigger"
615
+ class="trigger"
616
+ id="${this.#id}-trigger"
617
+ type="button"
618
+ role="combobox"
619
+ aria-labelledby="${this.#id}-label ${this.#id}-value"
620
+ aria-controls="${this.#id}-listbox"
621
+ aria-haspopup="listbox"
622
+ aria-expanded=${String(this.#open)}
623
+ aria-activedescendant=${this.#open && activeIndex >= 0 ? `${this.#id}-option-${activeIndex}` : nothing}
624
+ aria-required=${this.required ? 'true' : 'false'}
625
+ aria-invalid=${invalid ? 'true' : 'false'}
626
+ aria-describedby=${describedBy || nothing}
627
+ ?disabled=${effectiveDisabled}
628
+ @keydown=${this.#onTriggerKeyDown}
629
+ @click=${() => {
630
+ if (this.#open) {
631
+ this.#closePopup(true);
632
+ return;
633
+ }
634
+ this.#activeValue = getListboxInitialActive(options, this.value ? [this.value] : []);
635
+ this.#openPopup();
636
+ }}
637
+ >
638
+ <span
639
+ part=${selected ? 'value' : 'value placeholder'}
640
+ class=${selected ? 'value' : 'value placeholder'}
641
+ id="${this.#id}-value"
642
+ >${selected?.label ?? this.placeholder}</span>
643
+ <cosm-icon class="trigger-icon" aria-hidden="true" .icon=${chevronDown}></cosm-icon>
644
+ </button>
645
+ </div>
646
+ <div
647
+ part="popup"
648
+ class="popup"
649
+ popover="manual"
650
+ data-open=${String(this.#open)}
651
+ >
652
+ <div
653
+ part="listbox"
654
+ class="listbox"
655
+ id="${this.#id}-listbox"
656
+ role="listbox"
657
+ aria-labelledby="${this.#id}-label"
658
+ aria-required=${this.required ? 'true' : 'false'}
659
+ >${items}</div>
660
+ </div>
661
+ <select
662
+ class="native-validation"
663
+ tabindex="-1"
664
+ aria-hidden="true"
665
+ .value=${this.value ?? ''}
666
+ ?required=${this.required}
667
+ >
668
+ <option value=""></option>
669
+ ${options.map(option => html `<option value=${option.value} ?disabled=${option.disabled}>${option.label}</option>`)}
670
+ </select>
671
+ ${this.helperText
672
+ ? html `<div part="help" class="help" id="${this.#id}-help">${this.helperText}</div>`
673
+ : nothing}
674
+ ${errorText || this.#showNativeError
675
+ ? html `<div part="error" class="error" id="${this.#id}-error">${errorText || this.renderRoot.querySelector('.native-validation')?.validationMessage || ''}</div>`
676
+ : nothing}
677
+ </div>
678
+ `;
679
+ }
680
+ }
681
+ define('cosm-select', CosmSelect);
682
+ //# sourceMappingURL=cosm-select.js.map