@boxcustodia/library 2.0.0-alpha.10 → 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.
- package/dist/index.cjs.js +70 -70
- package/dist/index.css +2 -0
- package/dist/index.d.ts +420 -272
- package/dist/index.es.js +34448 -27816
- package/dist/theme.css +1 -1
- package/package.json +11 -6
- package/src/__doc__/Changelog.mdx +6 -0
- package/src/__doc__/Components.mdx +73 -0
- package/src/__doc__/Examples.tsx +69 -0
- package/src/__doc__/Icons.mdx +41 -0
- package/src/__doc__/Intro.mdx +138 -0
- package/src/__doc__/MCP.mdx +71 -0
- package/src/__doc__/Migration.mdx +475 -0
- package/src/__doc__/Theme.mdx +132 -0
- package/src/__doc__/Types.mdx +252 -0
- package/src/components/alert/alert.stories.tsx +142 -0
- package/src/components/alert/alert.tsx +109 -0
- package/src/components/alert/index.ts +7 -0
- package/src/components/alert-dialog/alert-dialog.stories.tsx +173 -0
- package/src/components/alert-dialog/alert-dialog.test.tsx +49 -0
- package/src/components/alert-dialog/alert-dialog.tsx +265 -0
- package/src/components/alert-dialog/index.ts +1 -0
- package/src/components/auto-complete/auto-complete-primitives.tsx +155 -0
- package/src/components/auto-complete/auto-complete.stories.tsx +241 -0
- package/src/components/auto-complete/auto-complete.tsx +82 -0
- package/src/components/auto-complete/index.ts +2 -0
- package/src/components/avatar/avatar.stories.tsx +84 -0
- package/src/components/avatar/avatar.test.tsx +61 -0
- package/src/components/avatar/avatar.tsx +104 -0
- package/src/components/avatar/index.ts +1 -0
- package/src/components/background-image/background-image.stories.tsx +21 -0
- package/src/components/background-image/background-image.test.tsx +29 -0
- package/src/components/background-image/background-image.tsx +23 -0
- package/src/components/background-image/index.ts +1 -0
- package/src/components/button/button.stories.tsx +396 -0
- package/src/components/button/button.test.tsx +58 -0
- package/src/components/button/button.tsx +31 -0
- package/src/components/button/button.variants.ts +44 -0
- package/src/components/button/components/base-button.tsx +86 -0
- package/src/components/button/components/loader-overlay.tsx +21 -0
- package/src/components/button/components/loading-icon.tsx +47 -0
- package/src/components/button/index.ts +3 -0
- package/src/components/calendar/calendar.model.ts +86 -0
- package/src/components/calendar/calendar.stories.tsx +155 -0
- package/src/components/calendar/calendar.test.tsx +12 -0
- package/src/components/calendar/calendar.tsx +185 -0
- package/src/components/calendar/components/calendar-navigation.tsx +141 -0
- package/src/components/calendar/components/day.tsx +61 -0
- package/src/components/calendar/components/decade-view.tsx +45 -0
- package/src/components/calendar/components/index.ts +6 -0
- package/src/components/calendar/components/month-view.tsx +58 -0
- package/src/components/calendar/components/week-days.tsx +27 -0
- package/src/components/calendar/components/year-view.tsx +29 -0
- package/src/components/calendar/hooks/index.ts +4 -0
- package/src/components/calendar/hooks/use-calendar-navigation.ts +79 -0
- package/src/components/calendar/hooks/use-calendar.ts +90 -0
- package/src/components/calendar/hooks/use-multiple-calendar.ts +34 -0
- package/src/components/calendar/hooks/use-range-calendar.ts +91 -0
- package/src/components/calendar/hooks/use-single-calendar.ts +18 -0
- package/src/components/calendar/index.ts +1 -0
- package/src/components/calendar/utils/typeguards.ts +7 -0
- package/src/components/card/card.stories.tsx +116 -0
- package/src/components/card/card.tsx +74 -0
- package/src/components/card/index.ts +1 -0
- package/src/components/center/center.stories.tsx +81 -0
- package/src/components/center/center.tsx +24 -0
- package/src/components/center/index.ts +1 -0
- package/src/components/checkbox/checkbox.stories.tsx +307 -0
- package/src/components/checkbox/checkbox.tsx +273 -0
- package/src/components/checkbox/index.ts +1 -0
- package/src/components/checkbox-group/checkbox-group.stories.tsx +104 -0
- package/src/components/checkbox-group/checkbox-group.tsx +16 -0
- package/src/components/checkbox-group/index.ts +1 -0
- package/src/components/combobox/combobox.stories.tsx +339 -0
- package/src/components/combobox/combobox.tsx +892 -0
- package/src/components/combobox/index.ts +1 -0
- package/src/components/date-picker/date-input.stories.tsx +158 -0
- package/src/components/date-picker/date-input.tsx +163 -0
- package/src/components/date-picker/date-picker.model.ts +90 -0
- package/src/components/date-picker/date-picker.stories.tsx +200 -0
- package/src/components/date-picker/date-picker.test.tsx +23 -0
- package/src/components/date-picker/date-picker.tsx +298 -0
- package/src/components/date-picker/date-picker.utils.ts +260 -0
- package/src/components/date-picker/index.ts +3 -0
- package/src/components/date-picker/use-date-input-popover.ts +48 -0
- package/src/components/date-picker/use-date-input.ts +125 -0
- package/src/components/dialog/dialog.stories.tsx +171 -0
- package/src/components/dialog/dialog.test.tsx +68 -0
- package/src/components/dialog/dialog.tsx +277 -0
- package/src/components/dialog/index.ts +1 -0
- package/src/components/divider/divider.stories.tsx +70 -0
- package/src/components/divider/divider.test.tsx +22 -0
- package/src/components/divider/divider.tsx +23 -0
- package/src/components/divider/index.ts +1 -0
- package/src/components/dropzone/dropzone.stories.tsx +210 -0
- package/src/components/dropzone/dropzone.tsx +154 -0
- package/src/components/dropzone/file-types.ts +64 -0
- package/src/components/dropzone/index.ts +3 -0
- package/src/components/dropzone/upload-primitives.tsx +310 -0
- package/src/components/dropzone/use-dropzone.ts +122 -0
- package/src/components/empty-state/empty-state.stories.tsx +56 -0
- package/src/components/empty-state/empty-state.tsx +39 -0
- package/src/components/empty-state/index.ts +1 -0
- package/src/components/field/field.stories.tsx +223 -0
- package/src/components/field/field.tsx +229 -0
- package/src/components/field/index.ts +1 -0
- package/src/components/form/form.stories.tsx +594 -0
- package/src/components/form/form.tsx +30 -0
- package/src/components/form/index.ts +1 -0
- package/src/components/heading/heading.stories.tsx +74 -0
- package/src/components/heading/heading.tsx +28 -0
- package/src/components/heading/heading.variants.ts +27 -0
- package/src/components/heading/index.ts +1 -0
- package/src/components/index.ts +46 -0
- package/src/components/input/index.ts +1 -0
- package/src/components/input/input.stories.tsx +104 -0
- package/src/components/input/input.tsx +75 -0
- package/src/components/kbd/index.ts +1 -0
- package/src/components/kbd/kbd.stories.tsx +40 -0
- package/src/components/kbd/kbd.tsx +31 -0
- package/src/components/kbd/kbd.variants.ts +26 -0
- package/src/components/label/index.ts +1 -0
- package/src/components/label/label.stories.tsx +68 -0
- package/src/components/label/label.test.tsx +61 -0
- package/src/components/label/label.tsx +62 -0
- package/src/components/loader/index.ts +1 -0
- package/src/components/loader/loader.stories.tsx +60 -0
- package/src/components/loader/loader.test.tsx +26 -0
- package/src/components/loader/loader.tsx +60 -0
- package/src/components/menu/index.ts +2 -0
- package/src/components/menu/menu-primitives.tsx +248 -0
- package/src/components/menu/menu.stories.tsx +203 -0
- package/src/components/menu/menu.tsx +100 -0
- package/src/components/menu/util/render-menu-item.tsx +54 -0
- package/src/components/multi-select/hooks/use-multi-select.ts +66 -0
- package/src/components/multi-select/index.ts +1 -0
- package/src/components/multi-select/multi-select.stories.tsx +294 -0
- package/src/components/multi-select/multi-select.tsx +300 -0
- package/src/components/multi-select/multi-select.variants.ts +22 -0
- package/src/components/number-input/index.ts +1 -0
- package/src/components/number-input/number-input.stories.tsx +209 -0
- package/src/components/number-input/number-input.test.tsx +87 -0
- package/src/components/number-input/number-input.tsx +230 -0
- package/src/components/pagination/components/pagination-option.tsx +27 -0
- package/src/components/pagination/index.ts +1 -0
- package/src/components/pagination/pagination.stories.tsx +80 -0
- package/src/components/pagination/pagination.test.tsx +76 -0
- package/src/components/pagination/pagination.tsx +102 -0
- package/src/components/password/index.ts +1 -0
- package/src/components/password/password.stories.tsx +104 -0
- package/src/components/password/password.tsx +71 -0
- package/src/components/popover/index.ts +1 -0
- package/src/components/popover/popover.stories.tsx +213 -0
- package/src/components/popover/popover.tsx +203 -0
- package/src/components/progress/index.ts +1 -0
- package/src/components/progress/progress.stories.tsx +124 -0
- package/src/components/progress/progress.test.tsx +25 -0
- package/src/components/progress/progress.tsx +124 -0
- package/src/components/scroll-area/index.ts +1 -0
- package/src/components/scroll-area/scroll-area.stories.tsx +166 -0
- package/src/components/scroll-area/scroll-area.tsx +64 -0
- package/src/components/select/index.ts +1 -0
- package/src/components/select/select.stories.tsx +253 -0
- package/src/components/select/select.tsx +430 -0
- package/src/components/show/index.ts +1 -0
- package/src/components/show/show.stories.tsx +197 -0
- package/src/components/show/show.test.tsx +41 -0
- package/src/components/show/show.tsx +16 -0
- package/src/components/skeleton/index.ts +1 -0
- package/src/components/skeleton/skeleton.stories.tsx +36 -0
- package/src/components/skeleton/skeleton.test.tsx +14 -0
- package/src/components/skeleton/skeleton.tsx +15 -0
- package/src/components/stack/index.ts +1 -0
- package/src/components/stack/stack.stories.tsx +194 -0
- package/src/components/stack/stack.tsx +52 -0
- package/src/components/stepper/Stepper.tsx +190 -0
- package/src/components/stepper/context/stepper-context.tsx +11 -0
- package/src/components/stepper/index.ts +1 -0
- package/src/components/stepper/stepper.stories.tsx +130 -0
- package/src/components/stepper/stepper.test.tsx +91 -0
- package/src/components/switch/index.ts +1 -0
- package/src/components/switch/switch.stories.tsx +122 -0
- package/src/components/switch/switch.test.tsx +30 -0
- package/src/components/switch/switch.tsx +86 -0
- package/src/components/table/index.ts +3 -0
- package/src/components/table/table-primitives.tsx +122 -0
- package/src/components/table/table.model.ts +20 -0
- package/src/components/table/table.stories.tsx +169 -0
- package/src/components/table/table.test.tsx +91 -0
- package/src/components/table/table.tsx +109 -0
- package/src/components/table-pagination/index.ts +2 -0
- package/src/components/table-pagination/table-pagination.model.ts +2 -0
- package/src/components/table-pagination/table-pagination.stories.tsx +23 -0
- package/src/components/table-pagination/table-pagination.test.tsx +32 -0
- package/src/components/table-pagination/table-pagination.tsx +108 -0
- package/src/components/tabs/context/tabs-context.tsx +14 -0
- package/src/components/tabs/index.ts +1 -0
- package/src/components/tabs/tabs.stories.tsx +182 -0
- package/src/components/tabs/tabs.test.tsx +61 -0
- package/src/components/tabs/tabs.tsx +175 -0
- package/src/components/tag/index.ts +2 -0
- package/src/components/tag/tag.stories.tsx +170 -0
- package/src/components/tag/tag.test.tsx +18 -0
- package/src/components/tag/tag.tsx +99 -0
- package/src/components/tag/tag.variants.ts +31 -0
- package/src/components/textarea/index.ts +1 -0
- package/src/components/textarea/textarea.stories.tsx +73 -0
- package/src/components/textarea/textarea.tsx +105 -0
- package/src/components/timeline/index.ts +1 -0
- package/src/components/timeline/timeline-status.ts +5 -0
- package/src/components/timeline/timeline.stories.tsx +84 -0
- package/src/components/timeline/timeline.tsx +147 -0
- package/src/components/toast/index.ts +1 -0
- package/src/components/toast/toast.stories.tsx +392 -0
- package/src/components/toast/toast.test.tsx +50 -0
- package/src/components/toast/toast.tsx +411 -0
- package/src/components/tooltip/index.ts +1 -0
- package/src/components/tooltip/tooltip.stories.tsx +226 -0
- package/src/components/tooltip/tooltip.test.tsx +46 -0
- package/src/components/tooltip/tooltip.tsx +171 -0
- package/src/components/tree/hooks/use-controllable-tree-state.ts +80 -0
- package/src/components/tree/index.ts +2 -0
- package/src/components/tree/tree-primitives.tsx +126 -0
- package/src/components/tree/tree.stories.tsx +468 -0
- package/src/components/tree/tree.tsx +42 -0
- package/src/hooks/index.ts +26 -0
- package/src/hooks/useArray/__doc__/useArray.stories.tsx +100 -0
- package/src/hooks/useArray/__test__/useArray.test.tsx +88 -0
- package/src/hooks/useArray/index.ts +1 -0
- package/src/hooks/useArray/useArray.ts +76 -0
- package/src/hooks/useAsync/__doc__/useAsync.stories.tsx +149 -0
- package/src/hooks/useAsync/__test__/useAsync.test.tsx +68 -0
- package/src/hooks/useAsync/index.ts +1 -0
- package/src/hooks/useAsync/useAsync.ts +58 -0
- package/src/hooks/useClickOutside/__doc__/useClickOutside.stories.tsx +40 -0
- package/src/hooks/useClickOutside/__test__/useClickOutside.test.tsx +33 -0
- package/src/hooks/useClickOutside/index.ts +1 -0
- package/src/hooks/useClickOutside/useClickOutside.ts +26 -0
- package/src/hooks/useClipboard/__doc__/useClipboard.stories.tsx +45 -0
- package/src/hooks/useClipboard/__test__/useClipboard.test.tsx +19 -0
- package/src/hooks/useClipboard/index.ts +1 -0
- package/src/hooks/useClipboard/useClipboard.tsx +28 -0
- package/src/hooks/useDebounceCallback/__doc__/useDebouncedCallback.stories.tsx +84 -0
- package/src/hooks/useDebounceCallback/index.ts +1 -0
- package/src/hooks/useDebounceCallback/useDebouncedCallback.ts +23 -0
- package/src/hooks/useDebounceValue/__doc__/useDebouncedValue.stories.tsx +75 -0
- package/src/hooks/useDebounceValue/index.ts +1 -0
- package/src/hooks/useDebounceValue/useDebouncedValue.ts +17 -0
- package/src/hooks/useDisclosure/__doc__/useDisclosure.stories.tsx +39 -0
- package/src/hooks/useDisclosure/__test__/useDisclosure.test.ts +43 -0
- package/src/hooks/useDisclosure/index.ts +1 -0
- package/src/hooks/useDisclosure/useDisclosure.ts +37 -0
- package/src/hooks/useDocumentTitle/__doc__/useDocumentTitle.stories.tsx +26 -0
- package/src/hooks/useDocumentTitle/index.ts +1 -0
- package/src/hooks/useDocumentTitle/useDocumentTitle.tsx +11 -0
- package/src/hooks/useEventListener/__doc__/useEventListener.stories.tsx +28 -0
- package/src/hooks/useEventListener/__test__/useEventListener.test.tsx +26 -0
- package/src/hooks/useEventListener/index.ts +1 -0
- package/src/hooks/useEventListener/useEventListener.ts +25 -0
- package/src/hooks/useFocusTrap/__doc__/useFocusTrap.stories.tsx +37 -0
- package/src/hooks/useFocusTrap/index.ts +1 -0
- package/src/hooks/useFocusTrap/scopeTab.ts +38 -0
- package/src/hooks/useFocusTrap/tabbable.ts +70 -0
- package/src/hooks/useFocusTrap/useFocusTrap.ts +78 -0
- package/src/hooks/useHotkey/__docs__/useHotkey.stories.tsx +116 -0
- package/src/hooks/useHotkey/__test__/useHotkey.test.tsx +105 -0
- package/src/hooks/useHotkey/__utils__/create-hotkey-listener.ts +25 -0
- package/src/hooks/useHotkey/__utils__/index.ts +3 -0
- package/src/hooks/useHotkey/__utils__/is-input-field.ts +14 -0
- package/src/hooks/useHotkey/__utils__/match-key-modifiers.ts +25 -0
- package/src/hooks/useHotkey/index.ts +1 -0
- package/src/hooks/useHotkey/useHotkey.ts +34 -0
- package/src/hooks/useHover/__doc__/useHover.stories.tsx +41 -0
- package/src/hooks/useHover/__test__/useHover.test.tsx +45 -0
- package/src/hooks/useHover/index.ts +1 -0
- package/src/hooks/useHover/useHover.tsx +40 -0
- package/src/hooks/useIsVisible/__doc__/useIsVisible.stories.tsx +60 -0
- package/src/hooks/useIsVisible/index.ts +1 -0
- package/src/hooks/useIsVisible/useIsVisible.tsx +50 -0
- package/src/hooks/useLocalStorage/__doc__/useLocalStorage.stories.tsx +86 -0
- package/src/hooks/useLocalStorage/__test__/useLocalStorage.test.ts +85 -0
- package/src/hooks/useLocalStorage/index.ts +1 -0
- package/src/hooks/useLocalStorage/useLocalStorage.ts +57 -0
- package/src/hooks/useMediaQuery/__doc__/useMediaQuery.stories.tsx +39 -0
- package/src/hooks/useMediaQuery/index.ts +1 -0
- package/src/hooks/useMediaQuery/useMediaQuery.ts +22 -0
- package/src/hooks/useMemoizedFn/index.ts +1 -0
- package/src/hooks/useMemoizedFn/useMemoizedFn.ts +32 -0
- package/src/hooks/useMutation/__doc__/useMutation.stories.tsx +111 -0
- package/src/hooks/useMutation/__test__/useMutation.test.tsx +83 -0
- package/src/hooks/useMutation/index.ts +1 -0
- package/src/hooks/useMutation/useMutation.tsx +60 -0
- package/src/hooks/useObject/__doc__/useObject.stories.tsx +119 -0
- package/src/hooks/useObject/__test__/useObject.test.tsx +87 -0
- package/src/hooks/useObject/index.ts +1 -0
- package/src/hooks/useObject/useObject.tsx +48 -0
- package/src/hooks/usePagination/__doc__/usePagination.stories.tsx +72 -0
- package/src/hooks/usePagination/__test__/usePagination.test.tsx +98 -0
- package/src/hooks/usePagination/index.ts +2 -0
- package/src/hooks/usePagination/usePagination.tsx +74 -0
- package/src/hooks/usePortal/__doc__/usePortal.stories.tsx +19 -0
- package/src/hooks/usePortal/__test__/usePortal.test.tsx +20 -0
- package/src/hooks/usePortal/index.ts +1 -0
- package/src/hooks/usePortal/usePortal.ts +40 -0
- package/src/hooks/usePreventCloseWindow/__doc__/usePreventCloseWindow.stories.tsx +32 -0
- package/src/hooks/usePreventCloseWindow/index.ts +1 -0
- package/src/hooks/usePreventCloseWindow/usePreventCloseWindow.ts +33 -0
- package/src/hooks/useRangePagination/__test__/useRangePagination.test.tsx +63 -0
- package/src/hooks/useRangePagination/index.ts +2 -0
- package/src/hooks/useRangePagination/useRangePagination.tsx +72 -0
- package/src/hooks/useSelection/__doc__/useSelection.stories.tsx +140 -0
- package/src/hooks/useSelection/__test__/useSelection.test.tsx +57 -0
- package/src/hooks/useSelection/index.ts +1 -0
- package/src/hooks/useSelection/useSelection.ts +121 -0
- package/src/hooks/useStep/__doc__/useStep.stories.tsx +98 -0
- package/src/hooks/useStep/__test__/useStep.test.ts +51 -0
- package/src/hooks/useStep/index.ts +1 -0
- package/src/hooks/useStep/useStep.ts +57 -0
- package/src/hooks/useToggle/__doc__/useToggle.stories.tsx +25 -0
- package/src/hooks/useToggle/__test__/useToggle.test.tsx +43 -0
- package/src/hooks/useToggle/index.ts +1 -0
- package/src/hooks/useToggle/useToggle.ts +16 -0
- package/src/index.ts +6 -0
- package/src/lib/cn.ts +8 -0
- package/src/lib/index.ts +1 -0
- package/src/models/Generic.model.ts +67 -0
- package/src/models/index.ts +1 -0
- package/src/providers/index.ts +2 -0
- package/src/providers/library-provider.tsx +44 -0
- package/src/providers/theme/ThemeProvider.tsx +25 -0
- package/src/providers/theme/index.ts +3 -0
- package/src/providers/theme/types.ts +11 -0
- package/src/providers/theme/useThemeProps.ts +25 -0
- package/src/stores/theme.store.ts +31 -0
- package/src/styles/components.css +4 -0
- package/src/styles/index.css +2 -0
- package/src/styles/library.css +2 -0
- package/src/styles/theme.css +232 -0
- package/src/utils/dates/parseDateRange.utility.ts +39 -0
- package/src/utils/form.tsx +91 -0
- package/src/utils/functions/createSafeContext.ts +17 -0
- package/src/utils/functions/ensureReactElement.tsx +30 -0
- package/src/utils/functions/getFormData.ts +19 -0
- package/src/utils/functions/index.ts +4 -0
- package/src/utils/functions/mergeRefs.ts +18 -0
- package/src/utils/index.ts +3 -0
- package/src/utils/strings/extractInitials.utility.ts +10 -0
- package/src/utils/strings/index.ts +1 -0
- package/src/utils/tests/click.ts +3 -0
- package/src/utils/tests/index.ts +2 -0
- package/src/utils/tests/keyboard.ts +21 -0
- package/src/utils/tests/type.ts +6 -0
|
@@ -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>↑</Kbd>
|
|
47
|
+
<Kbd>←</Kbd>
|
|
48
|
+
<Kbd>↓</Kbd>
|
|
49
|
+
<Kbd>→</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,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;
|