tyrell-components 1.0.0-RC16 → 1.0.0-RC18

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