shadcn_phlexcomponents 0.1.5 → 0.1.11

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 (249) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +14 -0
  3. data/app/javascript/controllers/accordion_controller.ts +133 -0
  4. data/app/javascript/controllers/{avatar_controller.js → avatar_controller.ts} +4 -0
  5. data/app/javascript/controllers/checkbox_controller.ts +34 -0
  6. data/app/javascript/controllers/collapsible_controller.ts +45 -0
  7. data/app/javascript/controllers/combobox_controller.ts +145 -0
  8. data/app/javascript/controllers/command_controller.ts +129 -0
  9. data/app/javascript/controllers/command_root_controller.ts +355 -0
  10. data/app/javascript/controllers/date_picker_controller.ts +274 -0
  11. data/app/javascript/controllers/date_range_picker_controller.ts +243 -0
  12. data/app/javascript/controllers/dialog_controller.ts +113 -0
  13. data/app/javascript/controllers/dropdown_menu_controller.ts +133 -0
  14. data/app/javascript/controllers/dropdown_menu_root_controller.ts +234 -0
  15. data/app/javascript/controllers/dropdown_menu_sub_controller.ts +150 -0
  16. data/app/javascript/controllers/form_field_controller.ts +22 -0
  17. data/app/javascript/controllers/hover_card_controller.ts +93 -0
  18. data/app/javascript/controllers/{loading_button_controller.js → loading_button_controller.ts} +2 -2
  19. data/app/javascript/controllers/popover_controller.ts +141 -0
  20. data/app/javascript/controllers/progress_controller.ts +17 -0
  21. data/app/javascript/controllers/radio_group_controller.ts +106 -0
  22. data/app/javascript/controllers/select_controller.ts +200 -0
  23. data/app/javascript/controllers/{sidebar_controller.js → sidebar_controller.ts} +6 -2
  24. data/app/javascript/controllers/sidebar_trigger_controller.ts +21 -0
  25. data/app/javascript/controllers/slider_controller.ts +107 -0
  26. data/app/javascript/controllers/switch_controller.ts +30 -0
  27. data/app/javascript/controllers/tabs_controller.ts +79 -0
  28. data/app/javascript/controllers/{theme_switcher_controller.js → theme_switcher_controller.ts} +12 -9
  29. data/app/javascript/controllers/toast_container_controller.ts +62 -0
  30. data/app/javascript/controllers/toast_controller.ts +28 -0
  31. data/app/javascript/controllers/tooltip_controller.ts +98 -0
  32. data/app/javascript/shadcn_phlexcomponents.ts +57 -0
  33. data/app/javascript/utils.ts +437 -0
  34. data/app/stylesheets/date_picker.css +74 -0
  35. data/app/stylesheets/nouislider.css +173 -0
  36. data/app/stylesheets/tw-animate.css +486 -0
  37. data/lib/install/install_shadcn_phlexcomponents.rb +22 -9
  38. data/lib/shadcn_phlexcomponents/alias.rb +3 -1
  39. data/lib/shadcn_phlexcomponents/components/accordion.rb +129 -0
  40. data/lib/shadcn_phlexcomponents/components/alert.rb +59 -0
  41. data/lib/shadcn_phlexcomponents/components/alert_dialog.rb +276 -0
  42. data/lib/{components → shadcn_phlexcomponents/components}/aspect_ratio.rb +2 -2
  43. data/lib/shadcn_phlexcomponents/components/avatar.rb +63 -0
  44. data/lib/shadcn_phlexcomponents/components/badge.rb +35 -0
  45. data/lib/{components → shadcn_phlexcomponents/components}/base.rb +44 -7
  46. data/lib/shadcn_phlexcomponents/components/breadcrumb.rb +150 -0
  47. data/lib/shadcn_phlexcomponents/components/button.rb +49 -0
  48. data/lib/shadcn_phlexcomponents/components/card.rb +88 -0
  49. data/lib/{components → shadcn_phlexcomponents/components}/checkbox.rb +21 -17
  50. data/lib/{components → shadcn_phlexcomponents/components}/checkbox_group.rb +27 -16
  51. data/lib/shadcn_phlexcomponents/components/collapsible.rb +91 -0
  52. data/lib/shadcn_phlexcomponents/components/combobox.rb +398 -0
  53. data/lib/shadcn_phlexcomponents/components/command.rb +351 -0
  54. data/lib/shadcn_phlexcomponents/components/date_picker.rb +264 -0
  55. data/lib/shadcn_phlexcomponents/components/date_range_picker.rb +126 -0
  56. data/lib/shadcn_phlexcomponents/components/dialog.rb +234 -0
  57. data/lib/shadcn_phlexcomponents/components/dropdown_menu.rb +282 -0
  58. data/lib/shadcn_phlexcomponents/components/dropdown_menu_sub.rb +135 -0
  59. data/lib/shadcn_phlexcomponents/components/form/form_checkbox.rb +82 -0
  60. data/lib/shadcn_phlexcomponents/components/form/form_checkbox_group.rb +116 -0
  61. data/lib/shadcn_phlexcomponents/components/form/form_date_picker.rb +46 -0
  62. data/lib/shadcn_phlexcomponents/components/form/form_date_range_picker.rb +82 -0
  63. data/lib/{components → shadcn_phlexcomponents/components/form}/form_error.rb +7 -3
  64. data/lib/shadcn_phlexcomponents/components/form/form_helpers.rb +143 -0
  65. data/lib/shadcn_phlexcomponents/components/form/form_hint.rb +21 -0
  66. data/lib/{components → shadcn_phlexcomponents/components/form}/form_input.rb +3 -4
  67. data/lib/shadcn_phlexcomponents/components/form/form_radio_group.rb +106 -0
  68. data/lib/shadcn_phlexcomponents/components/form/form_select.rb +64 -0
  69. data/lib/shadcn_phlexcomponents/components/form/form_slider.rb +91 -0
  70. data/lib/shadcn_phlexcomponents/components/form/form_switch.rb +67 -0
  71. data/lib/shadcn_phlexcomponents/components/form/form_textarea.rb +59 -0
  72. data/lib/shadcn_phlexcomponents/components/form.rb +157 -0
  73. data/lib/shadcn_phlexcomponents/components/hover_card.rb +110 -0
  74. data/lib/shadcn_phlexcomponents/components/input.rb +31 -0
  75. data/lib/shadcn_phlexcomponents/components/label.rb +16 -0
  76. data/lib/{components → shadcn_phlexcomponents/components}/link.rb +10 -3
  77. data/lib/shadcn_phlexcomponents/components/loading_button.rb +28 -0
  78. data/lib/shadcn_phlexcomponents/components/pagination.rb +166 -0
  79. data/lib/shadcn_phlexcomponents/components/popover.rb +116 -0
  80. data/lib/{components → shadcn_phlexcomponents/components}/progress.rb +5 -5
  81. data/lib/shadcn_phlexcomponents/components/radio_group.rb +155 -0
  82. data/lib/shadcn_phlexcomponents/components/select.rb +421 -0
  83. data/lib/{components → shadcn_phlexcomponents/components}/separator.rb +9 -8
  84. data/lib/shadcn_phlexcomponents/components/sheet.rb +239 -0
  85. data/lib/{components → shadcn_phlexcomponents/components}/skeleton.rb +1 -1
  86. data/lib/shadcn_phlexcomponents/components/slider.rb +72 -0
  87. data/lib/shadcn_phlexcomponents/components/switch.rb +75 -0
  88. data/lib/shadcn_phlexcomponents/components/table.rb +140 -0
  89. data/lib/shadcn_phlexcomponents/components/tabs.rb +135 -0
  90. data/lib/shadcn_phlexcomponents/components/textarea.rb +24 -0
  91. data/lib/{components → shadcn_phlexcomponents/components}/theme_switcher.rb +2 -2
  92. data/lib/shadcn_phlexcomponents/components/toast.rb +153 -0
  93. data/lib/{components → shadcn_phlexcomponents/components}/toast_container.rb +24 -5
  94. data/lib/shadcn_phlexcomponents/components/tooltip.rb +131 -0
  95. data/lib/shadcn_phlexcomponents/initializers/shadcn_phlexcomponents.rb +25 -0
  96. data/lib/shadcn_phlexcomponents/version.rb +1 -1
  97. data/lib/tasks/install.rake +1 -1
  98. metadata +92 -168
  99. data/app/assets/tailwind/choices.css +0 -324
  100. data/app/assets/tailwind/tailwindcss-animate.css +0 -318
  101. data/app/assets/tailwind/vanilla-calendar-pro.css +0 -466
  102. data/app/javascript/controllers/accordion_controller.js +0 -133
  103. data/app/javascript/controllers/alert_dialog_controller.js +0 -157
  104. data/app/javascript/controllers/checkbox_controller.js +0 -28
  105. data/app/javascript/controllers/collapsible_controller.js +0 -35
  106. data/app/javascript/controllers/combobox_controller.js +0 -34
  107. data/app/javascript/controllers/date_picker_controller.js +0 -118
  108. data/app/javascript/controllers/date_range_picker_controller.js +0 -231
  109. data/app/javascript/controllers/dialog_controller.js +0 -159
  110. data/app/javascript/controllers/dropdown_menu_controller.js +0 -193
  111. data/app/javascript/controllers/hover_card_controller.js +0 -42
  112. data/app/javascript/controllers/popover_controller.js +0 -124
  113. data/app/javascript/controllers/progress_controller.js +0 -14
  114. data/app/javascript/controllers/radio_group_controller.js +0 -90
  115. data/app/javascript/controllers/select_controller.js +0 -294
  116. data/app/javascript/controllers/sheet_controller.js +0 -159
  117. data/app/javascript/controllers/sidebar_trigger_controller.js +0 -15
  118. data/app/javascript/controllers/switch_controller.js +0 -24
  119. data/app/javascript/controllers/tabs_controller.js +0 -73
  120. data/app/javascript/controllers/toast_container_controller.js +0 -22
  121. data/app/javascript/controllers/toast_controller.js +0 -45
  122. data/app/javascript/controllers/tooltip_controller.js +0 -41
  123. data/lib/components/accordion.rb +0 -38
  124. data/lib/components/accordion_content.rb +0 -30
  125. data/lib/components/accordion_item.rb +0 -26
  126. data/lib/components/accordion_trigger.rb +0 -45
  127. data/lib/components/alert.rb +0 -40
  128. data/lib/components/alert_description.rb +0 -11
  129. data/lib/components/alert_dialog.rb +0 -60
  130. data/lib/components/alert_dialog_action.rb +0 -22
  131. data/lib/components/alert_dialog_action_to.rb +0 -40
  132. data/lib/components/alert_dialog_cancel.rb +0 -22
  133. data/lib/components/alert_dialog_content.rb +0 -40
  134. data/lib/components/alert_dialog_description.rb +0 -22
  135. data/lib/components/alert_dialog_footer.rb +0 -11
  136. data/lib/components/alert_dialog_header.rb +0 -11
  137. data/lib/components/alert_dialog_title.rb +0 -22
  138. data/lib/components/alert_dialog_trigger.rb +0 -50
  139. data/lib/components/alert_title.rb +0 -11
  140. data/lib/components/avatar.rb +0 -31
  141. data/lib/components/avatar_fallback.rb +0 -21
  142. data/lib/components/avatar_image.rb +0 -19
  143. data/lib/components/badge.rb +0 -30
  144. data/lib/components/breadcrumb.rb +0 -51
  145. data/lib/components/breadcrumb_ellipsis.rb +0 -23
  146. data/lib/components/breadcrumb_item.rb +0 -11
  147. data/lib/components/breadcrumb_link.rb +0 -7
  148. data/lib/components/breadcrumb_page.rb +0 -21
  149. data/lib/components/breadcrumb_separator.rb +0 -26
  150. data/lib/components/button.rb +0 -53
  151. data/lib/components/card.rb +0 -31
  152. data/lib/components/card_content.rb +0 -11
  153. data/lib/components/card_description.rb +0 -11
  154. data/lib/components/card_footer.rb +0 -11
  155. data/lib/components/card_header.rb +0 -11
  156. data/lib/components/card_title.rb +0 -11
  157. data/lib/components/collapsible.rb +0 -31
  158. data/lib/components/collapsible_content.rb +0 -24
  159. data/lib/components/collapsible_trigger.rb +0 -50
  160. data/lib/components/combobox.rb +0 -57
  161. data/lib/components/combobox_item.rb +0 -9
  162. data/lib/components/date_picker.rb +0 -94
  163. data/lib/components/date_range_picker.rb +0 -113
  164. data/lib/components/dialog.rb +0 -52
  165. data/lib/components/dialog_close.rb +0 -42
  166. data/lib/components/dialog_content.rb +0 -54
  167. data/lib/components/dialog_description.rb +0 -22
  168. data/lib/components/dialog_footer.rb +0 -11
  169. data/lib/components/dialog_header.rb +0 -11
  170. data/lib/components/dialog_title.rb +0 -22
  171. data/lib/components/dialog_trigger.rb +0 -50
  172. data/lib/components/dropdown_menu.rb +0 -50
  173. data/lib/components/dropdown_menu_content.rb +0 -52
  174. data/lib/components/dropdown_menu_item.rb +0 -56
  175. data/lib/components/dropdown_menu_item_to.rb +0 -28
  176. data/lib/components/dropdown_menu_label.rb +0 -11
  177. data/lib/components/dropdown_menu_separator.rb +0 -20
  178. data/lib/components/dropdown_menu_trigger.rb +0 -57
  179. data/lib/components/form.rb +0 -59
  180. data/lib/components/form_hint.rb +0 -17
  181. data/lib/components/hover_card.rb +0 -33
  182. data/lib/components/hover_card_content.rb +0 -32
  183. data/lib/components/hover_card_trigger.rb +0 -44
  184. data/lib/components/input.rb +0 -32
  185. data/lib/components/label.rb +0 -14
  186. data/lib/components/loading_button.rb +0 -21
  187. data/lib/components/pagination.rb +0 -38
  188. data/lib/components/pagination_ellipsis.rb +0 -24
  189. data/lib/components/pagination_link.rb +0 -34
  190. data/lib/components/pagination_next.rb +0 -32
  191. data/lib/components/pagination_previous.rb +0 -32
  192. data/lib/components/popover.rb +0 -34
  193. data/lib/components/popover_content.rb +0 -40
  194. data/lib/components/popover_trigger.rb +0 -51
  195. data/lib/components/radio_group.rb +0 -62
  196. data/lib/components/radio_group_item.rb +0 -66
  197. data/lib/components/select.rb +0 -184
  198. data/lib/components/select_content.rb +0 -64
  199. data/lib/components/select_group.rb +0 -23
  200. data/lib/components/select_item.rb +0 -59
  201. data/lib/components/select_label.rb +0 -24
  202. data/lib/components/select_trigger.rb +0 -56
  203. data/lib/components/sheet.rb +0 -53
  204. data/lib/components/sheet_close.rb +0 -42
  205. data/lib/components/sheet_content.rb +0 -65
  206. data/lib/components/sheet_description.rb +0 -22
  207. data/lib/components/sheet_footer.rb +0 -11
  208. data/lib/components/sheet_header.rb +0 -11
  209. data/lib/components/sheet_title.rb +0 -22
  210. data/lib/components/sheet_trigger.rb +0 -50
  211. data/lib/components/sidebar.rb +0 -108
  212. data/lib/components/sidebar_container.rb +0 -11
  213. data/lib/components/sidebar_content.rb +0 -11
  214. data/lib/components/sidebar_footer.rb +0 -11
  215. data/lib/components/sidebar_group.rb +0 -11
  216. data/lib/components/sidebar_group_content.rb +0 -11
  217. data/lib/components/sidebar_group_label.rb +0 -16
  218. data/lib/components/sidebar_header.rb +0 -11
  219. data/lib/components/sidebar_inset.rb +0 -15
  220. data/lib/components/sidebar_menu.rb +0 -11
  221. data/lib/components/sidebar_menu_button.rb +0 -61
  222. data/lib/components/sidebar_menu_item.rb +0 -9
  223. data/lib/components/sidebar_menu_sub.rb +0 -14
  224. data/lib/components/sidebar_menu_sub_button.rb +0 -48
  225. data/lib/components/sidebar_menu_sub_item.rb +0 -9
  226. data/lib/components/sidebar_trigger.rb +0 -40
  227. data/lib/components/switch.rb +0 -66
  228. data/lib/components/table.rb +0 -75
  229. data/lib/components/table_body.rb +0 -11
  230. data/lib/components/table_caption.rb +0 -11
  231. data/lib/components/table_cell.rb +0 -11
  232. data/lib/components/table_footer.rb +0 -11
  233. data/lib/components/table_head.rb +0 -14
  234. data/lib/components/table_header.rb +0 -11
  235. data/lib/components/table_row.rb +0 -11
  236. data/lib/components/tabs.rb +0 -38
  237. data/lib/components/tabs_content.rb +0 -35
  238. data/lib/components/tabs_list.rb +0 -23
  239. data/lib/components/tabs_trigger.rb +0 -45
  240. data/lib/components/textarea.rb +0 -28
  241. data/lib/components/toast.rb +0 -101
  242. data/lib/components/toast_action.rb +0 -39
  243. data/lib/components/toast_action_to.rb +0 -28
  244. data/lib/components/toast_content.rb +0 -11
  245. data/lib/components/toast_description.rb +0 -11
  246. data/lib/components/toast_title.rb +0 -11
  247. data/lib/components/tooltip.rb +0 -34
  248. data/lib/components/tooltip_content.rb +0 -39
  249. data/lib/components/tooltip_trigger.rb +0 -48
@@ -0,0 +1,355 @@
1
+ import { Controller } from '@hotwired/stimulus'
2
+ import { useClickOutside, useDebounce } from 'stimulus-use'
3
+ import Fuse from 'fuse.js'
4
+ import { ON_OPEN_FOCUS_DELAY } from '../utils'
5
+
6
+ export default class extends Controller<HTMLElement> {
7
+ static targets = [
8
+ 'trigger',
9
+ 'content',
10
+ 'item',
11
+ 'triggerText',
12
+ 'group',
13
+ 'label',
14
+ 'searchInput',
15
+ 'results',
16
+ 'empty',
17
+ 'list',
18
+ ]
19
+
20
+ static values = {
21
+ isOpen: Boolean,
22
+ filteredItemIndexes: Array,
23
+ }
24
+
25
+ static debounces = ['search']
26
+
27
+ declare readonly emptyTarget: HTMLElement
28
+ declare readonly triggerTarget: HTMLElement
29
+ declare readonly listTarget: HTMLElement
30
+ declare readonly groupTargets: HTMLElement[]
31
+ declare readonly hasEmptyTarget: boolean
32
+ declare readonly contentTarget: HTMLElement
33
+ declare readonly searchInputTarget: HTMLInputElement
34
+ declare readonly itemTargets: HTMLInputElement[]
35
+ declare items: HTMLElement[]
36
+ declare filteredItemIndexesValue: number[]
37
+ declare itemsInnerText: string[]
38
+ declare filteredItems: HTMLElement[]
39
+ declare fuse: Fuse<string>
40
+ declare isOpenValue: boolean
41
+ declare resultsTarget: HTMLElement
42
+ declare DOMKeydownListener: (event: KeyboardEvent) => void
43
+ declare scrollingViaKeyboard: boolean
44
+ declare keyboardScrollTimeout: number
45
+
46
+ connect() {
47
+ this.DOMKeydownListener = this.onDOMKeydown.bind(this)
48
+ useClickOutside(this, { element: this.contentTarget })
49
+ useDebounce(this)
50
+ this.items = this.itemTargets.filter(
51
+ (i) => i.dataset.disabled === undefined,
52
+ )
53
+ this.itemsInnerText = this.items.map((i) => i.innerText.trim())
54
+ this.setAriaLabelledby()
55
+ this.setItemsGroupIds()
56
+ this.fuse = new Fuse(this.items.map((i) => i.innerText.trim()))
57
+ this.filteredItemIndexesValue = Array.from(
58
+ { length: this.items.length },
59
+ (_, i) => i,
60
+ )
61
+
62
+ this.filteredItems = this.items
63
+ }
64
+
65
+ setItemsGroupIds() {
66
+ this.items.forEach((item, index) => {
67
+ const parent = item.parentElement
68
+
69
+ if (parent?.dataset[`${this.identifier}Target`] === 'group') {
70
+ item.dataset.groupId = parent.getAttribute('aria-labelledby') as string
71
+ }
72
+ })
73
+ }
74
+
75
+ setAriaLabelledby() {
76
+ this.groupTargets.forEach((g) => {
77
+ const label = g.querySelector(
78
+ `[data-${this.identifier}-target="label"]`,
79
+ ) as HTMLElement
80
+
81
+ if (label) {
82
+ label.id = g.getAttribute('aria-labelledby') as string
83
+ }
84
+ })
85
+ }
86
+
87
+ scrollToItem(index: number) {
88
+ const item = this.filteredItems[index]
89
+ const containerRect = this.contentTarget.getBoundingClientRect()
90
+ const itemRect = item.getBoundingClientRect()
91
+ const listRect = this.listTarget.getBoundingClientRect()
92
+ let newScrollTop = null as number | null
93
+
94
+ const maxScrollTop =
95
+ this.listTarget.scrollHeight - this.listTarget.clientHeight
96
+
97
+ // scroll to bottom
98
+ if (itemRect.bottom - containerRect.bottom > 0) {
99
+ if (index === this.filteredItems.length - 1) {
100
+ newScrollTop = maxScrollTop
101
+ } else {
102
+ newScrollTop =
103
+ this.listTarget.scrollTop + (itemRect.bottom - containerRect.bottom)
104
+ }
105
+ } else if (listRect.top - itemRect.top > 0) {
106
+ // scroll to top
107
+ if (index === 0) {
108
+ newScrollTop = 0
109
+ } else {
110
+ newScrollTop = this.listTarget.scrollTop - (listRect.top - itemRect.top)
111
+ }
112
+ }
113
+
114
+ if (newScrollTop !== null) {
115
+ this.scrollingViaKeyboard = true
116
+
117
+ if (newScrollTop >= 0 && newScrollTop <= maxScrollTop) {
118
+ this.listTarget.scrollTop = newScrollTop
119
+ }
120
+
121
+ // Clear the flag after scroll settles
122
+ clearTimeout(this.keyboardScrollTimeout)
123
+ this.keyboardScrollTimeout = window.setTimeout(() => {
124
+ this.scrollingViaKeyboard = false
125
+ }, 200)
126
+ }
127
+ }
128
+
129
+ highlightItem(
130
+ event: MouseEvent | KeyboardEvent | null = null,
131
+ index: number | null = null,
132
+ ) {
133
+ if (event !== null) {
134
+ if (event instanceof KeyboardEvent) {
135
+ const key = event.key
136
+ const item = this.filteredItems.find(
137
+ (i) => i.dataset.highlighted === 'true',
138
+ )
139
+
140
+ if (item) {
141
+ const index = this.filteredItems.indexOf(item)
142
+
143
+ let newIndex = 0
144
+ if (key === 'ArrowUp') {
145
+ newIndex = index - 1
146
+
147
+ if (newIndex < 0) {
148
+ newIndex = 0
149
+ }
150
+ } else {
151
+ newIndex = index + 1
152
+
153
+ if (newIndex > this.filteredItems.length - 1) {
154
+ newIndex = this.filteredItems.length - 1
155
+ }
156
+ }
157
+
158
+ this.highlightItemByIndex(newIndex)
159
+ this.scrollToItem(newIndex)
160
+ } else {
161
+ if (key === 'ArrowUp') {
162
+ this.highlightItemByIndex(this.filteredItems.length - 1)
163
+ } else {
164
+ this.highlightItemByIndex(0)
165
+ }
166
+ }
167
+ } else {
168
+ // mouse event
169
+ if (this.scrollingViaKeyboard) {
170
+ event.stopImmediatePropagation()
171
+ return
172
+ } else {
173
+ const item = event.currentTarget as HTMLElement
174
+ const index = this.filteredItems.indexOf(item)
175
+ this.highlightItemByIndex(index)
176
+ }
177
+ }
178
+ } else if (index !== null) {
179
+ this.highlightItemByIndex(index)
180
+ }
181
+ }
182
+
183
+ highlightItemByIndex(index: number) {
184
+ this.filteredItems.forEach((item, i) => {
185
+ if (i === index) {
186
+ item.dataset.highlighted = 'true'
187
+ } else {
188
+ item.dataset.highlighted = 'false'
189
+ }
190
+ })
191
+ }
192
+
193
+ open() {
194
+ this.isOpenValue = true
195
+ this.highlightItemByIndex(0)
196
+
197
+ setTimeout(() => {
198
+ this.searchInputTarget.focus()
199
+ }, ON_OPEN_FOCUS_DELAY)
200
+ }
201
+
202
+ close() {
203
+ this.isOpenValue = false
204
+ this.searchInputTarget.value = ''
205
+ this.filteredItemIndexesValue = Array.from(
206
+ { length: this.items.length },
207
+ (_, i) => i,
208
+ )
209
+ }
210
+
211
+ select(event: MouseEvent | KeyboardEvent) {
212
+ if (!this.isOpenValue) return
213
+
214
+ if (event instanceof KeyboardEvent) {
215
+ const item = this.filteredItems.find(
216
+ (i) => i.dataset.highlighted === 'true',
217
+ )
218
+
219
+ if (item) {
220
+ this.onSelect(item.dataset.value as string)
221
+ this.close()
222
+ }
223
+ } else {
224
+ // mouse event
225
+ const item = event.currentTarget as HTMLElement
226
+ this.onSelect(item.dataset.value as string)
227
+ this.close()
228
+ }
229
+ }
230
+
231
+ onSelect(value: string) {}
232
+
233
+ onDOMKeydown(event: KeyboardEvent) {
234
+ if (!this.isOpenValue) return
235
+
236
+ const key = event.key
237
+
238
+ if (['Tab', 'Enter', ' '].includes(key)) event.preventDefault()
239
+
240
+ if (key === 'Escape') {
241
+ this.close()
242
+ }
243
+ }
244
+
245
+ setupEventListeners() {
246
+ document.addEventListener('keydown', this.DOMKeydownListener)
247
+ }
248
+
249
+ cleanupEventListeners() {
250
+ document.removeEventListener('keydown', this.DOMKeydownListener)
251
+ }
252
+
253
+ disconnect() {
254
+ this.cleanupEventListeners()
255
+ }
256
+
257
+ search(event: InputEvent) {
258
+ const input = event.target as HTMLInputElement
259
+ const value = input.value
260
+
261
+ if (value.length > 0) {
262
+ const results = this.fuse.search(value)
263
+
264
+ this.filteredItemIndexesValue = results.map((result) => result.refIndex)
265
+ } else {
266
+ this.filteredItemIndexesValue = Array.from(
267
+ { length: this.items.length },
268
+ (_, i) => i,
269
+ )
270
+ }
271
+ }
272
+
273
+ filteredItemIndexesValueChanged(filteredItemIndexes: number[]) {
274
+ if (this.items) {
275
+ const filteredItems = filteredItemIndexes.map((i) => this.items[i])
276
+
277
+ // 1. Toggle visibility of items
278
+ this.items.forEach((item) => {
279
+ if (filteredItems.includes(item)) {
280
+ item.ariaHidden = 'false'
281
+ item.classList.remove('hidden')
282
+ } else {
283
+ item.ariaHidden = 'true'
284
+ item.classList.add('hidden')
285
+ }
286
+ })
287
+
288
+ // 2. Get groups based on order of filtered items
289
+ const groupIds = filteredItems.map((item) => item.dataset.groupId)
290
+ const uniqueGroupIds = [...new Set(groupIds)].filter(
291
+ (groupId) => !!groupId,
292
+ )
293
+ const orderedGroups = uniqueGroupIds.map((groupId) => {
294
+ return this.resultsTarget.querySelector(
295
+ `[aria-labelledby=${groupId}]`,
296
+ ) as HTMLElement
297
+ })
298
+
299
+ // 3. Append items and groups based on filtered items
300
+ const appendedGroupIds = [] as string[]
301
+
302
+ filteredItems.forEach((item, index) => {
303
+ const groupId = item.dataset.groupId
304
+
305
+ if (groupId) {
306
+ const group = orderedGroups.find(
307
+ (g) => g.getAttribute('aria-labelledby') === groupId,
308
+ )
309
+
310
+ if (group) {
311
+ group.appendChild(item)
312
+
313
+ if (!appendedGroupIds.includes(groupId)) {
314
+ this.resultsTarget.appendChild(group)
315
+ appendedGroupIds.push(groupId)
316
+ }
317
+ }
318
+ } else {
319
+ this.resultsTarget.appendChild(item)
320
+ }
321
+ })
322
+
323
+ // 4. Toggle visibility of groups
324
+ this.groupTargets.forEach((group) => {
325
+ const itemsCount = group.querySelectorAll(
326
+ `[data-${this.identifier}-target=item][aria-hidden=false]`,
327
+ ).length
328
+ if (itemsCount > 0) {
329
+ group.classList.remove('hidden')
330
+ } else {
331
+ group.classList.add('hidden')
332
+ }
333
+ })
334
+
335
+ // 5. Assign filteredItems based on the order it is displayed in the DOM
336
+ this.filteredItems = Array.from(
337
+ this.resultsTarget.querySelectorAll(
338
+ `[data-${this.identifier}-target=item][aria-hidden=false]`,
339
+ ),
340
+ )
341
+
342
+ // 6. Highlight first item
343
+ this.highlightItemByIndex(0)
344
+
345
+ // 7. Toggle visibility of empty
346
+ if (this.hasEmptyTarget) {
347
+ if (this.filteredItems.length > 0) {
348
+ this.emptyTarget.classList.add('hidden')
349
+ } else {
350
+ this.emptyTarget.classList.remove('hidden')
351
+ }
352
+ }
353
+ }
354
+ }
355
+ }
@@ -0,0 +1,274 @@
1
+ import {
2
+ initFloatingUi,
3
+ showOverlay,
4
+ hideOverlay,
5
+ lockScroll,
6
+ unlockScroll,
7
+ ON_OPEN_FOCUS_DELAY,
8
+ getFocusableElements,
9
+ } from '../utils'
10
+ import { Calendar, Options } from 'vanilla-calendar-pro'
11
+ import Inputmask from 'inputmask'
12
+ import PopoverController from './popover_controller'
13
+ import dayjs from 'dayjs'
14
+ import customParseFormat from 'dayjs/plugin/customParseFormat'
15
+ import utc from 'dayjs/plugin/utc'
16
+ dayjs.extend(customParseFormat)
17
+ dayjs.extend(utc)
18
+
19
+ const DAYJS_FORMAT = 'YYYY-MM-DD'
20
+
21
+ export default class extends PopoverController {
22
+ static targets = [
23
+ 'trigger',
24
+ 'triggerText',
25
+ 'contentContainer',
26
+ 'content',
27
+ 'input',
28
+ 'hiddenInput',
29
+ 'inputContainer',
30
+ 'calendar',
31
+ ]
32
+
33
+ static values = { isOpen: Boolean, date: String }
34
+
35
+ declare readonly triggerTextTarget: HTMLElement
36
+ declare readonly inputTarget: HTMLInputElement
37
+ declare readonly hiddenInputTarget: HTMLInputElement
38
+ declare readonly inputContainerTarget: HTMLElement
39
+ declare readonly calendarTarget: HTMLElement
40
+ declare onClickDateListener: (event: any, self: any) => void
41
+ declare format: string
42
+ declare mask: boolean
43
+ declare dateValue: string
44
+ declare calendar: Calendar
45
+ declare readonly hasInputTarget: boolean
46
+ declare readonly hasTriggerTextTarget: boolean
47
+
48
+ connect() {
49
+ super.connect()
50
+ this.onClickDateListener = this.onClickDate.bind(this)
51
+ this.format = this.element.dataset.format || 'DD/MM/YYYY'
52
+ this.mask = this.element.dataset.mask === 'true'
53
+
54
+ const options = this.getOptions()
55
+
56
+ this.calendar = new Calendar(this.calendarTarget, options)
57
+ this.calendar.init()
58
+
59
+ if (this.hasInputTarget && this.mask) {
60
+ this.setupInputMask()
61
+ }
62
+
63
+ this.calendarTarget.removeAttribute('tabindex')
64
+ }
65
+
66
+ inputBlur() {
67
+ let dateDisplay = ''
68
+ const date = this.calendar.context.selectedDates[0]
69
+
70
+ if (date) {
71
+ dateDisplay = dayjs(date).format(this.format)
72
+ }
73
+
74
+ this.inputTarget.value = dateDisplay
75
+ this.inputContainerTarget.dataset.focus = 'false'
76
+ }
77
+
78
+ inputDate(event: KeyboardEvent) {
79
+ const value = (event.target as HTMLInputElement).value
80
+
81
+ if (value.length === 0) {
82
+ this.calendar.set({
83
+ selectedDates: [],
84
+ })
85
+ this.dateValue = ''
86
+ }
87
+
88
+ if (value.length > 0 && dayjs(value, this.format, true).isValid()) {
89
+ const dayjsDate = dayjs(value, this.format).format(DAYJS_FORMAT)
90
+ this.calendar.set({
91
+ selectedDates: [dayjsDate],
92
+ })
93
+ this.dateValue = dayjsDate
94
+ }
95
+ }
96
+
97
+ setContainerFocus() {
98
+ this.inputContainerTarget.dataset.focus = 'true'
99
+ }
100
+
101
+ onOpenFocusedElement() {
102
+ const focusableElements = getFocusableElements(this.contentTarget)
103
+
104
+ const selectedElement = Array.from(focusableElements).find(
105
+ (e) => e.ariaSelected,
106
+ ) as HTMLElement
107
+
108
+ const currentElement = this.contentTarget.querySelector(
109
+ '[aria-current]',
110
+ ) as HTMLElement
111
+
112
+ if (selectedElement) {
113
+ return selectedElement
114
+ } else if (currentElement) {
115
+ const firstElementChild = currentElement.firstElementChild as HTMLElement
116
+ return firstElementChild
117
+ } else {
118
+ return focusableElements[0]
119
+ }
120
+ }
121
+
122
+ referenceElement() {
123
+ return this.hasInputTarget ? this.inputTarget : this.triggerTarget
124
+ }
125
+
126
+ onOpen() {
127
+ if (this.isMobile()) {
128
+ lockScroll()
129
+ showOverlay({ elementId: this.contentTarget.id })
130
+ }
131
+
132
+ setTimeout(() => {
133
+ // Prevent width from changing when changing to month/year view
134
+ if (!this.contentTarget.dataset.width) {
135
+ const contentWidth = this.contentTarget.offsetWidth
136
+ this.contentTarget.dataset.width = `${contentWidth}`
137
+
138
+ this.contentTarget.style.maxWidth = `${contentWidth}px`
139
+ this.contentTarget.style.minWidth = `${contentWidth}px`
140
+ }
141
+
142
+ if (this.isMobile()) {
143
+ // Prevent position from changing when toggling between month/year on mobile
144
+ if (!this.contentTarget.dataset.top) {
145
+ const rect = this.contentTarget.getBoundingClientRect()
146
+ this.contentTarget.dataset.top = `${rect.top}`
147
+ this.contentTarget.style.top = `${rect.top}px`
148
+ this.contentTarget.classList.remove('-translate-y-1/2', 'top-1/2')
149
+ }
150
+ }
151
+ }, ON_OPEN_FOCUS_DELAY)
152
+ }
153
+
154
+ onClose() {
155
+ if (this.isMobile()) {
156
+ hideOverlay(this.contentTarget.id)
157
+ unlockScroll()
158
+ }
159
+ }
160
+
161
+ // Popover is shown as a dialog on small screens with position: fixed
162
+ isMobile() {
163
+ const styles = window.getComputedStyle(this.contentTarget)
164
+ return styles.position === 'fixed'
165
+ }
166
+
167
+ getOptions() {
168
+ let options = {
169
+ type: 'default',
170
+ enableJumpToSelectedDate: true,
171
+ onClickDate: this.onClickDateListener,
172
+ } as Options
173
+
174
+ const date = this.element.dataset.value
175
+
176
+ if (date && dayjs(date).isValid()) {
177
+ const dayjsDate = dayjs(date).format(DAYJS_FORMAT)
178
+ options.selectedDates = [dayjsDate]
179
+ }
180
+
181
+ try {
182
+ options = {
183
+ ...options,
184
+ ...JSON.parse(this.element.dataset.options || ''),
185
+ }
186
+ } catch {
187
+ options = options
188
+ }
189
+
190
+ if (options.selectedDates && options.selectedDates.length > 0) {
191
+ this.dateValue = `${options.selectedDates[0]}`
192
+ }
193
+
194
+ return options
195
+ }
196
+
197
+ onDOMKeydown(event: KeyboardEvent) {
198
+ if (!this.isOpenValue) return
199
+
200
+ const key = event.key
201
+
202
+ const focusableElements = getFocusableElements(this.contentTarget)
203
+
204
+ const firstElement = focusableElements[0]
205
+ const lastElement = focusableElements[focusableElements.length - 1]
206
+
207
+ if (key === 'Escape') {
208
+ this.close()
209
+ } else if (key === 'Tab') {
210
+ // If Shift + Tab pressed on first element, go to last element
211
+ if (event.shiftKey && document.activeElement === firstElement) {
212
+ event.preventDefault()
213
+ lastElement.focus()
214
+ }
215
+ // If Tab pressed on last element, go to first element
216
+ else if (!event.shiftKey && document.activeElement === lastElement) {
217
+ event.preventDefault()
218
+ firstElement.focus()
219
+ }
220
+ } else if (
221
+ ['ArrowUp', 'ArrowDown', 'ArrowRight', 'ArrowLeft'].includes(key) &&
222
+ document.activeElement != this.inputTarget
223
+ ) {
224
+ event.preventDefault()
225
+ }
226
+ }
227
+
228
+ onClickDate(self: Calendar) {
229
+ const date = self.context.selectedDates[0]
230
+
231
+ if (date) {
232
+ this.dateValue = date
233
+ this.close()
234
+ } else {
235
+ this.dateValue = ''
236
+ }
237
+ }
238
+
239
+ setupInputMask() {
240
+ const im = new Inputmask(this.format.replace(/[^\/]/g, '9'), {
241
+ showMaskOnHover: false,
242
+ })
243
+ im.mask(this.inputTarget)
244
+ }
245
+
246
+ dateValueChanged(value: string) {
247
+ if (value && value.length > 0) {
248
+ const dayjsDate = dayjs(value)
249
+ const formattedDate = dayjsDate.format(this.format)
250
+
251
+ if (this.hasInputTarget) this.inputTarget.value = formattedDate
252
+ if (this.hasTriggerTextTarget) {
253
+ this.triggerTarget.dataset.hasValue = 'true'
254
+ this.triggerTextTarget.textContent = formattedDate
255
+ }
256
+
257
+ this.hiddenInputTarget.value = dayjsDate.utc().format()
258
+ } else {
259
+ if (this.hasInputTarget) this.inputTarget.value = ''
260
+
261
+ if (this.hasTriggerTextTarget) {
262
+ this.triggerTarget.dataset.hasValue = 'false'
263
+ if (this.triggerTarget.dataset.placeholder) {
264
+ this.triggerTextTarget.textContent =
265
+ this.triggerTarget.dataset.placeholder
266
+ } else {
267
+ this.triggerTextTarget.textContent = ''
268
+ }
269
+ }
270
+
271
+ this.hiddenInputTarget.value = ''
272
+ }
273
+ }
274
+ }