@arolariu/components 1.0.0 → 2.0.0
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/CHANGELOG.md +89 -0
- package/EXAMPLES.md +2510 -0
- package/dist/components/ui/accordion.js +3 -3
- package/dist/components/ui/accordion.js.map +1 -1
- package/dist/components/ui/accordion_module.css.map +1 -1
- package/dist/components/ui/alert-dialog.d.ts +4 -16
- package/dist/components/ui/alert-dialog.d.ts.map +1 -1
- package/dist/components/ui/alert-dialog.js +21 -17
- package/dist/components/ui/alert-dialog.js.map +1 -1
- package/dist/components/ui/alert-dialog_module.css +1 -1
- package/dist/components/ui/alert-dialog_module.css.map +1 -1
- package/dist/components/ui/alert.js +4 -4
- package/dist/components/ui/alert.js.map +1 -1
- package/dist/components/ui/alert_module.css.map +1 -1
- package/dist/components/ui/aspect-ratio.js +2 -2
- package/dist/components/ui/aspect-ratio.js.map +1 -1
- package/dist/components/ui/aspect-ratio_module.css.map +1 -1
- package/dist/components/ui/async-boundary.js +2 -2
- package/dist/components/ui/async-boundary.js.map +1 -1
- package/dist/components/ui/avatar.d.ts +3 -12
- package/dist/components/ui/avatar.d.ts.map +1 -1
- package/dist/components/ui/avatar.js +18 -15
- package/dist/components/ui/avatar.js.map +1 -1
- package/dist/components/ui/avatar_module.css.map +1 -1
- package/dist/components/ui/background-beams.js +3 -3
- package/dist/components/ui/background-beams.js.map +1 -1
- package/dist/components/ui/background-beams_module.css.map +1 -1
- package/dist/components/ui/badge.js +2 -2
- package/dist/components/ui/badge.js.map +1 -1
- package/dist/components/ui/badge_module.css.map +1 -1
- package/dist/components/ui/breadcrumb.js +10 -10
- package/dist/components/ui/breadcrumb.js.map +1 -1
- package/dist/components/ui/breadcrumb_module.css.map +1 -1
- package/dist/components/ui/bubble-background.js +5 -5
- package/dist/components/ui/bubble-background.js.map +1 -1
- package/dist/components/ui/bubble-background_module.css.map +1 -1
- package/dist/components/ui/button-group.d.ts +1 -1
- package/dist/components/ui/button-group.d.ts.map +1 -1
- package/dist/components/ui/button-group.js +6 -6
- package/dist/components/ui/button-group.js.map +1 -1
- package/dist/components/ui/button-group_module.css.map +1 -1
- package/dist/components/ui/button.js +4 -4
- package/dist/components/ui/button.js.map +1 -1
- package/dist/components/ui/button_module.css.map +1 -1
- package/dist/components/ui/calendar.d.ts +3 -5
- package/dist/components/ui/calendar.d.ts.map +1 -1
- package/dist/components/ui/calendar.js +9 -9
- package/dist/components/ui/calendar.js.map +1 -1
- package/dist/components/ui/calendar_module.css.map +1 -1
- package/dist/components/ui/card-skeleton.js +2 -2
- package/dist/components/ui/card-skeleton.js.map +1 -1
- package/dist/components/ui/card-skeleton_module.css.map +1 -1
- package/dist/components/ui/card.js +8 -8
- package/dist/components/ui/card.js.map +1 -1
- package/dist/components/ui/card_module.css.map +1 -1
- package/dist/components/ui/carousel.d.ts.map +1 -1
- package/dist/components/ui/carousel.js +16 -16
- package/dist/components/ui/carousel.js.map +1 -1
- package/dist/components/ui/carousel_module.css +1 -1
- package/dist/components/ui/carousel_module.css.map +1 -1
- package/dist/components/ui/chart.d.ts +6 -3
- package/dist/components/ui/chart.d.ts.map +1 -1
- package/dist/components/ui/chart.js +70 -70
- package/dist/components/ui/chart.js.map +1 -1
- package/dist/components/ui/chart_module.css.map +1 -1
- package/dist/components/ui/checkbox-group.d.ts +2 -6
- package/dist/components/ui/checkbox-group.d.ts.map +1 -1
- package/dist/components/ui/checkbox-group.js +8 -7
- package/dist/components/ui/checkbox-group.js.map +1 -1
- package/dist/components/ui/checkbox-group_module.css.map +1 -1
- package/dist/components/ui/checkbox.d.ts +3 -1
- package/dist/components/ui/checkbox.d.ts.map +1 -1
- package/dist/components/ui/checkbox.js +6 -3
- package/dist/components/ui/checkbox.js.map +1 -1
- package/dist/components/ui/checkbox_module.css.map +1 -1
- package/dist/components/ui/collapsible.d.ts.map +1 -1
- package/dist/components/ui/collapsible.js +4 -4
- package/dist/components/ui/collapsible.js.map +1 -1
- package/dist/components/ui/collapsible_module.css.map +1 -1
- package/dist/components/ui/combobox.d.ts +335 -0
- package/dist/components/ui/combobox.d.ts.map +1 -0
- package/dist/components/ui/combobox.js +206 -0
- package/dist/components/ui/combobox.js.map +1 -0
- package/dist/components/ui/combobox.module.js +23 -0
- package/dist/components/ui/combobox.module.js.map +1 -0
- package/dist/components/ui/combobox_module.css +142 -0
- package/dist/components/ui/combobox_module.css.map +1 -0
- package/dist/components/ui/command.d.ts.map +1 -1
- package/dist/components/ui/command.js +62 -53
- package/dist/components/ui/command.js.map +1 -1
- package/dist/components/ui/command_module.css +1 -1
- package/dist/components/ui/command_module.css.map +1 -1
- package/dist/components/ui/context-menu.d.ts.map +1 -1
- package/dist/components/ui/context-menu.js +6 -6
- package/dist/components/ui/context-menu.js.map +1 -1
- package/dist/components/ui/context-menu_module.css.map +1 -1
- package/dist/components/ui/copy-button.js +6 -6
- package/dist/components/ui/copy-button.js.map +1 -1
- package/dist/components/ui/copy-button_module.css.map +1 -1
- package/dist/components/ui/counting-number.js +6 -6
- package/dist/components/ui/counting-number.js.map +1 -1
- package/dist/components/ui/counting-number_module.css.map +1 -1
- package/dist/components/ui/dialog.js +6 -6
- package/dist/components/ui/dialog.js.map +1 -1
- package/dist/components/ui/dialog_module.css +1 -1
- package/dist/components/ui/dialog_module.css.map +1 -1
- package/dist/components/ui/dot-background_module.css.map +1 -1
- package/dist/components/ui/drawer.d.ts.map +1 -1
- package/dist/components/ui/drawer.js +5 -5
- package/dist/components/ui/drawer.js.map +1 -1
- package/dist/components/ui/drawer_module.css.map +1 -1
- package/dist/components/ui/dropdown-menu.d.ts.map +1 -1
- package/dist/components/ui/dropdown-menu.js +6 -6
- package/dist/components/ui/dropdown-menu.js.map +1 -1
- package/dist/components/ui/dropdown-menu_module.css.map +1 -1
- package/dist/components/ui/dropdrawer.d.ts +10 -16
- package/dist/components/ui/dropdrawer.d.ts.map +1 -1
- package/dist/components/ui/dropdrawer.js +73 -65
- package/dist/components/ui/dropdrawer.js.map +1 -1
- package/dist/components/ui/dropdrawer_module.css.map +1 -1
- package/dist/components/ui/empty.js +7 -7
- package/dist/components/ui/empty.js.map +1 -1
- package/dist/components/ui/empty_module.css.map +1 -1
- package/dist/components/ui/error-boundary.js +2 -2
- package/dist/components/ui/error-boundary.js.map +1 -1
- package/dist/components/ui/error-boundary_module.css.map +1 -1
- package/dist/components/ui/field.js +12 -12
- package/dist/components/ui/field.js.map +1 -1
- package/dist/components/ui/field_module.css.map +1 -1
- package/dist/components/ui/fireworks-background.js +6 -6
- package/dist/components/ui/fireworks-background.js.map +1 -1
- package/dist/components/ui/fireworks-background_module.css.map +1 -1
- package/dist/components/ui/flip-button.js +5 -5
- package/dist/components/ui/flip-button.js.map +1 -1
- package/dist/components/ui/flip-button_module.css.map +1 -1
- package/dist/components/ui/focus-scope.js +6 -6
- package/dist/components/ui/focus-scope.js.map +1 -1
- package/dist/components/ui/focus-scope_module.css.map +1 -1
- package/dist/components/ui/form-skeleton.js +2 -2
- package/dist/components/ui/form-skeleton.js.map +1 -1
- package/dist/components/ui/form-skeleton_module.css.map +1 -1
- package/dist/components/ui/form.d.ts +3 -3
- package/dist/components/ui/form.d.ts.map +1 -1
- package/dist/components/ui/form.js +13 -13
- package/dist/components/ui/form.js.map +1 -1
- package/dist/components/ui/form_module.css.map +1 -1
- package/dist/components/ui/gradient-background.js +2 -2
- package/dist/components/ui/gradient-background.js.map +1 -1
- package/dist/components/ui/gradient-background_module.css.map +1 -1
- package/dist/components/ui/gradient-text.js +2 -2
- package/dist/components/ui/gradient-text.js.map +1 -1
- package/dist/components/ui/gradient-text_module.css.map +1 -1
- package/dist/components/ui/highlight-text.js +4 -4
- package/dist/components/ui/highlight-text.js.map +1 -1
- package/dist/components/ui/highlight-text_module.css.map +1 -1
- package/dist/components/ui/hole-background.js +21 -21
- package/dist/components/ui/hole-background.js.map +1 -1
- package/dist/components/ui/hole-background_module.css.map +1 -1
- package/dist/components/ui/hover-card.js +3 -3
- package/dist/components/ui/hover-card.js.map +1 -1
- package/dist/components/ui/hover-card_module.css.map +1 -1
- package/dist/components/ui/input-group.js +7 -7
- package/dist/components/ui/input-group.js.map +1 -1
- package/dist/components/ui/input-group_module.css.map +1 -1
- package/dist/components/ui/input-otp.d.ts +3 -3
- package/dist/components/ui/input-otp.d.ts.map +1 -1
- package/dist/components/ui/input-otp.js +6 -6
- package/dist/components/ui/input-otp.js.map +1 -1
- package/dist/components/ui/input-otp_module.css.map +1 -1
- package/dist/components/ui/input.js +2 -2
- package/dist/components/ui/input.js.map +1 -1
- package/dist/components/ui/input_module.css.map +1 -1
- package/dist/components/ui/item.d.ts +1 -1
- package/dist/components/ui/item.d.ts.map +1 -1
- package/dist/components/ui/item.js +13 -13
- package/dist/components/ui/item.js.map +1 -1
- package/dist/components/ui/item_module.css.map +1 -1
- package/dist/components/ui/kbd.js +3 -3
- package/dist/components/ui/kbd.js.map +1 -1
- package/dist/components/ui/kbd_module.css.map +1 -1
- package/dist/components/ui/label.js +2 -2
- package/dist/components/ui/label.js.map +1 -1
- package/dist/components/ui/label_module.css.map +1 -1
- package/dist/components/ui/list-skeleton.js +2 -2
- package/dist/components/ui/list-skeleton.js.map +1 -1
- package/dist/components/ui/list-skeleton_module.css.map +1 -1
- package/dist/components/ui/loading-overlay.js +2 -2
- package/dist/components/ui/loading-overlay.js.map +1 -1
- package/dist/components/ui/loading-overlay_module.css.map +1 -1
- package/dist/components/ui/menubar.d.ts +11 -13
- package/dist/components/ui/menubar.d.ts.map +1 -1
- package/dist/components/ui/menubar.js +4 -4
- package/dist/components/ui/menubar.js.map +1 -1
- package/dist/components/ui/menubar_module.css.map +1 -1
- package/dist/components/ui/meter.d.ts +8 -24
- package/dist/components/ui/meter.d.ts.map +1 -1
- package/dist/components/ui/meter.js +23 -19
- package/dist/components/ui/meter.js.map +1 -1
- package/dist/components/ui/meter_module.css.map +1 -1
- package/dist/components/ui/navigation-menu.d.ts +3 -12
- package/dist/components/ui/navigation-menu.d.ts.map +1 -1
- package/dist/components/ui/navigation-menu.js +15 -12
- package/dist/components/ui/navigation-menu.js.map +1 -1
- package/dist/components/ui/navigation-menu_module.css +1 -1
- package/dist/components/ui/navigation-menu_module.css.map +1 -1
- package/dist/components/ui/number-field.d.ts +6 -12
- package/dist/components/ui/number-field.d.ts.map +1 -1
- package/dist/components/ui/number-field.js +3 -3
- package/dist/components/ui/number-field.js.map +1 -1
- package/dist/components/ui/number-field_module.css.map +1 -1
- package/dist/components/ui/pagination.js +8 -8
- package/dist/components/ui/pagination.js.map +1 -1
- package/dist/components/ui/pagination_module.css.map +1 -1
- package/dist/components/ui/popover.js +5 -5
- package/dist/components/ui/popover.js.map +1 -1
- package/dist/components/ui/popover_module.css.map +1 -1
- package/dist/components/ui/progress.d.ts +1 -4
- package/dist/components/ui/progress.d.ts.map +1 -1
- package/dist/components/ui/progress.js +10 -9
- package/dist/components/ui/progress.js.map +1 -1
- package/dist/components/ui/progress_module.css.map +1 -1
- package/dist/components/ui/radio-group.d.ts +2 -4
- package/dist/components/ui/radio-group.d.ts.map +1 -1
- package/dist/components/ui/radio-group.js +3 -3
- package/dist/components/ui/radio-group.js.map +1 -1
- package/dist/components/ui/radio-group_module.css.map +1 -1
- package/dist/components/ui/resizable.d.ts +13 -29
- package/dist/components/ui/resizable.d.ts.map +1 -1
- package/dist/components/ui/resizable.js +8 -7
- package/dist/components/ui/resizable.js.map +1 -1
- package/dist/components/ui/resizable_module.css.map +1 -1
- package/dist/components/ui/ripple-button.js +9 -9
- package/dist/components/ui/ripple-button.js.map +1 -1
- package/dist/components/ui/ripple-button_module.css.map +1 -1
- package/dist/components/ui/scratcher.d.ts +1 -1
- package/dist/components/ui/scratcher.d.ts.map +1 -1
- package/dist/components/ui/scratcher.js +5 -4
- package/dist/components/ui/scratcher.js.map +1 -1
- package/dist/components/ui/scratcher_module.css.map +1 -1
- package/dist/components/ui/scroll-area.d.ts +2 -4
- package/dist/components/ui/scroll-area.d.ts.map +1 -1
- package/dist/components/ui/scroll-area.js +2 -2
- package/dist/components/ui/scroll-area.js.map +1 -1
- package/dist/components/ui/scroll-area_module.css.map +1 -1
- package/dist/components/ui/select.js +4 -4
- package/dist/components/ui/select.js.map +1 -1
- package/dist/components/ui/select_module.css.map +1 -1
- package/dist/components/ui/separator.d.ts +1 -4
- package/dist/components/ui/separator.d.ts.map +1 -1
- package/dist/components/ui/separator.js +9 -8
- package/dist/components/ui/separator.js.map +1 -1
- package/dist/components/ui/separator_module.css.map +1 -1
- package/dist/components/ui/sheet.d.ts.map +1 -1
- package/dist/components/ui/sheet.js +6 -6
- package/dist/components/ui/sheet.js.map +1 -1
- package/dist/components/ui/sheet_module.css.map +1 -1
- package/dist/components/ui/sidebar.d.ts +1 -1
- package/dist/components/ui/sidebar.d.ts.map +1 -1
- package/dist/components/ui/sidebar.js +36 -36
- package/dist/components/ui/sidebar.js.map +1 -1
- package/dist/components/ui/sidebar_module.css.map +1 -1
- package/dist/components/ui/skeleton.js +2 -2
- package/dist/components/ui/skeleton.js.map +1 -1
- package/dist/components/ui/skeleton_module.css.map +1 -1
- package/dist/components/ui/slider.js +2 -2
- package/dist/components/ui/slider.js.map +1 -1
- package/dist/components/ui/slider_module.css.map +1 -1
- package/dist/components/ui/spinner.js +2 -2
- package/dist/components/ui/spinner.js.map +1 -1
- package/dist/components/ui/spinner_module.css.map +1 -1
- package/dist/components/ui/stepper.js +2 -2
- package/dist/components/ui/stepper.js.map +1 -1
- package/dist/components/ui/stepper_module.css.map +1 -1
- package/dist/components/ui/switch.js +2 -2
- package/dist/components/ui/switch.js.map +1 -1
- package/dist/components/ui/switch_module.css.map +1 -1
- package/dist/components/ui/table-skeleton.js +2 -2
- package/dist/components/ui/table-skeleton.js.map +1 -1
- package/dist/components/ui/table-skeleton_module.css.map +1 -1
- package/dist/components/ui/table.js +9 -9
- package/dist/components/ui/table.js.map +1 -1
- package/dist/components/ui/table_module.css.map +1 -1
- package/dist/components/ui/tabs.js +3 -3
- package/dist/components/ui/tabs.js.map +1 -1
- package/dist/components/ui/tabs_module.css.map +1 -1
- package/dist/components/ui/textarea.js +2 -2
- package/dist/components/ui/textarea.js.map +1 -1
- package/dist/components/ui/textarea_module.css.map +1 -1
- package/dist/components/ui/timeline.js +5 -5
- package/dist/components/ui/timeline.js.map +1 -1
- package/dist/components/ui/timeline_module.css.map +1 -1
- package/dist/components/ui/{sonner.d.ts → toast.d.ts} +15 -6
- package/dist/components/ui/toast.d.ts.map +1 -0
- package/dist/components/ui/{sonner.js → toast.js} +43 -42
- package/dist/components/ui/toast.js.map +1 -0
- package/dist/components/ui/toast.module.js +34 -0
- package/dist/components/ui/toast.module.js.map +1 -0
- package/dist/components/ui/{sonner_module.css → toast_module.css} +35 -35
- package/dist/components/ui/toast_module.css.map +1 -0
- package/dist/components/ui/toggle-group.d.ts +2 -8
- package/dist/components/ui/toggle-group.d.ts.map +1 -1
- package/dist/components/ui/toggle-group.js +14 -12
- package/dist/components/ui/toggle-group.js.map +1 -1
- package/dist/components/ui/toggle-group_module.css.map +1 -1
- package/dist/components/ui/toggle.js +2 -2
- package/dist/components/ui/toggle.js.map +1 -1
- package/dist/components/ui/toggle_module.css.map +1 -1
- package/dist/components/ui/toolbar.d.ts +10 -30
- package/dist/components/ui/toolbar.d.ts.map +1 -1
- package/dist/components/ui/toolbar.js +28 -23
- package/dist/components/ui/toolbar.js.map +1 -1
- package/dist/components/ui/toolbar_module.css.map +1 -1
- package/dist/components/ui/tooltip.js +4 -4
- package/dist/components/ui/tooltip.js.map +1 -1
- package/dist/components/ui/tooltip_module.css.map +1 -1
- package/dist/components/ui/typewriter.js +4 -4
- package/dist/components/ui/typewriter.js.map +1 -1
- package/dist/components/ui/typewriter_module.css.map +1 -1
- package/dist/components/ui/visually-hidden.js +2 -2
- package/dist/components/ui/visually-hidden.js.map +1 -1
- package/dist/components/ui/visually-hidden_module.css.map +1 -1
- package/dist/hooks/useAnnounce.js +5 -5
- package/dist/hooks/useAnnounce.js.map +1 -1
- package/dist/hooks/useClipboard.d.ts +77 -0
- package/dist/hooks/useClipboard.d.ts.map +1 -0
- package/dist/hooks/useClipboard.js +42 -0
- package/dist/hooks/useClipboard.js.map +1 -0
- package/dist/hooks/useControllableState.d.ts +54 -0
- package/dist/hooks/useControllableState.d.ts.map +1 -0
- package/dist/hooks/useControllableState.js +29 -0
- package/dist/hooks/useControllableState.js.map +1 -0
- package/dist/hooks/useDebounce.d.ts +33 -0
- package/dist/hooks/useDebounce.d.ts.map +1 -0
- package/dist/hooks/useDebounce.js +20 -0
- package/dist/hooks/useDebounce.js.map +1 -0
- package/dist/hooks/useEventCallback.d.ts +34 -0
- package/dist/hooks/useEventCallback.d.ts.map +1 -0
- package/dist/hooks/useEventCallback.js +12 -0
- package/dist/hooks/useEventCallback.js.map +1 -0
- package/dist/hooks/useFocusManager.js +6 -6
- package/dist/hooks/useFocusManager.js.map +1 -1
- package/dist/hooks/useFocusVisible.js +5 -5
- package/dist/hooks/useFocusVisible.js.map +1 -1
- package/dist/hooks/useId.d.ts +30 -0
- package/dist/hooks/useId.d.ts.map +1 -0
- package/dist/hooks/useId.js +9 -0
- package/dist/hooks/useId.js.map +1 -0
- package/dist/hooks/useIntersectionObserver.d.ts +51 -0
- package/dist/hooks/useIntersectionObserver.d.ts.map +1 -0
- package/dist/hooks/useIntersectionObserver.js +25 -0
- package/dist/hooks/useIntersectionObserver.js.map +1 -0
- package/dist/hooks/useInterval.d.ts +55 -0
- package/dist/hooks/useInterval.d.ts.map +1 -0
- package/dist/hooks/useInterval.js +24 -0
- package/dist/hooks/useInterval.js.map +1 -0
- package/dist/hooks/useLocalStorage.d.ts +43 -0
- package/dist/hooks/useLocalStorage.d.ts.map +1 -0
- package/dist/hooks/useLocalStorage.js +53 -0
- package/dist/hooks/useLocalStorage.js.map +1 -0
- package/dist/hooks/useMediaQuery.js +3 -3
- package/dist/hooks/useMediaQuery.js.map +1 -1
- package/dist/hooks/useMergedRefs.d.ts +27 -0
- package/dist/hooks/useMergedRefs.d.ts.map +1 -0
- package/dist/hooks/useMergedRefs.js +11 -0
- package/dist/hooks/useMergedRefs.js.map +1 -0
- package/dist/hooks/useOnClickOutside.d.ts +32 -0
- package/dist/hooks/useOnClickOutside.d.ts.map +1 -0
- package/dist/hooks/useOnClickOutside.js +23 -0
- package/dist/hooks/useOnClickOutside.js.map +1 -0
- package/dist/hooks/usePrevious.d.ts +33 -0
- package/dist/hooks/usePrevious.d.ts.map +1 -0
- package/dist/hooks/usePrevious.js +14 -0
- package/dist/hooks/usePrevious.js.map +1 -0
- package/dist/hooks/useThrottle.d.ts +37 -0
- package/dist/hooks/useThrottle.d.ts.map +1 -0
- package/dist/hooks/useThrottle.js +34 -0
- package/dist/hooks/useThrottle.js.map +1 -0
- package/dist/hooks/useTimeout.d.ts +28 -0
- package/dist/hooks/useTimeout.d.ts.map +1 -0
- package/dist/hooks/useTimeout.js +24 -0
- package/dist/hooks/useTimeout.js.map +1 -0
- package/dist/index.css.map +1 -1
- package/dist/index.d.ts +17 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +15 -1
- package/dist/lib/utilities.d.ts +2 -3
- package/dist/lib/utilities.d.ts.map +1 -1
- package/dist/lib/utilities.js.map +1 -1
- package/dist/motion/Collapse.js +2 -2
- package/dist/motion/Collapse.js.map +1 -1
- package/dist/motion/Collapse_module.css.map +1 -1
- package/dist/motion/tokens.js +5 -5
- package/dist/motion/tokens.js.map +1 -1
- package/package.json +88 -10
- package/src/components/ui/alert-dialog.tsx +15 -8
- package/src/components/ui/avatar.tsx +9 -6
- package/src/components/ui/calendar.tsx +9 -14
- package/src/components/ui/carousel.tsx +2 -0
- package/src/components/ui/chart.tsx +65 -62
- package/src/components/ui/checkbox-group.tsx +4 -5
- package/src/components/ui/checkbox.tsx +10 -2
- package/src/components/ui/collapsible.tsx +1 -0
- package/src/components/ui/combobox.module.css +158 -0
- package/src/components/ui/combobox.tsx +569 -0
- package/src/components/ui/command.tsx +31 -15
- package/src/components/ui/context-menu.tsx +3 -0
- package/src/components/ui/drawer.tsx +2 -0
- package/src/components/ui/dropdown-menu.tsx +3 -0
- package/src/components/ui/dropdrawer.tsx +80 -62
- package/src/components/ui/form.tsx +28 -3
- package/src/components/ui/input-otp.tsx +3 -3
- package/src/components/ui/menubar.tsx +9 -10
- package/src/components/ui/meter.tsx +16 -17
- package/src/components/ui/navigation-menu.tsx +41 -33
- package/src/components/ui/number-field.tsx +6 -13
- package/src/components/ui/progress.tsx +3 -2
- package/src/components/ui/radio-group.tsx +2 -5
- package/src/components/ui/resizable.tsx +15 -18
- package/src/components/ui/scratcher.tsx +6 -10
- package/src/components/ui/scroll-area.tsx +2 -5
- package/src/components/ui/separator.tsx +4 -3
- package/src/components/ui/sheet.tsx +3 -0
- package/src/components/ui/sidebar.tsx +1 -0
- package/src/components/ui/{sonner.module.css → toast.module.css} +1 -1
- package/src/components/ui/{sonner.tsx → toast.tsx} +22 -14
- package/src/components/ui/toggle-group.tsx +6 -4
- package/src/components/ui/toolbar.tsx +20 -21
- package/src/hooks/useClipboard.tsx +137 -0
- package/src/hooks/useControllableState.tsx +81 -0
- package/src/hooks/useDebounce.tsx +50 -0
- package/src/hooks/useEventCallback.tsx +47 -0
- package/src/hooks/useId.tsx +36 -0
- package/src/hooks/useIntersectionObserver.tsx +81 -0
- package/src/hooks/useInterval.tsx +80 -0
- package/src/hooks/useLocalStorage.tsx +111 -0
- package/src/hooks/useMergedRefs.tsx +48 -0
- package/src/hooks/useOnClickOutside.tsx +55 -0
- package/src/hooks/usePrevious.tsx +44 -0
- package/src/hooks/useThrottle.tsx +78 -0
- package/src/hooks/useTimeout.tsx +51 -0
- package/src/index.ts +27 -4
- package/src/lib/utilities.ts +4 -4
- package/src/motion/tokens.ts +4 -4
- package/src/stories/DesignPrinciples.mdx +48 -0
- package/src/stories/GettingStarted.mdx +92 -0
- package/src/stories/Welcome.mdx +44 -0
- package/dist/components/ui/sonner.d.ts.map +0 -1
- package/dist/components/ui/sonner.js.map +0 -1
- package/dist/components/ui/sonner.module.js +0 -34
- package/dist/components/ui/sonner.module.js.map +0 -1
- package/dist/components/ui/sonner_module.css.map +0 -1
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import * as React from "react";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Options for the `useClipboard` hook.
|
|
7
|
+
*/
|
|
8
|
+
export interface UseClipboardOptions {
|
|
9
|
+
/**
|
|
10
|
+
* The duration in milliseconds after which the `copied` state resets to `false`.
|
|
11
|
+
*
|
|
12
|
+
* @defaultValue 2000
|
|
13
|
+
*/
|
|
14
|
+
timeout?: number;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Return type for the `useClipboard` hook.
|
|
19
|
+
*/
|
|
20
|
+
export interface UseClipboardReturn {
|
|
21
|
+
/**
|
|
22
|
+
* Whether the text was successfully copied to the clipboard.
|
|
23
|
+
*/
|
|
24
|
+
copied: boolean;
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Copies the provided text to the clipboard.
|
|
28
|
+
*
|
|
29
|
+
* @param text - The text to copy.
|
|
30
|
+
* @returns A promise that resolves when the copy operation completes.
|
|
31
|
+
*/
|
|
32
|
+
copy: (text: string) => Promise<void>;
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* The error that occurred during the copy operation, or `null` if no error occurred.
|
|
36
|
+
*/
|
|
37
|
+
error: Error | null;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Copies text to the clipboard with success/error state management.
|
|
42
|
+
*
|
|
43
|
+
* @remarks
|
|
44
|
+
* This hook provides a simple interface for copying text to the clipboard using
|
|
45
|
+
* the Clipboard API. It manages the `copied` state that automatically resets
|
|
46
|
+
* after a configurable timeout, and handles errors gracefully when the Clipboard
|
|
47
|
+
* API is unavailable or the operation fails.
|
|
48
|
+
*
|
|
49
|
+
* The hook is SSR-safe and will handle the case where `navigator.clipboard` is
|
|
50
|
+
* not available (e.g., in non-secure contexts or older browsers).
|
|
51
|
+
*
|
|
52
|
+
* @param options - Configuration options for the hook.
|
|
53
|
+
* @returns An object containing the copied state, copy function, and any error.
|
|
54
|
+
*
|
|
55
|
+
* @example
|
|
56
|
+
* ```tsx
|
|
57
|
+
* function CopyButton({textToCopy}: {textToCopy: string}) {
|
|
58
|
+
* const {copied, copy, error} = useClipboard({timeout: 3000});
|
|
59
|
+
*
|
|
60
|
+
* return (
|
|
61
|
+
* <div>
|
|
62
|
+
* <button onClick={() => copy(textToCopy)}>
|
|
63
|
+
* {copied ? "Copied!" : "Copy to clipboard"}
|
|
64
|
+
* </button>
|
|
65
|
+
* {error && <span>Failed to copy: {error.message}</span>}
|
|
66
|
+
* </div>
|
|
67
|
+
* );
|
|
68
|
+
* }
|
|
69
|
+
* ```
|
|
70
|
+
*
|
|
71
|
+
* @example
|
|
72
|
+
* ```tsx
|
|
73
|
+
* function ShareLink({url}: {url: string}) {
|
|
74
|
+
* const {copied, copy} = useClipboard();
|
|
75
|
+
*
|
|
76
|
+
* return (
|
|
77
|
+
* <button onClick={() => copy(url)} disabled={copied}>
|
|
78
|
+
* {copied ? "✓ Copied" : "Share link"}
|
|
79
|
+
* </button>
|
|
80
|
+
* );
|
|
81
|
+
* }
|
|
82
|
+
* ```
|
|
83
|
+
*/
|
|
84
|
+
export function useClipboard(options: UseClipboardOptions = {}): UseClipboardReturn {
|
|
85
|
+
const {timeout = 2000} = options;
|
|
86
|
+
|
|
87
|
+
const [copied, setCopied] = React.useState(false);
|
|
88
|
+
const [error, setError] = React.useState<Error | null>(null);
|
|
89
|
+
const timeoutRef = React.useRef<ReturnType<typeof setTimeout> | null>(null);
|
|
90
|
+
|
|
91
|
+
const copy = React.useCallback(
|
|
92
|
+
async (text: string): Promise<void> => {
|
|
93
|
+
// Clear any existing timeout
|
|
94
|
+
if (timeoutRef.current !== null) {
|
|
95
|
+
clearTimeout(timeoutRef.current);
|
|
96
|
+
timeoutRef.current = null;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// Reset error state
|
|
100
|
+
setError(null);
|
|
101
|
+
|
|
102
|
+
try {
|
|
103
|
+
// Check if Clipboard API is available
|
|
104
|
+
if (typeof globalThis.navigator === "undefined" || !globalThis.navigator.clipboard) {
|
|
105
|
+
throw new Error("Clipboard API is not available");
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
await globalThis.navigator.clipboard.writeText(text);
|
|
109
|
+
setCopied(true);
|
|
110
|
+
|
|
111
|
+
// Reset copied state after timeout
|
|
112
|
+
timeoutRef.current = setTimeout(() => {
|
|
113
|
+
setCopied(false);
|
|
114
|
+
timeoutRef.current = null;
|
|
115
|
+
}, timeout);
|
|
116
|
+
} catch (err) {
|
|
117
|
+
const errorMessage = err instanceof Error ? err : new Error("Failed to copy to clipboard");
|
|
118
|
+
|
|
119
|
+
setError(errorMessage);
|
|
120
|
+
setCopied(false);
|
|
121
|
+
console.error("Copy to clipboard failed:", errorMessage);
|
|
122
|
+
}
|
|
123
|
+
},
|
|
124
|
+
[timeout],
|
|
125
|
+
);
|
|
126
|
+
|
|
127
|
+
// Cleanup timeout on unmount
|
|
128
|
+
React.useEffect(() => {
|
|
129
|
+
return () => {
|
|
130
|
+
if (timeoutRef.current !== null) {
|
|
131
|
+
clearTimeout(timeoutRef.current);
|
|
132
|
+
}
|
|
133
|
+
};
|
|
134
|
+
}, []);
|
|
135
|
+
|
|
136
|
+
return {copied, copy, error};
|
|
137
|
+
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import * as React from "react";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Options for configuring controllable state behavior.
|
|
7
|
+
*
|
|
8
|
+
* @typeParam T - The type of the state value.
|
|
9
|
+
*/
|
|
10
|
+
export interface UseControllableStateOptions<T> {
|
|
11
|
+
/**
|
|
12
|
+
* The controlled value from props. When provided, the component operates in controlled mode.
|
|
13
|
+
*/
|
|
14
|
+
controlled?: T;
|
|
15
|
+
/**
|
|
16
|
+
* The default value used when the component is uncontrolled.
|
|
17
|
+
*/
|
|
18
|
+
defaultValue: T;
|
|
19
|
+
/**
|
|
20
|
+
* Callback fired when the internal state changes in controlled mode.
|
|
21
|
+
*
|
|
22
|
+
* @param value - The new state value.
|
|
23
|
+
*/
|
|
24
|
+
onChange?: (value: T) => void;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Manages state that can be either controlled or uncontrolled.
|
|
29
|
+
*
|
|
30
|
+
* @remarks
|
|
31
|
+
* This hook enables components to support both controlled and uncontrolled patterns
|
|
32
|
+
* seamlessly. When a `controlled` value is provided, it takes precedence; otherwise,
|
|
33
|
+
* the hook manages internal state initialized with `defaultValue`.
|
|
34
|
+
*
|
|
35
|
+
* @typeParam T - The type of the state value.
|
|
36
|
+
* @param options - Configuration object for controllable state.
|
|
37
|
+
* @returns A tuple containing the current state value and a setter function.
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* ```tsx
|
|
41
|
+
* function CustomInput({value, defaultValue = "", onChange}) {
|
|
42
|
+
* const [internalValue, setValue] = useControllableState({
|
|
43
|
+
* controlled: value,
|
|
44
|
+
* defaultValue,
|
|
45
|
+
* onChange,
|
|
46
|
+
* });
|
|
47
|
+
*
|
|
48
|
+
* return (
|
|
49
|
+
* <input
|
|
50
|
+
* type="text"
|
|
51
|
+
* value={internalValue}
|
|
52
|
+
* onChange={(e) => setValue(e.target.value)}
|
|
53
|
+
* />
|
|
54
|
+
* );
|
|
55
|
+
* }
|
|
56
|
+
* ```
|
|
57
|
+
*/
|
|
58
|
+
export function useControllableState<T>(options: UseControllableStateOptions<T>): [T, (value: T | ((prev: T) => T)) => void] {
|
|
59
|
+
const {controlled, defaultValue, onChange} = options;
|
|
60
|
+
const [uncontrolledState, setUncontrolledState] = React.useState<T>(defaultValue);
|
|
61
|
+
const isControlled = controlled !== undefined;
|
|
62
|
+
const value = isControlled ? controlled : uncontrolledState;
|
|
63
|
+
|
|
64
|
+
const setValue = React.useCallback(
|
|
65
|
+
(nextValue: T | ((prev: T) => T)) => {
|
|
66
|
+
if (!isControlled) {
|
|
67
|
+
setUncontrolledState((currentValue) => {
|
|
68
|
+
const resolvedValue = typeof nextValue === "function" ? (nextValue as (prev: T) => T)(currentValue) : nextValue;
|
|
69
|
+
onChange?.(resolvedValue);
|
|
70
|
+
return resolvedValue;
|
|
71
|
+
});
|
|
72
|
+
} else {
|
|
73
|
+
const resolvedValue = typeof nextValue === "function" ? (nextValue as (prev: T) => T)(controlled as T) : nextValue;
|
|
74
|
+
onChange?.(resolvedValue);
|
|
75
|
+
}
|
|
76
|
+
},
|
|
77
|
+
[isControlled, onChange, controlled],
|
|
78
|
+
);
|
|
79
|
+
|
|
80
|
+
return [value, setValue];
|
|
81
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import * as React from "react";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Debounces a value, delaying updates until after the specified delay has elapsed.
|
|
7
|
+
*
|
|
8
|
+
* @remarks
|
|
9
|
+
* This hook returns a debounced version of the provided value that only updates
|
|
10
|
+
* after the value has stopped changing for the specified delay. Useful for optimizing
|
|
11
|
+
* performance in scenarios like search inputs, where you want to avoid triggering
|
|
12
|
+
* expensive operations on every keystroke.
|
|
13
|
+
*
|
|
14
|
+
* The debounce timer resets on every value change and cleans up automatically on unmount.
|
|
15
|
+
*
|
|
16
|
+
* @typeParam T - The type of the value being debounced.
|
|
17
|
+
* @param value - The value to debounce.
|
|
18
|
+
* @param delay - The delay in milliseconds before the debounced value updates.
|
|
19
|
+
* @returns The debounced value.
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* ```tsx
|
|
23
|
+
* function SearchInput() {
|
|
24
|
+
* const [searchTerm, setSearchTerm] = useState("");
|
|
25
|
+
* const debouncedSearchTerm = useDebounce(searchTerm, 500);
|
|
26
|
+
*
|
|
27
|
+
* useEffect(() => {
|
|
28
|
+
* // Expensive search operation
|
|
29
|
+
* performSearch(debouncedSearchTerm);
|
|
30
|
+
* }, [debouncedSearchTerm]);
|
|
31
|
+
*
|
|
32
|
+
* return <input value={searchTerm} onChange={(e) => setSearchTerm(e.target.value)} />;
|
|
33
|
+
* }
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
36
|
+
export function useDebounce<T>(value: T, delay: number): T {
|
|
37
|
+
const [debouncedValue, setDebouncedValue] = React.useState<T>(value);
|
|
38
|
+
|
|
39
|
+
React.useEffect(() => {
|
|
40
|
+
const timeoutId = globalThis.setTimeout(() => {
|
|
41
|
+
setDebouncedValue(value);
|
|
42
|
+
}, delay);
|
|
43
|
+
|
|
44
|
+
return () => {
|
|
45
|
+
globalThis.clearTimeout(timeoutId);
|
|
46
|
+
};
|
|
47
|
+
}, [value, delay]);
|
|
48
|
+
|
|
49
|
+
return debouncedValue;
|
|
50
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import * as React from "react";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Creates a stable callback reference that always calls the latest version of the provided function.
|
|
7
|
+
*
|
|
8
|
+
* @remarks
|
|
9
|
+
* Unlike `useCallback`, this hook returns a stable function reference that never changes,
|
|
10
|
+
* but always invokes the most recent version of the callback. This is useful when you need
|
|
11
|
+
* to pass callbacks to optimized child components or effects without triggering re-renders
|
|
12
|
+
* when dependencies change.
|
|
13
|
+
*
|
|
14
|
+
* The returned function is safe to use in dependency arrays because its identity never changes.
|
|
15
|
+
*
|
|
16
|
+
* @typeParam Args - The tuple type of the callback's arguments.
|
|
17
|
+
* @typeParam Return - The return type of the callback.
|
|
18
|
+
* @param callback - The function to wrap with a stable reference.
|
|
19
|
+
* @returns A stable function reference that invokes the latest callback.
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* ```tsx
|
|
23
|
+
* function SearchInput({onSearch}) {
|
|
24
|
+
* const [query, setQuery] = useState("");
|
|
25
|
+
* // stableOnSearch never changes identity, but always calls the latest onSearch
|
|
26
|
+
* const stableOnSearch = useEventCallback(onSearch);
|
|
27
|
+
*
|
|
28
|
+
* useEffect(() => {
|
|
29
|
+
* const timer = setTimeout(() => stableOnSearch(query), 500);
|
|
30
|
+
* return () => clearTimeout(timer);
|
|
31
|
+
* }, [query, stableOnSearch]); // Safe to include in deps
|
|
32
|
+
*
|
|
33
|
+
* return <input value={query} onChange={(e) => setQuery(e.target.value)} />;
|
|
34
|
+
* }
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
37
|
+
export function useEventCallback<Args extends unknown[], Return>(callback: (...args: Args) => Return): (...args: Args) => Return {
|
|
38
|
+
const callbackRef = React.useRef(callback);
|
|
39
|
+
|
|
40
|
+
React.useLayoutEffect(() => {
|
|
41
|
+
callbackRef.current = callback;
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
return React.useCallback((...args: Args) => {
|
|
45
|
+
return callbackRef.current(...args);
|
|
46
|
+
}, []);
|
|
47
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import * as React from "react";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Generates a unique, stable identifier that is safe for server-side rendering.
|
|
7
|
+
*
|
|
8
|
+
* @remarks
|
|
9
|
+
* This hook wraps React's `useId` and optionally prepends a custom prefix.
|
|
10
|
+
* The generated ID remains stable across re-renders and matches between server
|
|
11
|
+
* and client, making it ideal for associating form labels with inputs or
|
|
12
|
+
* managing accessible ARIA relationships.
|
|
13
|
+
*
|
|
14
|
+
* @param prefix - Optional string to prepend to the generated ID.
|
|
15
|
+
* @returns A unique identifier string.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```tsx
|
|
19
|
+
* function FormField({label}) {
|
|
20
|
+
* const id = useId("field");
|
|
21
|
+
*
|
|
22
|
+
* return (
|
|
23
|
+
* <div>
|
|
24
|
+
* <label htmlFor={id}>{label}</label>
|
|
25
|
+
* <input id={id} type="text" />
|
|
26
|
+
* </div>
|
|
27
|
+
* );
|
|
28
|
+
* }
|
|
29
|
+
* ```
|
|
30
|
+
*
|
|
31
|
+
* @see {@link https://react.dev/reference/react/useId | React useId}
|
|
32
|
+
*/
|
|
33
|
+
export function useId(prefix?: string): string {
|
|
34
|
+
const reactId = React.useId();
|
|
35
|
+
return prefix ? `${prefix}-${reactId}` : reactId;
|
|
36
|
+
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import * as React from "react";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Observes element visibility using the Intersection Observer API.
|
|
7
|
+
*
|
|
8
|
+
* @remarks
|
|
9
|
+
* This hook creates an IntersectionObserver that watches the provided element
|
|
10
|
+
* reference and returns the latest `IntersectionObserverEntry`. It's useful
|
|
11
|
+
* for implementing lazy loading, infinite scroll, animations on scroll, and
|
|
12
|
+
* tracking element visibility.
|
|
13
|
+
*
|
|
14
|
+
* The observer automatically disconnects when the component unmounts or when
|
|
15
|
+
* the element reference changes. The hook is SSR-safe and returns `null` when
|
|
16
|
+
* running on the server or when the observer is not yet initialized.
|
|
17
|
+
*
|
|
18
|
+
* @param ref - A React ref object pointing to the element to observe.
|
|
19
|
+
* @param options - Optional IntersectionObserver configuration (threshold, root, rootMargin).
|
|
20
|
+
* @returns The latest IntersectionObserverEntry or null if not intersecting yet.
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* ```tsx
|
|
24
|
+
* function LazyImage({src, alt}: {src: string; alt: string}) {
|
|
25
|
+
* const imageRef = useRef<HTMLImageElement>(null);
|
|
26
|
+
* const entry = useIntersectionObserver(imageRef, {threshold: 0.1});
|
|
27
|
+
*
|
|
28
|
+
* return (
|
|
29
|
+
* <img
|
|
30
|
+
* ref={imageRef}
|
|
31
|
+
* src={entry?.isIntersecting ? src : undefined}
|
|
32
|
+
* alt={alt}
|
|
33
|
+
* />
|
|
34
|
+
* );
|
|
35
|
+
* }
|
|
36
|
+
* ```
|
|
37
|
+
*
|
|
38
|
+
* @example
|
|
39
|
+
* ```tsx
|
|
40
|
+
* function AnimateOnScroll({children}: {children: React.ReactNode}) {
|
|
41
|
+
* const ref = useRef<HTMLDivElement>(null);
|
|
42
|
+
* const entry = useIntersectionObserver(ref, {threshold: 0.5});
|
|
43
|
+
* const isVisible = entry?.isIntersecting ?? false;
|
|
44
|
+
*
|
|
45
|
+
* return (
|
|
46
|
+
* <div ref={ref} className={isVisible ? "fade-in" : "hidden"}>
|
|
47
|
+
* {children}
|
|
48
|
+
* </div>
|
|
49
|
+
* );
|
|
50
|
+
* }
|
|
51
|
+
* ```
|
|
52
|
+
*/
|
|
53
|
+
export function useIntersectionObserver(
|
|
54
|
+
ref: React.RefObject<Element | null>,
|
|
55
|
+
options?: IntersectionObserverInit,
|
|
56
|
+
): IntersectionObserverEntry | null {
|
|
57
|
+
const [entry, setEntry] = React.useState<IntersectionObserverEntry | null>(null);
|
|
58
|
+
|
|
59
|
+
React.useEffect(() => {
|
|
60
|
+
const element = ref.current;
|
|
61
|
+
|
|
62
|
+
// SSR safety: IntersectionObserver is not available on server
|
|
63
|
+
if (typeof globalThis.IntersectionObserver === "undefined" || !element) {
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const observer = new globalThis.IntersectionObserver(([observerEntry]) => {
|
|
68
|
+
if (observerEntry) {
|
|
69
|
+
setEntry(observerEntry);
|
|
70
|
+
}
|
|
71
|
+
}, options);
|
|
72
|
+
|
|
73
|
+
observer.observe(element);
|
|
74
|
+
|
|
75
|
+
return () => {
|
|
76
|
+
observer.disconnect();
|
|
77
|
+
};
|
|
78
|
+
}, [ref, options?.threshold, options?.root, options?.rootMargin]);
|
|
79
|
+
|
|
80
|
+
return entry;
|
|
81
|
+
}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import * as React from "react";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Executes a callback function at specified intervals with automatic cleanup.
|
|
7
|
+
*
|
|
8
|
+
* @remarks
|
|
9
|
+
* This hook provides a declarative interface for `setInterval` that automatically
|
|
10
|
+
* handles cleanup on unmount and ensures the latest callback is always invoked
|
|
11
|
+
* (preventing stale closures). Setting the delay to `null` pauses the interval,
|
|
12
|
+
* which is useful for implementing play/pause functionality.
|
|
13
|
+
*
|
|
14
|
+
* Unlike raw `setInterval`, this hook guarantees that the interval is cleared
|
|
15
|
+
* when the component unmounts or when the delay changes, preventing memory leaks
|
|
16
|
+
* and unexpected behavior.
|
|
17
|
+
*
|
|
18
|
+
* @param callback - The function to execute at each interval.
|
|
19
|
+
* @param delay - The interval delay in milliseconds, or `null` to pause the interval.
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* ```tsx
|
|
23
|
+
* function Timer() {
|
|
24
|
+
* const [count, setCount] = useState(0);
|
|
25
|
+
*
|
|
26
|
+
* useInterval(() => {
|
|
27
|
+
* setCount((c) => c + 1);
|
|
28
|
+
* }, 1000);
|
|
29
|
+
*
|
|
30
|
+
* return <div>Count: {count}</div>;
|
|
31
|
+
* }
|
|
32
|
+
* ```
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* ```tsx
|
|
36
|
+
* function PausableTimer() {
|
|
37
|
+
* const [count, setCount] = useState(0);
|
|
38
|
+
* const [isRunning, setIsRunning] = useState(true);
|
|
39
|
+
*
|
|
40
|
+
* useInterval(
|
|
41
|
+
* () => {
|
|
42
|
+
* setCount((c) => c + 1);
|
|
43
|
+
* },
|
|
44
|
+
* isRunning ? 1000 : null,
|
|
45
|
+
* );
|
|
46
|
+
*
|
|
47
|
+
* return (
|
|
48
|
+
* <div>
|
|
49
|
+
* <div>Count: {count}</div>
|
|
50
|
+
* <button onClick={() => setIsRunning(!isRunning)}>
|
|
51
|
+
* {isRunning ? "Pause" : "Resume"}
|
|
52
|
+
* </button>
|
|
53
|
+
* </div>
|
|
54
|
+
* );
|
|
55
|
+
* }
|
|
56
|
+
* ```
|
|
57
|
+
*/
|
|
58
|
+
export function useInterval(callback: () => void, delay: number | null): void {
|
|
59
|
+
const savedCallback = React.useRef(callback);
|
|
60
|
+
|
|
61
|
+
// Update ref to latest callback on every render to avoid stale closures
|
|
62
|
+
React.useEffect(() => {
|
|
63
|
+
savedCallback.current = callback;
|
|
64
|
+
}, [callback]);
|
|
65
|
+
|
|
66
|
+
React.useEffect(() => {
|
|
67
|
+
// Don't schedule if delay is null
|
|
68
|
+
if (delay === null) {
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const intervalId = globalThis.setInterval(() => {
|
|
73
|
+
savedCallback.current();
|
|
74
|
+
}, delay);
|
|
75
|
+
|
|
76
|
+
return () => {
|
|
77
|
+
globalThis.clearInterval(intervalId);
|
|
78
|
+
};
|
|
79
|
+
}, [delay]);
|
|
80
|
+
}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import * as React from "react";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Persists state to localStorage with SSR safety and JSON serialization.
|
|
7
|
+
*
|
|
8
|
+
* @remarks
|
|
9
|
+
* This hook synchronizes state with localStorage, allowing data to persist
|
|
10
|
+
* across page refreshes and browser sessions. It is SSR-safe and returns the
|
|
11
|
+
* initial value on the server until hydration completes. The hook also syncs
|
|
12
|
+
* state across tabs/windows via the `storage` event and handles JSON parse
|
|
13
|
+
* errors gracefully by falling back to the initial value.
|
|
14
|
+
*
|
|
15
|
+
* @typeParam T - The type of the value being stored.
|
|
16
|
+
* @param key - The localStorage key to store the value under.
|
|
17
|
+
* @param initialValue - The default value to use if no value is found in localStorage.
|
|
18
|
+
* @returns A tuple containing the current value and a setter function.
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* ```tsx
|
|
22
|
+
* function UserSettings() {
|
|
23
|
+
* const [theme, setTheme] = useLocalStorage("theme", "light");
|
|
24
|
+
*
|
|
25
|
+
* return (
|
|
26
|
+
* <button onClick={() => setTheme(theme === "light" ? "dark" : "light")}>
|
|
27
|
+
* Toggle theme (current: {theme})
|
|
28
|
+
* </button>
|
|
29
|
+
* );
|
|
30
|
+
* }
|
|
31
|
+
* ```
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* ```tsx
|
|
35
|
+
* function ShoppingCart() {
|
|
36
|
+
* const [cart, setCart] = useLocalStorage<Product[]>("cart", []);
|
|
37
|
+
*
|
|
38
|
+
* return (
|
|
39
|
+
* <button onClick={() => setCart((prev) => [...prev, newProduct])}>
|
|
40
|
+
* Add to cart ({cart.length} items)
|
|
41
|
+
* </button>
|
|
42
|
+
* );
|
|
43
|
+
* }
|
|
44
|
+
* ```
|
|
45
|
+
*/
|
|
46
|
+
export function useLocalStorage<T>(key: string, initialValue: T): [T, (value: T | ((prev: T) => T)) => void] {
|
|
47
|
+
const [storedValue, setStoredValue] = React.useState<T>(() => {
|
|
48
|
+
// SSR safety: return initial value on server
|
|
49
|
+
if (typeof globalThis.window === "undefined") {
|
|
50
|
+
return initialValue;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
try {
|
|
54
|
+
const item = globalThis.window.localStorage.getItem(key);
|
|
55
|
+
|
|
56
|
+
return item !== null ? (JSON.parse(item) as T) : initialValue;
|
|
57
|
+
} catch (error) {
|
|
58
|
+
console.error(`Error reading localStorage key "${key}":`, error);
|
|
59
|
+
|
|
60
|
+
return initialValue;
|
|
61
|
+
}
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
const setValue = React.useCallback(
|
|
65
|
+
(value: T | ((prev: T) => T)) => {
|
|
66
|
+
try {
|
|
67
|
+
setStoredValue((currentValue) => {
|
|
68
|
+
const valueToStore = value instanceof Function ? value(currentValue) : value;
|
|
69
|
+
|
|
70
|
+
if (typeof globalThis.window !== "undefined") {
|
|
71
|
+
globalThis.window.localStorage.setItem(key, JSON.stringify(valueToStore));
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
return valueToStore;
|
|
75
|
+
});
|
|
76
|
+
} catch (error) {
|
|
77
|
+
console.error(`Error setting localStorage key "${key}":`, error);
|
|
78
|
+
}
|
|
79
|
+
},
|
|
80
|
+
[key],
|
|
81
|
+
);
|
|
82
|
+
|
|
83
|
+
React.useEffect(() => {
|
|
84
|
+
// SSR safety: window is not available on server
|
|
85
|
+
if (typeof globalThis.window === "undefined") {
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const handleStorageChange = (event: StorageEvent) => {
|
|
90
|
+
if (event.key !== key || event.storageArea !== globalThis.window.localStorage) {
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
try {
|
|
95
|
+
const newValue = event.newValue !== null ? (JSON.parse(event.newValue) as T) : initialValue;
|
|
96
|
+
|
|
97
|
+
setStoredValue(newValue);
|
|
98
|
+
} catch (error) {
|
|
99
|
+
console.error(`Error parsing storage event for key "${key}":`, error);
|
|
100
|
+
}
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
globalThis.window.addEventListener("storage", handleStorageChange);
|
|
104
|
+
|
|
105
|
+
return () => {
|
|
106
|
+
globalThis.window.removeEventListener("storage", handleStorageChange);
|
|
107
|
+
};
|
|
108
|
+
}, [key, initialValue]);
|
|
109
|
+
|
|
110
|
+
return [storedValue, setValue];
|
|
111
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import * as React from "react";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Merges multiple refs into a single callback ref.
|
|
7
|
+
*
|
|
8
|
+
* @remarks
|
|
9
|
+
* This hook is essential when you need to attach multiple refs to the same element,
|
|
10
|
+
* such as combining a forwarded ref with an internal ref for measurements or
|
|
11
|
+
* imperative operations. All provided refs will receive the same element instance.
|
|
12
|
+
*
|
|
13
|
+
* Supports all ref types: callback refs, mutable ref objects, and `null`/`undefined`.
|
|
14
|
+
*
|
|
15
|
+
* @typeParam T - The type of the element being referenced.
|
|
16
|
+
* @param refs - An array of refs to merge. Can include callback refs, ref objects, or undefined.
|
|
17
|
+
* @returns A callback ref that updates all provided refs.
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* ```tsx
|
|
21
|
+
* const MyComponent = React.forwardRef<HTMLDivElement, Props>((props, forwardedRef) => {
|
|
22
|
+
* const internalRef = useRef<HTMLDivElement>(null);
|
|
23
|
+
* const mergedRef = useMergedRefs(forwardedRef, internalRef);
|
|
24
|
+
*
|
|
25
|
+
* return <div ref={mergedRef}>Content</div>;
|
|
26
|
+
* });
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
export function useMergedRefs<T>(...refs: Array<React.Ref<T> | undefined>): React.RefCallback<T> {
|
|
30
|
+
return React.useCallback(
|
|
31
|
+
(element: T | null) => {
|
|
32
|
+
for (const ref of refs) {
|
|
33
|
+
if (!ref) {
|
|
34
|
+
continue;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
if (typeof ref === "function") {
|
|
38
|
+
ref(element);
|
|
39
|
+
} else {
|
|
40
|
+
// Mutable ref object
|
|
41
|
+
(ref as React.MutableRefObject<T | null>).current = element;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
},
|
|
45
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
46
|
+
refs,
|
|
47
|
+
);
|
|
48
|
+
}
|