mtrl 0.2.6 → 0.2.8

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 (226) hide show
  1. package/demo/build.ts +349 -0
  2. package/demo/index.html +110 -0
  3. package/demo/main.js +448 -0
  4. package/demo/styles.css +239 -0
  5. package/index.ts +18 -0
  6. package/package.json +14 -3
  7. package/server.ts +86 -0
  8. package/src/components/badge/api.ts +70 -63
  9. package/src/components/badge/badge.ts +16 -2
  10. package/src/components/badge/config.ts +66 -13
  11. package/src/components/badge/features.ts +51 -42
  12. package/src/components/badge/index.ts +27 -2
  13. package/src/components/badge/types.ts +62 -30
  14. package/src/components/bottom-app-bar/bottom-app-bar.ts +154 -0
  15. package/src/components/bottom-app-bar/config.ts +29 -0
  16. package/src/components/bottom-app-bar/index.ts +17 -0
  17. package/src/components/bottom-app-bar/types.ts +114 -0
  18. package/src/components/button/api.ts +5 -0
  19. package/src/components/button/button.ts +0 -1
  20. package/src/components/button/config.ts +6 -2
  21. package/src/components/button/index.ts +10 -2
  22. package/src/components/button/types.ts +20 -2
  23. package/src/components/card/card.ts +13 -25
  24. package/src/components/card/config.ts +83 -30
  25. package/src/components/card/content.ts +8 -10
  26. package/src/components/card/features.ts +4 -3
  27. package/src/components/card/index.ts +29 -2
  28. package/src/components/card/types.ts +33 -22
  29. package/src/components/checkbox/config.ts +3 -4
  30. package/src/components/checkbox/index.ts +1 -2
  31. package/src/components/checkbox/types.ts +12 -3
  32. package/src/components/chip/api.ts +170 -221
  33. package/src/components/chip/chip.ts +34 -302
  34. package/src/components/chip/config.ts +1 -2
  35. package/src/components/chip/index.ts +10 -2
  36. package/src/components/chip/types.ts +224 -35
  37. package/src/components/datepicker/api.ts +265 -0
  38. package/src/components/datepicker/config.ts +141 -0
  39. package/src/components/datepicker/datepicker.ts +341 -0
  40. package/src/components/datepicker/index.ts +12 -0
  41. package/src/components/datepicker/render.ts +450 -0
  42. package/src/components/datepicker/types.ts +397 -0
  43. package/src/components/datepicker/utils.ts +289 -0
  44. package/src/components/dialog/api.ts +55 -21
  45. package/src/components/dialog/config.ts +12 -9
  46. package/src/components/dialog/dialog.ts +6 -3
  47. package/src/components/dialog/features.ts +345 -151
  48. package/src/components/dialog/index.ts +38 -8
  49. package/src/components/dialog/types.ts +40 -14
  50. package/src/components/divider/config.ts +81 -0
  51. package/src/components/divider/divider.ts +37 -0
  52. package/src/components/divider/features.ts +207 -0
  53. package/src/components/divider/index.ts +9 -0
  54. package/src/components/divider/types.ts +55 -0
  55. package/src/components/extended-fab/api.ts +141 -0
  56. package/src/components/extended-fab/config.ts +112 -0
  57. package/src/components/extended-fab/extended-fab.ts +125 -0
  58. package/src/components/extended-fab/index.ts +9 -0
  59. package/src/components/extended-fab/types.ts +304 -0
  60. package/src/components/fab/api.ts +97 -0
  61. package/src/components/fab/config.ts +93 -0
  62. package/src/components/fab/fab.ts +67 -0
  63. package/src/components/fab/index.ts +9 -0
  64. package/src/components/fab/types.ts +251 -0
  65. package/src/components/list/config.ts +4 -5
  66. package/src/components/list/features.ts +6 -7
  67. package/src/components/list/index.ts +7 -9
  68. package/src/components/list/list-item.ts +12 -13
  69. package/src/components/list/types.ts +50 -5
  70. package/src/components/list/utils.ts +30 -3
  71. package/src/components/menu/features/items-manager.ts +9 -9
  72. package/src/components/menu/features/positioning.ts +7 -7
  73. package/src/components/menu/features/visibility.ts +7 -7
  74. package/src/components/menu/index.ts +7 -9
  75. package/src/components/menu/menu-item.ts +6 -6
  76. package/src/components/menu/menu.ts +22 -0
  77. package/src/components/menu/types.ts +29 -10
  78. package/src/components/menu/utils.ts +67 -0
  79. package/src/components/navigation/api.ts +78 -50
  80. package/src/components/navigation/config.ts +22 -10
  81. package/src/components/navigation/features/items.ts +284 -0
  82. package/src/components/navigation/index.ts +0 -6
  83. package/src/components/navigation/nav-item.ts +70 -33
  84. package/src/components/navigation/navigation.ts +53 -3
  85. package/src/components/navigation/types.ts +117 -70
  86. package/src/components/progress/api.ts +2 -3
  87. package/src/components/progress/config.ts +2 -3
  88. package/src/components/progress/index.ts +0 -1
  89. package/src/components/progress/progress.ts +1 -2
  90. package/src/components/progress/types.ts +186 -33
  91. package/src/components/radios/config.ts +1 -1
  92. package/src/components/radios/index.ts +0 -1
  93. package/src/components/radios/types.ts +0 -7
  94. package/src/components/search/api.ts +203 -0
  95. package/src/components/search/config.ts +86 -0
  96. package/src/components/search/features/index.ts +4 -0
  97. package/src/components/search/features/search.ts +717 -0
  98. package/src/components/search/features/states.ts +169 -0
  99. package/src/components/search/features/structure.ts +197 -0
  100. package/src/components/search/index.ts +7 -0
  101. package/src/components/search/search.ts +52 -0
  102. package/src/components/search/types.ts +175 -0
  103. package/src/components/segmented-button/config.ts +80 -0
  104. package/src/components/segmented-button/index.ts +4 -0
  105. package/src/components/segmented-button/segment.ts +154 -0
  106. package/src/components/segmented-button/segmented-button.ts +249 -0
  107. package/src/components/segmented-button/types.ts +254 -0
  108. package/src/components/slider/accessibility.md +5 -5
  109. package/src/components/slider/api.ts +41 -120
  110. package/src/components/slider/config.ts +51 -47
  111. package/src/components/slider/features/handlers.ts +495 -0
  112. package/src/components/slider/features/index.ts +1 -2
  113. package/src/components/slider/features/slider.ts +66 -84
  114. package/src/components/slider/features/states.ts +195 -0
  115. package/src/components/slider/features/structure.ts +136 -206
  116. package/src/components/slider/features/ui.ts +145 -206
  117. package/src/components/slider/index.ts +2 -11
  118. package/src/components/slider/slider.ts +9 -12
  119. package/src/components/slider/types.ts +67 -26
  120. package/src/components/snackbar/config.ts +2 -3
  121. package/src/components/snackbar/constants.ts +0 -32
  122. package/src/components/snackbar/index.ts +0 -1
  123. package/src/components/snackbar/position.ts +9 -1
  124. package/src/components/snackbar/types.ts +122 -46
  125. package/src/components/switch/config.ts +2 -3
  126. package/src/components/switch/index.ts +0 -1
  127. package/src/components/switch/types.ts +3 -2
  128. package/src/components/tabs/config.ts +3 -4
  129. package/src/components/tabs/features.ts +4 -2
  130. package/src/components/tabs/index.ts +0 -15
  131. package/src/components/tabs/indicator.ts +73 -13
  132. package/src/components/tabs/tab-api.ts +12 -4
  133. package/src/components/tabs/tab.ts +18 -6
  134. package/src/components/tabs/types.ts +23 -5
  135. package/src/components/textfield/config.ts +2 -3
  136. package/src/components/textfield/index.ts +0 -1
  137. package/src/components/textfield/types.ts +17 -3
  138. package/src/components/timepicker/README.md +277 -0
  139. package/src/components/timepicker/api.ts +632 -0
  140. package/src/components/timepicker/clockdial.ts +482 -0
  141. package/src/components/timepicker/config.ts +228 -0
  142. package/src/components/timepicker/index.ts +3 -0
  143. package/src/components/timepicker/render.ts +613 -0
  144. package/src/components/timepicker/timepicker.ts +117 -0
  145. package/src/components/timepicker/types.ts +336 -0
  146. package/src/components/timepicker/utils.ts +241 -0
  147. package/src/components/tooltip/api.ts +1 -1
  148. package/src/components/tooltip/config.ts +27 -6
  149. package/src/components/tooltip/index.ts +0 -1
  150. package/src/components/tooltip/types.ts +13 -3
  151. package/src/components/top-app-bar/config.ts +83 -0
  152. package/src/components/top-app-bar/index.ts +11 -0
  153. package/src/components/top-app-bar/top-app-bar.ts +316 -0
  154. package/src/components/top-app-bar/types.ts +140 -0
  155. package/src/core/build/_ripple.scss +6 -6
  156. package/src/core/build/ripple.ts +72 -95
  157. package/src/core/compose/features/icon.ts +3 -1
  158. package/src/core/compose/features/ripple.ts +4 -1
  159. package/src/core/compose/features/textlabel.ts +23 -2
  160. package/src/core/dom/create.ts +5 -0
  161. package/src/index.ts +9 -0
  162. package/src/styles/abstract/_theme.scss +9 -1
  163. package/src/styles/components/_badge.scss +182 -0
  164. package/src/styles/components/_bottom-app-bar.scss +103 -0
  165. package/src/{components/button/_styles.scss → styles/components/_button.scss} +0 -10
  166. package/src/{components/checkbox/_styles.scss → styles/components/_checkbox.scss} +0 -2
  167. package/src/styles/components/_datepicker.scss +358 -0
  168. package/src/styles/components/_dialog.scss +259 -0
  169. package/src/styles/components/_divider.scss +57 -0
  170. package/src/styles/components/_extended-fab.scss +267 -0
  171. package/src/styles/components/_fab.scss +225 -0
  172. package/src/{components/navigation/_styles.scss → styles/components/_navigation.scss} +1 -0
  173. package/src/styles/components/_search.scss +306 -0
  174. package/src/styles/components/_segmented-button.scss +117 -0
  175. package/src/{components/slider/_styles.scss → styles/components/_slider.scss} +83 -24
  176. package/src/{components/switch/_styles.scss → styles/components/_switch.scss} +0 -2
  177. package/src/{components/tabs/_styles.scss → styles/components/_tabs.scss} +95 -33
  178. package/src/{components/textfield/_styles.scss → styles/components/_textfield.scss} +70 -67
  179. package/src/styles/components/_timepicker.scss +451 -0
  180. package/src/styles/components/_top-app-bar.scss +225 -0
  181. package/src/styles/main.scss +98 -49
  182. package/src/styles/themes/_autumn.scss +21 -0
  183. package/src/styles/themes/_base-theme.scss +61 -0
  184. package/src/styles/themes/_baseline.scss +58 -0
  185. package/src/styles/themes/_bluekhaki.scss +125 -0
  186. package/src/styles/themes/_brownbeige.scss +125 -0
  187. package/src/styles/themes/_browngreen.scss +125 -0
  188. package/src/styles/themes/_forest.scss +6 -0
  189. package/src/styles/themes/_greenbeige.scss +125 -0
  190. package/src/styles/themes/_material.scss +125 -0
  191. package/src/styles/themes/_ocean.scss +6 -0
  192. package/src/styles/themes/_sageivory.scss +125 -0
  193. package/src/styles/themes/_spring.scss +6 -0
  194. package/src/styles/themes/_summer.scss +5 -0
  195. package/src/styles/themes/_sunset.scss +5 -0
  196. package/src/styles/themes/_tealcaramel.scss +125 -0
  197. package/src/styles/themes/_winter.scss +6 -0
  198. package/src/components/badge/_styles.scss +0 -174
  199. package/src/components/badge/constants.ts +0 -30
  200. package/src/components/button/constants.ts +0 -11
  201. package/src/components/card/constants.ts +0 -84
  202. package/src/components/dialog/_styles.scss +0 -213
  203. package/src/components/dialog/constants.ts +0 -32
  204. package/src/components/menu/constants.ts +0 -154
  205. package/src/components/navigation/constants.ts +0 -200
  206. package/src/components/navigation/features/items.js +0 -192
  207. package/src/components/progress/constants.ts +0 -29
  208. package/src/components/slider/features/appearance.ts +0 -94
  209. package/src/components/slider/features/disabled.ts +0 -68
  210. package/src/components/slider/features/events.ts +0 -164
  211. package/src/components/slider/features/interactions.ts +0 -396
  212. package/src/components/slider/features/keyboard.ts +0 -233
  213. package/src/components/switch/constants.ts +0 -80
  214. package/src/components/tabs/constants.ts +0 -89
  215. package/src/core/collection/adapters/mongodb.js +0 -232
  216. /package/src/{components/card/_styles.scss → styles/components/_card.scss} +0 -0
  217. /package/src/{components/carousel/_styles.scss → styles/components/_carousel.scss} +0 -0
  218. /package/src/{components/chip/_styles.scss → styles/components/_chip.scss} +0 -0
  219. /package/src/{components/list/_styles.scss → styles/components/_list.scss} +0 -0
  220. /package/src/{components/menu/_styles.scss → styles/components/_menu.scss} +0 -0
  221. /package/src/{components/progress/_styles.scss → styles/components/_progress.scss} +0 -0
  222. /package/src/{components/radios/_styles.scss → styles/components/_radios.scss} +0 -0
  223. /package/src/{components/sheet/_styles.scss → styles/components/_sheet.scss} +0 -0
  224. /package/src/{components/snackbar/_styles.scss → styles/components/_snackbar.scss} +0 -0
  225. /package/src/{components/tooltip/_styles.scss → styles/components/_tooltip.scss} +0 -0
  226. /package/src/styles/utilities/{_color.scss → _colors.scss} +0 -0
@@ -0,0 +1,717 @@
1
+ // src/components/search/features/search.ts
2
+ import { SearchConfig } from '../types';
3
+
4
+ /**
5
+ * Add main search functionality to component
6
+ * @param config Search configuration
7
+ * @returns Component enhancer with search functionality
8
+ */
9
+ export const withSearch = (config: SearchConfig) => component => {
10
+ // Ensure component has events capability
11
+ if (!component.emit) {
12
+ console.warn('Search component requires event emission capability');
13
+ }
14
+
15
+ // Ensure component structure exists
16
+ if (!component.structure) {
17
+ console.error('Search component missing structure');
18
+ return component;
19
+ }
20
+
21
+ // Get elements from structure
22
+ const {
23
+ container,
24
+ input,
25
+ inputWrapper,
26
+ leadingIcon,
27
+ clearButton,
28
+ trailingIcon,
29
+ trailingIcon2,
30
+ avatar,
31
+ divider,
32
+ suggestionsContainer
33
+ } = component.structure;
34
+
35
+ // Create state object
36
+ const state = {
37
+ value: config.value || '',
38
+ placeholder: config.placeholder || 'Search',
39
+ suggestions: config.suggestions || [],
40
+ isFocused: false,
41
+ isExpanded: config.variant === 'view',
42
+ component
43
+ };
44
+
45
+ // Create event helpers
46
+ const eventHelpers = {
47
+ triggerEvent(eventName, originalEvent = null) {
48
+ const eventData = {
49
+ search: state.component,
50
+ value: state.value,
51
+ originalEvent,
52
+ preventDefault: () => { eventData.defaultPrevented = true; },
53
+ defaultPrevented: false
54
+ };
55
+
56
+ if (component.emit) {
57
+ component.emit(eventName, eventData);
58
+ }
59
+
60
+ // Call onEvent handlers from config if they exist
61
+ const handlerName = `on${eventName.charAt(0).toUpperCase() + eventName.slice(1)}`;
62
+ if (config[handlerName] && typeof config[handlerName] === 'function') {
63
+ config[handlerName](state.value);
64
+ }
65
+
66
+ return eventData;
67
+ }
68
+ };
69
+
70
+ /**
71
+ * Updates the value and UI
72
+ */
73
+ const updateValue = (newValue, triggerEvent = true) => {
74
+ // Update internal state
75
+ state.value = newValue;
76
+
77
+ // Update input value
78
+ if (input) {
79
+ input.value = newValue;
80
+ }
81
+
82
+ // Show/hide clear button
83
+ if (clearButton) {
84
+ if (newValue) {
85
+ clearButton.classList.remove(`${component.getClass('search-clear-button')}--hidden`);
86
+ if (!component.disabled?.isDisabled()) {
87
+ clearButton.tabIndex = 0;
88
+ }
89
+ } else {
90
+ clearButton.classList.add(`${component.getClass('search-clear-button')}--hidden`);
91
+ clearButton.tabIndex = -1;
92
+ }
93
+ }
94
+
95
+ // Trigger input event
96
+ if (triggerEvent) {
97
+ eventHelpers.triggerEvent('input');
98
+ }
99
+ };
100
+
101
+ /**
102
+ * Submits the current search value
103
+ */
104
+ const submitSearch = () => {
105
+ if (state.value) {
106
+ eventHelpers.triggerEvent('submit');
107
+
108
+ // Hide suggestions if in bar mode
109
+ if (!state.isExpanded) {
110
+ hideSuggestions();
111
+ }
112
+ }
113
+ };
114
+
115
+ /**
116
+ * Clears the search input
117
+ */
118
+ const clearSearch = (triggerEvent = true) => {
119
+ updateValue('', triggerEvent);
120
+
121
+ if (input) {
122
+ input.focus();
123
+ }
124
+
125
+ if (triggerEvent) {
126
+ eventHelpers.triggerEvent('clear');
127
+ }
128
+ };
129
+
130
+ /**
131
+ * Shows suggestions
132
+ */
133
+ const showSuggestions = () => {
134
+ if (suggestionsContainer) {
135
+ renderSuggestions();
136
+ }
137
+ };
138
+
139
+ /**
140
+ * Hides suggestions
141
+ */
142
+ const hideSuggestions = () => {
143
+ if (suggestionsContainer) {
144
+ suggestionsContainer.classList.remove(`${component.getClass('search-suggestions-container')}--visible`);
145
+ }
146
+ };
147
+
148
+ /**
149
+ * Expands search bar to view mode
150
+ */
151
+ const expandToView = () => {
152
+ if (state.isExpanded) return;
153
+
154
+ state.isExpanded = true;
155
+ component.element.classList.add(`${component.getClass('search')}--expanded`);
156
+
157
+ // Apply view variant class if not already present
158
+ if (!component.element.classList.contains(`${component.getClass('search')}--view`)) {
159
+ component.element.classList.remove(`${component.getClass('search')}--bar`);
160
+ component.element.classList.add(`${component.getClass('search')}--view`);
161
+ }
162
+
163
+ // Show suggestions
164
+ showSuggestions();
165
+
166
+ // Focus input
167
+ if (input) {
168
+ setTimeout(() => {
169
+ input.focus();
170
+ }, 50);
171
+ }
172
+ };
173
+
174
+ /**
175
+ * Collapses view mode to search bar
176
+ */
177
+ const collapseToBar = () => {
178
+ if (!state.isExpanded) return;
179
+
180
+ state.isExpanded = false;
181
+ component.element.classList.remove(`${component.getClass('search')}--expanded`);
182
+
183
+ // Apply bar variant class if not already present
184
+ if (!component.element.classList.contains(`${component.getClass('search')}--bar`)) {
185
+ component.element.classList.remove(`${component.getClass('search')}--view`);
186
+ component.element.classList.add(`${component.getClass('search')}--bar`);
187
+ }
188
+
189
+ // Hide suggestions
190
+ hideSuggestions();
191
+
192
+ // Blur input
193
+ if (input) {
194
+ input.blur();
195
+ }
196
+ };
197
+
198
+ /**
199
+ * Renders suggestions in the suggestions container
200
+ */
201
+ const renderSuggestions = () => {
202
+ if (!suggestionsContainer || !state.suggestions || !state.suggestions.length) {
203
+ return;
204
+ }
205
+
206
+ // Clear previous suggestions
207
+ suggestionsContainer.innerHTML = '';
208
+
209
+ // Create a list for suggestions
210
+ const list = document.createElement('ul');
211
+ list.className = component.getClass('search-suggestions-list');
212
+ list.setAttribute('role', 'listbox');
213
+
214
+ // Add suggestions
215
+ state.suggestions.forEach((suggestion, index) => {
216
+ const item = document.createElement('li');
217
+ item.className = component.getClass('search-suggestion-item');
218
+ item.setAttribute('role', 'option');
219
+ item.tabIndex = 0;
220
+
221
+ // Determine if suggestion is a string or object
222
+ if (typeof suggestion === 'string') {
223
+ item.textContent = suggestion;
224
+ // Highlight matched text if current input is a substring
225
+ if (state.value && suggestion.toLowerCase().includes(state.value.toLowerCase())) {
226
+ const matchedIndex = suggestion.toLowerCase().indexOf(state.value.toLowerCase());
227
+ const beforeMatch = suggestion.slice(0, matchedIndex);
228
+ const match = suggestion.slice(matchedIndex, matchedIndex + state.value.length);
229
+ const afterMatch = suggestion.slice(matchedIndex + state.value.length);
230
+
231
+ item.innerHTML = `${beforeMatch}<strong>${match}</strong>${afterMatch}`;
232
+ }
233
+ } else {
234
+ // Object with text, value, and optional icon
235
+ if (suggestion.icon) {
236
+ const iconElement = document.createElement('span');
237
+ iconElement.className = component.getClass('search-suggestion-icon');
238
+ iconElement.innerHTML = suggestion.icon;
239
+ item.appendChild(iconElement);
240
+ }
241
+
242
+ const textElement = document.createElement('span');
243
+ textElement.className = component.getClass('search-suggestion-text');
244
+ textElement.textContent = suggestion.text;
245
+
246
+ // Highlight matched text if current input is a substring
247
+ if (state.value && suggestion.text.toLowerCase().includes(state.value.toLowerCase())) {
248
+ const matchedIndex = suggestion.text.toLowerCase().indexOf(state.value.toLowerCase());
249
+ const beforeMatch = suggestion.text.slice(0, matchedIndex);
250
+ const match = suggestion.text.slice(matchedIndex, matchedIndex + state.value.length);
251
+ const afterMatch = suggestion.text.slice(matchedIndex + state.value.length);
252
+
253
+ textElement.innerHTML = `${beforeMatch}<strong>${match}</strong>${afterMatch}`;
254
+ }
255
+
256
+ item.appendChild(textElement);
257
+
258
+ // Store value as data attribute
259
+ if (suggestion.value) {
260
+ item.dataset.value = suggestion.value;
261
+ }
262
+ }
263
+
264
+ // Add click handler
265
+ item.addEventListener('click', (e) => {
266
+ e.preventDefault();
267
+ e.stopPropagation();
268
+ const selectedValue = item.dataset.value || (typeof suggestion === 'string' ? suggestion : suggestion.text);
269
+ updateValue(selectedValue);
270
+ submitSearch();
271
+ });
272
+
273
+ list.appendChild(item);
274
+ });
275
+
276
+ // Add divider if configured and not already present
277
+ if (config.showDividers && divider && divider.parentElement !== suggestionsContainer) {
278
+ const dividerClone = divider.cloneNode(true);
279
+ suggestionsContainer.appendChild(dividerClone);
280
+ }
281
+
282
+ suggestionsContainer.appendChild(list);
283
+
284
+ // Show suggestions container
285
+ suggestionsContainer.classList.add(`${component.getClass('search-suggestions-container')}--visible`);
286
+ };
287
+
288
+ /**
289
+ * Sets up all event listeners
290
+ */
291
+ const setupEventListeners = () => {
292
+ // Input events
293
+ if (input) {
294
+ // Input value change
295
+ input.addEventListener('input', (e) => {
296
+ updateValue(e.target.value);
297
+
298
+ // Show suggestions if expanded
299
+ if (state.isExpanded) {
300
+ showSuggestions();
301
+ }
302
+ });
303
+
304
+ // Focus event
305
+ input.addEventListener('focus', (e) => {
306
+ state.isFocused = true;
307
+ component.element.classList.add(`${component.getClass('search')}--focused`);
308
+
309
+ // Expand search bar to view if in bar mode
310
+ if (!state.isExpanded && config.variant === 'bar') {
311
+ expandToView();
312
+ }
313
+
314
+ eventHelpers.triggerEvent('focus', e);
315
+ });
316
+
317
+ // Blur event
318
+ input.addEventListener('blur', (e) => {
319
+ // Don't blur if clicking inside the search component
320
+ if (component.element.contains(e.relatedTarget)) {
321
+ return;
322
+ }
323
+
324
+ state.isFocused = false;
325
+ component.element.classList.remove(`${component.getClass('search')}--focused`);
326
+
327
+ // Hide suggestions with slight delay to allow for clicks
328
+ setTimeout(() => {
329
+ if (!state.isFocused) {
330
+ hideSuggestions();
331
+
332
+ // Collapse to bar mode if in expanded state and originally a bar
333
+ if (state.isExpanded && config.variant === 'bar') {
334
+ collapseToBar();
335
+ }
336
+ }
337
+ }, 200);
338
+
339
+ eventHelpers.triggerEvent('blur', e);
340
+ });
341
+
342
+ // Enter key for submit
343
+ input.addEventListener('keydown', (e) => {
344
+ if (e.key === 'Enter') {
345
+ e.preventDefault();
346
+ submitSearch();
347
+ } else if (e.key === 'Escape') {
348
+ e.preventDefault();
349
+
350
+ // Clear if there's a value, otherwise collapse
351
+ if (state.value) {
352
+ clearSearch();
353
+ } else if (state.isExpanded && config.variant === 'bar') {
354
+ collapseToBar();
355
+ }
356
+ }
357
+ });
358
+ }
359
+
360
+ // Leading icon click
361
+ if (leadingIcon) {
362
+ leadingIcon.addEventListener('click', (e) => {
363
+ e.preventDefault();
364
+
365
+ // If disabled, do nothing
366
+ if (component.disabled?.isDisabled()) return;
367
+
368
+ // Toggle between expanded and collapsed
369
+ if (state.isExpanded) {
370
+ collapseToBar();
371
+ } else {
372
+ expandToView();
373
+ }
374
+
375
+ eventHelpers.triggerEvent('iconClick', e);
376
+ });
377
+
378
+ // Keyboard access
379
+ leadingIcon.addEventListener('keydown', (e) => {
380
+ if (e.key === 'Enter' || e.key === ' ') {
381
+ e.preventDefault();
382
+ leadingIcon.click();
383
+ }
384
+ });
385
+ }
386
+
387
+ // Clear button click
388
+ if (clearButton) {
389
+ clearButton.addEventListener('click', (e) => {
390
+ e.preventDefault();
391
+
392
+ // If disabled, do nothing
393
+ if (component.disabled?.isDisabled()) return;
394
+
395
+ clearSearch();
396
+ });
397
+
398
+ // Keyboard access
399
+ clearButton.addEventListener('keydown', (e) => {
400
+ if (e.key === 'Enter' || e.key === ' ') {
401
+ e.preventDefault();
402
+ clearButton.click();
403
+ }
404
+ });
405
+ }
406
+
407
+ // Trailing icon click
408
+ if (trailingIcon) {
409
+ trailingIcon.addEventListener('click', (e) => {
410
+ e.preventDefault();
411
+
412
+ // If disabled, do nothing
413
+ if (component.disabled?.isDisabled()) return;
414
+
415
+ eventHelpers.triggerEvent('iconClick', e);
416
+ });
417
+
418
+ // Keyboard access
419
+ trailingIcon.addEventListener('keydown', (e) => {
420
+ if (e.key === 'Enter' || e.key === ' ') {
421
+ e.preventDefault();
422
+ trailingIcon.click();
423
+ }
424
+ });
425
+ }
426
+
427
+ // Second trailing icon click
428
+ if (trailingIcon2) {
429
+ trailingIcon2.addEventListener('click', (e) => {
430
+ e.preventDefault();
431
+
432
+ // If disabled, do nothing
433
+ if (component.disabled?.isDisabled()) return;
434
+
435
+ eventHelpers.triggerEvent('iconClick', e);
436
+ });
437
+
438
+ // Keyboard access
439
+ trailingIcon2.addEventListener('keydown', (e) => {
440
+ if (e.key === 'Enter' || e.key === ' ') {
441
+ e.preventDefault();
442
+ trailingIcon2.click();
443
+ }
444
+ });
445
+ }
446
+
447
+ // Handle clicks outside to close suggestions
448
+ document.addEventListener('click', (e) => {
449
+ if (!component.element.contains(e.target) && state.isExpanded) {
450
+ hideSuggestions();
451
+
452
+ // Collapse to bar mode if in expanded state and originally a bar
453
+ if (state.isExpanded && config.variant === 'bar') {
454
+ collapseToBar();
455
+ }
456
+ }
457
+ });
458
+ };
459
+
460
+ /**
461
+ * Clean up all event listeners
462
+ */
463
+ const cleanupEventListeners = () => {
464
+ // Nothing to do if component is already destroyed
465
+ if (!component.element) return;
466
+
467
+ // Document click listener cleanup
468
+ document.removeEventListener('click', (e) => {
469
+ if (!component.element.contains(e.target) && state.isExpanded) {
470
+ hideSuggestions();
471
+
472
+ // Collapse to bar mode if in expanded state and originally a bar
473
+ if (state.isExpanded && config.variant === 'bar') {
474
+ collapseToBar();
475
+ }
476
+ }
477
+ });
478
+ };
479
+
480
+ // Initialize search component
481
+ const initSearch = () => {
482
+ // Set initial value if provided
483
+ if (config.value && input) {
484
+ input.value = config.value;
485
+
486
+ // Show clear button if value exists
487
+ if (clearButton && config.showClearButton !== false) {
488
+ clearButton.classList.remove(`${component.getClass('search-clear-button')}--hidden`);
489
+ }
490
+ }
491
+
492
+ // Set ARIA attributes
493
+ if (input) {
494
+ input.setAttribute('role', 'searchbox');
495
+ input.setAttribute('aria-label', state.placeholder || 'Search');
496
+ }
497
+
498
+ // Setup event listeners
499
+ setupEventListeners();
500
+
501
+ // If in view mode, show suggestions
502
+ if (state.isExpanded && config.suggestions && config.suggestions.length > 0) {
503
+ showSuggestions();
504
+ }
505
+ };
506
+
507
+ // Register with lifecycle if available
508
+ if (component.lifecycle) {
509
+ const originalDestroy = component.lifecycle.destroy || (() => {});
510
+ component.lifecycle.destroy = () => {
511
+ cleanupEventListeners();
512
+ originalDestroy();
513
+ };
514
+ }
515
+
516
+ // Initialize search
517
+ initSearch();
518
+
519
+ // Return enhanced component
520
+ return {
521
+ ...component,
522
+ search: {
523
+ /**
524
+ * Sets search value
525
+ * @param value New value
526
+ * @param triggerEvent Whether to trigger change event
527
+ * @returns Search controller for chaining
528
+ */
529
+ setValue(value, triggerEvent = true) {
530
+ updateValue(value, triggerEvent);
531
+ return this;
532
+ },
533
+
534
+ /**
535
+ * Gets search value
536
+ * @returns Current value
537
+ */
538
+ getValue() {
539
+ return state.value;
540
+ },
541
+
542
+ /**
543
+ * Sets placeholder text
544
+ * @param text New placeholder text
545
+ * @returns Search controller for chaining
546
+ */
547
+ setPlaceholder(text) {
548
+ state.placeholder = text;
549
+ if (input) {
550
+ input.placeholder = text;
551
+ input.setAttribute('aria-label', text);
552
+ }
553
+ return this;
554
+ },
555
+
556
+ /**
557
+ * Gets placeholder text
558
+ * @returns Current placeholder
559
+ */
560
+ getPlaceholder() {
561
+ return state.placeholder;
562
+ },
563
+
564
+ /**
565
+ * Focuses the search input
566
+ * @returns Search controller for chaining
567
+ */
568
+ focus() {
569
+ if (input && !component.disabled?.isDisabled()) {
570
+ input.focus();
571
+ }
572
+ return this;
573
+ },
574
+
575
+ /**
576
+ * Blurs the search input
577
+ * @returns Search controller for chaining
578
+ */
579
+ blur() {
580
+ if (input) {
581
+ input.blur();
582
+ }
583
+ return this;
584
+ },
585
+
586
+ /**
587
+ * Expands the search bar into view mode
588
+ * @returns Search controller for chaining
589
+ */
590
+ expand() {
591
+ expandToView();
592
+ return this;
593
+ },
594
+
595
+ /**
596
+ * Collapses the search view back to bar mode
597
+ * @returns Search controller for chaining
598
+ */
599
+ collapse() {
600
+ collapseToBar();
601
+ return this;
602
+ },
603
+
604
+ /**
605
+ * Clears the search input
606
+ * @returns Search controller for chaining
607
+ */
608
+ clear() {
609
+ clearSearch();
610
+ return this;
611
+ },
612
+
613
+ /**
614
+ * Submits the search
615
+ * @returns Search controller for chaining
616
+ */
617
+ submit() {
618
+ submitSearch();
619
+ return this;
620
+ },
621
+
622
+ /**
623
+ * Sets suggestions
624
+ * @param suggestions Array of suggestions
625
+ * @returns Search controller for chaining
626
+ */
627
+ setSuggestions(suggestions) {
628
+ state.suggestions = suggestions;
629
+ if (state.isExpanded) {
630
+ renderSuggestions();
631
+ }
632
+ return this;
633
+ },
634
+
635
+ /**
636
+ * Shows or hides suggestions
637
+ * @param show Whether to show suggestions
638
+ * @returns Search controller for chaining
639
+ */
640
+ showSuggestions(show) {
641
+ if (show) {
642
+ showSuggestions();
643
+ } else {
644
+ hideSuggestions();
645
+ }
646
+ return this;
647
+ }
648
+ },
649
+
650
+ // Icon management - separate from appearance to make API cleaner
651
+ icons: {
652
+ /**
653
+ * Sets leading icon
654
+ * @param iconHtml HTML content for icon
655
+ * @returns Icon manager for chaining
656
+ */
657
+ setLeadingIcon(iconHtml) {
658
+ if (leadingIcon) {
659
+ leadingIcon.innerHTML = iconHtml || '';
660
+ }
661
+ return this;
662
+ },
663
+
664
+ /**
665
+ * Sets trailing icon
666
+ * @param iconHtml HTML content for icon
667
+ * @returns Icon manager for chaining
668
+ */
669
+ setTrailingIcon(iconHtml) {
670
+ if (trailingIcon) {
671
+ trailingIcon.innerHTML = iconHtml || '';
672
+ }
673
+ return this;
674
+ },
675
+
676
+ /**
677
+ * Sets second trailing icon
678
+ * @param iconHtml HTML content for icon
679
+ * @returns Icon manager for chaining
680
+ */
681
+ setTrailingIcon2(iconHtml) {
682
+ if (trailingIcon2) {
683
+ trailingIcon2.innerHTML = iconHtml || '';
684
+ }
685
+ return this;
686
+ },
687
+
688
+ /**
689
+ * Sets avatar
690
+ * @param avatarHtml HTML content for avatar
691
+ * @returns Icon manager for chaining
692
+ */
693
+ setAvatar(avatarHtml) {
694
+ if (avatar) {
695
+ avatar.innerHTML = avatarHtml || '';
696
+ }
697
+ return this;
698
+ },
699
+
700
+ /**
701
+ * Shows or hides clear button
702
+ * @param show Whether to show clear button
703
+ * @returns Icon manager for chaining
704
+ */
705
+ showClearButton(show) {
706
+ if (clearButton) {
707
+ if (show) {
708
+ clearButton.classList.remove(`${component.getClass('search-clear-button')}--hidden`);
709
+ } else {
710
+ clearButton.classList.add(`${component.getClass('search-clear-button')}--hidden`);
711
+ }
712
+ }
713
+ return this;
714
+ }
715
+ }
716
+ };
717
+ };