@boxcustodia/library 2.0.0-alpha.11 → 2.0.0-alpha.12

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 (349) hide show
  1. package/dist/index.css +1 -1
  2. package/package.json +4 -3
  3. package/src/__doc__/Changelog.mdx +6 -0
  4. package/src/__doc__/Components.mdx +73 -0
  5. package/src/__doc__/Examples.tsx +69 -0
  6. package/src/__doc__/Icons.mdx +41 -0
  7. package/src/__doc__/Intro.mdx +138 -0
  8. package/src/__doc__/MCP.mdx +71 -0
  9. package/src/__doc__/Migration.mdx +475 -0
  10. package/src/__doc__/Theme.mdx +132 -0
  11. package/src/__doc__/Types.mdx +252 -0
  12. package/src/components/alert/alert.stories.tsx +142 -0
  13. package/src/components/alert/alert.tsx +109 -0
  14. package/src/components/alert/index.ts +7 -0
  15. package/src/components/alert-dialog/alert-dialog.stories.tsx +173 -0
  16. package/src/components/alert-dialog/alert-dialog.test.tsx +49 -0
  17. package/src/components/alert-dialog/alert-dialog.tsx +265 -0
  18. package/src/components/alert-dialog/index.ts +1 -0
  19. package/src/components/auto-complete/auto-complete-primitives.tsx +155 -0
  20. package/src/components/auto-complete/auto-complete.stories.tsx +241 -0
  21. package/src/components/auto-complete/auto-complete.tsx +82 -0
  22. package/src/components/auto-complete/index.ts +2 -0
  23. package/src/components/avatar/avatar.stories.tsx +84 -0
  24. package/src/components/avatar/avatar.test.tsx +61 -0
  25. package/src/components/avatar/avatar.tsx +104 -0
  26. package/src/components/avatar/index.ts +1 -0
  27. package/src/components/background-image/background-image.stories.tsx +21 -0
  28. package/src/components/background-image/background-image.test.tsx +29 -0
  29. package/src/components/background-image/background-image.tsx +23 -0
  30. package/src/components/background-image/index.ts +1 -0
  31. package/src/components/button/button.stories.tsx +396 -0
  32. package/src/components/button/button.test.tsx +58 -0
  33. package/src/components/button/button.tsx +31 -0
  34. package/src/components/button/button.variants.ts +44 -0
  35. package/src/components/button/components/base-button.tsx +86 -0
  36. package/src/components/button/components/loader-overlay.tsx +21 -0
  37. package/src/components/button/components/loading-icon.tsx +47 -0
  38. package/src/components/button/index.ts +3 -0
  39. package/src/components/calendar/calendar.model.ts +86 -0
  40. package/src/components/calendar/calendar.stories.tsx +155 -0
  41. package/src/components/calendar/calendar.test.tsx +12 -0
  42. package/src/components/calendar/calendar.tsx +185 -0
  43. package/src/components/calendar/components/calendar-navigation.tsx +141 -0
  44. package/src/components/calendar/components/day.tsx +61 -0
  45. package/src/components/calendar/components/decade-view.tsx +45 -0
  46. package/src/components/calendar/components/index.ts +6 -0
  47. package/src/components/calendar/components/month-view.tsx +58 -0
  48. package/src/components/calendar/components/week-days.tsx +27 -0
  49. package/src/components/calendar/components/year-view.tsx +29 -0
  50. package/src/components/calendar/hooks/index.ts +4 -0
  51. package/src/components/calendar/hooks/use-calendar-navigation.ts +79 -0
  52. package/src/components/calendar/hooks/use-calendar.ts +90 -0
  53. package/src/components/calendar/hooks/use-multiple-calendar.ts +34 -0
  54. package/src/components/calendar/hooks/use-range-calendar.ts +91 -0
  55. package/src/components/calendar/hooks/use-single-calendar.ts +18 -0
  56. package/src/components/calendar/index.ts +1 -0
  57. package/src/components/calendar/utils/typeguards.ts +7 -0
  58. package/src/components/card/card.stories.tsx +116 -0
  59. package/src/components/card/card.tsx +74 -0
  60. package/src/components/card/index.ts +1 -0
  61. package/src/components/center/center.stories.tsx +81 -0
  62. package/src/components/center/center.tsx +24 -0
  63. package/src/components/center/index.ts +1 -0
  64. package/src/components/checkbox/checkbox.stories.tsx +307 -0
  65. package/src/components/checkbox/checkbox.tsx +273 -0
  66. package/src/components/checkbox/index.ts +1 -0
  67. package/src/components/checkbox-group/checkbox-group.stories.tsx +104 -0
  68. package/src/components/checkbox-group/checkbox-group.tsx +16 -0
  69. package/src/components/checkbox-group/index.ts +1 -0
  70. package/src/components/combobox/combobox.stories.tsx +339 -0
  71. package/src/components/combobox/combobox.tsx +892 -0
  72. package/src/components/combobox/index.ts +1 -0
  73. package/src/components/date-picker/date-input.stories.tsx +158 -0
  74. package/src/components/date-picker/date-input.tsx +163 -0
  75. package/src/components/date-picker/date-picker.model.ts +90 -0
  76. package/src/components/date-picker/date-picker.stories.tsx +200 -0
  77. package/src/components/date-picker/date-picker.test.tsx +23 -0
  78. package/src/components/date-picker/date-picker.tsx +298 -0
  79. package/src/components/date-picker/date-picker.utils.ts +260 -0
  80. package/src/components/date-picker/index.ts +3 -0
  81. package/src/components/date-picker/use-date-input-popover.ts +48 -0
  82. package/src/components/date-picker/use-date-input.ts +125 -0
  83. package/src/components/dialog/dialog.stories.tsx +171 -0
  84. package/src/components/dialog/dialog.test.tsx +68 -0
  85. package/src/components/dialog/dialog.tsx +277 -0
  86. package/src/components/dialog/index.ts +1 -0
  87. package/src/components/divider/divider.stories.tsx +70 -0
  88. package/src/components/divider/divider.test.tsx +22 -0
  89. package/src/components/divider/divider.tsx +23 -0
  90. package/src/components/divider/index.ts +1 -0
  91. package/src/components/dropzone/dropzone.stories.tsx +210 -0
  92. package/src/components/dropzone/dropzone.tsx +154 -0
  93. package/src/components/dropzone/file-types.ts +64 -0
  94. package/src/components/dropzone/index.ts +3 -0
  95. package/src/components/dropzone/upload-primitives.tsx +310 -0
  96. package/src/components/dropzone/use-dropzone.ts +122 -0
  97. package/src/components/empty-state/empty-state.stories.tsx +56 -0
  98. package/src/components/empty-state/empty-state.tsx +39 -0
  99. package/src/components/empty-state/index.ts +1 -0
  100. package/src/components/field/field.stories.tsx +223 -0
  101. package/src/components/field/field.tsx +229 -0
  102. package/src/components/field/index.ts +1 -0
  103. package/src/components/form/form.stories.tsx +594 -0
  104. package/src/components/form/form.tsx +30 -0
  105. package/src/components/form/index.ts +1 -0
  106. package/src/components/heading/heading.stories.tsx +74 -0
  107. package/src/components/heading/heading.tsx +28 -0
  108. package/src/components/heading/heading.variants.ts +27 -0
  109. package/src/components/heading/index.ts +1 -0
  110. package/src/components/index.ts +46 -0
  111. package/src/components/input/index.ts +1 -0
  112. package/src/components/input/input.stories.tsx +104 -0
  113. package/src/components/input/input.tsx +75 -0
  114. package/src/components/kbd/index.ts +1 -0
  115. package/src/components/kbd/kbd.stories.tsx +40 -0
  116. package/src/components/kbd/kbd.tsx +31 -0
  117. package/src/components/kbd/kbd.variants.ts +26 -0
  118. package/src/components/label/index.ts +1 -0
  119. package/src/components/label/label.stories.tsx +68 -0
  120. package/src/components/label/label.test.tsx +61 -0
  121. package/src/components/label/label.tsx +62 -0
  122. package/src/components/loader/index.ts +1 -0
  123. package/src/components/loader/loader.stories.tsx +60 -0
  124. package/src/components/loader/loader.test.tsx +26 -0
  125. package/src/components/loader/loader.tsx +60 -0
  126. package/src/components/menu/index.ts +2 -0
  127. package/src/components/menu/menu-primitives.tsx +248 -0
  128. package/src/components/menu/menu.stories.tsx +203 -0
  129. package/src/components/menu/menu.tsx +100 -0
  130. package/src/components/menu/util/render-menu-item.tsx +54 -0
  131. package/src/components/multi-select/hooks/use-multi-select.ts +66 -0
  132. package/src/components/multi-select/index.ts +1 -0
  133. package/src/components/multi-select/multi-select.stories.tsx +294 -0
  134. package/src/components/multi-select/multi-select.tsx +300 -0
  135. package/src/components/multi-select/multi-select.variants.ts +22 -0
  136. package/src/components/number-input/index.ts +1 -0
  137. package/src/components/number-input/number-input.stories.tsx +209 -0
  138. package/src/components/number-input/number-input.test.tsx +87 -0
  139. package/src/components/number-input/number-input.tsx +230 -0
  140. package/src/components/pagination/components/pagination-option.tsx +27 -0
  141. package/src/components/pagination/index.ts +1 -0
  142. package/src/components/pagination/pagination.stories.tsx +80 -0
  143. package/src/components/pagination/pagination.test.tsx +76 -0
  144. package/src/components/pagination/pagination.tsx +102 -0
  145. package/src/components/password/index.ts +1 -0
  146. package/src/components/password/password.stories.tsx +104 -0
  147. package/src/components/password/password.tsx +71 -0
  148. package/src/components/popover/index.ts +1 -0
  149. package/src/components/popover/popover.stories.tsx +213 -0
  150. package/src/components/popover/popover.tsx +203 -0
  151. package/src/components/progress/index.ts +1 -0
  152. package/src/components/progress/progress.stories.tsx +124 -0
  153. package/src/components/progress/progress.test.tsx +25 -0
  154. package/src/components/progress/progress.tsx +124 -0
  155. package/src/components/scroll-area/index.ts +1 -0
  156. package/src/components/scroll-area/scroll-area.stories.tsx +166 -0
  157. package/src/components/scroll-area/scroll-area.tsx +64 -0
  158. package/src/components/select/index.ts +1 -0
  159. package/src/components/select/select.stories.tsx +253 -0
  160. package/src/components/select/select.tsx +430 -0
  161. package/src/components/show/index.ts +1 -0
  162. package/src/components/show/show.stories.tsx +197 -0
  163. package/src/components/show/show.test.tsx +41 -0
  164. package/src/components/show/show.tsx +16 -0
  165. package/src/components/skeleton/index.ts +1 -0
  166. package/src/components/skeleton/skeleton.stories.tsx +36 -0
  167. package/src/components/skeleton/skeleton.test.tsx +14 -0
  168. package/src/components/skeleton/skeleton.tsx +15 -0
  169. package/src/components/stack/index.ts +1 -0
  170. package/src/components/stack/stack.stories.tsx +194 -0
  171. package/src/components/stack/stack.tsx +52 -0
  172. package/src/components/stepper/Stepper.tsx +190 -0
  173. package/src/components/stepper/context/stepper-context.tsx +11 -0
  174. package/src/components/stepper/index.ts +1 -0
  175. package/src/components/stepper/stepper.stories.tsx +130 -0
  176. package/src/components/stepper/stepper.test.tsx +91 -0
  177. package/src/components/switch/index.ts +1 -0
  178. package/src/components/switch/switch.stories.tsx +122 -0
  179. package/src/components/switch/switch.test.tsx +30 -0
  180. package/src/components/switch/switch.tsx +86 -0
  181. package/src/components/table/index.ts +3 -0
  182. package/src/components/table/table-primitives.tsx +122 -0
  183. package/src/components/table/table.model.ts +20 -0
  184. package/src/components/table/table.stories.tsx +169 -0
  185. package/src/components/table/table.test.tsx +91 -0
  186. package/src/components/table/table.tsx +109 -0
  187. package/src/components/table-pagination/index.ts +2 -0
  188. package/src/components/table-pagination/table-pagination.model.ts +2 -0
  189. package/src/components/table-pagination/table-pagination.stories.tsx +23 -0
  190. package/src/components/table-pagination/table-pagination.test.tsx +32 -0
  191. package/src/components/table-pagination/table-pagination.tsx +108 -0
  192. package/src/components/tabs/context/tabs-context.tsx +14 -0
  193. package/src/components/tabs/index.ts +1 -0
  194. package/src/components/tabs/tabs.stories.tsx +182 -0
  195. package/src/components/tabs/tabs.test.tsx +61 -0
  196. package/src/components/tabs/tabs.tsx +175 -0
  197. package/src/components/tag/index.ts +2 -0
  198. package/src/components/tag/tag.stories.tsx +170 -0
  199. package/src/components/tag/tag.test.tsx +18 -0
  200. package/src/components/tag/tag.tsx +99 -0
  201. package/src/components/tag/tag.variants.ts +31 -0
  202. package/src/components/textarea/index.ts +1 -0
  203. package/src/components/textarea/textarea.stories.tsx +73 -0
  204. package/src/components/textarea/textarea.tsx +105 -0
  205. package/src/components/timeline/index.ts +1 -0
  206. package/src/components/timeline/timeline-status.ts +5 -0
  207. package/src/components/timeline/timeline.stories.tsx +84 -0
  208. package/src/components/timeline/timeline.tsx +147 -0
  209. package/src/components/toast/index.ts +1 -0
  210. package/src/components/toast/toast.stories.tsx +392 -0
  211. package/src/components/toast/toast.test.tsx +50 -0
  212. package/src/components/toast/toast.tsx +411 -0
  213. package/src/components/tooltip/index.ts +1 -0
  214. package/src/components/tooltip/tooltip.stories.tsx +226 -0
  215. package/src/components/tooltip/tooltip.test.tsx +46 -0
  216. package/src/components/tooltip/tooltip.tsx +171 -0
  217. package/src/components/tree/hooks/use-controllable-tree-state.ts +80 -0
  218. package/src/components/tree/index.ts +2 -0
  219. package/src/components/tree/tree-primitives.tsx +126 -0
  220. package/src/components/tree/tree.stories.tsx +468 -0
  221. package/src/components/tree/tree.tsx +42 -0
  222. package/src/hooks/index.ts +26 -0
  223. package/src/hooks/useArray/__doc__/useArray.stories.tsx +100 -0
  224. package/src/hooks/useArray/__test__/useArray.test.tsx +88 -0
  225. package/src/hooks/useArray/index.ts +1 -0
  226. package/src/hooks/useArray/useArray.ts +76 -0
  227. package/src/hooks/useAsync/__doc__/useAsync.stories.tsx +149 -0
  228. package/src/hooks/useAsync/__test__/useAsync.test.tsx +68 -0
  229. package/src/hooks/useAsync/index.ts +1 -0
  230. package/src/hooks/useAsync/useAsync.ts +58 -0
  231. package/src/hooks/useClickOutside/__doc__/useClickOutside.stories.tsx +40 -0
  232. package/src/hooks/useClickOutside/__test__/useClickOutside.test.tsx +33 -0
  233. package/src/hooks/useClickOutside/index.ts +1 -0
  234. package/src/hooks/useClickOutside/useClickOutside.ts +26 -0
  235. package/src/hooks/useClipboard/__doc__/useClipboard.stories.tsx +45 -0
  236. package/src/hooks/useClipboard/__test__/useClipboard.test.tsx +19 -0
  237. package/src/hooks/useClipboard/index.ts +1 -0
  238. package/src/hooks/useClipboard/useClipboard.tsx +28 -0
  239. package/src/hooks/useDebounceCallback/__doc__/useDebouncedCallback.stories.tsx +84 -0
  240. package/src/hooks/useDebounceCallback/index.ts +1 -0
  241. package/src/hooks/useDebounceCallback/useDebouncedCallback.ts +23 -0
  242. package/src/hooks/useDebounceValue/__doc__/useDebouncedValue.stories.tsx +75 -0
  243. package/src/hooks/useDebounceValue/index.ts +1 -0
  244. package/src/hooks/useDebounceValue/useDebouncedValue.ts +17 -0
  245. package/src/hooks/useDisclosure/__doc__/useDisclosure.stories.tsx +39 -0
  246. package/src/hooks/useDisclosure/__test__/useDisclosure.test.ts +43 -0
  247. package/src/hooks/useDisclosure/index.ts +1 -0
  248. package/src/hooks/useDisclosure/useDisclosure.ts +37 -0
  249. package/src/hooks/useDocumentTitle/__doc__/useDocumentTitle.stories.tsx +26 -0
  250. package/src/hooks/useDocumentTitle/index.ts +1 -0
  251. package/src/hooks/useDocumentTitle/useDocumentTitle.tsx +11 -0
  252. package/src/hooks/useEventListener/__doc__/useEventListener.stories.tsx +28 -0
  253. package/src/hooks/useEventListener/__test__/useEventListener.test.tsx +26 -0
  254. package/src/hooks/useEventListener/index.ts +1 -0
  255. package/src/hooks/useEventListener/useEventListener.ts +25 -0
  256. package/src/hooks/useFocusTrap/__doc__/useFocusTrap.stories.tsx +37 -0
  257. package/src/hooks/useFocusTrap/index.ts +1 -0
  258. package/src/hooks/useFocusTrap/scopeTab.ts +38 -0
  259. package/src/hooks/useFocusTrap/tabbable.ts +70 -0
  260. package/src/hooks/useFocusTrap/useFocusTrap.ts +78 -0
  261. package/src/hooks/useHotkey/__docs__/useHotkey.stories.tsx +116 -0
  262. package/src/hooks/useHotkey/__test__/useHotkey.test.tsx +105 -0
  263. package/src/hooks/useHotkey/__utils__/create-hotkey-listener.ts +25 -0
  264. package/src/hooks/useHotkey/__utils__/index.ts +3 -0
  265. package/src/hooks/useHotkey/__utils__/is-input-field.ts +14 -0
  266. package/src/hooks/useHotkey/__utils__/match-key-modifiers.ts +25 -0
  267. package/src/hooks/useHotkey/index.ts +1 -0
  268. package/src/hooks/useHotkey/useHotkey.ts +34 -0
  269. package/src/hooks/useHover/__doc__/useHover.stories.tsx +41 -0
  270. package/src/hooks/useHover/__test__/useHover.test.tsx +45 -0
  271. package/src/hooks/useHover/index.ts +1 -0
  272. package/src/hooks/useHover/useHover.tsx +40 -0
  273. package/src/hooks/useIsVisible/__doc__/useIsVisible.stories.tsx +60 -0
  274. package/src/hooks/useIsVisible/index.ts +1 -0
  275. package/src/hooks/useIsVisible/useIsVisible.tsx +50 -0
  276. package/src/hooks/useLocalStorage/__doc__/useLocalStorage.stories.tsx +86 -0
  277. package/src/hooks/useLocalStorage/__test__/useLocalStorage.test.ts +85 -0
  278. package/src/hooks/useLocalStorage/index.ts +1 -0
  279. package/src/hooks/useLocalStorage/useLocalStorage.ts +57 -0
  280. package/src/hooks/useMediaQuery/__doc__/useMediaQuery.stories.tsx +39 -0
  281. package/src/hooks/useMediaQuery/index.ts +1 -0
  282. package/src/hooks/useMediaQuery/useMediaQuery.ts +22 -0
  283. package/src/hooks/useMemoizedFn/index.ts +1 -0
  284. package/src/hooks/useMemoizedFn/useMemoizedFn.ts +32 -0
  285. package/src/hooks/useMutation/__doc__/useMutation.stories.tsx +111 -0
  286. package/src/hooks/useMutation/__test__/useMutation.test.tsx +83 -0
  287. package/src/hooks/useMutation/index.ts +1 -0
  288. package/src/hooks/useMutation/useMutation.tsx +60 -0
  289. package/src/hooks/useObject/__doc__/useObject.stories.tsx +119 -0
  290. package/src/hooks/useObject/__test__/useObject.test.tsx +87 -0
  291. package/src/hooks/useObject/index.ts +1 -0
  292. package/src/hooks/useObject/useObject.tsx +48 -0
  293. package/src/hooks/usePagination/__doc__/usePagination.stories.tsx +72 -0
  294. package/src/hooks/usePagination/__test__/usePagination.test.tsx +98 -0
  295. package/src/hooks/usePagination/index.ts +2 -0
  296. package/src/hooks/usePagination/usePagination.tsx +74 -0
  297. package/src/hooks/usePortal/__doc__/usePortal.stories.tsx +19 -0
  298. package/src/hooks/usePortal/__test__/usePortal.test.tsx +20 -0
  299. package/src/hooks/usePortal/index.ts +1 -0
  300. package/src/hooks/usePortal/usePortal.ts +40 -0
  301. package/src/hooks/usePreventCloseWindow/__doc__/usePreventCloseWindow.stories.tsx +32 -0
  302. package/src/hooks/usePreventCloseWindow/index.ts +1 -0
  303. package/src/hooks/usePreventCloseWindow/usePreventCloseWindow.ts +33 -0
  304. package/src/hooks/useRangePagination/__test__/useRangePagination.test.tsx +63 -0
  305. package/src/hooks/useRangePagination/index.ts +2 -0
  306. package/src/hooks/useRangePagination/useRangePagination.tsx +72 -0
  307. package/src/hooks/useSelection/__doc__/useSelection.stories.tsx +140 -0
  308. package/src/hooks/useSelection/__test__/useSelection.test.tsx +57 -0
  309. package/src/hooks/useSelection/index.ts +1 -0
  310. package/src/hooks/useSelection/useSelection.ts +121 -0
  311. package/src/hooks/useStep/__doc__/useStep.stories.tsx +98 -0
  312. package/src/hooks/useStep/__test__/useStep.test.ts +51 -0
  313. package/src/hooks/useStep/index.ts +1 -0
  314. package/src/hooks/useStep/useStep.ts +57 -0
  315. package/src/hooks/useToggle/__doc__/useToggle.stories.tsx +25 -0
  316. package/src/hooks/useToggle/__test__/useToggle.test.tsx +43 -0
  317. package/src/hooks/useToggle/index.ts +1 -0
  318. package/src/hooks/useToggle/useToggle.ts +16 -0
  319. package/src/index.ts +6 -0
  320. package/src/lib/cn.ts +8 -0
  321. package/src/lib/index.ts +1 -0
  322. package/src/models/Generic.model.ts +67 -0
  323. package/src/models/index.ts +1 -0
  324. package/src/providers/index.ts +2 -0
  325. package/src/providers/library-provider.tsx +44 -0
  326. package/src/providers/theme/ThemeProvider.tsx +25 -0
  327. package/src/providers/theme/index.ts +3 -0
  328. package/src/providers/theme/types.ts +11 -0
  329. package/src/providers/theme/useThemeProps.ts +25 -0
  330. package/src/stores/theme.store.ts +31 -0
  331. package/src/styles/components.css +4 -0
  332. package/src/styles/index.css +2 -0
  333. package/src/styles/library.css +2 -0
  334. package/src/styles/theme.css +232 -0
  335. package/src/utils/dates/parseDateRange.utility.ts +39 -0
  336. package/src/utils/form.tsx +91 -0
  337. package/src/utils/functions/createSafeContext.ts +17 -0
  338. package/src/utils/functions/ensureReactElement.tsx +30 -0
  339. package/src/utils/functions/getFormData.ts +19 -0
  340. package/src/utils/functions/index.ts +4 -0
  341. package/src/utils/functions/mergeRefs.ts +18 -0
  342. package/src/utils/index.ts +3 -0
  343. package/src/utils/strings/extractInitials.utility.ts +10 -0
  344. package/src/utils/strings/index.ts +1 -0
  345. package/src/utils/tests/click.ts +3 -0
  346. package/src/utils/tests/index.ts +2 -0
  347. package/src/utils/tests/keyboard.ts +21 -0
  348. package/src/utils/tests/type.ts +6 -0
  349. package/dist/components.css +0 -2
@@ -0,0 +1,78 @@
1
+ import { useCallback, useEffect, useRef } from "react";
2
+ import { scopeTab } from "./scopeTab";
3
+ import { FOCUS_SELECTOR, focusable, tabbable } from "./tabbable";
4
+
5
+ function useFocusTrap(active = true): (instance: HTMLElement | null) => void {
6
+ const ref = useRef<HTMLElement | null>(null);
7
+
8
+ const focusNode = (node: HTMLElement) => {
9
+ let focusElement: HTMLElement | null =
10
+ node.querySelector("[data-autofocus]");
11
+
12
+ if (!focusElement) {
13
+ const children = Array.from<HTMLElement>(
14
+ node.querySelectorAll(FOCUS_SELECTOR),
15
+ );
16
+ focusElement =
17
+ children.find(tabbable) || children.find(focusable) || null;
18
+ if (!focusElement && focusable(node)) {
19
+ focusElement = node;
20
+ }
21
+ }
22
+
23
+ if (focusElement) {
24
+ focusElement.focus({ preventScroll: true });
25
+ }
26
+ };
27
+
28
+ const setRef = useCallback(
29
+ (node: HTMLElement | null) => {
30
+ if (!active) {
31
+ return;
32
+ }
33
+
34
+ if (node === null) {
35
+ return;
36
+ }
37
+
38
+ if (ref.current === node) {
39
+ return;
40
+ }
41
+
42
+ if (node) {
43
+ // Delay processing the HTML node by a frame. This ensures focus is assigned correctly.
44
+ setTimeout(() => {
45
+ if (node.getRootNode()) {
46
+ focusNode(node);
47
+ }
48
+ });
49
+
50
+ ref.current = node;
51
+ } else {
52
+ ref.current = null;
53
+ }
54
+ },
55
+ [active],
56
+ );
57
+
58
+ useEffect(() => {
59
+ if (!active) {
60
+ return undefined;
61
+ }
62
+
63
+ ref.current && setTimeout(() => focusNode(ref.current!));
64
+
65
+ const handleKeyDown = (event: KeyboardEvent) => {
66
+ if (event.key === "Tab" && ref.current) {
67
+ scopeTab(ref.current, event);
68
+ }
69
+ };
70
+
71
+ document.addEventListener("keydown", handleKeyDown);
72
+ return () => document.removeEventListener("keydown", handleKeyDown);
73
+ }, [active]);
74
+
75
+ return setRef;
76
+ }
77
+
78
+ export default useFocusTrap;
@@ -0,0 +1,116 @@
1
+ import { Meta, StoryObj } from "@storybook/react-vite";
2
+ import { HTMLProps, ReactNode, useState } from "react";
3
+ import { cn } from "../../../lib";
4
+ import useHotkey from "../useHotkey";
5
+
6
+ const meta: Meta = {
7
+ title: "hooks/useHotkey",
8
+ };
9
+
10
+ export default meta;
11
+ type Story = StoryObj<typeof meta>;
12
+
13
+ const generateRandomColor = () => {
14
+ const letters = "0123456789ABCDEF";
15
+ let color = "#";
16
+ for (let i = 0; i < 6; i++) {
17
+ color += letters[Math.floor(Math.random() * 16)];
18
+ }
19
+ return color;
20
+ };
21
+
22
+ const Kbd = (props: HTMLProps<HTMLDivElement>) => (
23
+ <kbd
24
+ {...props}
25
+ className="inline-block px-3 py-1 text-sm font-semibold text-gray-800 bg-gray-200 border rounded-md shadow-inner border-input"
26
+ />
27
+ );
28
+
29
+ const Wrapper = ({ children }: { children: ReactNode }) => (
30
+ <div>
31
+ <div className="space-y-2">
32
+ <div className="flex gap-2">
33
+ <span>Usa</span>
34
+ <Kbd>C</Kbd>
35
+ <span>+</span>
36
+ <Kbd>R</Kbd>
37
+ <span>para cambiar el color</span>
38
+ </div>
39
+ <div className="flex gap-2">
40
+ <span>Usa</span>
41
+ <Kbd>w</Kbd>
42
+ <Kbd>a</Kbd>
43
+ <Kbd>s</Kbd>
44
+ <Kbd>d</Kbd>
45
+ <span>o</span>
46
+ <Kbd>&#8593;</Kbd>
47
+ <Kbd>&#8592;</Kbd>
48
+ <Kbd>&#8595;</Kbd>
49
+ <Kbd>&#8594;</Kbd>
50
+ <span>Para moverte</span>
51
+ </div>
52
+ </div>
53
+ <div className="flex items-center justify-center w-80 h-52">{children}</div>
54
+ </div>
55
+ );
56
+
57
+ /**
58
+ * Opciones
59
+ *
60
+ * ```tsx
61
+ * type HotkeyOptions = {
62
+ * // Es el evento que se va a utilizar; Keydown, Keyup, Keypress
63
+ * eventName?: HotkeyEventName;
64
+ * @default "keydown"
65
+ * // si se debe prevenir los comandos por defecto del navegador
66
+ * preventDefault?: boolean;
67
+ * @default true
68
+ * // si los comandos se deberían ejecutar mientras el usuario está escribiendo en un input o textarea
69
+ * ignoreInputFields?: boolean;
70
+ * @default false
71
+ * };
72
+ * ```
73
+ **/
74
+ export const Default: Story = {
75
+ render: () => {
76
+ const [posX, setPosX] = useState(0);
77
+ const [posY, setPosY] = useState(0);
78
+ const [color, setColor] = useState("#333333");
79
+ const [spacePressed, setSpacePressed] = useState(false);
80
+
81
+ useHotkey(["ctrl+r"], () => setColor(generateRandomColor()));
82
+
83
+ useHotkey(" ", () => {
84
+ setSpacePressed(true);
85
+ setTimeout(() => {
86
+ setSpacePressed(false);
87
+ }, 300);
88
+ });
89
+
90
+ useHotkey(
91
+ ["w", "s", "a", "d", "ArrowUp", "ArrowDown", "ArrowLeft", "ArrowRight"],
92
+ (e) => {
93
+ const { key } = e;
94
+ if (["w", "ArrowUp"].includes(key)) return setPosY((y) => y - 20);
95
+ if (["s", "ArrowDown"].includes(key)) return setPosY((y) => y + 20);
96
+ if (["a", "ArrowLeft"].includes(key)) return setPosX((x) => x - 20);
97
+ if (["d", "ArrowRight"].includes(key)) return setPosX((x) => x + 20);
98
+ },
99
+ );
100
+
101
+ return (
102
+ <Wrapper>
103
+ <div
104
+ className={cn(
105
+ "w-10 h-10 rounded-full",
106
+ spacePressed && "ring ring-offset-2",
107
+ )}
108
+ style={{
109
+ backgroundColor: color,
110
+ transform: `translate(${posX}px, ${posY}px)`,
111
+ }}
112
+ />
113
+ </Wrapper>
114
+ );
115
+ },
116
+ };
@@ -0,0 +1,105 @@
1
+ import { render, waitFor } from "@testing-library/react";
2
+ import { describe, expect, it, vi } from "vitest";
3
+ import { keyboard } from "../../../utils/tests/keyboard";
4
+ import useHotkey, { HotkeyOptions } from "../useHotkey";
5
+
6
+ describe("useHotkey hook", () => {
7
+ it("should be defined", () => {
8
+ expect(useHotkey).toBeDefined();
9
+ });
10
+
11
+ it("should call handler on keydown event", async () => {
12
+ const handler = vi.fn();
13
+ const TestComponent = () => {
14
+ useHotkey("a", handler);
15
+ return null;
16
+ };
17
+
18
+ render(<TestComponent />);
19
+
20
+ keyboard(document, {
21
+ key: "a",
22
+ });
23
+
24
+ await waitFor(() => {
25
+ expect(handler).toHaveBeenCalled();
26
+ });
27
+ });
28
+
29
+ it("should call handler with correct key combination", async () => {
30
+ const handler = vi.fn();
31
+ const TestComponent = () => {
32
+ useHotkey("ctrl+a", handler);
33
+ return null;
34
+ };
35
+
36
+ render(<TestComponent />);
37
+
38
+ keyboard(document, {
39
+ key: "a",
40
+ ctrlKey: true,
41
+ });
42
+
43
+ await waitFor(() => {
44
+ expect(handler).toHaveBeenCalled();
45
+ });
46
+ });
47
+
48
+ it("should prevent default action if preventDefault is true", async () => {
49
+ const handler = vi.fn();
50
+ const TestComponent = () => {
51
+ useHotkey("b", handler);
52
+ return null;
53
+ };
54
+
55
+ render(<TestComponent />);
56
+
57
+ const event = new KeyboardEvent("keydown", { key: "b" });
58
+ Object.defineProperty(event, "preventDefault", { value: vi.fn() });
59
+ document.dispatchEvent(event);
60
+
61
+ await waitFor(() => {
62
+ expect(event.preventDefault).toHaveBeenCalled();
63
+ });
64
+ });
65
+
66
+ it("should not prevent default action if preventDefault is false", async () => {
67
+ const handler = vi.fn();
68
+ const options: HotkeyOptions = { preventDefault: false };
69
+ const TestComponent = () => {
70
+ useHotkey("c", handler, options);
71
+ return null;
72
+ };
73
+
74
+ render(<TestComponent />);
75
+ const event = new KeyboardEvent("keydown", { key: "c" });
76
+ Object.defineProperty(event, "preventDefault", { value: vi.fn() });
77
+ document.dispatchEvent(event);
78
+
79
+ await waitFor(() => {
80
+ expect(event.preventDefault).not.toHaveBeenCalled();
81
+ });
82
+ });
83
+
84
+ it("should handle multiple keys", async () => {
85
+ const handler = vi.fn();
86
+ const TestComponent = () => {
87
+ useHotkey(["d", "e"], handler);
88
+ return null;
89
+ };
90
+
91
+ render(<TestComponent />);
92
+
93
+ keyboard(document, {
94
+ key: "d",
95
+ });
96
+
97
+ keyboard(document, {
98
+ key: "e",
99
+ });
100
+
101
+ await waitFor(() => {
102
+ expect(handler).toHaveBeenCalledTimes(2);
103
+ });
104
+ });
105
+ });
@@ -0,0 +1,25 @@
1
+ import { shouldIgnoreEvent } from "./is-input-field";
2
+ import { matchKeyModifiers } from "./match-key-modifiers";
3
+
4
+ export const createListener = (
5
+ keys: string | string[],
6
+ handler: (event: KeyboardEvent) => void,
7
+ preventDefault: boolean,
8
+ ignoreInputFields: boolean,
9
+ watch: boolean,
10
+ ) => {
11
+ return (event: KeyboardEvent) => {
12
+ if (!watch) return;
13
+ if (shouldIgnoreEvent(event, ignoreInputFields)) {
14
+ return;
15
+ }
16
+
17
+ const keyArray = Array.isArray(keys) ? keys : [keys];
18
+ const match = keyArray.some((key) => matchKeyModifiers(event, key));
19
+
20
+ if (match) {
21
+ preventDefault && event.preventDefault();
22
+ handler(event);
23
+ }
24
+ };
25
+ };
@@ -0,0 +1,3 @@
1
+ export * from "./create-hotkey-listener";
2
+ export * from "./is-input-field";
3
+ export * from "./match-key-modifiers";
@@ -0,0 +1,14 @@
1
+ export const isInputField = (element: HTMLElement): boolean => {
2
+ return (
3
+ element.tagName === "INPUT" ||
4
+ element.tagName === "TEXTAREA" ||
5
+ element.isContentEditable
6
+ );
7
+ };
8
+
9
+ export const shouldIgnoreEvent = (
10
+ event: KeyboardEvent,
11
+ ignoreInputFields: boolean,
12
+ ): boolean => {
13
+ return ignoreInputFields && isInputField(event.target as HTMLElement);
14
+ };
@@ -0,0 +1,25 @@
1
+ export const getKeyModifiers = (key: string) => {
2
+ const parts = key.toLowerCase().split("+");
3
+ const mainKey = parts.pop()!;
4
+ return {
5
+ mainKey,
6
+ isShift: parts.includes("shift"),
7
+ isCtrl: parts.includes("ctrl"),
8
+ isAlt: parts.includes("alt"),
9
+ isMeta: parts.includes("meta"),
10
+ };
11
+ };
12
+
13
+ export const matchKeyModifiers = (
14
+ event: KeyboardEvent,
15
+ key: string,
16
+ ): boolean => {
17
+ const { mainKey, isShift, isCtrl, isAlt, isMeta } = getKeyModifiers(key);
18
+ return (
19
+ event.key.toLowerCase() === mainKey &&
20
+ event.shiftKey === isShift &&
21
+ event.ctrlKey === isCtrl &&
22
+ event.altKey === isAlt &&
23
+ event.metaKey === isMeta
24
+ );
25
+ };
@@ -0,0 +1 @@
1
+ export { default as useHotkey } from "./useHotkey";
@@ -0,0 +1,34 @@
1
+ import { useCallback } from "react";
2
+ import { useEventListener } from "../useEventListener";
3
+ import { createListener } from "./__utils__";
4
+
5
+ export type Keys = string | string[];
6
+ export type HotkeyEventName = "keydown" | "keypress" | "keyup";
7
+ export type HotkeyOptions = {
8
+ watch?: boolean;
9
+ eventName?: HotkeyEventName;
10
+ preventDefault?: boolean;
11
+ ignoreInputFields?: boolean;
12
+ };
13
+
14
+ function useHotkey(
15
+ keys: string | string[],
16
+ handler: (event: KeyboardEvent) => void,
17
+ options: HotkeyOptions = {},
18
+ ) {
19
+ const {
20
+ eventName = "keydown",
21
+ preventDefault = true,
22
+ ignoreInputFields = true,
23
+ watch = true,
24
+ } = options;
25
+
26
+ const listener = useCallback(
27
+ createListener(keys, handler, preventDefault, ignoreInputFields, watch),
28
+ [keys, handler, preventDefault, ignoreInputFields, watch],
29
+ );
30
+
31
+ return useEventListener(eventName, listener);
32
+ }
33
+
34
+ export default useHotkey;
@@ -0,0 +1,41 @@
1
+ import { Meta } from "@storybook/react-vite";
2
+ import { cn } from "../../../lib";
3
+ import useHover from "../useHover";
4
+
5
+ /**
6
+ * Hook que facilita el manejo de un estado booleano. Provee métodos para abrir, cerrar y toggle el estado
7
+ */
8
+ const meta: Meta = {
9
+ title: "hooks/useHover",
10
+ };
11
+
12
+ export default meta;
13
+
14
+ export const Default = {
15
+ render: () => {
16
+ const { ref, isHovering } = useHover<HTMLDivElement>();
17
+
18
+ return (
19
+ <div className="space-y-2">
20
+ <div
21
+ ref={ref}
22
+ className={cn(
23
+ "rounded transition-colors w-32 h-32 shadow text-3xl grid place-items-center cursor-pointer",
24
+ { "bg-accent": isHovering },
25
+ )}
26
+ >
27
+ 🍅
28
+ </div>
29
+
30
+ <pre className="rounded-md bg-slate-950 p-4 text-white">
31
+ <code className="block">
32
+ hover?:{" "}
33
+ <span className={isHovering ? "text-green-500" : "text-red-500"}>
34
+ {JSON.stringify(isHovering, null, 2)}
35
+ </span>
36
+ </code>
37
+ </pre>
38
+ </div>
39
+ );
40
+ },
41
+ };
@@ -0,0 +1,45 @@
1
+ import { fireEvent } from "@testing-library/dom";
2
+ import { render, screen, waitFor } from "@testing-library/react";
3
+ import { describe, expect, it, vi } from "vitest";
4
+ import useHover from "../useHover";
5
+
6
+ describe("useHover hook", () => {
7
+ it("should be defined", () => {
8
+ expect(useHover).toBeDefined();
9
+ });
10
+
11
+ it("should handle hover start and end events", async () => {
12
+ const handleHoverStart = vi.fn();
13
+ const handleHoverEnd = vi.fn();
14
+
15
+ const Component = () => {
16
+ const { ref, isHovering } = useHover<HTMLDivElement>({
17
+ onHoverStart: handleHoverStart,
18
+ onHoverEnd: handleHoverEnd,
19
+ });
20
+ return (
21
+ <div ref={ref} data-testid="hover-target">
22
+ {isHovering ? "Hovering" : "Not Hovering"}
23
+ </div>
24
+ );
25
+ };
26
+
27
+ render(<Component />);
28
+
29
+ const target = screen.getByTestId("hover-target");
30
+
31
+ fireEvent.mouseOver(target);
32
+
33
+ await waitFor(() => {
34
+ expect(handleHoverStart).toHaveBeenCalled();
35
+ expect(target.textContent).toBe("Hovering");
36
+ });
37
+
38
+ fireEvent.mouseOut(target);
39
+
40
+ await waitFor(() => {
41
+ expect(handleHoverEnd).toHaveBeenCalled();
42
+ expect(target.textContent).toBe("Not Hovering");
43
+ });
44
+ });
45
+ });
@@ -0,0 +1 @@
1
+ export { default as useHover } from "./useHover";
@@ -0,0 +1,40 @@
1
+ import { useEffect, useRef, useState } from "react";
2
+
3
+ type UseHoverOptions = {
4
+ onHoverStart?: (event: MouseEvent) => void;
5
+ onHoverEnd?: (event: MouseEvent) => void;
6
+ };
7
+
8
+ function useHover<T extends HTMLElement>(
9
+ options?: UseHoverOptions,
10
+ ): { ref: React.RefObject<T | null>; isHovering: boolean } {
11
+ const [isHovering, setIsHovering] = useState(false);
12
+ const ref = useRef<T | null>(null);
13
+
14
+ useEffect(() => {
15
+ const handleMouseOver = (event: MouseEvent) => {
16
+ setIsHovering(true);
17
+ options?.onHoverStart?.(event);
18
+ };
19
+
20
+ const handleMouseOut = (event: MouseEvent) => {
21
+ setIsHovering(false);
22
+ options?.onHoverEnd?.(event);
23
+ };
24
+
25
+ const node = ref.current;
26
+ if (node) {
27
+ node.addEventListener("mouseover", handleMouseOver);
28
+ node.addEventListener("mouseout", handleMouseOut);
29
+
30
+ return () => {
31
+ node.removeEventListener("mouseover", handleMouseOver);
32
+ node.removeEventListener("mouseout", handleMouseOut);
33
+ };
34
+ }
35
+ }, [options]);
36
+
37
+ return { ref, isHovering };
38
+ }
39
+
40
+ export default useHover;
@@ -0,0 +1,60 @@
1
+ import { Meta, StoryObj } from "@storybook/react-vite";
2
+ import { ArrowDown } from "lucide-react";
3
+ import { ReactNode } from "react";
4
+ import { createToastManager, ToastProvider } from "../../../components/toast";
5
+ import useIsVisible from "../useIsVisible";
6
+
7
+ const toastManager = createToastManager();
8
+
9
+ /**
10
+ * Hook que permite detectar si un elemento es visible para el usuario
11
+ *
12
+ * Extiende la API de `IntersectionObserver`
13
+ */
14
+ const meta: Meta = {
15
+ title: "hooks/useIsVisible",
16
+ decorators: [
17
+ (Story) => (
18
+ <ToastProvider toastManager={toastManager}>
19
+ <Story />
20
+ </ToastProvider>
21
+ ),
22
+ ],
23
+ };
24
+
25
+ export default meta;
26
+
27
+ const Wrapper = ({ children }: { children: ReactNode }) => {
28
+ return (
29
+ <div className="h-[200px] border rounded overflow-y-scroll">{children}</div>
30
+ );
31
+ };
32
+ export const Default: StoryObj = {
33
+ render: () => {
34
+ const { ref, isVisible } = useIsVisible<HTMLDivElement>({
35
+ onVisible: () =>
36
+ toastManager.add({
37
+ variant: "success",
38
+ description: "El elemento es visible",
39
+ }),
40
+ });
41
+
42
+ return (
43
+ <>
44
+ <div className="flex items-center gap-x-2">
45
+ <span>Scroll</span>
46
+ <ArrowDown className="animate-bounce" />
47
+ </div>
48
+
49
+ <Wrapper>
50
+ <div
51
+ ref={ref}
52
+ className={`transition-colors p-2 rounded text-primary-foreground duration-1000 mt-[400px] ${isVisible ? "bg-primary" : "bg-error"}`}
53
+ >
54
+ {isVisible ? "El elemento es visible" : "El elemento no es visible"}
55
+ </div>
56
+ </Wrapper>
57
+ </>
58
+ );
59
+ },
60
+ };
@@ -0,0 +1 @@
1
+ export { default as useIsVisible } from "./useIsVisible";
@@ -0,0 +1,50 @@
1
+ import { RefObject, useCallback, useEffect, useRef, useState } from "react";
2
+
3
+ interface UseIsVisibleProps extends IntersectionObserverInit {
4
+ onVisible?: () => void;
5
+ }
6
+
7
+ interface UseIsVisibleReturn<T> {
8
+ ref: RefObject<T | null>;
9
+ isVisible: boolean;
10
+ }
11
+
12
+ function useIsVisible<T extends HTMLElement>({
13
+ onVisible,
14
+ root = null,
15
+ rootMargin = "0px",
16
+ threshold = 0,
17
+ }: UseIsVisibleProps = {}): UseIsVisibleReturn<T> {
18
+ const [isVisible, setIsVisible] = useState<boolean>(false);
19
+ const ref = useRef<T | null>(null);
20
+
21
+ const handleIntersect: IntersectionObserverCallback = useCallback(
22
+ ([entry]) => {
23
+ const isIntersecting = entry.isIntersecting;
24
+ setIsVisible(isIntersecting);
25
+ },
26
+ [onVisible],
27
+ );
28
+
29
+ useEffect(() => {
30
+ if (ref.current == null) return;
31
+ const observer = new IntersectionObserver(handleIntersect, {
32
+ root,
33
+ rootMargin,
34
+ threshold,
35
+ });
36
+ observer.observe(ref.current);
37
+ return () => {
38
+ if (ref.current == null) return;
39
+ observer.unobserve(ref.current);
40
+ };
41
+ }, [ref, root, rootMargin, threshold, handleIntersect]);
42
+
43
+ useEffect(() => {
44
+ isVisible && onVisible?.();
45
+ }, [isVisible]);
46
+
47
+ return { ref, isVisible };
48
+ }
49
+
50
+ export default useIsVisible;