tyrell-components 1.0.0-TC9 → 1.0.0-rc.22

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 (393) hide show
  1. package/AGENTS.md +122 -0
  2. package/CHANGELOG.md +1541 -0
  3. package/README.md +71 -53
  4. package/bin/tyrell.mjs +77 -0
  5. package/css/tyrell-colors-static.css +1 -0
  6. package/css/tyrell-theme.css +1 -0
  7. package/css/tyrell.css +1 -1783
  8. package/dist/tyrell-colors-static.css +1 -0
  9. package/dist/tyrell-theme.css +1 -0
  10. package/dist/tyrell.css +1 -1783
  11. package/dist/tyrell.js +1 -1
  12. package/guides/AGENT_INSTRUCTIONS.md +124 -0
  13. package/guides/AI_GUIDE.md +53 -0
  14. package/guides/CSS_GUIDE.md +1016 -0
  15. package/guides/DATASTAR_TY_GUIDE.md +657 -0
  16. package/guides/ICONS_GUIDE.md +212 -0
  17. package/guides/TY_GUIDE.md +937 -0
  18. package/guides/clj/CLOJURESCRIPT_GUIDE.md +300 -0
  19. package/guides/clj/CODE_SPLITTING.md +279 -0
  20. package/guides/clj/COMPONENT_GUIDE.md +391 -0
  21. package/guides/clj/HELIX_TY_GUIDE.md +428 -0
  22. package/guides/clj/I18N_GUIDE.md +478 -0
  23. package/guides/clj/LAYOUT_GUIDE.md +273 -0
  24. package/guides/clj/QUICKSTART.md +266 -0
  25. package/guides/clj/REAGENT_TY_GUIDE.md +408 -0
  26. package/guides/clj/REPLICANT_TY_GUIDE.md +623 -0
  27. package/guides/clj/ROUTING_GUIDE.md +346 -0
  28. package/guides/clj/UIX_TY_GUIDE.md +319 -0
  29. package/guides/js/HTMX_TY_GUIDE.md +252 -0
  30. package/guides/js/JAVASCRIPT_GUIDE.md +388 -0
  31. package/guides/js/REACT_TY_GUIDE.md +983 -0
  32. package/guides/js/SVELTE_TY_GUIDE.md +682 -0
  33. package/guides/js/TYCOMPONENT_GUIDE.md +962 -0
  34. package/guides/js/VUE_TY_GUIDE.md +738 -0
  35. package/lib/base/ty-component.d.ts +57 -13
  36. package/lib/base/ty-component.js +266 -292
  37. package/lib/components/button.d.ts +33 -18
  38. package/lib/components/button.js +317 -267
  39. package/lib/components/calendar-month.d.ts +30 -47
  40. package/lib/components/calendar-month.js +455 -429
  41. package/lib/components/calendar-navigation.d.ts +41 -49
  42. package/lib/components/calendar-navigation.js +287 -346
  43. package/lib/components/calendar.d.ts +73 -83
  44. package/lib/components/calendar.js +837 -762
  45. package/lib/components/checkbox.d.ts +26 -75
  46. package/lib/components/checkbox.js +298 -391
  47. package/lib/components/copy.d.ts +20 -60
  48. package/lib/components/copy.js +276 -380
  49. package/lib/components/date-picker.d.ts +100 -183
  50. package/lib/components/date-picker.js +1155 -1570
  51. package/lib/components/file-upload.d.ts +16 -15
  52. package/lib/components/file-upload.js +436 -426
  53. package/lib/components/icon.d.ts +3 -24
  54. package/lib/components/icon.js +141 -230
  55. package/lib/components/input.d.ts +93 -108
  56. package/lib/components/input.js +734 -710
  57. package/lib/components/modal.d.ts +39 -29
  58. package/lib/components/modal.js +419 -438
  59. package/lib/components/option.d.ts +20 -35
  60. package/lib/components/option.js +184 -309
  61. package/lib/components/popup.d.ts +2 -17
  62. package/lib/components/popup.js +353 -347
  63. package/lib/components/radio.d.ts +33 -25
  64. package/lib/components/radio.js +433 -422
  65. package/lib/components/resize-observer.d.ts +3 -30
  66. package/lib/components/resize-observer.js +71 -103
  67. package/lib/components/scroll-container.d.ts +21 -14
  68. package/lib/components/scroll-container.js +292 -234
  69. package/lib/components/select.d.ts +458 -0
  70. package/lib/components/select.js +1458 -0
  71. package/lib/components/selected-tags.d.ts +60 -0
  72. package/lib/components/selected-tags.js +225 -0
  73. package/lib/components/sortable.d.ts +175 -0
  74. package/lib/components/sortable.js +1844 -0
  75. package/lib/components/step.d.ts +1 -6
  76. package/lib/components/step.js +62 -68
  77. package/lib/components/switch.d.ts +15 -15
  78. package/lib/components/switch.js +237 -231
  79. package/lib/components/tab.d.ts +50 -7
  80. package/lib/components/tab.js +203 -69
  81. package/lib/components/tabs.d.ts +19 -64
  82. package/lib/components/tabs.js +600 -618
  83. package/lib/components/tag.d.ts +14 -52
  84. package/lib/components/tag.js +231 -306
  85. package/lib/components/textarea.d.ts +58 -89
  86. package/lib/components/textarea.js +523 -573
  87. package/lib/components/tooltip.d.ts +8 -13
  88. package/lib/components/tooltip.js +322 -409
  89. package/lib/components/wizard.d.ts +7 -54
  90. package/lib/components/wizard.js +376 -606
  91. package/lib/icons/fontawesome/brands.d.ts +0 -1
  92. package/lib/icons/fontawesome/brands.js +0 -8
  93. package/lib/icons/fontawesome/regular.d.ts +0 -1
  94. package/lib/icons/fontawesome/regular.js +0 -8
  95. package/lib/icons/fontawesome/solid.d.ts +0 -1
  96. package/lib/icons/fontawesome/solid.js +0 -8
  97. package/lib/icons/heroicons/micro.d.ts +0 -1
  98. package/lib/icons/heroicons/micro.js +392 -400
  99. package/lib/icons/heroicons/mini.d.ts +0 -1
  100. package/lib/icons/heroicons/mini.js +382 -390
  101. package/lib/icons/heroicons/outline.d.ts +0 -1
  102. package/lib/icons/heroicons/outline.js +337 -345
  103. package/lib/icons/heroicons/solid.d.ts +0 -1
  104. package/lib/icons/heroicons/solid.js +407 -415
  105. package/lib/icons/lucide.d.ts +0 -1
  106. package/lib/icons/lucide.js +24479 -26351
  107. package/lib/icons/material/filled.d.ts +0 -1
  108. package/lib/icons/material/filled.js +9653 -9663
  109. package/lib/icons/material/outlined.d.ts +0 -1
  110. package/lib/icons/material/outlined.js +10271 -10281
  111. package/lib/icons/material/round.d.ts +0 -1
  112. package/lib/icons/material/round.js +10495 -10505
  113. package/lib/icons/material/sharp.d.ts +0 -1
  114. package/lib/icons/material/sharp.js +9905 -9915
  115. package/lib/icons/material/two-tone.d.ts +0 -1
  116. package/lib/icons/material/two-tone.js +12792 -12802
  117. package/lib/index.d.ts +7 -4
  118. package/lib/index.js +26 -39
  119. package/lib/styles/button.d.ts +24 -7
  120. package/lib/styles/button.js +285 -445
  121. package/lib/styles/calendar-month.d.ts +7 -6
  122. package/lib/styles/calendar-month.js +158 -226
  123. package/lib/styles/calendar-navigation.d.ts +2 -6
  124. package/lib/styles/calendar-navigation.js +108 -127
  125. package/lib/styles/calendar.d.ts +3 -3
  126. package/lib/styles/calendar.js +87 -23
  127. package/lib/styles/checkbox.d.ts +7 -6
  128. package/lib/styles/checkbox.js +101 -15
  129. package/lib/styles/copy.d.ts +3 -2
  130. package/lib/styles/copy.js +91 -71
  131. package/lib/styles/custom-scrollbar.d.ts +1 -2
  132. package/lib/styles/custom-scrollbar.js +80 -115
  133. package/lib/styles/date-picker.d.ts +4 -6
  134. package/lib/styles/date-picker.js +321 -388
  135. package/lib/styles/field-size.d.ts +6 -0
  136. package/lib/styles/field-size.js +91 -0
  137. package/lib/styles/file-upload.d.ts +1 -2
  138. package/lib/styles/file-upload.js +143 -188
  139. package/lib/styles/icon.d.ts +2 -6
  140. package/lib/styles/icon.js +92 -170
  141. package/lib/styles/input.d.ts +4 -7
  142. package/lib/styles/input.js +148 -643
  143. package/lib/styles/modal.d.ts +1 -2
  144. package/lib/styles/modal.js +127 -108
  145. package/lib/styles/option.d.ts +2 -6
  146. package/lib/styles/option.js +53 -96
  147. package/lib/styles/popup.d.ts +1 -2
  148. package/lib/styles/popup.js +47 -77
  149. package/lib/styles/radio.d.ts +3 -2
  150. package/lib/styles/radio.js +90 -116
  151. package/lib/styles/resize-observer.d.ts +1 -2
  152. package/lib/styles/resize-observer.js +2 -12
  153. package/lib/styles/scroll-container.d.ts +1 -2
  154. package/lib/styles/scroll-container.js +103 -146
  155. package/lib/styles/select-base.d.ts +8 -0
  156. package/lib/styles/select-base.js +752 -0
  157. package/lib/styles/select.d.ts +11 -0
  158. package/lib/styles/select.js +229 -0
  159. package/lib/styles/sortable.d.ts +16 -0
  160. package/lib/styles/sortable.js +85 -0
  161. package/lib/styles/step.d.ts +1 -2
  162. package/lib/styles/step.js +15 -34
  163. package/lib/styles/switch.d.ts +3 -2
  164. package/lib/styles/switch.js +52 -68
  165. package/lib/styles/tab.d.ts +1 -2
  166. package/lib/styles/tab.js +86 -34
  167. package/lib/styles/tabs.d.ts +3 -11
  168. package/lib/styles/tabs.js +204 -162
  169. package/lib/styles/tag.d.ts +2 -6
  170. package/lib/styles/tag.js +168 -362
  171. package/lib/styles/textarea.d.ts +1 -2
  172. package/lib/styles/textarea.js +238 -306
  173. package/lib/styles/tooltip.d.ts +4 -3
  174. package/lib/styles/tooltip.js +3 -131
  175. package/lib/styles/wizard.d.ts +8 -20
  176. package/lib/styles/wizard.js +306 -295
  177. package/lib/types/common.d.ts +9 -8
  178. package/lib/types/common.js +8 -5
  179. package/lib/utils/calendar-utils.d.ts +49 -65
  180. package/lib/utils/calendar-utils.js +158 -343
  181. package/lib/utils/custom-scrollbar.d.ts +6 -20
  182. package/lib/utils/custom-scrollbar.js +273 -317
  183. package/lib/utils/flavor-sheet.d.ts +31 -0
  184. package/lib/utils/flavor-sheet.js +17 -0
  185. package/lib/utils/icon-registry.d.ts +0 -15
  186. package/lib/utils/icon-registry.js +138 -279
  187. package/lib/utils/icons.d.ts +9 -0
  188. package/lib/utils/icons.js +1 -0
  189. package/lib/utils/loader-registry.d.ts +2 -3
  190. package/lib/utils/loader-registry.js +3 -35
  191. package/lib/utils/locale.d.ts +2 -80
  192. package/lib/utils/locale.js +30 -205
  193. package/lib/utils/mobile.d.ts +0 -1
  194. package/lib/utils/mobile.js +1 -18
  195. package/lib/utils/number-format.d.ts +6 -24
  196. package/lib/utils/number-format.js +53 -136
  197. package/lib/utils/positioning.d.ts +72 -22
  198. package/lib/utils/positioning.js +224 -298
  199. package/lib/utils/property-capture.d.ts +5 -27
  200. package/lib/utils/property-capture.js +36 -144
  201. package/lib/utils/property-manager.d.ts +6 -6
  202. package/lib/utils/property-manager.js +127 -196
  203. package/lib/utils/resize-observer.d.ts +0 -1
  204. package/lib/utils/resize-observer.js +23 -59
  205. package/lib/utils/scroll-lock.d.ts +10 -11
  206. package/lib/utils/scroll-lock.js +165 -173
  207. package/lib/utils/styles.d.ts +0 -6
  208. package/lib/utils/styles.js +17 -48
  209. package/lib/utils/upgrade-properties.d.ts +21 -0
  210. package/lib/utils/upgrade-properties.js +47 -0
  211. package/lib/utils/validity.d.ts +122 -0
  212. package/lib/utils/validity.js +289 -0
  213. package/lib/version.d.ts +1 -2
  214. package/lib/version.js +1 -11
  215. package/package.json +66 -18
  216. package/lib/base/ty-component.d.ts.map +0 -1
  217. package/lib/base/ty-component.js.map +0 -1
  218. package/lib/components/button.d.ts.map +0 -1
  219. package/lib/components/button.js.map +0 -1
  220. package/lib/components/calendar-month.d.ts.map +0 -1
  221. package/lib/components/calendar-month.js.map +0 -1
  222. package/lib/components/calendar-navigation.d.ts.map +0 -1
  223. package/lib/components/calendar-navigation.js.map +0 -1
  224. package/lib/components/calendar.d.ts.map +0 -1
  225. package/lib/components/calendar.js.map +0 -1
  226. package/lib/components/checkbox.d.ts.map +0 -1
  227. package/lib/components/checkbox.js.map +0 -1
  228. package/lib/components/copy.d.ts.map +0 -1
  229. package/lib/components/copy.js.map +0 -1
  230. package/lib/components/date-picker.d.ts.map +0 -1
  231. package/lib/components/date-picker.js.map +0 -1
  232. package/lib/components/dropdown.d.ts +0 -404
  233. package/lib/components/dropdown.d.ts.map +0 -1
  234. package/lib/components/dropdown.js +0 -1563
  235. package/lib/components/dropdown.js.map +0 -1
  236. package/lib/components/file-upload.d.ts.map +0 -1
  237. package/lib/components/file-upload.js.map +0 -1
  238. package/lib/components/icon.d.ts.map +0 -1
  239. package/lib/components/icon.js.map +0 -1
  240. package/lib/components/input.d.ts.map +0 -1
  241. package/lib/components/input.js.map +0 -1
  242. package/lib/components/modal.d.ts.map +0 -1
  243. package/lib/components/modal.js.map +0 -1
  244. package/lib/components/multiselect.d.ts +0 -408
  245. package/lib/components/multiselect.d.ts.map +0 -1
  246. package/lib/components/multiselect.js +0 -1611
  247. package/lib/components/multiselect.js.map +0 -1
  248. package/lib/components/option.d.ts.map +0 -1
  249. package/lib/components/option.js.map +0 -1
  250. package/lib/components/popup.d.ts.map +0 -1
  251. package/lib/components/popup.js.map +0 -1
  252. package/lib/components/radio.d.ts.map +0 -1
  253. package/lib/components/radio.js.map +0 -1
  254. package/lib/components/resize-observer.d.ts.map +0 -1
  255. package/lib/components/resize-observer.js.map +0 -1
  256. package/lib/components/scroll-container.d.ts.map +0 -1
  257. package/lib/components/scroll-container.js.map +0 -1
  258. package/lib/components/step.d.ts.map +0 -1
  259. package/lib/components/step.js.map +0 -1
  260. package/lib/components/switch.d.ts.map +0 -1
  261. package/lib/components/switch.js.map +0 -1
  262. package/lib/components/tab.d.ts.map +0 -1
  263. package/lib/components/tab.js.map +0 -1
  264. package/lib/components/tabs.d.ts.map +0 -1
  265. package/lib/components/tabs.js.map +0 -1
  266. package/lib/components/tag.d.ts.map +0 -1
  267. package/lib/components/tag.js.map +0 -1
  268. package/lib/components/textarea.d.ts.map +0 -1
  269. package/lib/components/textarea.js.map +0 -1
  270. package/lib/components/tooltip.d.ts.map +0 -1
  271. package/lib/components/tooltip.js.map +0 -1
  272. package/lib/components/wizard.d.ts.map +0 -1
  273. package/lib/components/wizard.js.map +0 -1
  274. package/lib/icons/fontawesome/brands.d.ts.map +0 -1
  275. package/lib/icons/fontawesome/brands.js.map +0 -1
  276. package/lib/icons/fontawesome/regular.d.ts.map +0 -1
  277. package/lib/icons/fontawesome/regular.js.map +0 -1
  278. package/lib/icons/fontawesome/solid.d.ts.map +0 -1
  279. package/lib/icons/fontawesome/solid.js.map +0 -1
  280. package/lib/icons/heroicons/micro.d.ts.map +0 -1
  281. package/lib/icons/heroicons/micro.js.map +0 -1
  282. package/lib/icons/heroicons/mini.d.ts.map +0 -1
  283. package/lib/icons/heroicons/mini.js.map +0 -1
  284. package/lib/icons/heroicons/outline.d.ts.map +0 -1
  285. package/lib/icons/heroicons/outline.js.map +0 -1
  286. package/lib/icons/heroicons/solid.d.ts.map +0 -1
  287. package/lib/icons/heroicons/solid.js.map +0 -1
  288. package/lib/icons/lucide.d.ts.map +0 -1
  289. package/lib/icons/lucide.js.map +0 -1
  290. package/lib/icons/material/filled.d.ts.map +0 -1
  291. package/lib/icons/material/filled.js.map +0 -1
  292. package/lib/icons/material/outlined.d.ts.map +0 -1
  293. package/lib/icons/material/outlined.js.map +0 -1
  294. package/lib/icons/material/round.d.ts.map +0 -1
  295. package/lib/icons/material/round.js.map +0 -1
  296. package/lib/icons/material/sharp.d.ts.map +0 -1
  297. package/lib/icons/material/sharp.js.map +0 -1
  298. package/lib/icons/material/two-tone.d.ts.map +0 -1
  299. package/lib/icons/material/two-tone.js.map +0 -1
  300. package/lib/index.d.ts.map +0 -1
  301. package/lib/index.js.map +0 -1
  302. package/lib/styles/button.d.ts.map +0 -1
  303. package/lib/styles/button.js.map +0 -1
  304. package/lib/styles/calendar-month.d.ts.map +0 -1
  305. package/lib/styles/calendar-month.js.map +0 -1
  306. package/lib/styles/calendar-navigation.d.ts.map +0 -1
  307. package/lib/styles/calendar-navigation.js.map +0 -1
  308. package/lib/styles/calendar.d.ts.map +0 -1
  309. package/lib/styles/calendar.js.map +0 -1
  310. package/lib/styles/checkbox.d.ts.map +0 -1
  311. package/lib/styles/checkbox.js.map +0 -1
  312. package/lib/styles/copy.d.ts.map +0 -1
  313. package/lib/styles/copy.js.map +0 -1
  314. package/lib/styles/custom-scrollbar.d.ts.map +0 -1
  315. package/lib/styles/custom-scrollbar.js.map +0 -1
  316. package/lib/styles/date-picker.d.ts.map +0 -1
  317. package/lib/styles/date-picker.js.map +0 -1
  318. package/lib/styles/dropdown.d.ts +0 -12
  319. package/lib/styles/dropdown.d.ts.map +0 -1
  320. package/lib/styles/dropdown.js +0 -1048
  321. package/lib/styles/dropdown.js.map +0 -1
  322. package/lib/styles/file-upload.d.ts.map +0 -1
  323. package/lib/styles/file-upload.js.map +0 -1
  324. package/lib/styles/icon.d.ts.map +0 -1
  325. package/lib/styles/icon.js.map +0 -1
  326. package/lib/styles/input.d.ts.map +0 -1
  327. package/lib/styles/input.js.map +0 -1
  328. package/lib/styles/modal.d.ts.map +0 -1
  329. package/lib/styles/modal.js.map +0 -1
  330. package/lib/styles/multiselect.d.ts +0 -6
  331. package/lib/styles/multiselect.d.ts.map +0 -1
  332. package/lib/styles/multiselect.js +0 -837
  333. package/lib/styles/multiselect.js.map +0 -1
  334. package/lib/styles/option.d.ts.map +0 -1
  335. package/lib/styles/option.js.map +0 -1
  336. package/lib/styles/popup.d.ts.map +0 -1
  337. package/lib/styles/popup.js.map +0 -1
  338. package/lib/styles/radio.d.ts.map +0 -1
  339. package/lib/styles/radio.js.map +0 -1
  340. package/lib/styles/resize-observer.d.ts.map +0 -1
  341. package/lib/styles/resize-observer.js.map +0 -1
  342. package/lib/styles/scroll-container.d.ts.map +0 -1
  343. package/lib/styles/scroll-container.js.map +0 -1
  344. package/lib/styles/step.d.ts.map +0 -1
  345. package/lib/styles/step.js.map +0 -1
  346. package/lib/styles/switch.d.ts.map +0 -1
  347. package/lib/styles/switch.js.map +0 -1
  348. package/lib/styles/tab.d.ts.map +0 -1
  349. package/lib/styles/tab.js.map +0 -1
  350. package/lib/styles/tabs.d.ts.map +0 -1
  351. package/lib/styles/tabs.js.map +0 -1
  352. package/lib/styles/tag.d.ts.map +0 -1
  353. package/lib/styles/tag.js.map +0 -1
  354. package/lib/styles/textarea.d.ts.map +0 -1
  355. package/lib/styles/textarea.js.map +0 -1
  356. package/lib/styles/tooltip.d.ts.map +0 -1
  357. package/lib/styles/tooltip.js.map +0 -1
  358. package/lib/styles/wizard.d.ts.map +0 -1
  359. package/lib/styles/wizard.js.map +0 -1
  360. package/lib/types/common.d.ts.map +0 -1
  361. package/lib/types/common.js.map +0 -1
  362. package/lib/utils/calendar-utils.d.ts.map +0 -1
  363. package/lib/utils/calendar-utils.js.map +0 -1
  364. package/lib/utils/custom-scrollbar.d.ts.map +0 -1
  365. package/lib/utils/custom-scrollbar.js.map +0 -1
  366. package/lib/utils/icon-registry.d.ts.map +0 -1
  367. package/lib/utils/icon-registry.js.map +0 -1
  368. package/lib/utils/loader-registry.d.ts.map +0 -1
  369. package/lib/utils/loader-registry.js.map +0 -1
  370. package/lib/utils/locale.d.ts.map +0 -1
  371. package/lib/utils/locale.js.map +0 -1
  372. package/lib/utils/mobile.d.ts.map +0 -1
  373. package/lib/utils/mobile.js.map +0 -1
  374. package/lib/utils/number-format.d.ts.map +0 -1
  375. package/lib/utils/number-format.js.map +0 -1
  376. package/lib/utils/parse-boolean.d.ts +0 -39
  377. package/lib/utils/parse-boolean.d.ts.map +0 -1
  378. package/lib/utils/parse-boolean.js +0 -58
  379. package/lib/utils/parse-boolean.js.map +0 -1
  380. package/lib/utils/positioning.d.ts.map +0 -1
  381. package/lib/utils/positioning.js.map +0 -1
  382. package/lib/utils/property-capture.d.ts.map +0 -1
  383. package/lib/utils/property-capture.js.map +0 -1
  384. package/lib/utils/property-manager.d.ts.map +0 -1
  385. package/lib/utils/property-manager.js.map +0 -1
  386. package/lib/utils/resize-observer.d.ts.map +0 -1
  387. package/lib/utils/resize-observer.js.map +0 -1
  388. package/lib/utils/scroll-lock.d.ts.map +0 -1
  389. package/lib/utils/scroll-lock.js.map +0 -1
  390. package/lib/utils/styles.d.ts.map +0 -1
  391. package/lib/utils/styles.js.map +0 -1
  392. package/lib/version.d.ts.map +0 -1
  393. package/lib/version.js.map +0 -1
@@ -1,1563 +0,0 @@
1
- /**
2
- * TyDropdown Web Component
3
- * PORTED FROM: clj/ty/components/dropdown.cljs
4
- *
5
- * A semantic dropdown component with:
6
- * - Desktop mode with smart positioning
7
- * - Mobile mode with full-screen modal
8
- * - Search and filtering capabilities
9
- * - Keyboard navigation
10
- * - Form association for native form submission
11
- * - Rich option support (option, ty-option, ty-tag)
12
- *
13
- * @example
14
- * ```html
15
- * <!-- Basic dropdown -->
16
- * <ty-dropdown label="Country" placeholder="Select country" required>
17
- * <option value="us">United States</option>
18
- * <option value="uk">United Kingdom</option>
19
- * <option value="ca">Canada</option>
20
- * </ty-dropdown>
21
- *
22
- * <!-- With rich options -->
23
- * <ty-dropdown label="User">
24
- * <ty-option value="1">
25
- * <div class="flex items-center gap-2">
26
- * <img src="avatar1.jpg" class="w-8 h-8 rounded-full" />
27
- * <span>John Doe</span>
28
- * </div>
29
- * </ty-option>
30
- * </ty-dropdown>
31
- *
32
- * <!-- External search (parent owns filtering, dropdown emits `search` events) -->
33
- * <ty-dropdown label="Search API" external-search debounce="300">
34
- * <option value="1">Result 1</option>
35
- * </ty-dropdown>
36
- * ```
37
- */
38
- import { ensureStyles } from '../utils/styles.js';
39
- import { dropdownStyles } from '../styles/dropdown.js';
40
- import { lockScroll, unlockScroll } from '../utils/scroll-lock.js';
41
- import { isMobileTouch } from '../utils/mobile.js';
42
- import { TyComponent } from '../base/ty-component.js';
43
- import { CustomScrollbar, isCustomScrollbarEnabled } from '../utils/custom-scrollbar.js';
44
- import { getLoaderSvg } from '../utils/loader-registry.js';
45
- // ============================================================================
46
- // Element Hash Utility (equivalent to ClojureScript's hash function)
47
- // ============================================================================
48
- /**
49
- * Counter for generating unique element IDs
50
- */
51
- let elementIdCounter = 0;
52
- /**
53
- * WeakMap to store consistent element hashes
54
- * Automatically garbage collects when element is destroyed
55
- */
56
- const elementIds = new WeakMap();
57
- /**
58
- * Get a consistent unique ID for an element (similar to ClojureScript's hash function)
59
- * Returns the same ID for the same element across multiple calls
60
- *
61
- * @param element - The element to hash
62
- * @returns A consistent numeric hash for the element
63
- */
64
- function getElementHash(element) {
65
- let id = elementIds.get(element);
66
- if (id === undefined) {
67
- id = ++elementIdCounter;
68
- elementIds.set(element, id);
69
- }
70
- return id;
71
- }
72
- // ============================================================================
73
- // SVG Icons
74
- // ============================================================================
75
- /**
76
- * Required indicator SVG icon (from Lucide)
77
- */
78
- const REQUIRED_ICON_SVG = `<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-asterisk"><path d="M12 6v12"/><path d="M17.196 9 6.804 15"/><path d="m6.804 9 10.392 6"/></svg>`;
79
- /**
80
- * Chevron down icon SVG
81
- */
82
- const CHEVRON_DOWN_SVG = `<svg viewBox="0 0 20 20" fill="currentColor">
83
- <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" />
84
- </svg>`;
85
- /**
86
- * Clear button X icon SVG (simple X)
87
- */
88
- const CLEAR_ICON_SVG = `<svg viewBox="0 0 20 20" fill="none" stroke="currentColor" stroke-width="2">
89
- <path d="M6 6L14 14M14 6L6 14" stroke-linecap="round" />
90
- </svg>`;
91
- /**
92
- * Ty Dropdown Component
93
- */
94
- export class TyDropdown extends TyComponent {
95
- constructor() {
96
- super(); // TyComponent handles attachInternals() and attachShadow()
97
- // ============================================================================
98
- // PRIVATE FIELDS (will be removed in Phase 3)
99
- // ============================================================================
100
- this._value = '';
101
- this._placeholder = 'Select an option...';
102
- this._label = '';
103
- this._disabled = false;
104
- this._readonly = false;
105
- this._required = false;
106
- this._externalSearch = false;
107
- this._clearable = true;
108
- this._loading = false;
109
- this._size = 'md';
110
- // Component state
111
- this._state = {
112
- open: false,
113
- search: '',
114
- highlightedIndex: -1,
115
- filteredOptions: [],
116
- currentValue: null,
117
- mode: 'desktop' // Updated dynamically on render via syncMode()
118
- };
119
- // Scroll lock ID (consistent across open/close cycles)
120
- this._scrollLockId = null;
121
- // Event handler references for cleanup
122
- this._stubClickHandler = null;
123
- this._outsideClickHandler = null;
124
- this._optionClickHandler = null;
125
- this._searchInputHandler = null;
126
- this._blockSearchClick = null;
127
- this._keyboardHandler = null;
128
- this._clearClickHandler = null;
129
- // Debounce properties for search event
130
- this._debounce = 0;
131
- this._searchDebounceTimer = null;
132
- // Custom scrollbar for options list
133
- this._optionsScrollbar = null;
134
- const shadow = this.shadowRoot;
135
- ensureStyles(shadow, { css: dropdownStyles, id: 'ty-dropdown' });
136
- // Render based on device type
137
- }
138
- render() {
139
- // Sync mode on every render so rotation/resize is picked up
140
- this._state.mode = isMobileTouch() ? 'mobile' : 'desktop';
141
- if (this._state.mode === 'mobile') {
142
- this.renderMobile();
143
- }
144
- else {
145
- this.renderDesktop();
146
- }
147
- // Loading visual lives on a dynamically rendered wrapper; reapply each render
148
- this.applyLoadingState();
149
- }
150
- // ============================================================================
151
- // LIFECYCLE HOOKS - TyComponent integration
152
- // ============================================================================
153
- /**
154
- * Called when component is connected to DOM
155
- * TyComponent handles property capture automatically
156
- */
157
- onConnect() {
158
- this.initializeState();
159
- }
160
- /**
161
- * Called when component is disconnected from DOM
162
- * Clean up event listeners and timers
163
- */
164
- onDisconnect() {
165
- // Clean up document-level listeners
166
- const outsideClickHandler = this.tyOutsideClickHandler;
167
- if (outsideClickHandler) {
168
- document.removeEventListener('click', outsideClickHandler);
169
- this.tyOutsideClickHandler = null;
170
- }
171
- // Clear any pending debounce timer
172
- if (this._searchDebounceTimer !== null) {
173
- clearTimeout(this._searchDebounceTimer);
174
- this._searchDebounceTimer = null;
175
- }
176
- // Cleanup custom scrollbar
177
- this._destroyOptionsScrollbar();
178
- }
179
- /**
180
- * Called when properties change
181
- * Handle state synchronization BEFORE render
182
- */
183
- onPropertiesChanged(changes) {
184
- // Sync private fields from PropertyManager
185
- // (Render methods still use private fields)
186
- for (const { name, newValue } of changes) {
187
- switch (name) {
188
- case 'value':
189
- this._value = newValue || '';
190
- this._state.currentValue = newValue || null;
191
- this.syncSelectedOption();
192
- break;
193
- case 'placeholder':
194
- this._placeholder = newValue || 'Select an option...';
195
- this.updatePlaceholderInDOM();
196
- break;
197
- case 'label':
198
- this._label = newValue || '';
199
- break;
200
- case 'disabled':
201
- this._disabled = newValue;
202
- break;
203
- case 'readonly':
204
- this._readonly = newValue;
205
- break;
206
- case 'required':
207
- this._required = newValue;
208
- break;
209
- case 'externalSearch':
210
- this._externalSearch = newValue;
211
- break;
212
- case 'clearable':
213
- this._clearable = newValue;
214
- this.updateClearButton();
215
- break;
216
- case 'size':
217
- this._size = newValue;
218
- break;
219
- case 'debounce':
220
- this._debounce = newValue;
221
- break;
222
- case 'loading':
223
- this._loading = newValue;
224
- this.applyLoadingState();
225
- break;
226
- }
227
- }
228
- }
229
- /**
230
- * Toggle the loading visual state on the open popup.
231
- * Replaces the options list with a centered spinner; search input stays usable.
232
- * Pulls the latest registered loader SVG on each call so registry changes
233
- * take effect on the next loading toggle.
234
- */
235
- applyLoadingState() {
236
- const shadow = this.shadowRoot;
237
- if (!shadow)
238
- return;
239
- const svg = this._loading ? getLoaderSvg() : null;
240
- shadow.querySelectorAll('.dropdown-options-wrapper').forEach((wrapper) => {
241
- wrapper.classList.toggle('loading', this._loading);
242
- if (this._loading) {
243
- wrapper.setAttribute('aria-busy', 'true');
244
- const spinner = wrapper.querySelector('.dropdown-loading-spinner');
245
- if (spinner && svg)
246
- spinner.innerHTML = svg;
247
- }
248
- else {
249
- wrapper.removeAttribute('aria-busy');
250
- }
251
- });
252
- }
253
- /**
254
- * Get the form value for this component
255
- * TyComponent calls this automatically when formValue property changes
256
- */
257
- getFormValue() {
258
- return this._state.currentValue || null;
259
- }
260
- /**
261
- * Parse dropdown value (single string)
262
- */
263
- parseValue(value) {
264
- // Defensive check: ensure value is actually a string before calling .trim()
265
- if (!value || typeof value !== 'string' || value.trim() === '')
266
- return null;
267
- return value.trim();
268
- }
269
- /**
270
- * Initialize component state from attributes
271
- */
272
- initializeState() {
273
- if (this._value) {
274
- this._state.currentValue = this.parseValue(this._value);
275
- // CRITICAL: Options may not be slotted yet when connectedCallback runs
276
- // Defer sync to next frame to ensure options are available
277
- requestAnimationFrame(() => {
278
- this.syncSelectedOption();
279
- // updateFormValue() called automatically by TyComponent
280
- });
281
- }
282
- // Listen for clear-selection events from ty-options (mobile clear button)
283
- this.addEventListener('clear-selection', (e) => {
284
- e.stopPropagation(); // Prevent bubbling
285
- // Clear the selection
286
- this.clearSelection();
287
- this._state.currentValue = null;
288
- this.updateComponentValue();
289
- this.updateSelectionDisplay();
290
- // updateFormValue() called automatically by TyComponent
291
- // Dispatch change event
292
- this.dispatchEvent(new CustomEvent('change', {
293
- detail: {
294
- value: null,
295
- text: '',
296
- option: null,
297
- originalEvent: e
298
- },
299
- bubbles: true,
300
- composed: true
301
- }));
302
- // If in mobile modal, close it
303
- if (this._state.open && this._state.mode === 'mobile') {
304
- this.closeMobileModal();
305
- }
306
- });
307
- // Render the component with current state
308
- this.render();
309
- }
310
- /**
311
- * Update placeholder text in existing rendered HTML
312
- * Called when placeholder changes after initial render
313
- */
314
- updatePlaceholderInDOM() {
315
- const shadow = this.shadowRoot;
316
- // Update stub placeholder
317
- const stubPlaceholder = shadow.querySelector('.dropdown-placeholder');
318
- if (stubPlaceholder) {
319
- stubPlaceholder.textContent = this._placeholder;
320
- }
321
- // Update search input placeholder (desktop)
322
- const searchInput = shadow.querySelector('.dropdown-search-input');
323
- if (searchInput) {
324
- searchInput.placeholder = this._placeholder;
325
- }
326
- // Update mobile search input placeholder
327
- const mobileSearchInput = shadow.querySelector('.mobile-search-input');
328
- if (mobileSearchInput) {
329
- mobileSearchInput.placeholder = this._placeholder;
330
- }
331
- }
332
- // ============================================================================
333
- // SHARED CORE METHODS
334
- // ============================================================================
335
- // Methods used by BOTH desktop and mobile implementations:
336
- //
337
- // OPTION MANAGEMENT:
338
- // - getOptions(): Get all option elements from slot
339
- // - getOptionData(): Extract value and text from option element
340
- // - selectOption(): Select an option (used by both desktop/mobile clicks)
341
- // - clearSelection(): Clear all selections
342
- // - syncSelectedOption(): Sync selected option based on current value
343
- //
344
- // STATE SYNCHRONIZATION:
345
- // - updateComponentValue(): Update component value attribute
346
- // - updateFormValue(): Update form value via ElementInternals
347
- //
348
- // DISPLAY UPDATES:
349
- // - updateSelectionDisplay(): Show/hide placeholder
350
- // - updateClearButton(): Show/hide clear button (desktop only)
351
- //
352
- // EVENT DISPATCHING:
353
- // - dispatchChangeEvent(): Dispatch change event
354
- // - dispatchSearchEvent(): Dispatch search event with debounce
355
- // - fireSearchEvent(): Fire the actual search event
356
- //
357
- // FILTERING & SEARCH:
358
- // - filterOptions(): Filter options by query
359
- // - updateOptionVisibility(): Show/hide options based on filter
360
- //
361
- // HIGHLIGHTING (keyboard navigation):
362
- // - clearHighlights(): Clear all option highlights
363
- // - highlightOption(): Highlight option at index
364
- //
365
- // UTILITY:
366
- // - buildStubClasses(): Build CSS class list for stub
367
- // ============================================================================
368
- /**
369
- * Update form value via ElementInternals
370
- */
371
- /**
372
- * Get all option elements from slot
373
- * Supports: <option>, <ty-option>, <ty-tag>
374
- */
375
- getOptions() {
376
- const shadow = this.shadowRoot;
377
- const slot = shadow.querySelector('slot:not([name])');
378
- if (!slot)
379
- return [];
380
- const assigned = (slot.assignedElements ? slot.assignedElements() : []);
381
- return assigned.filter(el => {
382
- const tag = el.tagName;
383
- return tag === 'OPTION' || tag === 'TY-OPTION' || tag === 'TY-TAG';
384
- });
385
- }
386
- /**
387
- * Extract value and text from option element
388
- */
389
- getOptionData(element) {
390
- const tag = element.tagName;
391
- if (tag === 'OPTION') {
392
- const optionEl = element;
393
- return {
394
- value: optionEl.value || optionEl.textContent || '',
395
- text: optionEl.textContent || '',
396
- element
397
- };
398
- }
399
- if (tag === 'TY-OPTION') {
400
- // CRITICAL: Use .value property, not getAttribute!
401
- // Framework may set property before attribute
402
- const tyOption = element;
403
- return {
404
- value: tyOption.value || element.textContent || '',
405
- text: element.textContent || '',
406
- element
407
- };
408
- }
409
- if (tag === 'TY-TAG') {
410
- return {
411
- value: element.getAttribute('value') || element.textContent || '',
412
- text: element.textContent || '',
413
- element
414
- };
415
- }
416
- return {
417
- value: element.textContent || '',
418
- text: element.textContent || '',
419
- element
420
- };
421
- }
422
- /**
423
- * Clear all selections
424
- */
425
- clearSelection() {
426
- const shadow = this.shadowRoot;
427
- const options = this.getOptions();
428
- // Clear selected attribute from all options
429
- options.forEach(opt => opt.removeAttribute('selected'));
430
- // Remove clones from selected slot
431
- const selectedSlot = shadow.querySelector('slot[name="selected"]');
432
- if (selectedSlot) {
433
- const assigned = selectedSlot.assignedElements ? selectedSlot.assignedElements() : [];
434
- const clones = Array.from(assigned).filter((el) => el.hasAttribute('cloned'));
435
- clones.forEach((clone) => clone.remove());
436
- }
437
- }
438
- /**
439
- * Select an option
440
- */
441
- selectOption(option, originalEvent) {
442
- const optionData = this.getOptionData(option);
443
- const isEmpty = !optionData.value || optionData.value.trim() === '';
444
- this.clearSelection(); // If value is empty, just clear and don't clone anything
445
- if (isEmpty) {
446
- this._state.currentValue = null;
447
- }
448
- else {
449
- // Clone option for display in stub
450
- const clone = option.cloneNode(true);
451
- clone.setAttribute('slot', 'selected');
452
- clone.setAttribute('cloned', 'true');
453
- option.parentNode.appendChild(clone);
454
- // Mark original as selected
455
- option.setAttribute('selected', '');
456
- // Update state
457
- this._state.currentValue = optionData.value;
458
- }
459
- this.updateComponentValue();
460
- this.updateSelectionDisplay();
461
- // updateFormValue() called automatically by TyComponent
462
- // Dispatch change event
463
- this.dispatchChangeEvent(option, originalEvent);
464
- }
465
- /**
466
- * Sync selected option based on current value
467
- */
468
- syncSelectedOption() {
469
- const options = this.getOptions();
470
- const currentValue = this._state.currentValue;
471
- if (!currentValue) {
472
- this.clearSelection();
473
- return;
474
- }
475
- // Find matching option
476
- const matchingOption = options.find(opt => {
477
- const data = this.getOptionData(opt);
478
- return data.value === currentValue;
479
- });
480
- if (matchingOption) {
481
- this.selectOption(matchingOption);
482
- }
483
- }
484
- /**
485
- * Update component value attribute for consistency
486
- */
487
- updateComponentValue() {
488
- const currentValue = this._state.currentValue;
489
- if (currentValue !== null) {
490
- this.setAttribute('value', currentValue);
491
- }
492
- else {
493
- this.removeAttribute('value');
494
- }
495
- // Also set property
496
- this._value = currentValue || '';
497
- }
498
- /**
499
- * Update selection display (show/hide placeholder)
500
- */
501
- updateSelectionDisplay() {
502
- const shadow = this.shadowRoot;
503
- const stub = shadow.querySelector('.dropdown-stub');
504
- if (!stub)
505
- return;
506
- const options = this.getOptions();
507
- const hasSelected = options.some(opt => opt.hasAttribute('selected'));
508
- if (hasSelected) {
509
- stub.classList.add('has-selection');
510
- }
511
- else {
512
- stub.classList.remove('has-selection');
513
- }
514
- // Update clear button visibility
515
- this.updateClearButton();
516
- }
517
- updateClearButton() {
518
- const shadow = this.shadowRoot;
519
- const clearBtn = shadow.querySelector('.dropdown-clear-btn');
520
- if (!clearBtn)
521
- return;
522
- const hasSelection = this._state.currentValue !== null && this._state.currentValue !== '';
523
- const shouldShow = this._clearable &&
524
- hasSelection &&
525
- !this._disabled &&
526
- !this._readonly &&
527
- !this._state.open &&
528
- this._state.mode !== 'mobile';
529
- // CRITICAL: Only show clear button when dropdown is closed
530
- if (shouldShow) {
531
- clearBtn.style.display = 'block';
532
- }
533
- else {
534
- clearBtn.style.display = 'none';
535
- }
536
- }
537
- /**
538
- * Dispatch change event
539
- */
540
- dispatchChangeEvent(option, originalEvent) {
541
- const optionData = this.getOptionData(option);
542
- this.dispatchEvent(new CustomEvent('change', {
543
- detail: {
544
- value: this._state.currentValue,
545
- text: optionData.text,
546
- option,
547
- originalEvent: originalEvent || null
548
- },
549
- bubbles: true,
550
- composed: true
551
- }));
552
- }
553
- filterOptions(options, query) {
554
- if (!query || query.trim() === '') {
555
- return options;
556
- }
557
- const searchLower = query.toLowerCase();
558
- return options.filter(({ text }) => text.toLowerCase().includes(searchLower));
559
- }
560
- updateOptionVisibility(filteredOptions, allOptions) {
561
- const visibleValues = new Set(filteredOptions.map(opt => opt.value));
562
- allOptions.forEach(({ value, element }) => {
563
- if (visibleValues.has(value)) {
564
- element.removeAttribute('hidden');
565
- }
566
- else {
567
- element.setAttribute('hidden', '');
568
- }
569
- });
570
- }
571
- /**
572
- * Clear all option highlights
573
- */
574
- clearHighlights(options) {
575
- options.forEach(({ element }) => {
576
- element.removeAttribute('highlighted');
577
- });
578
- }
579
- highlightOption(options, index) {
580
- this.clearHighlights(options);
581
- if (index >= 0 && index < options.length) {
582
- const { element } = options[index];
583
- element.setAttribute('highlighted', '');
584
- // Scroll into view
585
- element.scrollIntoView({
586
- behavior: 'smooth',
587
- block: 'nearest',
588
- inline: 'nearest'
589
- });
590
- }
591
- }
592
- // ============================================================================
593
- // SHARED EVENT HANDLERS
594
- // ============================================================================
595
- // Event handlers used by BOTH desktop and mobile:
596
- //
597
- // - handleSearchInput(): Handles search input for both dialog and modal
598
- // * Desktop: Filters options locally OR dispatches search event
599
- // * Mobile: Same behavior
600
- // ============================================================================
601
- /**
602
- * Setup event listeners
603
- */
604
- setupDesktopEventListeners() {
605
- const shadow = this.shadowRoot;
606
- const stub = shadow.querySelector('.dropdown-stub');
607
- const optionsSlot = shadow.querySelector('#options-slot');
608
- const searchInput = shadow.querySelector('.dropdown-search-input');
609
- if (stub) {
610
- this._stubClickHandler = this.handleDesktopStubClick.bind(this);
611
- stub.addEventListener('click', this._stubClickHandler);
612
- }
613
- // Add option click handler to slot
614
- if (optionsSlot) {
615
- this._optionClickHandler = this.handleDesktopOptionClick.bind(this);
616
- optionsSlot.addEventListener('click', this._optionClickHandler);
617
- }
618
- // Add search input handlers
619
- if (searchInput) {
620
- this._searchInputHandler = this.handleSearchInput.bind(this);
621
- this._blockSearchClick = this.blockSearchClick.bind(this);
622
- searchInput.addEventListener('input', this._searchInputHandler);
623
- searchInput.addEventListener('click', this._blockSearchClick);
624
- }
625
- // Add clear button handler
626
- const clearBtn = shadow.querySelector('.dropdown-clear-btn');
627
- if (clearBtn) {
628
- this._clearClickHandler = this.handleDesktopClearClick.bind(this);
629
- clearBtn.addEventListener('click', this._clearClickHandler);
630
- }
631
- // Setup document-level event listeners immediately (like ClojureScript version)
632
- this._outsideClickHandler = this.handleDesktopOutsideClick.bind(this);
633
- this._keyboardHandler = this.handleDesktopKeyboard.bind(this);
634
- document.addEventListener('click', this._outsideClickHandler);
635
- document.addEventListener('keydown', this._keyboardHandler);
636
- }
637
- // removeEventListeners removed - now using ClojureScript pattern
638
- // ============================================================================
639
- // SHARED HELPER METHODS (Internal)
640
- // ============================================================================
641
- // Internal helpers that reduce duplication between desktop and mobile.
642
- // These are called by both implementations but not exposed publicly.
643
- // ============================================================================
644
- /**
645
- * Lock scroll with consistent ID generation
646
- */
647
- lockDropdownScroll() {
648
- const dropdownId = `dropdown-${this.id || 'anon'}-${getElementHash(this)}`;
649
- this._scrollLockId = dropdownId;
650
- lockScroll(dropdownId);
651
- }
652
- /**
653
- * Unlock scroll using stored ID
654
- */
655
- unlockDropdownScroll() {
656
- if (this._scrollLockId) {
657
- unlockScroll(this._scrollLockId);
658
- this._scrollLockId = null;
659
- }
660
- }
661
- /**
662
- * Initialize options state when opening dropdown/modal
663
- * @param highlightSelected - Whether to highlight the currently selected option
664
- */
665
- initializeOptionsState(highlightSelected = false) {
666
- const options = this.getOptions().map(el => this.getOptionData(el));
667
- const currentValue = this._state.currentValue;
668
- this._state.filteredOptions = options;
669
- if (highlightSelected && currentValue) {
670
- // Find the index of the currently selected option
671
- const selectedIndex = options.findIndex(opt => opt.value === currentValue);
672
- this._state.highlightedIndex = selectedIndex;
673
- // Highlight and scroll to selected option if it exists
674
- if (selectedIndex >= 0) {
675
- this.highlightOption(options, selectedIndex);
676
- }
677
- }
678
- else {
679
- this._state.highlightedIndex = -1;
680
- }
681
- }
682
- /**
683
- * Base stub click handler - validates state then calls open callback
684
- * @param e - Click event
685
- * @param openCallback - Function to call to open (desktop or mobile)
686
- */
687
- handleStubClickBase(e, openCallback) {
688
- e.preventDefault();
689
- e.stopPropagation();
690
- if (this._disabled || this._readonly) {
691
- return;
692
- }
693
- openCallback();
694
- }
695
- /**
696
- * Base option click handler - finds option, selects it, then calls close callback
697
- * @param e - Click event
698
- * @param closeCallback - Function to call to close (desktop or mobile)
699
- */
700
- handleOptionClickBase(e, closeCallback) {
701
- e.preventDefault();
702
- e.stopPropagation();
703
- const target = e.target;
704
- // Find the option element (might be clicking on child element)
705
- const option = target.closest('option, ty-option, ty-tag');
706
- if (!option)
707
- return;
708
- // Select the option
709
- this.selectOption(option, e);
710
- // Update display
711
- this.updateSelectionDisplay();
712
- // Close dropdown/modal
713
- closeCallback();
714
- }
715
- /**
716
- * Base clear click handler - clears selection and optionally closes
717
- * @param e - Click event
718
- * @param closeCallback - Optional function to call to close (mobile only)
719
- */
720
- handleClearClickBase(e, closeCallback) {
721
- e.preventDefault();
722
- e.stopPropagation();
723
- // Clear the selection
724
- this.clearSelection();
725
- this._state.currentValue = null;
726
- this.updateComponentValue();
727
- this.updateSelectionDisplay();
728
- // updateFormValue() called automatically by TyComponent
729
- // Dispatch change event with null value
730
- this.dispatchEvent(new CustomEvent('change', {
731
- detail: {
732
- value: null,
733
- text: '',
734
- option: null,
735
- originalEvent: e
736
- },
737
- bubbles: true,
738
- composed: true
739
- }));
740
- // Close if callback provided (mobile modal case)
741
- if (closeCallback) {
742
- closeCallback();
743
- }
744
- }
745
- // ============================================================================
746
- // DESKTOP IMPLEMENTATION (Dialog-based)
747
- // ============================================================================
748
- // Desktop uses <dialog> element with smart positioning.
749
- //
750
- // EVENT FLOW:
751
- // 1. User clicks stub → handleDesktopStubClick() → openDesktopDropdown()
752
- // 2. Dialog shown with showModal(), positioned via calculateDesktopPosition()
753
- // 3. User types in search → handleSearchInput() [SHARED]
754
- // 4. User clicks option → handleDesktopOptionClick() → selectOption() [SHARED] → closeDesktopDropdown()
755
- // 5. User clicks outside → handleDesktopOutsideClick() → closeDesktopDropdown()
756
- // 6. User presses Escape → handleDesktopKeyboard() → closeDesktopDropdown()
757
- // 7. User clicks clear button → handleDesktopClearClick() → clearSelection() [SHARED]
758
- //
759
- // METHODS:
760
- // - calculateDesktopPosition(): Smart dropdown positioning (below/above stub)
761
- // - openDesktopDropdown(): Open desktop dialog
762
- // - closeDesktopDropdown(): Close desktop dialog
763
- // - handleDesktopStubClick(): Open on stub click
764
- // - handleDesktopOptionClick(): Select option and close
765
- // - handleDesktopClearClick(): Clear selection
766
- // - handleDesktopOutsideClick(): Close on outside click
767
- // - handleDesktopKeyboard(): Arrow navigation + Enter/Escape
768
- // - renderDesktop(): Render desktop HTML
769
- // - setupDesktopEventListeners(): Setup desktop event handlers
770
- // ============================================================================
771
- /**
772
- * Calculate and set dropdown position with smart direction detection
773
- */
774
- calculateDesktopPosition() {
775
- const shadow = this.shadowRoot;
776
- const stub = shadow.querySelector('.dropdown-stub');
777
- const dialog = shadow.querySelector('.dropdown-dialog');
778
- if (!stub || !dialog)
779
- return;
780
- const stubRect = stub.getBoundingClientRect();
781
- const viewportHeight = window.innerHeight;
782
- const viewportWidth = window.innerWidth;
783
- // Get dialog dimensions (it's already shown with showModal)
784
- const dialogRect = dialog.getBoundingClientRect();
785
- const estimatedHeight = dialogRect.height || 200;
786
- const padding = 8;
787
- const wrapPadding = 20;
788
- // Available space calculations
789
- const spaceBelow = viewportHeight - stubRect.bottom;
790
- const spaceRight = viewportWidth - stubRect.left;
791
- // Smart direction logic
792
- const positionBelow = spaceBelow >= estimatedHeight + padding;
793
- const fitsHorizontally = spaceRight >= stubRect.width;
794
- // Calculate position coordinates
795
- const x = fitsHorizontally
796
- ? stubRect.left - wrapPadding
797
- : Math.max(padding, viewportWidth - stubRect.width - padding);
798
- const y = positionBelow
799
- ? stubRect.top - wrapPadding
800
- : viewportHeight - stubRect.bottom - wrapPadding;
801
- const width = stubRect.width + wrapPadding + wrapPadding;
802
- // Set CSS variables for positioning
803
- this.style.setProperty('--dropdown-x', `${x}px`);
804
- this.style.setProperty('--dropdown-y', `${y}px`);
805
- this.style.setProperty('--dropdown-width', `${width}px`);
806
- this.style.setProperty('--dropdown-offset-x', '0px');
807
- this.style.setProperty('--dropdown-offset-y', '0px');
808
- this.style.setProperty('--dropdown-padding', `${wrapPadding}px`);
809
- // Set direction classes for CSS styling
810
- if (positionBelow) {
811
- dialog.classList.add('position-below');
812
- dialog.classList.remove('position-above');
813
- }
814
- else {
815
- dialog.classList.add('position-above');
816
- dialog.classList.remove('position-below');
817
- }
818
- // Optional: Store direction for debugging
819
- this.style.setProperty('--dropdown-direction', positionBelow ? 'below' : 'above');
820
- }
821
- /**
822
- * Open dropdown dialog
823
- */
824
- openDesktopDropdown() {
825
- console.log('opening desktop dropdown');
826
- const shadow = this.shadowRoot;
827
- const dialog = shadow.querySelector('.dropdown-dialog');
828
- if (!dialog)
829
- return;
830
- // Lock scroll
831
- this.lockDropdownScroll();
832
- // Show modal first so browser can calculate dimensions
833
- dialog.showModal();
834
- dialog.classList.add('open');
835
- // Position dropdown AFTER showing modal
836
- this.calculateDesktopPosition();
837
- // Update component state
838
- this._state.open = true;
839
- // Update visual states
840
- const chevron = shadow.querySelector('.dropdown-chevron');
841
- if (chevron)
842
- chevron.classList.add('open');
843
- const searchChevron = shadow.querySelector('.dropdown-search-chevron');
844
- if (searchChevron)
845
- searchChevron.classList.add('open');
846
- // Initialize options state and highlight selected option
847
- this.initializeOptionsState(true);
848
- // Focus search input
849
- const searchInput = shadow.querySelector('.dropdown-search-input');
850
- if (searchInput) {
851
- setTimeout(() => searchInput.focus(), 100);
852
- }
853
- // Setup custom scrollbar on options
854
- this._setupOptionsScrollbar();
855
- // Hide clear button when dropdown is open
856
- this.updateClearButton();
857
- }
858
- /**
859
- * Close dropdown dialog
860
- */
861
- closeDesktopDropdown() {
862
- const shadow = this.shadowRoot;
863
- const dialog = shadow.querySelector('.dropdown-dialog');
864
- if (!dialog)
865
- return;
866
- // Unlock scroll
867
- this.unlockDropdownScroll();
868
- // Destroy custom scrollbar
869
- this._destroyOptionsScrollbar();
870
- // Close dialog
871
- dialog.classList.remove('open');
872
- dialog.classList.remove('position-above');
873
- dialog.classList.remove('position-below');
874
- dialog.close();
875
- // Update state
876
- this._state.open = false;
877
- this._state.highlightedIndex = -1;
878
- // Update visual states
879
- const chevron = shadow.querySelector('.dropdown-chevron');
880
- if (chevron)
881
- chevron.classList.remove('open');
882
- const searchChevron = shadow.querySelector('.dropdown-search-chevron');
883
- if (searchChevron)
884
- searchChevron.classList.remove('open');
885
- // Reset search query when in external mode (parent owns the options)
886
- if (this._externalSearch) {
887
- this._state.search = '';
888
- }
889
- // Show clear button when dropdown is closed (if applicable)
890
- this.updateClearButton();
891
- }
892
- /**
893
- * Handle outside click to close dropdown
894
- */
895
- handleDesktopOutsideClick(e) {
896
- if (!this._state.open)
897
- return;
898
- const target = e.target;
899
- const shadow = this.shadowRoot;
900
- // CRITICAL: Check if click is inside .dropdown-wrapper (like ClojureScript version)
901
- // Not the whole component - this prevents the component itself from being counted
902
- const wrapper = shadow.querySelector('.dropdown-wrapper');
903
- const clickedInside = wrapper && wrapper.contains(target);
904
- if (!clickedInside) {
905
- this.closeDesktopDropdown();
906
- }
907
- }
908
- /**
909
- * Handle search input changes
910
- */
911
- handleSearchInput(e) {
912
- const target = e.target;
913
- const query = target.value;
914
- // Update search state
915
- this._state.search = query;
916
- if (this._externalSearch) {
917
- // External search: dispatch event for external handling
918
- this.dispatchSearchEvent(query, e);
919
- }
920
- else {
921
- // Internal search: filter options locally
922
- const allOptions = this.getOptions().map(el => this.getOptionData(el));
923
- const filtered = this.filterOptions(allOptions, query);
924
- // Update state
925
- this._state.filteredOptions = filtered;
926
- this._state.highlightedIndex = -1;
927
- // Update visibility
928
- this.updateOptionVisibility(filtered, allOptions);
929
- // Clear highlights
930
- this.clearHighlights(allOptions);
931
- }
932
- }
933
- /**
934
- * Block search input click from bubbling
935
- * Prevents search input clicks from triggering outside click handler
936
- */
937
- blockSearchClick(e) {
938
- e.stopPropagation();
939
- e.preventDefault();
940
- }
941
- /**
942
- * Dispatch search event for external search handling
943
- * With optional debounce support
944
- */
945
- dispatchSearchEvent(query, originalEvent) {
946
- // Clear existing timer
947
- if (this._searchDebounceTimer !== null) {
948
- clearTimeout(this._searchDebounceTimer);
949
- this._searchDebounceTimer = null;
950
- }
951
- // If debounce is set, debounce the event
952
- if (this._debounce > 0) {
953
- this._searchDebounceTimer = window.setTimeout(() => {
954
- this.fireSearchEvent(query, originalEvent);
955
- this._searchDebounceTimer = null;
956
- }, this._debounce);
957
- }
958
- else {
959
- // Fire immediately if no debounce
960
- this.fireSearchEvent(query, originalEvent);
961
- }
962
- }
963
- /**
964
- * Fire the actual search event
965
- */
966
- fireSearchEvent(query, originalEvent) {
967
- this.dispatchEvent(new CustomEvent('search', {
968
- detail: {
969
- query,
970
- originalEvent: originalEvent || null
971
- },
972
- bubbles: true,
973
- composed: true
974
- }));
975
- }
976
- // ============================================================================
977
- // DESKTOP KEYBOARD NAVIGATION
978
- // ============================================================================
979
- // Handles: Escape, Enter, ArrowUp, ArrowDown
980
- // Works with both search input focus and document-level events
981
- // Wraps around: ArrowUp at index 0 → last option, ArrowDown at last → first
982
- // ============================================================================
983
- /**
984
- * Handle keyboard navigation
985
- */
986
- handleDesktopKeyboard(e) {
987
- if (!this._state.open)
988
- return;
989
- const shadow = this.shadowRoot;
990
- const searchInput = shadow.querySelector('.dropdown-search-input');
991
- const target = e.target;
992
- // Only handle navigation keys when dropdown is open and either:
993
- // 1. Event comes from search input, OR
994
- // 2. Event comes from document but search input is not focused
995
- const shouldHandle = target === searchInput ||
996
- document.activeElement !== searchInput;
997
- if (!shouldHandle)
998
- return;
999
- // Get current state values
1000
- const filteredOptions = this._state.filteredOptions;
1001
- const optionsCount = filteredOptions.length;
1002
- const currentHighlightedIndex = this._state.highlightedIndex;
1003
- switch (e.key) {
1004
- case 'Escape':
1005
- e.preventDefault();
1006
- e.stopPropagation();
1007
- this.closeDesktopDropdown();
1008
- break;
1009
- case 'Enter':
1010
- e.preventDefault();
1011
- e.stopPropagation();
1012
- // Select highlighted option if any
1013
- if (currentHighlightedIndex >= 0 && currentHighlightedIndex < optionsCount) {
1014
- const option = filteredOptions[currentHighlightedIndex];
1015
- this.selectOption(option.element, e);
1016
- this.updateSelectionDisplay();
1017
- this.closeDesktopDropdown();
1018
- }
1019
- break;
1020
- case 'ArrowUp':
1021
- e.preventDefault();
1022
- e.stopPropagation();
1023
- let newIndexUp;
1024
- if (optionsCount === 0) {
1025
- newIndexUp = -1;
1026
- }
1027
- else if (currentHighlightedIndex === -1) {
1028
- // Nothing highlighted, go to last option
1029
- newIndexUp = optionsCount - 1;
1030
- }
1031
- else if (currentHighlightedIndex === 0) {
1032
- // At first option, wrap to last
1033
- newIndexUp = optionsCount - 1;
1034
- }
1035
- else {
1036
- // Move up one
1037
- newIndexUp = currentHighlightedIndex - 1;
1038
- }
1039
- this._state.highlightedIndex = newIndexUp;
1040
- this.highlightOption(filteredOptions, newIndexUp);
1041
- break;
1042
- case 'ArrowDown':
1043
- e.preventDefault();
1044
- e.stopPropagation();
1045
- let newIndexDown;
1046
- if (optionsCount === 0) {
1047
- newIndexDown = -1;
1048
- }
1049
- else if (currentHighlightedIndex === -1) {
1050
- // Nothing highlighted, go to first option
1051
- newIndexDown = 0;
1052
- }
1053
- else if (currentHighlightedIndex === optionsCount - 1) {
1054
- // At last option, wrap to first
1055
- newIndexDown = 0;
1056
- }
1057
- else {
1058
- // Move down one
1059
- newIndexDown = currentHighlightedIndex + 1;
1060
- }
1061
- this._state.highlightedIndex = newIndexDown;
1062
- this.highlightOption(filteredOptions, newIndexDown);
1063
- break;
1064
- }
1065
- }
1066
- /**
1067
- * Handle stub click - open dropdown
1068
- */
1069
- handleDesktopStubClick(e) {
1070
- this.handleStubClickBase(e, () => this.openDesktopDropdown());
1071
- }
1072
- /**
1073
- * Handle option click - select option and close
1074
- */
1075
- handleDesktopOptionClick(e) {
1076
- this.handleOptionClickBase(e, () => this.closeDesktopDropdown());
1077
- }
1078
- /**
1079
- * Handle clear button click - clear selection
1080
- * CRITICAL: Must prevent dropdown from opening!
1081
- */
1082
- handleDesktopClearClick(e) {
1083
- this.handleClearClickBase(e);
1084
- // Desktop doesn't close on clear, just clears the selection
1085
- }
1086
- // ============================================================================
1087
- // CUSTOM SCROLLBAR FOR OPTIONS
1088
- // ============================================================================
1089
- _setupOptionsScrollbar() {
1090
- if (!isCustomScrollbarEnabled())
1091
- return;
1092
- const shadow = this.shadowRoot;
1093
- const optionsDiv = shadow.querySelector('.dropdown-options');
1094
- const optionsWrapper = shadow.querySelector('.dropdown-options-wrapper');
1095
- if (!optionsDiv || !optionsWrapper)
1096
- return;
1097
- this._destroyOptionsScrollbar();
1098
- optionsDiv.classList.add('ty-custom-scroll');
1099
- this._optionsScrollbar = new CustomScrollbar(optionsDiv, { vertical: true });
1100
- // Append track to wrapper (outside scroll area)
1101
- if (this._optionsScrollbar.trackY) {
1102
- optionsWrapper.appendChild(this._optionsScrollbar.trackY);
1103
- }
1104
- }
1105
- _destroyOptionsScrollbar() {
1106
- if (this._optionsScrollbar) {
1107
- this._optionsScrollbar.trackY?.remove();
1108
- this._optionsScrollbar.destroy();
1109
- this._optionsScrollbar = null;
1110
- }
1111
- }
1112
- /**
1113
- * Build CSS class list for stub
1114
- */
1115
- buildStubClasses() {
1116
- const classes = [this._size];
1117
- if (this._disabled)
1118
- classes.push('disabled');
1119
- if (this._clearable)
1120
- classes.push('clearable');
1121
- return classes.join(' ');
1122
- }
1123
- /**
1124
- * Render desktop mode with dialog
1125
- */
1126
- renderDesktop() {
1127
- const shadow = this.shadowRoot;
1128
- // Only set innerHTML and setup listeners if container doesn't exist (like ClojureScript)
1129
- if (!shadow.querySelector('.dropdown-container')) {
1130
- const stubClasses = this.buildStubClasses();
1131
- const labelHtml = this._label ? `
1132
- <label class="dropdown-label">
1133
- ${this._label}
1134
- ${this._required ? `<span class="required-icon">${REQUIRED_ICON_SVG}</span>` : ''}
1135
- </label>
1136
- ` : '';
1137
- shadow.innerHTML = `
1138
- <div class="dropdown-container dropdown-mode-desktop">
1139
- ${labelHtml}
1140
- <div class="dropdown-wrapper">
1141
- <div class="dropdown-stub ${stubClasses}"
1142
- ${this._disabled ? 'disabled' : ''}>
1143
- <slot name="start"></slot>
1144
- <slot name="selected"></slot>
1145
- <span class="dropdown-placeholder">${this._placeholder}</span>
1146
- <button class="dropdown-clear-btn" type="button" aria-label="Clear selection">
1147
- ${CLEAR_ICON_SVG}
1148
- </button>
1149
- <div class="dropdown-chevron">
1150
- ${CHEVRON_DOWN_SVG}
1151
- </div>
1152
- </div>
1153
- <dialog class="dropdown-dialog">
1154
- <div class="dropdown-header">
1155
- <input
1156
- class="dropdown-search-input ${this._size}"
1157
- type="text"
1158
- placeholder="${this._placeholder}"
1159
- ${this._disabled ? 'disabled' : ''}
1160
- />
1161
- <div class="dropdown-search-chevron">
1162
- ${CHEVRON_DOWN_SVG}
1163
- </div>
1164
- </div>
1165
- <div class="dropdown-options-wrapper">
1166
- <div class="dropdown-options">
1167
- <slot id="options-slot"></slot>
1168
- </div>
1169
- <div class="dropdown-loading" aria-hidden="true">
1170
- <span class="dropdown-loading-spinner"></span>
1171
- <span class="dropdown-loading-text">Searching…</span>
1172
- </div>
1173
- </div>
1174
- </dialog>
1175
- </div>
1176
- </div>
1177
- `;
1178
- // Setup event listeners ONCE (only when rendering for the first time)
1179
- this.setupDesktopEventListeners();
1180
- }
1181
- // Dynamic label creation (like input.ts fix)
1182
- const existingLabel = shadow.querySelector('.dropdown-label');
1183
- const container = shadow.querySelector('.dropdown-container');
1184
- const wrapper = shadow.querySelector('.dropdown-wrapper');
1185
- if (this._label) {
1186
- if (existingLabel) {
1187
- // Label exists, update it
1188
- existingLabel.innerHTML = this._label + (this._required ? '<span class="required-icon">' + REQUIRED_ICON_SVG + '</span>' : '');
1189
- existingLabel.style.display = 'flex';
1190
- }
1191
- else if (container && wrapper) {
1192
- // Label doesn't exist but we need one - CREATE IT!
1193
- const labelEl = document.createElement('label');
1194
- labelEl.className = 'dropdown-label';
1195
- labelEl.innerHTML = this._label + (this._required ? '<span class="required-icon">' + REQUIRED_ICON_SVG + '</span>' : '');
1196
- container.insertBefore(labelEl, wrapper);
1197
- }
1198
- }
1199
- else if (existingLabel) {
1200
- // No label text, hide existing label
1201
- ;
1202
- existingLabel.style.display = 'none';
1203
- }
1204
- // Always update selection display
1205
- this.updateSelectionDisplay();
1206
- }
1207
- // ============================================================================
1208
- // MOBILE IMPLEMENTATION (Full-screen Modal)
1209
- // ============================================================================
1210
- // Mobile uses full-screen modal with backdrop.
1211
- //
1212
- // EVENT FLOW:
1213
- // 1. User clicks stub → handleMobileStubClick() → openMobileModal()
1214
- // 2. Modal shown with CSS animation (display: flex + open class)
1215
- // 3. User types in search → handleSearchInput() [SHARED]
1216
- // 4. User clicks option → handleMobileOptionClick() → selectOption() [SHARED] → closeMobileModal()
1217
- // 5. User clicks backdrop → closeMobileModal()
1218
- // 6. User clicks close button → closeMobileModal()
1219
- // 7. User presses Escape → handleMobileKeyboard() → closeMobileModal()
1220
- // 8. ty-option dispatches clear-selection → handleMobileClearClick() → closeMobileModal()
1221
- //
1222
- // METHODS:
1223
- // - openMobileModal(): Open mobile modal
1224
- // - closeMobileModal(): Close mobile modal
1225
- // - handleMobileStubClick(): Open on stub click
1226
- // - handleMobileOptionClick(): Select option and close
1227
- // - handleMobileClearClick(): Clear selection and close
1228
- // - handleMobileKeyboard(): Escape key handler only (no arrow navigation)
1229
- // - renderMobile(): Render mobile HTML
1230
- // - setupMobileEventListeners(): Setup mobile event handlers
1231
- // ============================================================================
1232
- /**
1233
- * Render mobile mode with full-screen modal
1234
- */
1235
- renderMobile() {
1236
- const shadow = this.shadowRoot;
1237
- // Only set innerHTML and setup listeners if container doesn't exist
1238
- if (!shadow.querySelector('.dropdown-container')) {
1239
- const stubClasses = this.buildStubClasses();
1240
- const labelHtml = this._label ? `
1241
- <label class="dropdown-label">
1242
- ${this._label}
1243
- ${this._required ? `<span class="required-icon">${REQUIRED_ICON_SVG}</span>` : ''}
1244
- </label>
1245
- ` : '';
1246
- // Close button SVG (X icon)
1247
- const closeButtonSvg = `<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
1248
- <line x1="18" y1="6" x2="6" y2="18"></line>
1249
- <line x1="6" y1="6" x2="18" y2="18"></line>
1250
- </svg>`;
1251
- // Search placeholder: "Search <label>..." or just "Search..."
1252
- const searchPlaceholder = this._label ? `Search ${this._label}...` : 'Search...';
1253
- // Search header — always present; externalSearch only changes who handles input
1254
- const searchHeaderHtml = `
1255
- <div class="mobile-search-header">
1256
- ${this._label ? `<span class="mobile-header-label">${this._label}</span>` : ''}
1257
- <div class="mobile-header-content">
1258
- <input
1259
- class="mobile-search-input ${this._size}"
1260
- type="text"
1261
- placeholder="${searchPlaceholder}"
1262
- ${this._disabled ? 'disabled' : ''}
1263
- />
1264
- <button class="mobile-close-button" type="button" aria-label="Close">
1265
- ${closeButtonSvg}
1266
- </button>
1267
- </div>
1268
- </div>
1269
- `;
1270
- shadow.innerHTML = `
1271
- <div class="dropdown-container dropdown-mode-mobile">
1272
- ${labelHtml}
1273
- <div class="dropdown-wrapper">
1274
- <div class="dropdown-stub ${stubClasses}"
1275
- ${this._disabled ? 'disabled' : ''}>
1276
- <slot name="start"></slot>
1277
- <slot name="selected"></slot>
1278
- <span class="dropdown-placeholder">${this._placeholder}</span>
1279
- <div class="dropdown-chevron">
1280
- ${CHEVRON_DOWN_SVG}
1281
- </div>
1282
- </div>
1283
- <dialog class="mobile-dialog">
1284
- <div class="mobile-dialog-content">
1285
- ${searchHeaderHtml}
1286
- <div class="mobile-options-container dropdown-options-wrapper">
1287
- <slot id="options-slot"></slot>
1288
- <div class="dropdown-loading" aria-hidden="true">
1289
- <span class="dropdown-loading-spinner"></span>
1290
- <span class="dropdown-loading-text">Searching…</span>
1291
- </div>
1292
- </div>
1293
- </div>
1294
- </dialog>
1295
- </div>
1296
- </div>
1297
- `;
1298
- // Setup event listeners ONCE (only when rendering for the first time)
1299
- this.setupMobileEventListeners();
1300
- }
1301
- // Always update selection display
1302
- this.updateSelectionDisplay();
1303
- }
1304
- /**
1305
- * Setup event listeners for mobile mode
1306
- */
1307
- setupMobileEventListeners() {
1308
- const shadow = this.shadowRoot;
1309
- const stub = shadow.querySelector('.dropdown-stub');
1310
- const optionsSlot = shadow.querySelector('#options-slot');
1311
- const searchInput = shadow.querySelector('.mobile-search-input');
1312
- const closeButton = shadow.querySelector('.mobile-close-button');
1313
- const dialog = shadow.querySelector('.mobile-dialog');
1314
- if (stub) {
1315
- stub.addEventListener('click', (e) => this.handleMobileStubClick(e));
1316
- }
1317
- // Add option click handler to slot
1318
- if (optionsSlot) {
1319
- optionsSlot.addEventListener('click', (e) => this.handleMobileOptionClick(e));
1320
- }
1321
- // Add search input handlers
1322
- if (searchInput) {
1323
- searchInput.addEventListener('input', (e) => this.handleSearchInput(e));
1324
- }
1325
- // Close button click
1326
- if (closeButton) {
1327
- closeButton.addEventListener('click', () => this.closeMobileModal());
1328
- }
1329
- // Listen for clear-selection events from ty-option
1330
- if (optionsSlot) {
1331
- optionsSlot.addEventListener('clear-selection', (e) => {
1332
- e.preventDefault();
1333
- e.stopPropagation();
1334
- this.handleMobileClearClick(e);
1335
- });
1336
- }
1337
- // Native dialog: backdrop click and Escape key
1338
- if (dialog) {
1339
- dialog.addEventListener('click', (e) => {
1340
- if (e.target === dialog) {
1341
- this.closeMobileModal();
1342
- }
1343
- });
1344
- dialog.addEventListener('cancel', (e) => {
1345
- e.preventDefault();
1346
- this.closeMobileModal();
1347
- });
1348
- }
1349
- }
1350
- /**
1351
- * Handle mobile stub click - open modal
1352
- */
1353
- handleMobileStubClick(e) {
1354
- this.handleStubClickBase(e, () => this.openMobileModal());
1355
- }
1356
- /**
1357
- * Handle mobile option click - select and close
1358
- */
1359
- handleMobileOptionClick(e) {
1360
- this.handleOptionClickBase(e, () => this.closeMobileModal());
1361
- }
1362
- /**
1363
- * Handle clear click in mobile modal
1364
- */
1365
- handleMobileClearClick(e) {
1366
- this.handleClearClickBase(e, () => this.closeMobileModal());
1367
- // Mobile closes modal after clearing
1368
- }
1369
- /**
1370
- * Open mobile modal
1371
- */
1372
- openMobileModal() {
1373
- const shadow = this.shadowRoot;
1374
- const dialog = shadow.querySelector('.mobile-dialog');
1375
- if (!dialog)
1376
- return;
1377
- // Lock scroll
1378
- this.lockDropdownScroll();
1379
- // Show dialog with animation
1380
- dialog.showModal();
1381
- requestAnimationFrame(() => {
1382
- dialog.classList.add('open');
1383
- });
1384
- // Update component state
1385
- this._state.open = true;
1386
- // Initialize options state (no highlight on mobile)
1387
- this.initializeOptionsState(false);
1388
- // Focus search input
1389
- const searchInput = shadow.querySelector('.mobile-search-input');
1390
- if (searchInput) {
1391
- // Small delay to ensure dialog is visible and keyboard doesn't glitch
1392
- setTimeout(() => searchInput.focus(), 300);
1393
- }
1394
- // Hide clear button when modal is open (stub clear button - desktop only)
1395
- this.updateClearButton();
1396
- }
1397
- /**
1398
- * Close mobile modal
1399
- */
1400
- closeMobileModal() {
1401
- const shadow = this.shadowRoot;
1402
- const dialog = shadow.querySelector('.mobile-dialog');
1403
- if (!dialog)
1404
- return;
1405
- // Unlock scroll
1406
- this.unlockDropdownScroll();
1407
- // Close immediately — ::backdrop doesn't support transitions
1408
- dialog.classList.remove('open');
1409
- dialog.close();
1410
- // Update state
1411
- this._state.open = false;
1412
- this._state.highlightedIndex = -1;
1413
- // Reset search and restore all options (internal mode only — parent owns options in external mode)
1414
- this._state.search = '';
1415
- this._state.filteredOptions = [];
1416
- const searchInput = shadow.querySelector('.mobile-search-input');
1417
- if (searchInput) {
1418
- searchInput.value = '';
1419
- }
1420
- if (!this._externalSearch) {
1421
- const allOptions = this.getOptions().map(el => this.getOptionData(el));
1422
- allOptions.forEach(({ element }) => element.removeAttribute('hidden'));
1423
- }
1424
- // Show clear button when modal is closed (if applicable)
1425
- this.updateClearButton();
1426
- }
1427
- // ============================================================================
1428
- // PUBLIC API - Getters/Setters
1429
- // ============================================================================
1430
- // ============================================================================
1431
- // PROPERTY ACCESSORS - Simplified with TyComponent
1432
- // ============================================================================
1433
- get value() { return this.getProperty('value'); }
1434
- set value(v) { this.setProperty('value', v); }
1435
- get name() { return this.getProperty('name'); }
1436
- set name(v) { this.setProperty('name', v); }
1437
- get placeholder() { return this.getProperty('placeholder'); }
1438
- set placeholder(v) { this.setProperty('placeholder', v); }
1439
- get label() { return this.getProperty('label'); }
1440
- set label(v) { this.setProperty('label', v); }
1441
- get disabled() { return this.getProperty('disabled'); }
1442
- set disabled(v) { this.setProperty('disabled', v); }
1443
- get readonly() { return this.getProperty('readonly'); }
1444
- set readonly(v) { this.setProperty('readonly', v); }
1445
- get required() { return this.getProperty('required'); }
1446
- set required(v) { this.setProperty('required', v); }
1447
- get externalSearch() { return this.getProperty('externalSearch'); }
1448
- set externalSearch(v) { this.setProperty('externalSearch', v); }
1449
- get clearable() { return this.getProperty('clearable'); }
1450
- set clearable(v) { this.setProperty('clearable', v); }
1451
- get loading() { return this.getProperty('loading'); }
1452
- set loading(v) { this.setProperty('loading', v); }
1453
- get size() { return this.getProperty('size'); }
1454
- set size(v) { this.setProperty('size', v); }
1455
- get flavor() { return this.getProperty('flavor'); }
1456
- set flavor(v) { this.setProperty('flavor', v); }
1457
- get debounce() { return this.getProperty('debounce'); }
1458
- set debounce(v) { this.setProperty('debounce', v); }
1459
- get form() {
1460
- return this._internals.form;
1461
- }
1462
- }
1463
- // ============================================================================
1464
- // PROPERTY CONFIGURATION - Declarative property lifecycle
1465
- // ============================================================================
1466
- TyDropdown.properties = {
1467
- value: {
1468
- type: 'string',
1469
- visual: true,
1470
- formValue: true,
1471
- emitChange: true,
1472
- default: ''
1473
- },
1474
- name: {
1475
- type: 'string',
1476
- default: ''
1477
- },
1478
- placeholder: {
1479
- type: 'string',
1480
- visual: true,
1481
- default: 'Select an option...'
1482
- },
1483
- label: {
1484
- type: 'string',
1485
- visual: true,
1486
- default: ''
1487
- },
1488
- disabled: {
1489
- type: 'boolean',
1490
- visual: true,
1491
- default: false
1492
- },
1493
- readonly: {
1494
- type: 'boolean',
1495
- visual: true,
1496
- default: false
1497
- },
1498
- required: {
1499
- type: 'boolean',
1500
- visual: true,
1501
- default: false
1502
- },
1503
- externalSearch: {
1504
- type: 'boolean',
1505
- visual: true,
1506
- default: false,
1507
- aliases: { 'external-search': true }
1508
- },
1509
- clearable: {
1510
- type: 'boolean',
1511
- visual: true,
1512
- default: true,
1513
- aliases: { 'not-clearable': false }
1514
- },
1515
- size: {
1516
- type: 'string',
1517
- visual: true,
1518
- default: 'md',
1519
- validate: (v) => ['xs', 'sm', 'md', 'lg', 'xl'].includes(v),
1520
- coerce: (v) => {
1521
- if (!['xs', 'sm', 'md', 'lg', 'xl'].includes(v)) {
1522
- console.warn(`[ty-dropdown] Invalid size. Using md.`);
1523
- return 'md';
1524
- }
1525
- return v;
1526
- }
1527
- },
1528
- flavor: {
1529
- type: 'string',
1530
- visual: true,
1531
- default: 'neutral',
1532
- validate: (v) => ['primary', 'secondary', 'success', 'danger', 'warning', 'neutral'].includes(v),
1533
- coerce: (v) => {
1534
- const valid = ['primary', 'secondary', 'success', 'danger', 'warning', 'neutral'];
1535
- if (!valid.includes(v)) {
1536
- console.warn(`[ty-dropdown] Invalid flavor. Using neutral.`);
1537
- return 'neutral';
1538
- }
1539
- return v;
1540
- }
1541
- },
1542
- debounce: {
1543
- type: 'number',
1544
- default: 0,
1545
- validate: (v) => v >= 0 && v <= 5000,
1546
- coerce: (v) => {
1547
- const num = Number(v);
1548
- if (isNaN(num))
1549
- return 0;
1550
- return Math.max(0, Math.min(5000, num));
1551
- }
1552
- },
1553
- loading: {
1554
- type: 'boolean',
1555
- visual: true,
1556
- default: false
1557
- }
1558
- };
1559
- // Register the custom element
1560
- if (!customElements.get('ty-dropdown')) {
1561
- customElements.define('ty-dropdown', TyDropdown);
1562
- }
1563
- //# sourceMappingURL=dropdown.js.map