tyrell-components 1.0.0-RC14 → 1.0.0-RC15

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 (113) hide show
  1. package/README.md +9 -1
  2. package/css/tyrell-theme.css +25 -2
  3. package/css/tyrell.css +4 -0
  4. package/dist/tyrell-colors-static.css +1 -316
  5. package/dist/tyrell-theme.css +1 -1443
  6. package/dist/tyrell.css +1 -1390
  7. package/dist/tyrell.js +1 -1
  8. package/lib/base/ty-component.js +196 -196
  9. package/lib/components/button.d.ts +1 -1
  10. package/lib/components/button.js +261 -259
  11. package/lib/components/calendar-month.d.ts +2 -2
  12. package/lib/components/calendar-month.js +439 -416
  13. package/lib/components/calendar-navigation.js +238 -238
  14. package/lib/components/calendar.d.ts +5 -3
  15. package/lib/components/calendar.js +611 -602
  16. package/lib/components/checkbox.d.ts +1 -1
  17. package/lib/components/checkbox.js +273 -272
  18. package/lib/components/copy.d.ts +1 -1
  19. package/lib/components/copy.js +280 -278
  20. package/lib/components/date-picker.d.ts +8 -1
  21. package/lib/components/date-picker.js +1128 -1108
  22. package/lib/components/file-upload.js +404 -404
  23. package/lib/components/icon.js +141 -141
  24. package/lib/components/input.d.ts +1 -1
  25. package/lib/components/input.js +593 -596
  26. package/lib/components/modal.js +319 -319
  27. package/lib/components/option.js +167 -167
  28. package/lib/components/popup.js +233 -233
  29. package/lib/components/radio.d.ts +2 -2
  30. package/lib/components/radio.js +407 -406
  31. package/lib/components/resize-observer.js +67 -67
  32. package/lib/components/scroll-container.js +292 -294
  33. package/lib/components/select.d.ts +71 -12
  34. package/lib/components/select.js +1605 -1529
  35. package/lib/components/selected-tags.d.ts +1 -1
  36. package/lib/components/selected-tags.js +188 -182
  37. package/lib/components/step.js +62 -62
  38. package/lib/components/switch.d.ts +1 -1
  39. package/lib/components/switch.js +214 -213
  40. package/lib/components/tab.js +62 -62
  41. package/lib/components/tabs.d.ts +1 -0
  42. package/lib/components/tabs.js +523 -492
  43. package/lib/components/tag.d.ts +1 -1
  44. package/lib/components/tag.js +236 -235
  45. package/lib/components/textarea.d.ts +1 -1
  46. package/lib/components/textarea.js +457 -456
  47. package/lib/components/tooltip.js +283 -283
  48. package/lib/components/wizard.js +384 -384
  49. package/lib/icons/heroicons/micro.js +392 -392
  50. package/lib/icons/heroicons/mini.js +382 -382
  51. package/lib/icons/heroicons/outline.js +337 -337
  52. package/lib/icons/heroicons/solid.js +407 -407
  53. package/lib/icons/lucide.js +22843 -22843
  54. package/lib/icons/material/filled.js +9653 -9653
  55. package/lib/icons/material/outlined.js +10271 -10271
  56. package/lib/icons/material/round.js +10495 -10495
  57. package/lib/icons/material/sharp.js +9905 -9905
  58. package/lib/icons/material/two-tone.js +12792 -12792
  59. package/lib/index.js +21 -21
  60. package/lib/styles/button.js +200 -302
  61. package/lib/styles/calendar-month.js +123 -183
  62. package/lib/styles/calendar-navigation.d.ts +1 -1
  63. package/lib/styles/calendar-navigation.js +64 -79
  64. package/lib/styles/calendar.d.ts +1 -1
  65. package/lib/styles/calendar.js +9 -9
  66. package/lib/styles/checkbox.js +45 -72
  67. package/lib/styles/copy.js +67 -91
  68. package/lib/styles/custom-scrollbar.js +80 -100
  69. package/lib/styles/date-picker.js +261 -331
  70. package/lib/styles/field-size.d.ts +6 -0
  71. package/lib/styles/field-size.js +91 -0
  72. package/lib/styles/file-upload.js +139 -169
  73. package/lib/styles/icon.js +92 -156
  74. package/lib/styles/input.js +125 -241
  75. package/lib/styles/modal.js +67 -102
  76. package/lib/styles/option.d.ts +1 -1
  77. package/lib/styles/option.js +47 -67
  78. package/lib/styles/popup.js +34 -65
  79. package/lib/styles/radio.js +69 -96
  80. package/lib/styles/resize-observer.js +2 -5
  81. package/lib/styles/scroll-container.js +100 -130
  82. package/lib/styles/select-base.js +596 -773
  83. package/lib/styles/select.js +140 -228
  84. package/lib/styles/step.js +22 -29
  85. package/lib/styles/switch.js +34 -55
  86. package/lib/styles/tab.d.ts +1 -1
  87. package/lib/styles/tab.js +22 -29
  88. package/lib/styles/tabs.d.ts +1 -1
  89. package/lib/styles/tabs.js +186 -180
  90. package/lib/styles/tag.js +135 -188
  91. package/lib/styles/textarea.d.ts +1 -1
  92. package/lib/styles/textarea.js +205 -271
  93. package/lib/styles/tooltip.js +3 -3
  94. package/lib/styles/wizard.js +170 -226
  95. package/lib/types/common.d.ts +2 -0
  96. package/lib/types/common.js +6 -5
  97. package/lib/utils/calendar-utils.js +100 -100
  98. package/lib/utils/custom-scrollbar.js +272 -272
  99. package/lib/utils/flavor-sheet.js +14 -14
  100. package/lib/utils/icon-registry.js +127 -127
  101. package/lib/utils/loader-registry.js +3 -3
  102. package/lib/utils/locale.js +34 -34
  103. package/lib/utils/mobile.js +1 -1
  104. package/lib/utils/number-format.js +54 -54
  105. package/lib/utils/positioning.js +212 -212
  106. package/lib/utils/property-capture.js +36 -36
  107. package/lib/utils/property-manager.js +125 -125
  108. package/lib/utils/resize-observer.js +23 -23
  109. package/lib/utils/scroll-lock.js +57 -57
  110. package/lib/utils/styles.js +17 -17
  111. package/lib/version.d.ts +1 -1
  112. package/lib/version.js +1 -1
  113. package/package.json +4 -4
@@ -1,3 +1,4 @@
1
+ import { DEFAULT_SIZE } from "../types/common.js";
1
2
  import { ensureStyles } from "../utils/styles.js";
2
3
  import { syncCustomFlavorSheet } from "../utils/flavor-sheet.js";
3
4
  import { selectBaseStyles, selectCustomFlavorCss } from "../styles/select-base.js";
@@ -11,1548 +12,1623 @@ import { CustomScrollbar, isCustomScrollbarEnabled, } from "../utils/custom-scro
11
12
  let elementIdCounter = 0;
12
13
  const elementIds = new WeakMap();
13
14
  function getElementHash(element) {
14
- let id = elementIds.get(element);
15
- if (id === undefined) {
16
- id = ++elementIdCounter;
17
- elementIds.set(element, id);
18
- }
19
- return id;
15
+ let id = elementIds.get(element);
16
+ if (id === undefined) {
17
+ id = ++elementIdCounter;
18
+ elementIds.set(element, id);
19
+ }
20
+ return id;
20
21
  }
21
22
  import { REQUIRED_ICON_SVG } from "../utils/icons.js";
23
+ const SEARCH_AUTO_THRESHOLD = 7;
22
24
  const CHEVRON_DOWN_SVG = `<svg viewBox="0 0 20 20" fill="currentColor">
23
- <path fill-rule="evenodd" d="M5.293 7.293a1 1 0 011.414 0L10 10.586l3.293-3.293a1 1 0 111.414 1.414l-4 4a1 1 0 01-1.414 0l-4-4a1 1 0 010-1.414z" clip-rule="evenodd" />
25
+ <path fill-rule="evenodd" d="M5.293 7.293a1 1 0 011.414 0L10 10.586l3.293-3.293a1 1 0 111.414 1.414l-4 4a1 1 0 01-1.414 0l-4-4a1 1 0 010-1.414z" clip-rule="evenodd" />
24
26
  </svg>`;
25
- const SEARCH_AUTO_THRESHOLD = 7;
26
27
  function slugify(text) {
27
- return text
28
- .trim()
29
- .toLowerCase()
30
- .replace(/\s+/g, "_")
31
- .replace(/[^a-z0-9_]/g, "")
32
- .replace(/_+/g, "_")
33
- .replace(/^_|_$/g, "");
28
+ return text
29
+ .trim()
30
+ .toLowerCase()
31
+ .replace(/\s+/g, "_")
32
+ .replace(/[^a-z0-9_]/g, "")
33
+ .replace(/_+/g, "_")
34
+ .replace(/^_|_$/g, "");
34
35
  }
35
36
  const SEARCH_ICON_SVG = `<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
36
- <circle cx="11" cy="11" r="8"/><path d="m21 21-4.3-4.3"/>
37
+ <circle cx="11" cy="11" r="8"/><path d="m21 21-4.3-4.3"/>
37
38
  </svg>`;
38
39
  const CLEAR_ICON_SVG = `<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
39
- <line x1="18" y1="6" x2="6" y2="18"></line>
40
- <line x1="6" y1="6" x2="18" y2="18"></line>
40
+ <line x1="18" y1="6" x2="6" y2="18"></line>
41
+ <line x1="6" y1="6" x2="18" y2="18"></line>
41
42
  </svg>`;
42
43
  export class TySelect extends TyComponent {
43
- constructor() {
44
- super();
45
- this._name = "";
46
- this._multiple = false;
47
- this._compact = false;
48
- this._placeholder = "Select...";
49
- this._label = "";
50
- this._disabled = false;
51
- this._readonly = false;
52
- this._required = false;
53
- this._clearable = true;
54
- this._externalSearch = false;
55
- this._allowCreate = false;
56
- this._createTransform = "none";
57
- this._searchable = "auto";
58
- this._loading = false;
59
- this._scrollLockId = null;
60
- this._size = "md";
61
- this._availableLabel = "Available";
62
- this._noOptionsMessage = "No options available";
63
- this._state = {
64
- open: false,
65
- search: "",
66
- highlightedIndex: -1,
67
- filteredTags: [],
68
- selectedValues: [],
69
- mode: "desktop",
70
- };
71
- this._stubClickHandler = null;
72
- this._tagClickHandler = null;
73
- this._searchInputHandler = null;
74
- this._blockSearchClick = null;
75
- this._keyboardHandler = null;
76
- this._debounce = 0;
77
- this._searchDebounceTimer = null;
78
- this._optionsScrollbar = null;
79
- this._childObserver = null;
80
- this._selectedClone = null;
81
- this._customFlavorSheet = null;
82
- const shadow = this.shadowRoot;
83
- ensureStyles(shadow, { css: selectBaseStyles, id: "ty-select-base" });
84
- ensureStyles(shadow, { css: selectStyles, id: "ty-select" });
85
- }
86
- onConnect() {
87
- const shadow = this.shadowRoot;
88
- const dialogs = shadow.querySelectorAll("dialog");
89
- dialogs.forEach((dialog) => {
90
- if (dialog.open) {
91
- console.warn("⚠️ Found open dialog on connect, closing it");
92
- dialog.close();
93
- }
94
- });
95
- this.render();
96
- requestAnimationFrame(() => {
97
- this.initializeState();
98
- });
99
- this._childObserver = new MutationObserver(() => {
100
- this.ensureOptionRoles();
101
- if (this._state.selectedValues.length > 0) {
102
- this.syncSelectedTags(this._state.selectedValues);
103
- }
104
- this.updateSearchVisibility();
105
- this.updateSelectionDisplay();
106
- this.updateMobileSelectedState();
107
- });
108
- this._childObserver.observe(this, { childList: true });
109
- this.ensureOptionRoles();
110
- this._syncCustomFlavor();
111
- }
112
- _syncCustomFlavor() {
113
- this._customFlavorSheet = syncCustomFlavorSheet(this.shadowRoot, this._customFlavorSheet, this.getProperty("flavor"), ({ base }) => selectCustomFlavorCss(base));
114
- }
115
- onDisconnect() {
116
- const shadow = this.shadowRoot;
117
- const dialogs = shadow.querySelectorAll("dialog");
118
- dialogs.forEach((dialog) => {
119
- if (dialog.open) {
120
- dialog.close();
121
- }
122
- });
123
- if (this._keyboardHandler) {
124
- document.removeEventListener("keydown", this._keyboardHandler);
125
- this._keyboardHandler = null;
126
- }
127
- if (this._searchDebounceTimer !== null) {
128
- clearTimeout(this._searchDebounceTimer);
129
- this._searchDebounceTimer = null;
130
- }
131
- this._destroyOptionsScrollbar();
132
- if (this._childObserver) {
133
- this._childObserver.disconnect();
134
- this._childObserver = null;
135
- }
136
- }
137
- onPropertiesChanged(changes) {
138
- for (const { name, newValue } of changes) {
139
- switch (name) {
140
- case "value":
141
- const selectedValues = this.parseValue(newValue);
142
- this._state.selectedValues = selectedValues;
143
- if (this.isConnected && this.shadowRoot) {
144
- this.syncSelectedTags(selectedValues);
145
- this.updateSelectionDisplay();
146
- this.updateMobileSelectedState();
147
- }
148
- break;
149
- case "name":
150
- this._name = newValue || "";
151
- break;
152
- case "flavor":
153
- this._syncCustomFlavor();
154
- break;
155
- case "multiple":
156
- this._multiple = newValue;
157
- if (!newValue && this._state.selectedValues.length > 1) {
158
- this.updateComponentValue([this._state.selectedValues[0]], false, "set", null);
159
- }
160
- break;
161
- case "compact":
162
- this._compact = newValue;
163
- break;
164
- case "placeholder":
165
- this._placeholder = newValue || "Select...";
166
- if (this.isConnected && this.shadowRoot)
167
- this.updateSelectionDisplay();
168
- break;
169
- case "label":
170
- this._label = newValue || "";
171
- break;
172
- case "disabled":
173
- this._disabled = newValue;
174
- break;
175
- case "readonly":
176
- this._readonly = newValue;
177
- break;
178
- case "required":
179
- this._required = newValue;
180
- break;
181
- case "clearable":
182
- this._clearable = newValue;
183
- if (this.isConnected && this.shadowRoot)
184
- this.updateSelectionDisplay();
185
- break;
186
- case "externalSearch":
187
- this._externalSearch = newValue;
188
- break;
189
- case "searchable":
190
- this._searchable = newValue;
191
- if (this.isConnected && this.shadowRoot)
192
- this.updateSearchVisibility();
193
- break;
194
- case "allowCreate":
195
- this._allowCreate = newValue;
196
- if (this.isConnected && this.shadowRoot)
197
- this.updateSearchVisibility();
198
- break;
199
- case "createTransform":
200
- this._createTransform = newValue;
201
- break;
202
- case "size":
203
- this._size = newValue;
204
- break;
205
- case "debounce":
206
- this._debounce = newValue;
207
- break;
208
- case "available-label":
209
- this._availableLabel = newValue || "Available";
210
- break;
211
- case "no-options-message":
212
- this._noOptionsMessage = newValue || "No options available";
213
- break;
214
- case "loading":
215
- this._loading = newValue;
216
- this.applyLoadingState();
217
- break;
218
- }
219
- }
220
- }
221
- applyLoadingState() {
222
- const shadow = this.shadowRoot;
223
- if (!shadow)
224
- return;
225
- const svg = this._loading ? getLoaderSvg() : null;
226
- shadow.querySelectorAll(".dropdown-options-wrapper").forEach((wrapper) => {
227
- wrapper.classList.toggle("loading", this._loading);
228
- if (this._loading) {
229
- wrapper.setAttribute("aria-busy", "true");
230
- const spinner = wrapper.querySelector(".dropdown-loading-spinner");
231
- if (spinner && svg)
232
- spinner.innerHTML = svg;
233
- }
234
- else {
235
- wrapper.removeAttribute("aria-busy");
236
- }
237
- });
238
- }
239
- getFormValue() {
240
- const selectedValues = this._state.selectedValues;
241
- if (this._name && selectedValues.length > 0) {
242
- if (!this._multiple)
243
- return selectedValues[0];
244
- const formData = new FormData();
245
- selectedValues.forEach((value) => {
246
- formData.append(this._name, value);
247
- });
248
- return formData;
249
- }
250
- return null;
251
- }
252
- parseValue(value) {
253
- if (!value || typeof value !== "string" || value.trim() === "")
254
- return [];
255
- return value
256
- .split(",")
257
- .map((v) => v.trim())
258
- .filter((v) => v !== "");
259
- }
260
- initializeState() {
261
- const initialValue = this.getProperty("value") || "";
262
- if (initialValue) {
263
- const selectedValues = this.parseValue(initialValue);
264
- this._state.selectedValues = selectedValues;
265
- this.syncSelectedTags(selectedValues);
266
- this.updateSelectionDisplay();
267
- this.updateMobileSelectedState();
268
- }
269
- else {
270
- const allTags = this.getTagElements();
271
- const preSelectedTags = allTags
272
- .filter((tag) => tag.hasAttribute("selected"))
273
- .map((tag) => this.getTagData(tag).value);
274
- if (preSelectedTags.length > 0) {
275
- this.updateComponentValue(preSelectedTags, false);
276
- }
277
- }
278
- }
279
- getTagElements() {
280
- return Array.from(this.querySelectorAll("ty-option:not([cloned]), ty-tag:not([cloned])"));
281
- }
282
- getTagData(element) {
283
- const value = element.value ||
284
- element.getAttribute("value") ||
285
- element.textContent ||
286
- "";
287
- const text = element.getAttribute("label") || element.textContent || "";
288
- return { value, text, element };
289
- }
290
- selectTag(tag) {
291
- tag.setAttribute("selected", "");
292
- }
293
- deselectTag(tag) {
294
- tag.removeAttribute("selected");
295
- }
296
- getSelectedValues() {
297
- return this.getTagElements()
298
- .filter((tag) => tag.hasAttribute("selected"))
299
- .map((tag) => this.getTagData(tag).value)
300
- .filter((value) => value !== "");
301
- }
302
- syncSelectedTags(selectedValues) {
303
- const selectedSet = new Set(selectedValues);
304
- const tags = this.getTagElements();
305
- tags.forEach((tag) => {
306
- const tagValue = this.getTagData(tag).value;
307
- const shouldBeSelected = selectedSet.has(tagValue);
308
- const isSelected = tag.hasAttribute("selected");
309
- if (shouldBeSelected && !isSelected) {
310
- this.selectTag(tag);
311
- }
312
- else if (!shouldBeSelected && isSelected) {
313
- this.deselectTag(tag);
314
- }
315
- tag.setAttribute("aria-selected", String(shouldBeSelected));
316
- });
317
- }
318
- ensureOptionRoles() {
319
- this.getTagElements().forEach((tag) => {
320
- if (!tag.hasAttribute("role"))
321
- tag.setAttribute("role", "option");
322
- if (!tag.hasAttribute("aria-selected"))
323
- tag.setAttribute("aria-selected", "false");
324
- });
325
- }
326
- updateComponentValue(newValues, dispatchChange = false, action = "set", item = null) {
327
- const oldValues = this.getSelectedValues();
328
- const valueStr = newValues.join(",");
329
- if (JSON.stringify(newValues.sort()) !== JSON.stringify(oldValues.sort())) {
330
- this.setProperty("value", valueStr);
331
- this.updateValidity();
332
- if (dispatchChange) {
333
- this.dispatchChangeEvent({
334
- value: this._multiple ? [...newValues] : (newValues[0] ?? null),
335
- values: newValues,
336
- items: this.getSelectedItems(newValues),
337
- action,
338
- item,
339
- });
340
- }
341
- }
342
- }
343
- getSelectedItems(values) {
344
- const byValue = new Map(this.getTagElements().map((el) => [this.getTagData(el).value, el]));
345
- return values.map((value) => {
346
- const el = byValue.get(value);
347
- return {
348
- value,
349
- label: el?.getAttribute("label") || el?.textContent?.trim() || value,
350
- flavor: el?.getAttribute("flavor") ?? null,
351
- };
352
- });
353
- }
354
- calculatePosition() {
355
- const shadow = this.shadowRoot;
356
- const stub = shadow.querySelector(".select-stub");
357
- const dialog = shadow.querySelector(".dropdown-dialog");
358
- if (!stub || !dialog)
359
- return;
360
- const stubRect = stub.getBoundingClientRect();
361
- const MAX_POPUP_ESTIMATE = 440;
362
- const dialogRect = dialog.getBoundingClientRect();
363
- const estimatedHeight = Math.min(dialogRect.height || 200, MAX_POPUP_ESTIMATE);
364
- const wrapPadding = 20;
365
- const cssWidth = parseFloat(getComputedStyle(this).getPropertyValue("--ty-select-popup-width"));
366
- const popupWidth = !isNaN(cssWidth)
367
- ? cssWidth
368
- : Math.max(stubRect.width, 320);
369
- const requested = this.getProperty("placement");
370
- const anchored = requested
371
- ? placementToAnchored(requested)
372
- : {
373
- side: "auto",
374
- align: this.getProperty("align"),
375
- };
376
- const pos = computeAnchoredPosition({
377
- anchorRect: stubRect,
378
- popupWidth,
379
- popupHeight: estimatedHeight,
380
- align: anchored.align,
381
- side: anchored.side,
382
- gap: 8,
383
- });
384
- const positionBelow = pos.below;
385
- const x = pos.x - wrapPadding;
386
- const y = (positionBelow ? pos.topY : pos.bottomY) - wrapPadding;
387
- const width = popupWidth + wrapPadding + wrapPadding;
388
- this.style.setProperty("--dropdown-x", `${x}px`);
389
- this.style.setProperty("--dropdown-y", `${y}px`);
390
- this.style.setProperty("--dropdown-width", `${width}px`);
391
- this.style.setProperty("--dropdown-offset-x", "0px");
392
- this.style.setProperty("--dropdown-offset-y", "0px");
393
- this.style.setProperty("--dropdown-padding", `${wrapPadding}px`);
394
- if (positionBelow) {
395
- dialog.classList.add("position-below");
396
- dialog.classList.remove("position-above");
397
- }
398
- else {
399
- dialog.classList.add("position-above");
400
- dialog.classList.remove("position-below");
401
- }
402
- this.style.setProperty("--dropdown-direction", positionBelow ? "below" : "above");
403
- }
404
- _setupOptionsScrollbar() {
405
- if (!isCustomScrollbarEnabled())
406
- return;
407
- const shadow = this.shadowRoot;
408
- const optionsDiv = shadow.querySelector(".dropdown-options");
409
- const optionsWrapper = shadow.querySelector(".dropdown-options-wrapper");
410
- if (!optionsDiv || !optionsWrapper)
411
- return;
412
- this._destroyOptionsScrollbar();
413
- optionsDiv.classList.add("ty-custom-scroll");
414
- this._optionsScrollbar = new CustomScrollbar(optionsDiv, {
415
- vertical: true,
416
- });
417
- if (this._optionsScrollbar.trackY) {
418
- optionsWrapper.appendChild(this._optionsScrollbar.trackY);
419
- }
420
- }
421
- _destroyOptionsScrollbar() {
422
- if (this._optionsScrollbar) {
423
- this._optionsScrollbar.trackY?.remove();
424
- this._optionsScrollbar.destroy();
425
- this._optionsScrollbar = null;
426
- }
427
- }
428
- openDropdown() {
429
- const shadow = this.shadowRoot;
430
- const dialog = shadow.querySelector(".dropdown-dialog");
431
- if (!dialog)
432
- return;
433
- const stubEl = shadow.querySelector(".select-stub");
434
- if (stubEl)
435
- stubEl.setAttribute("aria-expanded", "true");
436
- const lockId = `select-${this.id || "anon"}-${getElementHash(this)}`;
437
- this._scrollLockId = lockId;
438
- lockScroll(lockId);
439
- dialog.showModal();
440
- this.calculatePosition();
441
- dialog.classList.add("open");
442
- this._state.open = true;
443
- const chevron = shadow.querySelector(".dropdown-chevron");
444
- if (chevron)
445
- chevron.classList.add("open");
446
- const searchChevron = shadow.querySelector(".dropdown-search-chevron");
447
- if (searchChevron)
448
- searchChevron.classList.add("open");
449
- const tags = this.getTagElements().map((el) => this.getTagData(el));
450
- this._state.filteredTags = tags;
451
- this._state.highlightedIndex = -1;
452
- this.updateOptionsVisibility(true);
453
- this._setupOptionsScrollbar();
454
- const searchInput = shadow.querySelector(".dropdown-search-input");
455
- if (searchInput && this.shouldShowSearch()) {
456
- setTimeout(() => searchInput.focus(), 100);
457
- }
458
- this.fireOpenEvent();
459
- }
460
- closeDropdown() {
461
- const shadow = this.shadowRoot;
462
- const dialog = shadow.querySelector(".dropdown-dialog");
463
- if (!dialog)
464
- return;
465
- const stubEl = shadow.querySelector(".select-stub");
466
- if (stubEl)
467
- stubEl.setAttribute("aria-expanded", "false");
468
- this._destroyOptionsScrollbar();
469
- dialog.classList.remove("open");
470
- dialog.classList.remove("position-above");
471
- dialog.classList.remove("position-below");
472
- dialog.close();
473
- if (this._scrollLockId) {
474
- unlockScroll(this._scrollLockId);
475
- this._scrollLockId = null;
476
- }
477
- this._state.open = false;
478
- this._state.highlightedIndex = -1;
479
- const chevron = shadow.querySelector(".dropdown-chevron");
480
- if (chevron)
481
- chevron.classList.remove("open");
482
- const searchChevron = shadow.querySelector(".dropdown-search-chevron");
483
- if (searchChevron)
484
- searchChevron.classList.remove("open");
485
- const hadQuery = this._state.search !== "";
486
- this._state.search = "";
487
- const searchInput = shadow.querySelector(".dropdown-search-input");
488
- if (searchInput) {
489
- searchInput.value = "";
490
- }
491
- if (this._externalSearch) {
492
- if (hadQuery) {
493
- if (this._searchDebounceTimer !== null) {
494
- clearTimeout(this._searchDebounceTimer);
495
- this._searchDebounceTimer = null;
496
- }
497
- this.fireSearchEvent("");
498
- }
499
- }
500
- else {
501
- const allTags = this.getTagElements().map((el) => this.getTagData(el));
502
- this._state.filteredTags = allTags;
503
- this.updateTagVisibility(allTags, allTags);
504
- this.clearHighlights(allTags);
505
- }
506
- this.fireCloseEvent();
507
- }
508
- openMobileModal() {
509
- const shadow = this.shadowRoot;
510
- const dialog = shadow.querySelector(".mobile-dialog");
511
- if (!dialog)
512
- return;
513
- const stubEl = shadow.querySelector(".select-stub");
514
- if (stubEl)
515
- stubEl.setAttribute("aria-expanded", "true");
516
- const lockId = `select-${this.id || "anon"}-${getElementHash(this)}`;
517
- this._scrollLockId = lockId;
518
- lockScroll(lockId);
519
- dialog.showModal();
520
- dialog.getBoundingClientRect();
521
- dialog.classList.add("open");
522
- this._state.open = true;
523
- const tags = this.getTagElements().map((el) => this.getTagData(el));
524
- this._state.filteredTags = tags;
525
- this.updateTagVisibility(tags, tags);
526
- requestAnimationFrame(() => {
527
- this.updateMobileSelectedState();
528
- });
529
- this.fireOpenEvent();
530
- }
531
- closeMobileModal() {
532
- const shadow = this.shadowRoot;
533
- const dialog = shadow.querySelector(".mobile-dialog");
534
- if (!dialog)
535
- return;
536
- const stubEl = shadow.querySelector(".select-stub");
537
- if (stubEl)
538
- stubEl.setAttribute("aria-expanded", "false");
539
- dialog.classList.remove("open");
540
- dialog.close();
541
- if (this._scrollLockId) {
542
- unlockScroll(this._scrollLockId);
543
- this._scrollLockId = null;
544
- }
545
- this._state.open = false;
546
- this._state.highlightedIndex = -1;
547
- const hadQuery = this._state.search !== "";
548
- this._state.search = "";
549
- const searchInput = shadow.querySelector(".mobile-search-input");
550
- if (searchInput) {
551
- searchInput.value = "";
552
- }
553
- if (this._externalSearch) {
554
- if (hadQuery) {
555
- if (this._searchDebounceTimer !== null) {
556
- clearTimeout(this._searchDebounceTimer);
557
- this._searchDebounceTimer = null;
558
- }
559
- this.fireSearchEvent("");
560
- }
561
- }
562
- else {
563
- const allTags = this.getTagElements();
564
- allTags.forEach((el) => el.removeAttribute("hidden"));
565
- }
566
- this.fireCloseEvent();
567
- }
568
- handleStubClick(e) {
569
- e.preventDefault();
570
- e.stopPropagation();
571
- if (this._disabled || this._readonly) {
572
- return;
573
- }
574
- this.openDropdown();
575
- }
576
- handleClearClick(e) {
577
- e.preventDefault();
578
- e.stopPropagation();
579
- if (this._disabled || this._readonly) {
580
- return;
581
- }
582
- this.clear();
583
- }
584
- handleStubKeydown(e) {
585
- if (this._disabled || this._readonly || this._state.open)
586
- return;
587
- if (e.key === "Enter" || e.key === " " || e.key === "ArrowDown") {
588
- e.preventDefault();
589
- this.openDropdown();
590
- }
591
- }
592
- handleTagClick(e) {
593
- const target = e.target;
594
- const tag = target.closest("ty-option, ty-tag");
595
- if (!tag || tag.hasAttribute("disabled"))
596
- return;
597
- e.preventDefault();
598
- e.stopPropagation();
599
- this.selectByTag(tag);
600
- }
601
- selectByTag(tag) {
602
- const tagValue = this.getTagData(tag).value;
603
- const currentValues = this.getSelectedValues();
604
- if (!this._multiple) {
605
- this.updateComponentValue([tagValue], true, "set", tagValue);
606
- if (this._state.mode === "mobile") {
607
- this.closeMobileModal();
608
- }
609
- else {
610
- this.closeDropdown();
611
- }
612
- return;
613
- }
614
- if (tag.hasAttribute("selected")) {
615
- const newValues = currentValues.filter((v) => v !== tagValue);
616
- this.updateComponentValue(newValues, true, "remove", tagValue);
617
- }
618
- else {
619
- this.updateComponentValue([...currentValues, tagValue], true, "add", tagValue);
620
- }
621
- }
622
- tryCreateFromSearch() {
623
- if (!this._allowCreate)
624
- return false;
625
- const query = (this._state.search || "").trim();
626
- if (!query)
627
- return false;
628
- const queryLower = query.toLowerCase();
629
- const allTags = this.getTagElements().map((el) => this.getTagData(el));
630
- const existing = allTags.find((t) => t.value.toLowerCase() === queryLower ||
631
- t.text.toLowerCase() === queryLower);
632
- if (existing) {
633
- this.selectByTag(existing.element);
634
- return true;
635
- }
636
- const rawLabel = query;
637
- const defaultValue = this._createTransform === "slug" ? slugify(rawLabel) : rawLabel;
638
- if (!defaultValue)
639
- return true;
640
- const detail = { value: defaultValue, label: rawLabel };
641
- const event = new CustomEvent("create", {
642
- detail,
643
- bubbles: true,
644
- composed: true,
645
- cancelable: true,
646
- });
647
- this.dispatchEvent(event);
648
- if (event.defaultPrevented)
649
- return true;
650
- const finalValue = detail.value;
651
- const option = document.createElement("ty-option");
652
- option.setAttribute("value", finalValue);
653
- option.textContent = rawLabel;
654
- this.appendChild(option);
655
- if (!this._multiple) {
656
- this.updateComponentValue([finalValue], true, "create", finalValue);
657
- if (this._state.mode === "mobile") {
658
- this.closeMobileModal();
659
- }
660
- else {
661
- this.closeDropdown();
662
- }
663
- return true;
664
- }
665
- const currentValues = this.getSelectedValues();
666
- this.updateComponentValue([...currentValues, finalValue], true, "create", finalValue);
667
- this.clearSearchAfterCreate();
668
- return true;
669
- }
670
- clearSearchAfterCreate() {
671
- this._state.search = "";
672
- this._state.highlightedIndex = -1;
673
- const shadow = this.shadowRoot;
674
- if (!shadow)
675
- return;
676
- const desktopInput = shadow.querySelector(".dropdown-search-input");
677
- const mobileInput = shadow.querySelector(".mobile-search-input");
678
- if (desktopInput)
679
- desktopInput.value = "";
680
- if (mobileInput)
681
- mobileInput.value = "";
682
- const allTags = this.getTagElements().map((el) => this.getTagData(el));
683
- this._state.filteredTags = allTags;
684
- this.updateTagVisibility(allTags, allTags);
685
- this.updateOptionsVisibility(true);
686
- this.clearHighlights(allTags);
687
- this.updateMobileSelectedState();
688
- (desktopInput ?? mobileInput)?.focus();
689
- }
690
- blockSearchClick(e) {
691
- e.stopPropagation();
692
- e.preventDefault();
693
- }
694
- handleSearchInput(e) {
695
- const target = e.target;
696
- const query = target.value;
697
- this._state.search = query;
698
- if (this._externalSearch) {
699
- this.dispatchSearchEvent(query);
700
- return;
701
- }
702
- const allTags = this.getTagElements().map((el) => this.getTagData(el));
703
- const filtered = this.filterTags(allTags, query);
704
- this._state.filteredTags = filtered;
705
- this._state.highlightedIndex = -1;
706
- this.updateTagVisibility(filtered, allTags);
707
- this.updateOptionsVisibility(filtered.length > 0);
708
- this.updateMobileSelectedState();
709
- this.clearHighlights(allTags);
710
- }
711
- handleKeyboard(e) {
712
- if (!this._state.open)
713
- return;
714
- const shadow = this.shadowRoot;
715
- const searchInput = shadow.querySelector(".dropdown-search-input");
716
- const target = e.target;
717
- const shouldHandle = target === searchInput || document.activeElement !== searchInput;
718
- if (!shouldHandle)
719
- return;
720
- const filteredTags = this._state.filteredTags;
721
- const tagsCount = filteredTags.length;
722
- const currentHighlightedIndex = this._state.highlightedIndex;
723
- switch (e.key) {
724
- case "Escape":
725
- e.preventDefault();
726
- e.stopPropagation();
727
- this.closeDropdown();
728
- break;
729
- case "Enter":
730
- e.preventDefault();
731
- e.stopPropagation();
732
- if (currentHighlightedIndex >= 0 &&
733
- currentHighlightedIndex < tagsCount) {
734
- this.selectByTag(filteredTags[currentHighlightedIndex].element);
735
- }
736
- else {
737
- this.tryCreateFromSearch();
738
- }
739
- break;
740
- case "ArrowUp":
741
- e.preventDefault();
742
- e.stopPropagation();
743
- let newIndexUp;
744
- if (tagsCount === 0) {
745
- newIndexUp = -1;
746
- }
747
- else if (currentHighlightedIndex === -1) {
748
- newIndexUp = tagsCount - 1;
749
- }
750
- else if (currentHighlightedIndex === 0) {
751
- newIndexUp = tagsCount - 1;
752
- }
753
- else {
754
- newIndexUp = currentHighlightedIndex - 1;
755
- }
756
- this._state.highlightedIndex = newIndexUp;
757
- this.highlightTag(filteredTags, newIndexUp);
758
- break;
759
- case "ArrowDown":
760
- e.preventDefault();
761
- e.stopPropagation();
762
- let newIndexDown;
763
- if (tagsCount === 0) {
764
- newIndexDown = -1;
765
- }
766
- else if (currentHighlightedIndex === -1) {
767
- newIndexDown = 0;
768
- }
769
- else if (currentHighlightedIndex === tagsCount - 1) {
770
- newIndexDown = 0;
771
- }
772
- else {
773
- newIndexDown = currentHighlightedIndex + 1;
774
- }
775
- this._state.highlightedIndex = newIndexDown;
776
- this.highlightTag(filteredTags, newIndexDown);
777
- break;
778
- }
779
- }
780
- filterTags(tags, query) {
781
- if (!query || query.trim() === "") {
782
- return tags;
783
- }
784
- const searchLower = query.toLowerCase();
785
- return tags.filter(({ text }) => text.toLowerCase().includes(searchLower));
786
- }
787
- updateTagVisibility(filteredTags, allTags) {
788
- const visibleValues = new Set(filteredTags.map((tag) => tag.value));
789
- allTags.forEach(({ value, element }) => {
790
- if (visibleValues.has(value)) {
791
- element.removeAttribute("hidden");
792
- }
793
- else {
794
- element.setAttribute("hidden", "");
795
- }
796
- });
797
- }
798
- updateOptionsVisibility(hasOptions) {
799
- const shadow = this.shadowRoot;
800
- const options = shadow.querySelector(".dropdown-options");
801
- if (options) {
802
- options.style.display = hasOptions ? "" : "none";
803
- }
804
- }
805
- clearHighlights(tags) {
806
- tags.forEach(({ element }) => {
807
- element.removeAttribute("highlighted");
808
- });
809
- }
810
- highlightTag(tags, index) {
811
- this.clearHighlights(tags);
812
- if (index >= 0 && index < tags.length) {
813
- const { element } = tags[index];
814
- element.setAttribute("highlighted", "");
815
- element.scrollIntoView({
816
- behavior: "smooth",
817
- block: "nearest",
818
- inline: "nearest",
819
- });
820
- }
821
- }
822
- dispatchSearchEvent(query) {
823
- if (this._searchDebounceTimer !== null) {
824
- clearTimeout(this._searchDebounceTimer);
825
- this._searchDebounceTimer = null;
826
- }
827
- if (this._debounce > 0) {
828
- this._searchDebounceTimer = window.setTimeout(() => {
829
- this.fireSearchEvent(query);
830
- this._searchDebounceTimer = null;
831
- }, this._debounce);
832
- }
833
- else {
834
- this.fireSearchEvent(query);
835
- }
836
- }
837
- fireSearchEvent(query) {
838
- this.dispatchEvent(new CustomEvent("search", {
839
- detail: {
840
- query,
841
- element: this,
842
- },
843
- bubbles: true,
844
- composed: true,
845
- }));
846
- }
847
- fireOpenEvent() {
848
- this.dispatchEvent(new CustomEvent("open", {
849
- detail: { mode: this._state.mode, element: this },
850
- }));
851
- if (this._externalSearch) {
852
- this.fireSearchEvent("");
853
- }
854
- }
855
- fireCloseEvent() {
856
- this.dispatchEvent(new CustomEvent("close", {
857
- detail: { mode: this._state.mode, element: this },
858
- }));
859
- }
860
- dispatchChangeEvent(detail) {
861
- this.dispatchEvent(new CustomEvent("change", {
862
- detail,
863
- bubbles: true,
864
- cancelable: true,
865
- }));
866
- }
867
- render() {
868
- this._state.mode = isMobileTouch() ? "mobile" : "desktop";
869
- if (this._state.mode === "mobile") {
870
- this.renderMobile();
871
- }
872
- else {
873
- this.renderDesktop();
874
- }
875
- this.updateSearchVisibility();
876
- this.applyLoadingState();
877
- this.updateValidity();
878
- }
879
- updateValidity() {
880
- if (this.disabled) {
881
- this._internals.setValidity({});
882
- return;
883
- }
884
- if (this.required && this.getSelectedValues().length === 0) {
885
- const stub = this.shadowRoot?.querySelector(".select-stub");
886
- this._internals.setValidity({ valueMissing: true }, "Please select a value", stub ?? undefined);
887
- }
888
- else {
889
- this._internals.setValidity({});
890
- }
891
- }
892
- setupTriggerSlot() {
893
- const shadow = this.shadowRoot;
894
- const stub = shadow.querySelector(".select-stub");
895
- const slot = shadow.querySelector('slot[name="trigger"]');
896
- if (!stub || !slot)
897
- return;
898
- const sync = () => stub.classList.toggle("custom-trigger", slot.assignedElements().length > 0);
899
- slot.addEventListener("slotchange", sync);
900
- sync();
901
- }
902
- setupEventListeners() {
903
- const shadow = this.shadowRoot;
904
- const stub = shadow.querySelector(".select-stub");
905
- const optionsSlot = shadow.querySelector("#options-slot");
906
- const searchInput = shadow.querySelector(".dropdown-search-input");
907
- if (stub) {
908
- this._stubClickHandler = this.handleStubClick.bind(this);
909
- stub.addEventListener("click", this._stubClickHandler);
910
- stub.addEventListener("keydown", this.handleStubKeydown.bind(this));
911
- }
912
- const clearBtn = shadow.querySelector(".select-clear");
913
- clearBtn?.addEventListener("click", this.handleClearClick.bind(this));
914
- this.setupTriggerSlot();
915
- if (optionsSlot) {
916
- this._tagClickHandler = this.handleTagClick.bind(this);
917
- optionsSlot.addEventListener("click", this._tagClickHandler);
918
- }
919
- if (searchInput) {
920
- this._searchInputHandler = this.handleSearchInput.bind(this);
921
- this._blockSearchClick = this.blockSearchClick.bind(this);
922
- searchInput.addEventListener("input", this._searchInputHandler);
923
- searchInput.addEventListener("click", this._blockSearchClick);
924
- }
925
- const dialog = shadow.querySelector(".dropdown-dialog");
926
- if (dialog) {
927
- dialog.addEventListener("click", (e) => {
928
- if (e.target === dialog) {
929
- this.closeDropdown();
930
- }
931
- });
932
- }
933
- this._keyboardHandler = this.handleKeyboard.bind(this);
934
- document.addEventListener("keydown", this._keyboardHandler);
935
- }
936
- buildStubClasses() {
937
- const classes = [this._size];
938
- if (this._disabled)
939
- classes.push("disabled");
940
- if (this._compact)
941
- classes.push("compact");
942
- return classes.join(" ");
943
- }
944
- renderDesktop() {
945
- const shadow = this.shadowRoot;
946
- if (!shadow.querySelector(".select-container")) {
947
- const stubClasses = this.buildStubClasses();
948
- const listboxId = `select-listbox-${getElementHash(this)}`;
949
- const labelId = `select-label-${getElementHash(this)}`;
950
- const labelHtml = this._label
951
- ? `
952
- <label class="ty-field-label" id="${labelId}">
953
- ${this._label}
954
- ${this._required ? `<span class="required-icon">${REQUIRED_ICON_SVG}</span>` : ""}
955
- </label>
956
- `
957
- : "";
958
- const searchPlaceholder = "Search...";
959
- shadow.innerHTML = `
960
- <div class="select-container dropdown-mode-desktop">
961
- ${labelHtml}
962
- <div class="dropdown-wrapper">
963
- <div class="dropdown-stub select-stub ${stubClasses}"
964
- role="combobox"
965
- aria-haspopup="listbox"
966
- aria-expanded="false"
967
- aria-controls="${listboxId}"
968
- ${this._label ? `aria-labelledby="${labelId}"` : ""}
969
- tabindex="${this._disabled ? "-1" : "0"}"
970
- ${this._disabled ? 'disabled aria-disabled="true"' : ""}>
971
- <slot name="trigger">
972
- <slot name="start"></slot>
973
- <slot name="selected"></slot>
974
- <span class="dropdown-placeholder">${this._placeholder}</span>
975
- <span class="select-count" hidden></span>
976
- <slot name="end"></slot>
977
- <button class="select-clear" type="button" aria-label="Clear selection" hidden>
978
- ${CLEAR_ICON_SVG}
979
- </button>
980
- <div class="dropdown-chevron">
981
- ${CHEVRON_DOWN_SVG}
982
- </div>
983
- </slot>
984
- </div>
985
- <dialog class="dropdown-dialog">
986
- <div class="dropdown-header">
987
- <div class="dropdown-search-icon" aria-hidden="true">
988
- ${SEARCH_ICON_SVG}
989
- </div>
990
- <input
991
- class="dropdown-search-input ${this._size}"
992
- type="text"
993
- placeholder="${searchPlaceholder}"
994
- ${this._disabled ? "disabled" : ""}
995
- />
996
- <div class="dropdown-search-chevron">
997
- ${CHEVRON_DOWN_SVG}
998
- </div>
999
- </div>
1000
- <div class="dropdown-options-wrapper">
1001
- <div class="dropdown-options" id="${listboxId}" role="listbox" ${this._multiple ? 'aria-multiselectable="true"' : ""}>
1002
- <slot id="options-slot"></slot>
1003
- </div>
1004
- <div class="dropdown-loading" aria-hidden="true">
1005
- <slot name="loading">
1006
- <span class="dropdown-loading-spinner"></span>
1007
- <span class="dropdown-loading-text">Searching…</span>
1008
- </slot>
1009
- </div>
1010
- </div>
1011
- </dialog>
1012
- </div>
1013
- </div>
1014
- `;
1015
- this.setupEventListeners();
1016
- }
1017
- this.updateSelectionDisplay();
1018
- }
1019
- renderMobile() {
1020
- const shadow = this.shadowRoot;
1021
- if (!shadow.querySelector(".select-container")) {
1022
- const stubClasses = this.buildStubClasses();
1023
- const listboxId = `select-listbox-${getElementHash(this)}`;
1024
- const labelId = `select-label-${getElementHash(this)}`;
1025
- const labelHtml = this._label
1026
- ? `
1027
- <label class="ty-field-label" id="${labelId}">
1028
- ${this._label}
1029
- ${this._required ? `<span class="required-icon">${REQUIRED_ICON_SVG}</span>` : ""}
1030
- </label>
1031
- `
1032
- : "";
1033
- const closeButtonSvg = `<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
1034
- <line x1="18" y1="6" x2="6" y2="18"></line>
1035
- <line x1="6" y1="6" x2="18" y2="18"></line>
1036
- </svg>`;
1037
- const searchPlaceholder = this._label
1038
- ? `Search ${this._label}...`
1039
- : "Search...";
1040
- const searchHeaderHtml = `
1041
- <div class="mobile-search-header">
1042
- ${this._label ? `<span class="mobile-header-label">${this._label}</span>` : ""}
1043
- <div class="mobile-header-content">
1044
- <div class="dropdown-search-icon" aria-hidden="true">
1045
- ${SEARCH_ICON_SVG}
1046
- </div>
1047
- <input
1048
- class="mobile-search-input ${this._size}"
1049
- type="text"
1050
- placeholder="${searchPlaceholder}"
1051
- autofocus
1052
- ${this._disabled ? "disabled" : ""}
1053
- />
1054
- <button class="mobile-close-button" type="button" aria-label="Close">
1055
- ${closeButtonSvg}
1056
- </button>
1057
- </div>
1058
- </div>
1059
- `;
1060
- shadow.innerHTML = `
1061
- <div class="select-container dropdown-mode-mobile">
1062
- ${labelHtml}
1063
- <div class="dropdown-wrapper">
1064
- <div class="dropdown-stub select-stub ${stubClasses}"
1065
- role="combobox"
1066
- aria-haspopup="listbox"
1067
- aria-expanded="false"
1068
- aria-controls="${listboxId}"
1069
- ${this._label ? `aria-labelledby="${labelId}"` : ""}
1070
- tabindex="${this._disabled ? "-1" : "0"}"
1071
- ${this._disabled ? 'disabled aria-disabled="true"' : ""}>
1072
- <slot name="trigger">
1073
- <slot name="start"></slot>
1074
- <slot name="selected"></slot>
1075
- <span class="dropdown-placeholder">${this._placeholder}</span>
1076
- <span class="select-count" hidden></span>
1077
- <slot name="end"></slot>
1078
- <button class="select-clear" type="button" aria-label="Clear selection" hidden>
1079
- ${CLEAR_ICON_SVG}
1080
- </button>
1081
- <div class="dropdown-chevron">
1082
- ${CHEVRON_DOWN_SVG}
1083
- </div>
1084
- </slot>
1085
- </div>
1086
-
1087
- <dialog class="mobile-dialog">
1088
- <div class="mobile-dialog-content">
1089
-
1090
- <!-- HEADER (matches dropdown.ts) -->
1091
- ${searchHeaderHtml}
1092
-
1093
- <!-- BODY: options list with toggleable selected state -->
1094
- <div class="mobile-body">
1095
-
1096
- <!-- AVAILABLE LIST (always visible, takes remaining space) -->
1097
- <div class="mobile-available-section" data-empty="false">
1098
- <div class="section-header">
1099
- <span class="section-title">${this._availableLabel}</span>
1100
- </div>
1101
- <div class="section-content dropdown-options-wrapper" id="${listboxId}" role="listbox" ${this._multiple ? 'aria-multiselectable="true"' : ""}>
1102
- <slot id="options-slot"></slot>
1103
- <div class="empty-state">${this._noOptionsMessage}</div>
1104
- <div class="dropdown-loading" aria-hidden="true">
1105
- <slot name="loading">
1106
- <span class="dropdown-loading-spinner"></span>
1107
- <span class="dropdown-loading-text">Searching…</span>
1108
- </slot>
1109
- </div>
1110
- </div>
1111
- </div>
1112
-
1113
- </div>
1114
- </div>
1115
- </dialog>
1116
- </div>
1117
- </div>
1118
- `;
1119
- this.setupMobileEventListeners();
1120
- }
1121
- this.updateSelectionDisplay();
1122
- }
1123
- setupMobileEventListeners() {
1124
- const shadow = this.shadowRoot;
1125
- const stub = shadow.querySelector(".select-stub");
1126
- const optionsSlot = shadow.querySelector("#options-slot");
1127
- const searchInput = shadow.querySelector(".mobile-search-input");
1128
- const closeButton = shadow.querySelector(".mobile-close-button");
1129
- const dialog = shadow.querySelector(".mobile-dialog");
1130
- if (stub) {
1131
- stub.addEventListener("click", (e) => this.handleMobileStubClick(e));
1132
- stub.addEventListener("keydown", (e) => this.handleMobileStubKeydown(e));
1133
- }
1134
- const clearBtn = shadow.querySelector(".select-clear");
1135
- clearBtn?.addEventListener("click", this.handleClearClick.bind(this));
1136
- this.setupTriggerSlot();
1137
- if (optionsSlot) {
1138
- optionsSlot.addEventListener("click", (e) => this.handleMobileTagClick(e));
1139
- }
1140
- if (searchInput) {
1141
- searchInput.addEventListener("input", (e) => this.handleSearchInput(e));
1142
- }
1143
- if (closeButton) {
1144
- closeButton.addEventListener("click", () => this.closeMobileModal());
1145
- }
1146
- if (dialog) {
1147
- dialog.addEventListener("click", (e) => {
1148
- if (e.target === dialog) {
1149
- this.closeMobileModal();
1150
- }
1151
- });
1152
- dialog.addEventListener("cancel", (e) => {
1153
- e.preventDefault();
1154
- this.closeMobileModal();
1155
- });
1156
- }
1157
- }
1158
- handleMobileStubClick(e) {
1159
- e.preventDefault();
1160
- e.stopPropagation();
1161
- if (this._disabled || this._readonly) {
1162
- return;
1163
- }
1164
- this.openMobileModal();
1165
- }
1166
- handleMobileStubKeydown(e) {
1167
- if (this._disabled || this._readonly || this._state.open)
1168
- return;
1169
- if (e.key === "Enter" || e.key === " " || e.key === "ArrowDown") {
1170
- e.preventDefault();
1171
- this.openMobileModal();
1172
- }
1173
- }
1174
- handleMobileTagClick(e) {
1175
- this.handleTagClick(e);
1176
- }
1177
- updateMobileSelectedState() {
1178
- if (this._state.mode !== "mobile")
1179
- return;
1180
- const shadow = this.shadowRoot;
1181
- const availableSection = shadow.querySelector(".mobile-available-section");
1182
- if (availableSection) {
1183
- const visibleAvailable = this.getTagElements().filter((tag) => !tag.hasAttribute("hidden")).length;
1184
- availableSection.setAttribute("data-empty", String(visibleAvailable === 0));
1185
- const availableTitleSpan = shadow.querySelector(".mobile-available-section .section-title");
1186
- if (availableTitleSpan) {
1187
- availableTitleSpan.textContent = `${this._availableLabel} (${visibleAvailable})`;
1188
- }
1189
- }
1190
- }
1191
- shouldShowSearch() {
1192
- if (this._externalSearch)
1193
- return true;
1194
- if (this._allowCreate)
1195
- return true;
1196
- if (this._searchable === "always")
1197
- return true;
1198
- if (this._searchable === "never")
1199
- return false;
1200
- return this.getTagElements().length > SEARCH_AUTO_THRESHOLD;
1201
- }
1202
- updateSearchVisibility() {
1203
- const shadow = this.shadowRoot;
1204
- if (!shadow)
1205
- return;
1206
- const show = this.shouldShowSearch();
1207
- const header = shadow.querySelector(".dropdown-header");
1208
- if (header)
1209
- header.hidden = !show;
1210
- const mobileInput = shadow.querySelector(".mobile-search-input");
1211
- if (mobileInput)
1212
- mobileInput.hidden = !show;
1213
- const mobileIcon = shadow.querySelector(".mobile-header-content .dropdown-search-icon");
1214
- if (mobileIcon)
1215
- mobileIcon.hidden = !show;
1216
- }
1217
- updateSelectionDisplay() {
1218
- const shadow = this.shadowRoot;
1219
- const stub = shadow.querySelector(".select-stub");
1220
- if (!stub)
1221
- return;
1222
- const values = this._state.selectedValues;
1223
- const count = values.length;
1224
- stub.classList.toggle("has-selection", count > 0);
1225
- stub.classList.toggle("compact", this._compact);
1226
- const badge = stub.querySelector(".select-count");
1227
- if (badge) {
1228
- badge.textContent = String(count);
1229
- badge.hidden = count === 0 || !this._multiple || !this._compact;
1230
- }
1231
- const clearBtn = stub.querySelector(".select-clear");
1232
- if (clearBtn) {
1233
- clearBtn.hidden = !(count > 0 && this._clearable && !this._disabled && !this._readonly);
1234
- }
1235
- const wantClone = !this._multiple && count > 0 ? values[0] : null;
1236
- this.syncSelectedClone(wantClone);
1237
- stub.classList.toggle("has-clone", wantClone !== null);
1238
- const textEl = stub.querySelector(".dropdown-placeholder");
1239
- if (textEl) {
1240
- textEl.hidden = wantClone !== null;
1241
- const showLabels = count > 0 && this._multiple && !this._compact;
1242
- if (showLabels) {
1243
- textEl.textContent = this.getSelectedItems(values)
1244
- .map((i) => i.label)
1245
- .join(", ");
1246
- textEl.classList.remove("placeholder-shown");
1247
- }
1248
- else {
1249
- textEl.textContent = this._placeholder;
1250
- textEl.classList.add("placeholder-shown");
1251
- }
1252
- }
1253
- }
1254
- syncSelectedClone(value) {
1255
- const existing = this.querySelector(':scope > [cloned][slot="selected"]');
1256
- if (existing?.getAttribute("value") === value) {
1257
- this._selectedClone = existing;
1258
- return;
1259
- }
1260
- existing?.remove();
1261
- if (value === null) {
1262
- this._selectedClone = null;
1263
- return;
1264
- }
1265
- const option = this.getTagElements().find((el) => this.getTagData(el).value === value);
1266
- if (option) {
1267
- const clone = option.cloneNode(true);
1268
- clone.removeAttribute("selected");
1269
- clone.setAttribute("slot", "selected");
1270
- clone.setAttribute("cloned", "true");
1271
- this.appendChild(clone);
1272
- this._selectedClone = clone;
1273
- }
1274
- else if (this._selectedClone?.getAttribute("value") === value) {
1275
- const restored = this._selectedClone.cloneNode(true);
1276
- this.appendChild(restored);
1277
- this._selectedClone = restored;
1278
- }
1279
- }
1280
- deselectValue(value) {
1281
- const current = this.getSelectedValues();
1282
- if (!current.includes(value))
1283
- return;
1284
- this.updateComponentValue(current.filter((v) => v !== value), true, "remove", value);
1285
- }
1286
- clear() {
1287
- this.updateComponentValue([], true, "clear", null);
1288
- }
1289
- get value() {
1290
- return this.getSelectedValues().join(",");
1291
- }
1292
- set value(val) {
1293
- this.setProperty("value", val);
1294
- }
1295
- get name() {
1296
- return this.getProperty("name");
1297
- }
1298
- set name(val) {
1299
- this.setProperty("name", val);
1300
- }
1301
- get multiple() {
1302
- return this.getProperty("multiple");
1303
- }
1304
- set multiple(value) {
1305
- this.setProperty("multiple", value);
1306
- }
1307
- get compact() {
1308
- return this.getProperty("compact");
1309
- }
1310
- set compact(value) {
1311
- this.setProperty("compact", value);
1312
- }
1313
- get placeholder() {
1314
- return this.getProperty("placeholder");
1315
- }
1316
- set placeholder(val) {
1317
- this.setProperty("placeholder", val);
1318
- }
1319
- get label() {
1320
- return this.getProperty("label");
1321
- }
1322
- set label(val) {
1323
- this.setProperty("label", val);
1324
- }
1325
- get disabled() {
1326
- return this.getProperty("disabled");
1327
- }
1328
- set disabled(value) {
1329
- this.setProperty("disabled", value);
1330
- }
1331
- get loading() {
1332
- return this.getProperty("loading");
1333
- }
1334
- set loading(value) {
1335
- this.setProperty("loading", value);
1336
- }
1337
- get readonly() {
1338
- return this.getProperty("readonly");
1339
- }
1340
- set readonly(value) {
1341
- this.setProperty("readonly", value);
1342
- }
1343
- get clearable() {
1344
- return this.getProperty("clearable");
1345
- }
1346
- set clearable(value) {
1347
- this.setProperty("clearable", value);
1348
- }
1349
- get required() {
1350
- return this.getProperty("required");
1351
- }
1352
- set required(value) {
1353
- this.setProperty("required", value);
1354
- }
1355
- get searchable() {
1356
- return this.getProperty("searchable");
1357
- }
1358
- set searchable(value) {
1359
- this.setProperty("searchable", value);
1360
- }
1361
- get externalSearch() {
1362
- return this.getProperty("externalSearch");
1363
- }
1364
- set externalSearch(value) {
1365
- this.setProperty("externalSearch", value);
1366
- }
1367
- get debounce() {
1368
- return this.getProperty("debounce");
1369
- }
1370
- set debounce(value) {
1371
- const numValue = typeof value === "string" ? parseInt(value, 10) : value;
1372
- this.setProperty("debounce", numValue);
1373
- }
1374
- get size() {
1375
- return this.getProperty("size");
1376
- }
1377
- set size(value) {
1378
- this.setProperty("size", value);
1379
- }
1380
- get align() {
1381
- return this.getProperty("align");
1382
- }
1383
- set align(value) {
1384
- this.setProperty("align", value);
1385
- }
1386
- get placement() {
1387
- return this.getProperty("placement");
1388
- }
1389
- set placement(value) {
1390
- this.setProperty("placement", value);
1391
- }
1392
- get flavor() {
1393
- return this.getProperty("flavor");
1394
- }
1395
- set flavor(value) {
1396
- this.setProperty("flavor", value);
1397
- }
1398
- get form() {
1399
- return this._internals.form;
1400
- }
44
+ constructor() {
45
+ super();
46
+ this._name = "";
47
+ this._multiple = false;
48
+ this._compact = false;
49
+ this._placeholder = "Select...";
50
+ this._label = "";
51
+ this._disabled = false;
52
+ this._readonly = false;
53
+ this._required = false;
54
+ this._clearable = true;
55
+ this._externalSearch = false;
56
+ this._allowCreate = false;
57
+ this._createTransform = "none";
58
+ this._notSearchable = false;
59
+ this._loading = false;
60
+ this._scrollLockId = null;
61
+ this._size = DEFAULT_SIZE;
62
+ this._availableLabel = "Available";
63
+ this._selectedLabel = "Selected";
64
+ this._noOptionsMessage = "No options available";
65
+ this._state = {
66
+ open: false,
67
+ search: "",
68
+ highlightedIndex: -1,
69
+ filteredTags: [],
70
+ selectedValues: [],
71
+ mode: "desktop",
72
+ };
73
+ this._stubClickHandler = null;
74
+ this._tagClickHandler = null;
75
+ this._searchInputHandler = null;
76
+ this._blockSearchClick = null;
77
+ this._keyboardHandler = null;
78
+ this._debounce = 0;
79
+ this._searchDebounceTimer = null;
80
+ this._optionsScrollbar = null;
81
+ this._childObserver = null;
82
+ this._selectedClone = null;
83
+ this._customFlavorSheet = null;
84
+ const shadow = this.shadowRoot;
85
+ ensureStyles(shadow, { css: selectBaseStyles, id: "ty-select-base" });
86
+ ensureStyles(shadow, { css: selectStyles, id: "ty-select" });
87
+ }
88
+ onConnect() {
89
+ const shadow = this.shadowRoot;
90
+ const dialogs = shadow.querySelectorAll("dialog");
91
+ dialogs.forEach((dialog) => {
92
+ if (dialog.open) {
93
+ console.warn("⚠️ Found open dialog on connect, closing it");
94
+ dialog.close();
95
+ }
96
+ });
97
+ this.render();
98
+ this.initializeState();
99
+ this._childObserver = new MutationObserver(() => {
100
+ this.ensureOptionRoles();
101
+ if (this._state.selectedValues.length > 0) {
102
+ this.syncSelectedTags(this._state.selectedValues);
103
+ }
104
+ this.updateSearchVisibility();
105
+ this.updateSelectionDisplay();
106
+ this.updateMobileSelectedState();
107
+ });
108
+ this._childObserver.observe(this, { childList: true });
109
+ this.ensureOptionRoles();
110
+ this._syncCustomFlavor();
111
+ }
112
+ _syncCustomFlavor() {
113
+ this._customFlavorSheet = syncCustomFlavorSheet(this.shadowRoot, this._customFlavorSheet, this.getProperty("flavor"), ({ base }) => selectCustomFlavorCss(base));
114
+ }
115
+ onDisconnect() {
116
+ const shadow = this.shadowRoot;
117
+ const dialogs = shadow.querySelectorAll("dialog");
118
+ dialogs.forEach((dialog) => {
119
+ if (dialog.open) {
120
+ dialog.close();
121
+ }
122
+ });
123
+ if (this._keyboardHandler) {
124
+ document.removeEventListener("keydown", this._keyboardHandler);
125
+ this._keyboardHandler = null;
126
+ }
127
+ if (this._searchDebounceTimer !== null) {
128
+ clearTimeout(this._searchDebounceTimer);
129
+ this._searchDebounceTimer = null;
130
+ }
131
+ this._destroyOptionsScrollbar();
132
+ if (this._childObserver) {
133
+ this._childObserver.disconnect();
134
+ this._childObserver = null;
135
+ }
136
+ }
137
+ onPropertiesChanged(changes) {
138
+ for (const { name, newValue } of changes) {
139
+ switch (name) {
140
+ case "value":
141
+ const selectedValues = this.parseValue(newValue);
142
+ this._state.selectedValues = selectedValues;
143
+ if (this.isConnected && this.shadowRoot) {
144
+ this.syncSelectedTags(selectedValues);
145
+ this.updateSelectionDisplay();
146
+ this.updateMobileSelectedState();
147
+ }
148
+ break;
149
+ case "name":
150
+ this._name = newValue || "";
151
+ break;
152
+ case "flavor":
153
+ this._syncCustomFlavor();
154
+ break;
155
+ case "multiple":
156
+ this._multiple = newValue;
157
+ if (!newValue && this._state.selectedValues.length > 1) {
158
+ this.updateComponentValue([this._state.selectedValues[0]], false, "set", null);
159
+ }
160
+ break;
161
+ case "compact":
162
+ this._compact = newValue;
163
+ break;
164
+ case "placeholder":
165
+ this._placeholder = newValue || "Select...";
166
+ if (this.isConnected && this.shadowRoot)
167
+ this.updateSelectionDisplay();
168
+ break;
169
+ case "label":
170
+ this._label = newValue || "";
171
+ break;
172
+ case "disabled":
173
+ this._disabled = newValue;
174
+ break;
175
+ case "readonly":
176
+ this._readonly = newValue;
177
+ break;
178
+ case "required":
179
+ this._required = newValue;
180
+ break;
181
+ case "clearable":
182
+ this._clearable = newValue;
183
+ if (this.isConnected && this.shadowRoot)
184
+ this.updateSelectionDisplay();
185
+ break;
186
+ case "externalSearch":
187
+ this._externalSearch = newValue;
188
+ break;
189
+ case "notSearchable":
190
+ this._notSearchable = newValue;
191
+ if (this.isConnected && this.shadowRoot)
192
+ this.updateSearchVisibility();
193
+ break;
194
+ case "allowCreate":
195
+ this._allowCreate = newValue;
196
+ if (this.isConnected && this.shadowRoot)
197
+ this.updateSearchVisibility();
198
+ break;
199
+ case "createTransform":
200
+ this._createTransform = newValue;
201
+ break;
202
+ case "size":
203
+ this._size = newValue;
204
+ break;
205
+ case "debounce":
206
+ this._debounce = newValue;
207
+ break;
208
+ case "available-label":
209
+ this._availableLabel = newValue || "Available";
210
+ break;
211
+ case "selected-label":
212
+ this._selectedLabel = newValue || "Selected";
213
+ break;
214
+ case "no-options-message":
215
+ this._noOptionsMessage = newValue || "No options available";
216
+ break;
217
+ case "loading":
218
+ this._loading = newValue;
219
+ this.applyLoadingState();
220
+ break;
221
+ }
222
+ }
223
+ }
224
+ applyLoadingState() {
225
+ const shadow = this.shadowRoot;
226
+ if (!shadow)
227
+ return;
228
+ const svg = this._loading ? getLoaderSvg() : null;
229
+ shadow.querySelectorAll(".dropdown-options-wrapper").forEach((wrapper) => {
230
+ wrapper.classList.toggle("loading", this._loading);
231
+ if (this._loading) {
232
+ wrapper.setAttribute("aria-busy", "true");
233
+ const spinner = wrapper.querySelector(".dropdown-loading-spinner");
234
+ if (spinner && svg)
235
+ spinner.innerHTML = svg;
236
+ }
237
+ else {
238
+ wrapper.removeAttribute("aria-busy");
239
+ }
240
+ });
241
+ }
242
+ getFormValue() {
243
+ const selectedValues = this._state.selectedValues;
244
+ if (this._name && selectedValues.length > 0) {
245
+ if (!this._multiple)
246
+ return selectedValues[0];
247
+ const formData = new FormData();
248
+ selectedValues.forEach((value) => {
249
+ formData.append(this._name, value);
250
+ });
251
+ return formData;
252
+ }
253
+ return null;
254
+ }
255
+ parseValue(value) {
256
+ if (!value || typeof value !== "string" || value.trim() === "")
257
+ return [];
258
+ return value
259
+ .split(",")
260
+ .map((v) => v.trim())
261
+ .filter((v) => v !== "");
262
+ }
263
+ initializeState() {
264
+ const initialValue = this.getProperty("value") || "";
265
+ if (initialValue) {
266
+ const selectedValues = this.parseValue(initialValue);
267
+ this._state.selectedValues = selectedValues;
268
+ this.syncSelectedTags(selectedValues);
269
+ this.updateSelectionDisplay();
270
+ this.updateMobileSelectedState();
271
+ }
272
+ else {
273
+ this.resolveDeclarativeSelection();
274
+ }
275
+ }
276
+ resolveDeclarativeSelection() {
277
+ if (this.getAttribute("value") || this._state.selectedValues.length > 0) {
278
+ return;
279
+ }
280
+ const preSelectedTags = this.getTagElements()
281
+ .filter((tag) => tag.hasAttribute("selected"))
282
+ .map((tag) => this.getTagData(tag).value);
283
+ if (preSelectedTags.length > 0) {
284
+ this.updateComponentValue(preSelectedTags, false);
285
+ return;
286
+ }
287
+ if (document.readyState === "loading") {
288
+ document.addEventListener("DOMContentLoaded", () => { if (this.isConnected)
289
+ this.resolveDeclarativeSelection(); }, { once: true });
290
+ }
291
+ }
292
+ getTagElements() {
293
+ return Array.from(this.querySelectorAll("ty-option:not([cloned]), ty-tag:not([cloned])"));
294
+ }
295
+ getTagData(element) {
296
+ const value = element.value ||
297
+ element.getAttribute("value") ||
298
+ element.textContent ||
299
+ "";
300
+ const text = element.getAttribute("label") || element.textContent || "";
301
+ return { value, text, element };
302
+ }
303
+ selectTag(tag) {
304
+ tag.setAttribute("selected", "");
305
+ }
306
+ deselectTag(tag) {
307
+ tag.removeAttribute("selected");
308
+ }
309
+ getSelectedValues() {
310
+ return [...this._state.selectedValues];
311
+ }
312
+ syncSelectedTags(selectedValues) {
313
+ const selectedSet = new Set(selectedValues);
314
+ const tags = this.getTagElements();
315
+ tags.forEach((tag) => {
316
+ const tagValue = this.getTagData(tag).value;
317
+ const shouldBeSelected = selectedSet.has(tagValue);
318
+ const isSelected = tag.hasAttribute("selected");
319
+ if (shouldBeSelected && !isSelected) {
320
+ this.selectTag(tag);
321
+ }
322
+ else if (!shouldBeSelected && isSelected) {
323
+ this.deselectTag(tag);
324
+ }
325
+ tag.setAttribute("aria-selected", String(shouldBeSelected));
326
+ });
327
+ }
328
+ ensureOptionRoles() {
329
+ this.getTagElements().forEach((tag) => {
330
+ if (!tag.hasAttribute("role"))
331
+ tag.setAttribute("role", "option");
332
+ if (!tag.hasAttribute("aria-selected"))
333
+ tag.setAttribute("aria-selected", "false");
334
+ });
335
+ }
336
+ updateComponentValue(newValues, dispatchChange = false, action = "set", item = null) {
337
+ const oldValues = this.getSelectedValues();
338
+ const valueStr = newValues.join(",");
339
+ if (JSON.stringify(newValues.sort()) !== JSON.stringify(oldValues.sort())) {
340
+ this.setProperty("value", valueStr);
341
+ this.updateValidity();
342
+ if (dispatchChange) {
343
+ this.dispatchChangeEvent({
344
+ value: this._multiple ? [...newValues] : (newValues[0] ?? null),
345
+ values: newValues,
346
+ items: this.getSelectedItems(newValues),
347
+ action,
348
+ item,
349
+ });
350
+ }
351
+ }
352
+ }
353
+ getSelectedItems(values) {
354
+ const byValue = new Map(this.getTagElements().map((el) => [this.getTagData(el).value, el]));
355
+ return values.map((value) => {
356
+ const el = byValue.get(value);
357
+ return {
358
+ value,
359
+ label: el?.getAttribute("label") || el?.textContent?.trim() || value,
360
+ flavor: el?.getAttribute("flavor") ?? null,
361
+ };
362
+ });
363
+ }
364
+ calculatePosition() {
365
+ const shadow = this.shadowRoot;
366
+ const stub = shadow.querySelector(".select-stub");
367
+ const dialog = shadow.querySelector(".dropdown-dialog");
368
+ if (!stub || !dialog)
369
+ return;
370
+ const stubRect = stub.getBoundingClientRect();
371
+ const MAX_POPUP_ESTIMATE = 440;
372
+ const dialogRect = dialog.getBoundingClientRect();
373
+ const estimatedHeight = Math.min(dialogRect.height || 200, MAX_POPUP_ESTIMATE);
374
+ const wrapPadding = 20;
375
+ const cssWidth = parseFloat(getComputedStyle(this).getPropertyValue("--ty-select-popup-width"));
376
+ const popupWidth = !isNaN(cssWidth)
377
+ ? cssWidth
378
+ : Math.max(stubRect.width, 320);
379
+ const requested = this.getProperty("placement");
380
+ const anchored = requested
381
+ ? placementToAnchored(requested)
382
+ : {
383
+ side: "auto",
384
+ align: this.getProperty("align"),
385
+ };
386
+ const pos = computeAnchoredPosition({
387
+ anchorRect: stubRect,
388
+ popupWidth,
389
+ popupHeight: estimatedHeight,
390
+ align: anchored.align,
391
+ side: anchored.side,
392
+ gap: 8,
393
+ });
394
+ const positionBelow = pos.below;
395
+ const x = pos.x - wrapPadding;
396
+ const y = (positionBelow ? pos.topY : pos.bottomY) - wrapPadding;
397
+ const width = popupWidth + wrapPadding + wrapPadding;
398
+ this.style.setProperty("--dropdown-x", `${x}px`);
399
+ this.style.setProperty("--dropdown-y", `${y}px`);
400
+ this.style.setProperty("--dropdown-width", `${width}px`);
401
+ this.style.setProperty("--dropdown-offset-x", "0px");
402
+ this.style.setProperty("--dropdown-offset-y", "0px");
403
+ this.style.setProperty("--dropdown-padding", `${wrapPadding}px`);
404
+ if (positionBelow) {
405
+ dialog.classList.add("position-below");
406
+ dialog.classList.remove("position-above");
407
+ }
408
+ else {
409
+ dialog.classList.add("position-above");
410
+ dialog.classList.remove("position-below");
411
+ }
412
+ this.style.setProperty("--dropdown-direction", positionBelow ? "below" : "above");
413
+ }
414
+ _setupOptionsScrollbar() {
415
+ if (!isCustomScrollbarEnabled())
416
+ return;
417
+ const shadow = this.shadowRoot;
418
+ const optionsDiv = shadow.querySelector(".dropdown-options");
419
+ const optionsWrapper = shadow.querySelector(".dropdown-options-wrapper");
420
+ if (!optionsDiv || !optionsWrapper)
421
+ return;
422
+ this._destroyOptionsScrollbar();
423
+ optionsDiv.classList.add("ty-custom-scroll");
424
+ this._optionsScrollbar = new CustomScrollbar(optionsDiv, {
425
+ vertical: true,
426
+ });
427
+ if (this._optionsScrollbar.trackY) {
428
+ optionsWrapper.appendChild(this._optionsScrollbar.trackY);
429
+ }
430
+ }
431
+ _destroyOptionsScrollbar() {
432
+ if (this._optionsScrollbar) {
433
+ this._optionsScrollbar.trackY?.remove();
434
+ this._optionsScrollbar.destroy();
435
+ this._optionsScrollbar = null;
436
+ }
437
+ }
438
+ openDropdown() {
439
+ const shadow = this.shadowRoot;
440
+ const dialog = shadow.querySelector(".dropdown-dialog");
441
+ if (!dialog)
442
+ return;
443
+ const stubEl = shadow.querySelector(".select-stub");
444
+ if (stubEl)
445
+ stubEl.setAttribute("aria-expanded", "true");
446
+ const lockId = `select-${this.id || "anon"}-${getElementHash(this)}`;
447
+ this._scrollLockId = lockId;
448
+ lockScroll(lockId);
449
+ dialog.showModal();
450
+ this.calculatePosition();
451
+ dialog.classList.add("open");
452
+ this._state.open = true;
453
+ const chevron = shadow.querySelector(".dropdown-chevron");
454
+ if (chevron)
455
+ chevron.classList.add("open");
456
+ const searchChevron = shadow.querySelector(".dropdown-search-chevron");
457
+ if (searchChevron)
458
+ searchChevron.classList.add("open");
459
+ const tags = this.getTagElements().map((el) => this.getTagData(el));
460
+ this._state.filteredTags = tags;
461
+ this._state.highlightedIndex = -1;
462
+ this.updateOptionsVisibility(true);
463
+ this._setupOptionsScrollbar();
464
+ const searchInput = shadow.querySelector(".dropdown-search-input");
465
+ if (searchInput && this.shouldShowSearch()) {
466
+ setTimeout(() => searchInput.focus(), 100);
467
+ }
468
+ this.fireOpenEvent();
469
+ }
470
+ closeDropdown() {
471
+ const shadow = this.shadowRoot;
472
+ const dialog = shadow.querySelector(".dropdown-dialog");
473
+ if (!dialog)
474
+ return;
475
+ const stubEl = shadow.querySelector(".select-stub");
476
+ if (stubEl)
477
+ stubEl.setAttribute("aria-expanded", "false");
478
+ this._destroyOptionsScrollbar();
479
+ dialog.classList.remove("open");
480
+ dialog.classList.remove("position-above");
481
+ dialog.classList.remove("position-below");
482
+ dialog.close();
483
+ if (this._scrollLockId) {
484
+ unlockScroll(this._scrollLockId);
485
+ this._scrollLockId = null;
486
+ }
487
+ this._state.open = false;
488
+ this._state.highlightedIndex = -1;
489
+ const chevron = shadow.querySelector(".dropdown-chevron");
490
+ if (chevron)
491
+ chevron.classList.remove("open");
492
+ const searchChevron = shadow.querySelector(".dropdown-search-chevron");
493
+ if (searchChevron)
494
+ searchChevron.classList.remove("open");
495
+ const hadQuery = this._state.search !== "";
496
+ this._state.search = "";
497
+ const searchInput = shadow.querySelector(".dropdown-search-input");
498
+ if (searchInput) {
499
+ searchInput.value = "";
500
+ }
501
+ if (this._externalSearch) {
502
+ if (hadQuery) {
503
+ if (this._searchDebounceTimer !== null) {
504
+ clearTimeout(this._searchDebounceTimer);
505
+ this._searchDebounceTimer = null;
506
+ }
507
+ this.fireSearchEvent("");
508
+ }
509
+ }
510
+ else {
511
+ const allTags = this.getTagElements().map((el) => this.getTagData(el));
512
+ this._state.filteredTags = allTags;
513
+ this.updateTagVisibility(allTags, allTags);
514
+ this.clearHighlights(allTags);
515
+ }
516
+ this.fireCloseEvent();
517
+ }
518
+ openMobileModal() {
519
+ const shadow = this.shadowRoot;
520
+ const dialog = shadow.querySelector(".mobile-dialog");
521
+ if (!dialog)
522
+ return;
523
+ const stubEl = shadow.querySelector(".select-stub");
524
+ if (stubEl)
525
+ stubEl.setAttribute("aria-expanded", "true");
526
+ const lockId = `select-${this.id || "anon"}-${getElementHash(this)}`;
527
+ this._scrollLockId = lockId;
528
+ lockScroll(lockId);
529
+ dialog.showModal();
530
+ dialog.getBoundingClientRect();
531
+ dialog.classList.add("open");
532
+ this._state.open = true;
533
+ const tags = this.getTagElements().map((el) => this.getTagData(el));
534
+ this._state.filteredTags = tags;
535
+ this.updateTagVisibility(tags, tags);
536
+ requestAnimationFrame(() => {
537
+ this.updateMobileSelectedState();
538
+ });
539
+ this.fireOpenEvent();
540
+ }
541
+ closeMobileModal() {
542
+ const shadow = this.shadowRoot;
543
+ const dialog = shadow.querySelector(".mobile-dialog");
544
+ if (!dialog)
545
+ return;
546
+ const stubEl = shadow.querySelector(".select-stub");
547
+ if (stubEl)
548
+ stubEl.setAttribute("aria-expanded", "false");
549
+ dialog.classList.remove("open");
550
+ dialog.close();
551
+ if (this._scrollLockId) {
552
+ unlockScroll(this._scrollLockId);
553
+ this._scrollLockId = null;
554
+ }
555
+ this._state.open = false;
556
+ this._state.highlightedIndex = -1;
557
+ const hadQuery = this._state.search !== "";
558
+ this._state.search = "";
559
+ const searchInput = shadow.querySelector(".mobile-search-input");
560
+ if (searchInput) {
561
+ searchInput.value = "";
562
+ }
563
+ if (this._externalSearch) {
564
+ if (hadQuery) {
565
+ if (this._searchDebounceTimer !== null) {
566
+ clearTimeout(this._searchDebounceTimer);
567
+ this._searchDebounceTimer = null;
568
+ }
569
+ this.fireSearchEvent("");
570
+ }
571
+ }
572
+ else {
573
+ const allTags = this.getTagElements();
574
+ allTags.forEach((el) => el.removeAttribute("hidden"));
575
+ }
576
+ this.fireCloseEvent();
577
+ }
578
+ handleStubClick(e) {
579
+ e.preventDefault();
580
+ e.stopPropagation();
581
+ if (this._disabled || this._readonly) {
582
+ return;
583
+ }
584
+ this.openDropdown();
585
+ }
586
+ handleClearClick(e) {
587
+ e.preventDefault();
588
+ e.stopPropagation();
589
+ if (this._disabled || this._readonly) {
590
+ return;
591
+ }
592
+ this.clear();
593
+ }
594
+ handleStubKeydown(e) {
595
+ if (this._disabled || this._readonly || this._state.open)
596
+ return;
597
+ if (e.key === "Enter" || e.key === " " || e.key === "ArrowDown") {
598
+ e.preventDefault();
599
+ this.openDropdown();
600
+ }
601
+ }
602
+ handleTagClick(e) {
603
+ const target = e.target;
604
+ const tag = target.closest("ty-option, ty-tag");
605
+ if (!tag || tag.hasAttribute("disabled"))
606
+ return;
607
+ e.preventDefault();
608
+ e.stopPropagation();
609
+ this.selectByTag(tag);
610
+ }
611
+ selectByTag(tag) {
612
+ const tagValue = this.getTagData(tag).value;
613
+ const currentValues = this.getSelectedValues();
614
+ if (!this._multiple) {
615
+ this.updateComponentValue([tagValue], true, "set", tagValue);
616
+ if (this._state.mode === "mobile") {
617
+ this.closeMobileModal();
618
+ }
619
+ else {
620
+ this.closeDropdown();
621
+ }
622
+ return;
623
+ }
624
+ if (currentValues.includes(tagValue)) {
625
+ const newValues = currentValues.filter((v) => v !== tagValue);
626
+ this.updateComponentValue(newValues, true, "remove", tagValue);
627
+ }
628
+ else {
629
+ this.updateComponentValue([...currentValues, tagValue], true, "add", tagValue);
630
+ }
631
+ }
632
+ tryCreateFromSearch() {
633
+ if (!this._allowCreate)
634
+ return false;
635
+ const query = (this._state.search || "").trim();
636
+ if (!query)
637
+ return false;
638
+ const queryLower = query.toLowerCase();
639
+ const allTags = this.getTagElements().map((el) => this.getTagData(el));
640
+ const existing = allTags.find((t) => t.value.toLowerCase() === queryLower ||
641
+ t.text.toLowerCase() === queryLower);
642
+ if (existing) {
643
+ this.selectByTag(existing.element);
644
+ return true;
645
+ }
646
+ const rawLabel = query;
647
+ const defaultValue = this._createTransform === "slug" ? slugify(rawLabel) : rawLabel;
648
+ if (!defaultValue)
649
+ return true;
650
+ const detail = { value: defaultValue, label: rawLabel };
651
+ const event = new CustomEvent("create", {
652
+ detail,
653
+ bubbles: true,
654
+ composed: true,
655
+ cancelable: true,
656
+ });
657
+ this.dispatchEvent(event);
658
+ if (event.defaultPrevented)
659
+ return true;
660
+ const finalValue = detail.value;
661
+ const option = document.createElement("ty-option");
662
+ option.setAttribute("value", finalValue);
663
+ option.textContent = rawLabel;
664
+ this.appendChild(option);
665
+ if (!this._multiple) {
666
+ this.updateComponentValue([finalValue], true, "create", finalValue);
667
+ if (this._state.mode === "mobile") {
668
+ this.closeMobileModal();
669
+ }
670
+ else {
671
+ this.closeDropdown();
672
+ }
673
+ return true;
674
+ }
675
+ const currentValues = this.getSelectedValues();
676
+ this.updateComponentValue([...currentValues, finalValue], true, "create", finalValue);
677
+ this.clearSearchAfterCreate();
678
+ return true;
679
+ }
680
+ clearSearchAfterCreate() {
681
+ this._state.search = "";
682
+ this._state.highlightedIndex = -1;
683
+ const shadow = this.shadowRoot;
684
+ if (!shadow)
685
+ return;
686
+ const desktopInput = shadow.querySelector(".dropdown-search-input");
687
+ const mobileInput = shadow.querySelector(".mobile-search-input");
688
+ if (desktopInput)
689
+ desktopInput.value = "";
690
+ if (mobileInput)
691
+ mobileInput.value = "";
692
+ const allTags = this.getTagElements().map((el) => this.getTagData(el));
693
+ this._state.filteredTags = allTags;
694
+ this.updateTagVisibility(allTags, allTags);
695
+ this.updateOptionsVisibility(true);
696
+ this.clearHighlights(allTags);
697
+ this.updateMobileSelectedState();
698
+ (desktopInput ?? mobileInput)?.focus();
699
+ }
700
+ blockSearchClick(e) {
701
+ e.stopPropagation();
702
+ e.preventDefault();
703
+ }
704
+ handleSearchInput(e) {
705
+ if (this._state.mode === "mobile")
706
+ this.expandMobileSection("available");
707
+ const target = e.target;
708
+ const query = target.value;
709
+ this._state.search = query;
710
+ if (this._externalSearch) {
711
+ this.dispatchSearchEvent(query);
712
+ return;
713
+ }
714
+ const allTags = this.getTagElements().map((el) => this.getTagData(el));
715
+ const filtered = this.filterTags(allTags, query);
716
+ this._state.filteredTags = filtered;
717
+ this._state.highlightedIndex = -1;
718
+ this.updateTagVisibility(filtered, allTags);
719
+ this.updateOptionsVisibility(filtered.length > 0);
720
+ this.updateMobileSelectedState();
721
+ this.clearHighlights(allTags);
722
+ }
723
+ handleKeyboard(e) {
724
+ if (!this._state.open)
725
+ return;
726
+ const shadow = this.shadowRoot;
727
+ const searchInput = shadow.querySelector(".dropdown-search-input");
728
+ const target = e.target;
729
+ const shouldHandle = target === searchInput || document.activeElement !== searchInput;
730
+ if (!shouldHandle)
731
+ return;
732
+ const filteredTags = this._state.filteredTags;
733
+ const tagsCount = filteredTags.length;
734
+ const currentHighlightedIndex = this._state.highlightedIndex;
735
+ switch (e.key) {
736
+ case "Escape":
737
+ e.preventDefault();
738
+ e.stopPropagation();
739
+ if (this._state.mode === "mobile") {
740
+ this.closeMobileModal();
741
+ }
742
+ else {
743
+ this.closeDropdown();
744
+ }
745
+ break;
746
+ case "Enter":
747
+ e.preventDefault();
748
+ e.stopPropagation();
749
+ if (currentHighlightedIndex >= 0 &&
750
+ currentHighlightedIndex < tagsCount) {
751
+ this.selectByTag(filteredTags[currentHighlightedIndex].element);
752
+ }
753
+ else {
754
+ this.tryCreateFromSearch();
755
+ }
756
+ break;
757
+ case "ArrowUp":
758
+ e.preventDefault();
759
+ e.stopPropagation();
760
+ const newIndexUp = this.nextEnabledTagIndex(filteredTags, currentHighlightedIndex, -1);
761
+ this._state.highlightedIndex = newIndexUp;
762
+ this.highlightTag(filteredTags, newIndexUp);
763
+ break;
764
+ case "ArrowDown":
765
+ e.preventDefault();
766
+ e.stopPropagation();
767
+ const newIndexDown = this.nextEnabledTagIndex(filteredTags, currentHighlightedIndex, 1);
768
+ this._state.highlightedIndex = newIndexDown;
769
+ this.highlightTag(filteredTags, newIndexDown);
770
+ break;
771
+ }
772
+ }
773
+ nextEnabledTagIndex(tags, fromIndex, direction) {
774
+ const count = tags.length;
775
+ if (count === 0)
776
+ return -1;
777
+ let index = fromIndex === -1
778
+ ? (direction === 1 ? 0 : count - 1)
779
+ : (fromIndex + direction + count) % count;
780
+ for (let i = 0; i < count; i++) {
781
+ if (!tags[index].element.hasAttribute("disabled"))
782
+ return index;
783
+ index = (index + direction + count) % count;
784
+ }
785
+ return -1;
786
+ }
787
+ filterTags(tags, query) {
788
+ if (!query || query.trim() === "") {
789
+ return tags;
790
+ }
791
+ const searchLower = query.toLowerCase();
792
+ return tags.filter(({ text }) => text.toLowerCase().includes(searchLower));
793
+ }
794
+ updateTagVisibility(filteredTags, allTags) {
795
+ const visibleValues = new Set(filteredTags.map((tag) => tag.value));
796
+ allTags.forEach(({ value, element }) => {
797
+ if (visibleValues.has(value)) {
798
+ element.removeAttribute("hidden");
799
+ }
800
+ else {
801
+ element.setAttribute("hidden", "");
802
+ }
803
+ });
804
+ }
805
+ updateOptionsVisibility(hasOptions) {
806
+ const shadow = this.shadowRoot;
807
+ const options = shadow.querySelector(".dropdown-options");
808
+ if (options) {
809
+ options.style.display = hasOptions ? "" : "none";
810
+ }
811
+ }
812
+ clearHighlights(tags) {
813
+ tags.forEach(({ element }) => {
814
+ element.removeAttribute("highlighted");
815
+ });
816
+ }
817
+ highlightTag(tags, index) {
818
+ this.clearHighlights(tags);
819
+ if (index >= 0 && index < tags.length) {
820
+ const { element } = tags[index];
821
+ element.setAttribute("highlighted", "");
822
+ element.scrollIntoView({
823
+ behavior: "smooth",
824
+ block: "nearest",
825
+ inline: "nearest",
826
+ });
827
+ }
828
+ }
829
+ dispatchSearchEvent(query) {
830
+ if (this._searchDebounceTimer !== null) {
831
+ clearTimeout(this._searchDebounceTimer);
832
+ this._searchDebounceTimer = null;
833
+ }
834
+ if (this._debounce > 0) {
835
+ this._searchDebounceTimer = window.setTimeout(() => {
836
+ this.fireSearchEvent(query);
837
+ this._searchDebounceTimer = null;
838
+ }, this._debounce);
839
+ }
840
+ else {
841
+ this.fireSearchEvent(query);
842
+ }
843
+ }
844
+ fireSearchEvent(query) {
845
+ this.dispatchEvent(new CustomEvent("search", {
846
+ detail: {
847
+ query,
848
+ element: this,
849
+ },
850
+ bubbles: true,
851
+ composed: true,
852
+ }));
853
+ }
854
+ fireOpenEvent() {
855
+ this.dispatchEvent(new CustomEvent("open", {
856
+ detail: { mode: this._state.mode, element: this },
857
+ }));
858
+ if (this._externalSearch) {
859
+ this.fireSearchEvent("");
860
+ }
861
+ }
862
+ fireCloseEvent() {
863
+ this.dispatchEvent(new CustomEvent("close", {
864
+ detail: { mode: this._state.mode, element: this },
865
+ }));
866
+ }
867
+ dispatchChangeEvent(detail) {
868
+ this.dispatchEvent(new CustomEvent("change", {
869
+ detail,
870
+ bubbles: true,
871
+ cancelable: true,
872
+ }));
873
+ }
874
+ render() {
875
+ this._state.mode = isMobileTouch() ? "mobile" : "desktop";
876
+ if (this._state.mode === "mobile") {
877
+ this.renderMobile();
878
+ }
879
+ else {
880
+ this.renderDesktop();
881
+ }
882
+ this.updateSearchVisibility();
883
+ this.applyLoadingState();
884
+ this.updateValidity();
885
+ }
886
+ updateValidity() {
887
+ if (this.disabled) {
888
+ this._internals.setValidity({});
889
+ return;
890
+ }
891
+ if (this.required && this.getSelectedValues().length === 0) {
892
+ const stub = this.shadowRoot?.querySelector(".select-stub");
893
+ this._internals.setValidity({ valueMissing: true }, "Please select a value", stub ?? undefined);
894
+ }
895
+ else {
896
+ this._internals.setValidity({});
897
+ }
898
+ }
899
+ setupTriggerSlot() {
900
+ const shadow = this.shadowRoot;
901
+ const stub = shadow.querySelector(".select-stub");
902
+ const slot = shadow.querySelector('slot[name="trigger"]');
903
+ if (!stub || !slot)
904
+ return;
905
+ const sync = () => {
906
+ const hasTrigger = slot.assignedElements().length > 0;
907
+ stub.classList.toggle("custom-trigger", hasTrigger);
908
+ this.toggleAttribute("has-trigger", hasTrigger);
909
+ };
910
+ slot.addEventListener("slotchange", sync);
911
+ sync();
912
+ }
913
+ setupEventListeners() {
914
+ const shadow = this.shadowRoot;
915
+ const stub = shadow.querySelector(".select-stub");
916
+ const optionsSlot = shadow.querySelector("#options-slot");
917
+ const searchInput = shadow.querySelector(".dropdown-search-input");
918
+ if (stub) {
919
+ this._stubClickHandler = this.handleStubClick.bind(this);
920
+ stub.addEventListener("click", this._stubClickHandler);
921
+ stub.addEventListener("keydown", this.handleStubKeydown.bind(this));
922
+ }
923
+ const clearBtn = shadow.querySelector(".select-clear");
924
+ clearBtn?.addEventListener("click", this.handleClearClick.bind(this));
925
+ this.setupTriggerSlot();
926
+ if (optionsSlot) {
927
+ this._tagClickHandler = this.handleTagClick.bind(this);
928
+ optionsSlot.addEventListener("click", this._tagClickHandler);
929
+ }
930
+ if (searchInput) {
931
+ this._searchInputHandler = this.handleSearchInput.bind(this);
932
+ this._blockSearchClick = this.blockSearchClick.bind(this);
933
+ searchInput.addEventListener("input", this._searchInputHandler);
934
+ searchInput.addEventListener("click", this._blockSearchClick);
935
+ }
936
+ const dialog = shadow.querySelector(".dropdown-dialog");
937
+ if (dialog) {
938
+ dialog.addEventListener("click", (e) => {
939
+ if (e.target === dialog) {
940
+ this.closeDropdown();
941
+ }
942
+ });
943
+ }
944
+ this._keyboardHandler = this.handleKeyboard.bind(this);
945
+ document.addEventListener("keydown", this._keyboardHandler);
946
+ }
947
+ buildStubClasses() {
948
+ const classes = [];
949
+ if (this._disabled)
950
+ classes.push("disabled");
951
+ if (this._compact)
952
+ classes.push("compact");
953
+ return classes.join(" ");
954
+ }
955
+ renderDesktop() {
956
+ const shadow = this.shadowRoot;
957
+ if (!shadow.querySelector(".select-container")) {
958
+ const stubClasses = this.buildStubClasses();
959
+ const listboxId = `select-listbox-${getElementHash(this)}`;
960
+ const labelId = `select-label-${getElementHash(this)}`;
961
+ const labelHtml = this._label
962
+ ? `
963
+ <label class="ty-field-label" id="${labelId}">
964
+ ${this._label}
965
+ ${this._required ? `<span class="required-icon">${REQUIRED_ICON_SVG}</span>` : ""}
966
+ </label>
967
+ `
968
+ : "";
969
+ const searchPlaceholder = "Search...";
970
+ shadow.innerHTML = `
971
+ <div class="select-container dropdown-mode-desktop">
972
+ ${labelHtml}
973
+ <div class="dropdown-wrapper">
974
+ <div class="dropdown-stub select-stub ${stubClasses}"
975
+ role="combobox"
976
+ aria-haspopup="listbox"
977
+ aria-expanded="false"
978
+ aria-controls="${listboxId}"
979
+ ${this._label ? `aria-labelledby="${labelId}"` : ""}
980
+ tabindex="${this._disabled ? "-1" : "0"}"
981
+ ${this._disabled ? 'disabled aria-disabled="true"' : ""}>
982
+ <slot name="trigger">
983
+ <slot name="start"></slot>
984
+ <slot name="selected"></slot>
985
+ <span class="dropdown-placeholder">${this._placeholder}</span>
986
+ <span class="select-count" hidden></span>
987
+ <slot name="end"></slot>
988
+ <button class="select-clear" type="button" aria-label="Clear selection" hidden>
989
+ ${CLEAR_ICON_SVG}
990
+ </button>
991
+ <div class="dropdown-chevron">
992
+ ${CHEVRON_DOWN_SVG}
993
+ </div>
994
+ </slot>
995
+ </div>
996
+ <dialog class="dropdown-dialog">
997
+ <div class="dropdown-header">
998
+ <div class="dropdown-search-icon" aria-hidden="true">
999
+ ${SEARCH_ICON_SVG}
1000
+ </div>
1001
+ <input
1002
+ class="dropdown-search-input"
1003
+ type="text"
1004
+ placeholder="${searchPlaceholder}"
1005
+ ${this._disabled ? "disabled" : ""}
1006
+ />
1007
+ <div class="dropdown-search-chevron">
1008
+ ${CHEVRON_DOWN_SVG}
1009
+ </div>
1010
+ </div>
1011
+ <div class="dropdown-options-wrapper">
1012
+ <div class="dropdown-options" id="${listboxId}" role="listbox" ${this._multiple ? 'aria-multiselectable="true"' : ""}>
1013
+ <slot id="options-slot"></slot>
1014
+ </div>
1015
+ <div class="dropdown-loading" aria-hidden="true">
1016
+ <slot name="loading">
1017
+ <span class="dropdown-loading-spinner"></span>
1018
+ <span class="dropdown-loading-text">Searching…</span>
1019
+ </slot>
1020
+ </div>
1021
+ </div>
1022
+ </dialog>
1023
+ </div>
1024
+ </div>
1025
+ `;
1026
+ this.setupEventListeners();
1027
+ }
1028
+ this.updateSelectionDisplay();
1029
+ }
1030
+ renderMobile() {
1031
+ const shadow = this.shadowRoot;
1032
+ if (!shadow.querySelector(".select-container")) {
1033
+ const stubClasses = this.buildStubClasses();
1034
+ const listboxId = `select-listbox-${getElementHash(this)}`;
1035
+ const labelId = `select-label-${getElementHash(this)}`;
1036
+ const labelHtml = this._label
1037
+ ? `
1038
+ <label class="ty-field-label" id="${labelId}">
1039
+ ${this._label}
1040
+ ${this._required ? `<span class="required-icon">${REQUIRED_ICON_SVG}</span>` : ""}
1041
+ </label>
1042
+ `
1043
+ : "";
1044
+ const closeButtonSvg = `<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
1045
+ <line x1="18" y1="6" x2="6" y2="18"></line>
1046
+ <line x1="6" y1="6" x2="18" y2="18"></line>
1047
+ </svg>`;
1048
+ const searchPlaceholder = this._label
1049
+ ? `Search ${this._label}...`
1050
+ : "Search...";
1051
+ const searchHeaderHtml = `
1052
+ <div class="mobile-search-header">
1053
+ ${this._label ? `<span class="mobile-header-label">${this._label}</span>` : ""}
1054
+ <div class="mobile-header-content">
1055
+ <div class="dropdown-search-icon" aria-hidden="true">
1056
+ ${SEARCH_ICON_SVG}
1057
+ </div>
1058
+ <input
1059
+ class="mobile-search-input"
1060
+ type="text"
1061
+ placeholder="${searchPlaceholder}"
1062
+ autofocus
1063
+ ${this._disabled ? "disabled" : ""}
1064
+ />
1065
+ <button class="mobile-close-button" type="button" aria-label="Close">
1066
+ ${closeButtonSvg}
1067
+ </button>
1068
+ </div>
1069
+ </div>
1070
+ `;
1071
+ shadow.innerHTML = `
1072
+ <div class="select-container dropdown-mode-mobile">
1073
+ ${labelHtml}
1074
+ <div class="dropdown-wrapper">
1075
+ <div class="dropdown-stub select-stub ${stubClasses}"
1076
+ role="combobox"
1077
+ aria-haspopup="listbox"
1078
+ aria-expanded="false"
1079
+ aria-controls="${listboxId}"
1080
+ ${this._label ? `aria-labelledby="${labelId}"` : ""}
1081
+ tabindex="${this._disabled ? "-1" : "0"}"
1082
+ ${this._disabled ? 'disabled aria-disabled="true"' : ""}>
1083
+ <slot name="trigger">
1084
+ <slot name="start"></slot>
1085
+ <slot name="selected"></slot>
1086
+ <span class="dropdown-placeholder">${this._placeholder}</span>
1087
+ <span class="select-count" hidden></span>
1088
+ <slot name="end"></slot>
1089
+ <button class="select-clear" type="button" aria-label="Clear selection" hidden>
1090
+ ${CLEAR_ICON_SVG}
1091
+ </button>
1092
+ <div class="dropdown-chevron">
1093
+ ${CHEVRON_DOWN_SVG}
1094
+ </div>
1095
+ </slot>
1096
+ </div>
1097
+ <dialog class="mobile-dialog">
1098
+ <div class="mobile-dialog-content">
1099
+ <!-- HEADER (matches dropdown.ts) -->
1100
+ ${searchHeaderHtml}
1101
+ <!-- BODY: options list with toggleable selected state -->
1102
+ <div class="mobile-body" data-expanded="available">
1103
+ <!-- SELECTED STRIP (pinned above the list, collapses when empty) -->
1104
+ <div class="mobile-selected-section" data-empty="true">
1105
+ <button class="section-header" type="button" data-section="selected" aria-expanded="false">
1106
+ <span class="section-title">${this._selectedLabel} <span class="section-count">(0)</span></span>
1107
+ ${CHEVRON_DOWN_SVG}
1108
+ </button>
1109
+ <div class="section-content mobile-selected-chips"></div>
1110
+ </div>
1111
+ <!-- AVAILABLE LIST (always visible, takes remaining space) -->
1112
+ <div class="mobile-available-section" data-empty="false">
1113
+ <button class="section-header" type="button" data-section="available" aria-expanded="true">
1114
+ <span class="section-title">${this._availableLabel}</span>
1115
+ ${CHEVRON_DOWN_SVG}
1116
+ </button>
1117
+ <div class="section-content dropdown-options-wrapper" id="${listboxId}" role="listbox" ${this._multiple ? 'aria-multiselectable="true"' : ""}>
1118
+ <slot id="options-slot"></slot>
1119
+ <div class="empty-state">${this._noOptionsMessage}</div>
1120
+ <div class="dropdown-loading" aria-hidden="true">
1121
+ <slot name="loading">
1122
+ <span class="dropdown-loading-spinner"></span>
1123
+ <span class="dropdown-loading-text">Searching…</span>
1124
+ </slot>
1125
+ </div>
1126
+ </div>
1127
+ </div>
1128
+ </div>
1129
+ </div>
1130
+ </dialog>
1131
+ </div>
1132
+ </div>
1133
+ `;
1134
+ this.setupMobileEventListeners();
1135
+ }
1136
+ this.updateSelectionDisplay();
1137
+ }
1138
+ setupMobileEventListeners() {
1139
+ if (!this._keyboardHandler) {
1140
+ this._keyboardHandler = this.handleKeyboard.bind(this);
1141
+ document.addEventListener("keydown", this._keyboardHandler);
1142
+ }
1143
+ const shadow = this.shadowRoot;
1144
+ const stub = shadow.querySelector(".select-stub");
1145
+ const optionsSlot = shadow.querySelector("#options-slot");
1146
+ const searchInput = shadow.querySelector(".mobile-search-input");
1147
+ const closeButton = shadow.querySelector(".mobile-close-button");
1148
+ const dialog = shadow.querySelector(".mobile-dialog");
1149
+ if (stub) {
1150
+ stub.addEventListener("click", (e) => this.handleMobileStubClick(e));
1151
+ stub.addEventListener("keydown", (e) => this.handleMobileStubKeydown(e));
1152
+ }
1153
+ const clearBtn = shadow.querySelector(".select-clear");
1154
+ clearBtn?.addEventListener("click", this.handleClearClick.bind(this));
1155
+ this.setupTriggerSlot();
1156
+ if (optionsSlot) {
1157
+ optionsSlot.addEventListener("click", (e) => this.handleMobileTagClick(e));
1158
+ }
1159
+ if (searchInput) {
1160
+ searchInput.addEventListener("input", (e) => this.handleSearchInput(e));
1161
+ }
1162
+ shadow.querySelectorAll(".mobile-body .section-header").forEach((header) => {
1163
+ header.addEventListener("click", () => this.expandMobileSection(header.dataset.section === "selected" ? "selected" : "available"));
1164
+ });
1165
+ if (closeButton) {
1166
+ closeButton.addEventListener("click", () => this.closeMobileModal());
1167
+ }
1168
+ if (dialog) {
1169
+ dialog.addEventListener("click", (e) => {
1170
+ if (e.target === dialog) {
1171
+ this.closeMobileModal();
1172
+ }
1173
+ });
1174
+ dialog.addEventListener("cancel", (e) => {
1175
+ e.preventDefault();
1176
+ this.closeMobileModal();
1177
+ });
1178
+ }
1179
+ }
1180
+ handleMobileStubClick(e) {
1181
+ e.preventDefault();
1182
+ e.stopPropagation();
1183
+ if (this._disabled || this._readonly) {
1184
+ return;
1185
+ }
1186
+ this.openMobileModal();
1187
+ }
1188
+ handleMobileStubKeydown(e) {
1189
+ if (this._disabled || this._readonly || this._state.open)
1190
+ return;
1191
+ if (e.key === "Enter" || e.key === " " || e.key === "ArrowDown") {
1192
+ e.preventDefault();
1193
+ this.openMobileModal();
1194
+ }
1195
+ }
1196
+ handleMobileTagClick(e) {
1197
+ this.handleTagClick(e);
1198
+ }
1199
+ updateMobileSelectedState() {
1200
+ if (this._state.mode !== "mobile")
1201
+ return;
1202
+ const shadow = this.shadowRoot;
1203
+ this.renderMobileSelectedChips();
1204
+ const availableSection = shadow.querySelector(".mobile-available-section");
1205
+ if (availableSection) {
1206
+ const visibleAvailable = this.getTagElements().filter((tag) => !tag.hasAttribute("hidden") && !tag.hasAttribute("selected")).length;
1207
+ availableSection.setAttribute("data-empty", String(visibleAvailable === 0));
1208
+ const availableTitleSpan = shadow.querySelector(".mobile-available-section .section-title");
1209
+ if (availableTitleSpan) {
1210
+ availableTitleSpan.textContent = `${this._availableLabel} (${visibleAvailable})`;
1211
+ }
1212
+ }
1213
+ }
1214
+ expandMobileSection(section) {
1215
+ const shadow = this.shadowRoot;
1216
+ const body = shadow?.querySelector(".mobile-body");
1217
+ if (!body)
1218
+ return;
1219
+ if (section === "selected" && this._state.selectedValues.length === 0)
1220
+ return;
1221
+ body.dataset.expanded = section;
1222
+ shadow.querySelectorAll(".mobile-body .section-header").forEach((header) => {
1223
+ const owns = header.dataset.section === section;
1224
+ header.setAttribute("aria-expanded", String(owns));
1225
+ });
1226
+ }
1227
+ renderMobileSelectedChips() {
1228
+ const shadow = this.shadowRoot;
1229
+ const section = shadow.querySelector(".mobile-selected-section");
1230
+ const host = shadow.querySelector(".mobile-selected-chips");
1231
+ if (!section || !host)
1232
+ return;
1233
+ const selected = this._state.selectedValues;
1234
+ section.setAttribute("data-empty", String(selected.length === 0));
1235
+ const count = section.querySelector(".section-count");
1236
+ if (count)
1237
+ count.textContent = `(${selected.length})`;
1238
+ if (selected.length === 0)
1239
+ this.expandMobileSection("available");
1240
+ const labelFor = (value) => {
1241
+ const option = this.getTagElements().find((tag) => this.getTagData(tag).value === value);
1242
+ return option ? this.getTagData(option).text : value;
1243
+ };
1244
+ host.textContent = "";
1245
+ for (const value of selected) {
1246
+ const chip = document.createElement("button");
1247
+ chip.type = "button";
1248
+ chip.className = "mobile-selected-chip";
1249
+ chip.dataset.value = value;
1250
+ chip.setAttribute("aria-label", `Remove ${labelFor(value)}`);
1251
+ chip.innerHTML = `<span class="chip-label"></span>${CLEAR_ICON_SVG}`;
1252
+ chip.querySelector(".chip-label").textContent = labelFor(value);
1253
+ chip.addEventListener("click", (e) => {
1254
+ e.preventDefault();
1255
+ e.stopPropagation();
1256
+ this.deselectValue(value);
1257
+ });
1258
+ host.appendChild(chip);
1259
+ }
1260
+ }
1261
+ shouldShowSearch() {
1262
+ if (this._externalSearch)
1263
+ return true;
1264
+ if (this._allowCreate)
1265
+ return true;
1266
+ if (this._notSearchable)
1267
+ return false;
1268
+ return this.getTagElements().length > SEARCH_AUTO_THRESHOLD;
1269
+ }
1270
+ updateSearchVisibility() {
1271
+ const shadow = this.shadowRoot;
1272
+ if (!shadow)
1273
+ return;
1274
+ const show = this.shouldShowSearch();
1275
+ const header = shadow.querySelector(".dropdown-header");
1276
+ if (header)
1277
+ header.hidden = !show;
1278
+ const mobileInput = shadow.querySelector(".mobile-search-input");
1279
+ if (mobileInput)
1280
+ mobileInput.hidden = !show;
1281
+ const mobileIcon = shadow.querySelector(".mobile-header-content .dropdown-search-icon");
1282
+ if (mobileIcon)
1283
+ mobileIcon.hidden = !show;
1284
+ }
1285
+ updateSelectionDisplay() {
1286
+ const shadow = this.shadowRoot;
1287
+ const stub = shadow.querySelector(".select-stub");
1288
+ if (!stub)
1289
+ return;
1290
+ const values = this._state.selectedValues;
1291
+ const count = values.length;
1292
+ stub.classList.toggle("has-selection", count > 0);
1293
+ stub.classList.toggle("compact", this._compact);
1294
+ const badge = stub.querySelector(".select-count");
1295
+ if (badge) {
1296
+ badge.textContent = String(count);
1297
+ badge.hidden = count === 0 || !this._multiple || !this._compact;
1298
+ }
1299
+ const clearBtn = stub.querySelector(".select-clear");
1300
+ if (clearBtn) {
1301
+ clearBtn.hidden = !(count > 0 && this._clearable && !this._disabled && !this._readonly);
1302
+ }
1303
+ const wantClone = !this._multiple && count > 0 ? values[0] : null;
1304
+ this.syncSelectedClone(wantClone);
1305
+ const hasClone = this._selectedClone !== null;
1306
+ stub.classList.toggle("has-clone", hasClone);
1307
+ const textEl = stub.querySelector(".dropdown-placeholder");
1308
+ if (textEl) {
1309
+ textEl.hidden = hasClone;
1310
+ const showLabels = count > 0 && this._multiple && !this._compact;
1311
+ if (showLabels) {
1312
+ textEl.textContent = this.getSelectedItems(values)
1313
+ .map((i) => i.label)
1314
+ .join(", ");
1315
+ textEl.classList.remove("placeholder-shown");
1316
+ }
1317
+ else if (wantClone !== null) {
1318
+ textEl.textContent = values.join(", ");
1319
+ textEl.classList.remove("placeholder-shown");
1320
+ }
1321
+ else {
1322
+ textEl.textContent = this._placeholder;
1323
+ textEl.classList.add("placeholder-shown");
1324
+ }
1325
+ }
1326
+ }
1327
+ syncSelectedClone(value) {
1328
+ const existing = this.querySelector(':scope > [cloned][slot="selected"]');
1329
+ if (existing?.getAttribute("value") === value) {
1330
+ const stale = value !== null &&
1331
+ existing.childNodes.length === 0 &&
1332
+ (this.getTagElements().find((el) => this.getTagData(el).value === value)?.childNodes.length ?? 0) > 0;
1333
+ if (!stale) {
1334
+ this._selectedClone = existing;
1335
+ return;
1336
+ }
1337
+ }
1338
+ existing?.remove();
1339
+ if (value === null) {
1340
+ this._selectedClone = null;
1341
+ return;
1342
+ }
1343
+ const option = this.getTagElements().find((el) => this.getTagData(el).value === value);
1344
+ if (option) {
1345
+ const clone = option.cloneNode(true);
1346
+ clone.removeAttribute("selected");
1347
+ clone.setAttribute("slot", "selected");
1348
+ clone.setAttribute("cloned", "true");
1349
+ this.appendChild(clone);
1350
+ this._selectedClone = clone;
1351
+ if (option.childNodes.length === 0 && document.readyState === "loading") {
1352
+ document.addEventListener("DOMContentLoaded", () => { if (this.isConnected)
1353
+ this.updateSelectionDisplay(); }, { once: true });
1354
+ }
1355
+ }
1356
+ else if (this._selectedClone?.getAttribute("value") === value) {
1357
+ const restored = this._selectedClone.cloneNode(true);
1358
+ this.appendChild(restored);
1359
+ this._selectedClone = restored;
1360
+ }
1361
+ }
1362
+ deselectValue(value) {
1363
+ const current = this.getSelectedValues();
1364
+ if (!current.includes(value))
1365
+ return;
1366
+ this.updateComponentValue(current.filter((v) => v !== value), true, "remove", value);
1367
+ }
1368
+ clear() {
1369
+ this.updateComponentValue([], true, "clear", null);
1370
+ }
1371
+ get value() {
1372
+ return this.getSelectedValues().join(",");
1373
+ }
1374
+ set value(val) {
1375
+ this.setProperty("value", val);
1376
+ }
1377
+ get name() {
1378
+ return this.getProperty("name");
1379
+ }
1380
+ set name(val) {
1381
+ this.setProperty("name", val);
1382
+ }
1383
+ get multiple() {
1384
+ return this.getProperty("multiple");
1385
+ }
1386
+ set multiple(value) {
1387
+ this.setProperty("multiple", value);
1388
+ }
1389
+ get compact() {
1390
+ return this.getProperty("compact");
1391
+ }
1392
+ set compact(value) {
1393
+ this.setProperty("compact", value);
1394
+ }
1395
+ get placeholder() {
1396
+ return this.getProperty("placeholder");
1397
+ }
1398
+ set placeholder(val) {
1399
+ this.setProperty("placeholder", val);
1400
+ }
1401
+ get label() {
1402
+ return this.getProperty("label");
1403
+ }
1404
+ set label(val) {
1405
+ this.setProperty("label", val);
1406
+ }
1407
+ get disabled() {
1408
+ return this.getProperty("disabled");
1409
+ }
1410
+ set disabled(value) {
1411
+ this.setProperty("disabled", value);
1412
+ }
1413
+ get loading() {
1414
+ return this.getProperty("loading");
1415
+ }
1416
+ set loading(value) {
1417
+ this.setProperty("loading", value);
1418
+ }
1419
+ get readonly() {
1420
+ return this.getProperty("readonly");
1421
+ }
1422
+ set readonly(value) {
1423
+ this.setProperty("readonly", value);
1424
+ }
1425
+ get clearable() {
1426
+ return this.getProperty("clearable");
1427
+ }
1428
+ set clearable(value) {
1429
+ this.setProperty("clearable", value);
1430
+ }
1431
+ get required() {
1432
+ return this.getProperty("required");
1433
+ }
1434
+ set required(value) {
1435
+ this.setProperty("required", value);
1436
+ }
1437
+ get notSearchable() {
1438
+ return this.getProperty("notSearchable");
1439
+ }
1440
+ set notSearchable(value) {
1441
+ this.setProperty("notSearchable", value);
1442
+ }
1443
+ get externalSearch() {
1444
+ return this.getProperty("externalSearch");
1445
+ }
1446
+ set externalSearch(value) {
1447
+ this.setProperty("externalSearch", value);
1448
+ }
1449
+ get debounce() {
1450
+ return this.getProperty("debounce");
1451
+ }
1452
+ set debounce(value) {
1453
+ const numValue = typeof value === "string" ? parseInt(value, 10) : value;
1454
+ this.setProperty("debounce", numValue);
1455
+ }
1456
+ get size() {
1457
+ return this.getProperty("size");
1458
+ }
1459
+ set size(value) {
1460
+ this.setProperty("size", value);
1461
+ }
1462
+ get align() {
1463
+ return this.getProperty("align");
1464
+ }
1465
+ set align(value) {
1466
+ this.setProperty("align", value);
1467
+ }
1468
+ get placement() {
1469
+ return this.getProperty("placement");
1470
+ }
1471
+ set placement(value) {
1472
+ this.setProperty("placement", value);
1473
+ }
1474
+ get flavor() {
1475
+ return this.getProperty("flavor");
1476
+ }
1477
+ set flavor(value) {
1478
+ this.setProperty("flavor", value);
1479
+ }
1480
+ get form() {
1481
+ return this._internals.form;
1482
+ }
1401
1483
  }
1402
1484
  TySelect.properties = {
1403
- value: {
1404
- type: "string",
1405
- visual: true,
1406
- formValue: true,
1407
- emitChange: false,
1408
- default: "",
1409
- coerce: (v) => {
1410
- if (Array.isArray(v)) {
1411
- return v.join(",");
1412
- }
1413
- if (v === null || v === undefined) {
1414
- return "";
1415
- }
1416
- return String(v);
1417
- },
1418
- },
1419
- name: {
1420
- type: "string",
1421
- default: "",
1422
- },
1423
- multiple: {
1424
- type: "boolean",
1425
- visual: true,
1426
- default: false,
1427
- },
1428
- compact: {
1429
- type: "boolean",
1430
- visual: true,
1431
- default: false,
1432
- },
1433
- placeholder: {
1434
- type: "string",
1435
- visual: true,
1436
- default: "Select...",
1437
- },
1438
- label: {
1439
- type: "string",
1440
- visual: true,
1441
- default: "",
1442
- },
1443
- disabled: {
1444
- type: "boolean",
1445
- visual: true,
1446
- default: false,
1447
- },
1448
- readonly: {
1449
- type: "boolean",
1450
- visual: true,
1451
- default: false,
1452
- },
1453
- required: {
1454
- type: "boolean",
1455
- visual: true,
1456
- default: false,
1457
- },
1458
- clearable: {
1459
- type: "boolean",
1460
- visual: true,
1461
- default: true,
1462
- aliases: { "not-clearable": false },
1463
- },
1464
- externalSearch: {
1465
- type: "boolean",
1466
- visual: true,
1467
- default: false,
1468
- aliases: { "external-search": true },
1469
- },
1470
- allowCreate: {
1471
- type: "boolean",
1472
- visual: true,
1473
- default: false,
1474
- aliases: { "allow-create": true },
1475
- },
1476
- createTransform: {
1477
- type: "string",
1478
- visual: false,
1479
- default: "none",
1480
- aliases: { "create-transform": true },
1481
- coerce: (v) => (v === "slug" ? "slug" : "none"),
1482
- },
1483
- searchable: {
1484
- type: "string",
1485
- visual: true,
1486
- default: "auto",
1487
- coerce: (v) => {
1488
- if (v === true || v === "" || v === "true")
1489
- return "always";
1490
- if (v === false || v === "false")
1491
- return "never";
1492
- return "auto";
1493
- },
1494
- },
1495
- align: {
1496
- type: "string",
1497
- visual: true,
1498
- default: "start",
1499
- validate: (v) => ["start", "center", "end"].includes(v),
1500
- coerce: (v) => v === "end" ? "end" : v === "center" ? "center" : "start",
1501
- },
1502
- placement: {
1503
- type: "string",
1504
- visual: true,
1505
- default: "",
1506
- },
1507
- size: {
1508
- type: "string",
1509
- visual: true,
1510
- default: "md",
1511
- validate: (v) => ["sm", "md", "lg"].includes(v),
1512
- coerce: (v) => {
1513
- if (v === "xs")
1514
- return "sm";
1515
- if (v === "xl")
1516
- return "lg";
1517
- if (!["sm", "md", "lg"].includes(v)) {
1518
- console.warn(`[ty-select] Invalid size. Using md.`);
1519
- return "md";
1520
- }
1521
- return v;
1522
- },
1523
- },
1524
- debounce: {
1525
- type: "number",
1526
- default: 0,
1527
- validate: (v) => v >= 0 && v <= 5000,
1528
- coerce: (v) => {
1529
- const num = Number(v);
1530
- if (isNaN(num))
1531
- return 0;
1532
- return Math.max(0, Math.min(5000, num));
1533
- },
1534
- },
1535
- "available-label": {
1536
- type: "string",
1537
- visual: true,
1538
- default: "Available",
1539
- },
1540
- "no-options-message": {
1541
- type: "string",
1542
- visual: true,
1543
- default: "No options available",
1544
- },
1545
- loading: {
1546
- type: "boolean",
1547
- visual: true,
1548
- default: false,
1549
- },
1550
- flavor: {
1551
- type: "string",
1552
- visual: true,
1553
- default: "neutral",
1554
- },
1485
+ value: {
1486
+ type: "string",
1487
+ visual: true,
1488
+ formValue: true,
1489
+ emitChange: false,
1490
+ default: "",
1491
+ coerce: (v) => {
1492
+ if (Array.isArray(v)) {
1493
+ return v.join(",");
1494
+ }
1495
+ if (v === null || v === undefined) {
1496
+ return "";
1497
+ }
1498
+ return String(v);
1499
+ },
1500
+ },
1501
+ name: {
1502
+ type: "string",
1503
+ default: "",
1504
+ },
1505
+ multiple: {
1506
+ type: "boolean",
1507
+ visual: true,
1508
+ default: false,
1509
+ },
1510
+ compact: {
1511
+ type: "boolean",
1512
+ visual: true,
1513
+ default: false,
1514
+ },
1515
+ placeholder: {
1516
+ type: "string",
1517
+ visual: true,
1518
+ default: "Select...",
1519
+ },
1520
+ label: {
1521
+ type: "string",
1522
+ visual: true,
1523
+ default: "",
1524
+ },
1525
+ disabled: {
1526
+ type: "boolean",
1527
+ visual: true,
1528
+ default: false,
1529
+ },
1530
+ readonly: {
1531
+ type: "boolean",
1532
+ visual: true,
1533
+ default: false,
1534
+ },
1535
+ required: {
1536
+ type: "boolean",
1537
+ visual: true,
1538
+ default: false,
1539
+ },
1540
+ clearable: {
1541
+ type: "boolean",
1542
+ visual: true,
1543
+ default: true,
1544
+ aliases: { "not-clearable": false },
1545
+ },
1546
+ externalSearch: {
1547
+ type: "boolean",
1548
+ visual: true,
1549
+ default: false,
1550
+ aliases: { "external-search": true },
1551
+ },
1552
+ allowCreate: {
1553
+ type: "boolean",
1554
+ visual: true,
1555
+ default: false,
1556
+ aliases: { "allow-create": true },
1557
+ },
1558
+ createTransform: {
1559
+ type: "string",
1560
+ visual: false,
1561
+ default: "none",
1562
+ aliases: { "create-transform": true },
1563
+ coerce: (v) => (v === "slug" ? "slug" : "none"),
1564
+ },
1565
+ notSearchable: {
1566
+ type: "boolean",
1567
+ visual: true,
1568
+ default: false,
1569
+ },
1570
+ align: {
1571
+ type: "string",
1572
+ visual: true,
1573
+ default: "start",
1574
+ validate: (v) => ["start", "center", "end"].includes(v),
1575
+ coerce: (v) => v === "end" ? "end" : v === "center" ? "center" : "start",
1576
+ },
1577
+ placement: {
1578
+ type: "string",
1579
+ visual: true,
1580
+ default: "",
1581
+ },
1582
+ size: {
1583
+ type: "string",
1584
+ visual: true,
1585
+ default: DEFAULT_SIZE,
1586
+ validate: (v) => ["xs", "sm", "md", "lg", "xl"].includes(v),
1587
+ coerce: (v) => {
1588
+ if (!["xs", "sm", "md", "lg", "xl"].includes(v)) {
1589
+ console.warn(`[ty-select] Invalid size. Using ${DEFAULT_SIZE}.`);
1590
+ return DEFAULT_SIZE;
1591
+ }
1592
+ return v;
1593
+ },
1594
+ },
1595
+ debounce: {
1596
+ type: "number",
1597
+ default: 0,
1598
+ validate: (v) => v >= 0 && v <= 5000,
1599
+ coerce: (v) => {
1600
+ const num = Number(v);
1601
+ if (isNaN(num))
1602
+ return 0;
1603
+ return Math.max(0, Math.min(5000, num));
1604
+ },
1605
+ },
1606
+ "available-label": {
1607
+ type: "string",
1608
+ visual: true,
1609
+ default: "Available",
1610
+ },
1611
+ "selected-label": {
1612
+ type: "string",
1613
+ visual: true,
1614
+ default: "Selected",
1615
+ },
1616
+ "no-options-message": {
1617
+ type: "string",
1618
+ visual: true,
1619
+ default: "No options available",
1620
+ },
1621
+ loading: {
1622
+ type: "boolean",
1623
+ visual: true,
1624
+ default: false,
1625
+ },
1626
+ flavor: {
1627
+ type: "string",
1628
+ visual: true,
1629
+ default: "neutral",
1630
+ },
1555
1631
  };
1556
1632
  if (!customElements.get("ty-select")) {
1557
- customElements.define("ty-select", TySelect);
1633
+ customElements.define("ty-select", TySelect);
1558
1634
  }