@donotdev/components 0.3.1 → 0.3.2
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/advanced/Bento/Bento.js +1 -37
- package/dist/advanced/Bento/index.js +1 -2
- package/dist/advanced/Carousel/Carousel.js +1 -36
- package/dist/advanced/Carousel/CarouselContent.d.ts.map +1 -1
- package/dist/advanced/Carousel/CarouselContent.js +1 -653
- package/dist/advanced/Carousel/CarouselSkeleton.js +1 -51
- package/dist/advanced/Carousel/index.js +1 -2
- package/dist/advanced/Code/Code.js +2 -35
- package/dist/advanced/Code/CodeContent.d.ts.map +1 -1
- package/dist/advanced/Code/CodeContent.js +2 -182
- package/dist/advanced/Code/CodeSkeleton.js +1 -51
- package/dist/advanced/Code/index.js +1 -2
- package/dist/advanced/Crawl/Crawl.d.ts.map +1 -1
- package/dist/advanced/Crawl/Crawl.js +1 -84
- package/dist/advanced/Crawl/CrawlContent.d.ts.map +1 -1
- package/dist/advanced/Crawl/CrawlContent.js +1 -118
- package/dist/advanced/Crawl/index.js +1 -3
- package/dist/advanced/Crawl/utils/parseHtml.js +1 -159
- package/dist/advanced/ImageGallery/GalleryArrows.d.ts.map +1 -1
- package/dist/advanced/ImageGallery/GalleryArrows.js +1 -13
- package/dist/advanced/ImageGallery/ImageGallery.d.ts.map +1 -1
- package/dist/advanced/ImageGallery/ImageGallery.js +1 -181
- package/dist/advanced/ImageGallery/LightboxViewer.js +1 -138
- package/dist/advanced/ImageGallery/index.js +1 -2
- package/dist/advanced/ImageGallery/lightboxGestures.js +1 -425
- package/dist/advanced/ImageGallery/protectImages.js +1 -10
- package/dist/advanced/ImageGallery/snapStrip.d.ts.map +1 -1
- package/dist/advanced/ImageGallery/snapStrip.js +1 -74
- package/dist/advanced/JsonViewer/JsonViewer.js +1 -141
- package/dist/advanced/JsonViewer/index.js +1 -3
- package/dist/advanced/Marquee/Marquee.js +1 -35
- package/dist/advanced/Marquee/MarqueeContent.d.ts.map +1 -1
- package/dist/advanced/Marquee/MarqueeContent.js +1 -193
- package/dist/advanced/Marquee/MarqueeSkeleton.js +1 -36
- package/dist/advanced/Marquee/index.js +1 -3
- package/dist/advanced/Reveal/Reveal.d.ts.map +1 -1
- package/dist/advanced/Reveal/Reveal.js +1 -60
- package/dist/advanced/Reveal/index.js +1 -3
- package/dist/advanced/Roadmap/Roadmap.js +1 -6
- package/dist/advanced/Roadmap/RoadmapContent.d.ts.map +1 -1
- package/dist/advanced/Roadmap/RoadmapContent.js +1 -59
- package/dist/advanced/Roadmap/index.js +1 -3
- package/dist/advanced/SplitReveal/SplitReveal.js +1 -34
- package/dist/advanced/SplitReveal/index.js +1 -2
- package/dist/advanced/StackedCards/StackedCards.js +1 -22
- package/dist/advanced/StackedCards/StackedCardsContent.d.ts.map +1 -1
- package/dist/advanced/StackedCards/StackedCardsContent.js +1 -42
- package/dist/advanced/StackedCards/index.js +1 -3
- package/dist/advanced/index.js +1 -20
- package/dist/atomic/Accordion/AccordionPrimitive.d.ts +5 -1
- package/dist/atomic/Accordion/AccordionPrimitive.d.ts.map +1 -1
- package/dist/atomic/Accordion/AccordionPrimitive.js +1 -29
- package/dist/atomic/Accordion/index.d.ts +9 -1
- package/dist/atomic/Accordion/index.d.ts.map +1 -1
- package/dist/atomic/Accordion/index.js +1 -38
- package/dist/atomic/ActionButton/index.js +1 -70
- package/dist/atomic/ActionButton/useAction.d.ts.map +1 -1
- package/dist/atomic/ActionButton/useAction.js +1 -64
- package/dist/atomic/Alert/index.d.ts +1 -1
- package/dist/atomic/Alert/index.js +1 -78
- package/dist/atomic/AlertDialog/AlertDialogPrimitive.d.ts.map +1 -1
- package/dist/atomic/AlertDialog/AlertDialogPrimitive.js +1 -25
- package/dist/atomic/AlertDialog/index.d.ts.map +1 -1
- package/dist/atomic/AlertDialog/index.js +1 -36
- package/dist/atomic/Avatar/AvatarPrimitive.d.ts.map +1 -1
- package/dist/atomic/Avatar/AvatarPrimitive.js +1 -16
- package/dist/atomic/Avatar/index.js +1 -17
- package/dist/atomic/Badge/index.d.ts +1 -1
- package/dist/atomic/Badge/index.js +1 -84
- package/dist/atomic/Blockquote/index.js +1 -72
- package/dist/atomic/Button/index.d.ts +1 -1
- package/dist/atomic/Button/index.js +1 -173
- package/dist/atomic/ButtonGroup/index.d.ts.map +1 -1
- package/dist/atomic/ButtonGroup/index.js +1 -57
- package/dist/atomic/Calendar/CalendarPrimitive.d.ts.map +1 -1
- package/dist/atomic/Calendar/CalendarPrimitive.js +1 -10
- package/dist/atomic/Calendar/LazyCalendar.d.ts +38 -0
- package/dist/atomic/Calendar/LazyCalendar.d.ts.map +1 -0
- package/dist/atomic/Calendar/LazyCalendar.js +1 -0
- package/dist/atomic/Calendar/index.d.ts.map +1 -1
- package/dist/atomic/Calendar/index.js +1 -74
- package/dist/atomic/CallToAction/index.js +1 -78
- package/dist/atomic/Card/cover.js +1 -40
- package/dist/atomic/Card/index.d.ts +12 -2
- package/dist/atomic/Card/index.d.ts.map +1 -1
- package/dist/atomic/Card/index.js +1 -190
- package/dist/atomic/Checkbox/CheckboxPrimitive.d.ts.map +1 -1
- package/dist/atomic/Checkbox/CheckboxPrimitive.js +1 -26
- package/dist/atomic/Checkbox/index.js +1 -69
- package/dist/atomic/Collapsible/CollapsiblePrimitive.d.ts.map +1 -1
- package/dist/atomic/Collapsible/CollapsiblePrimitive.js +1 -25
- package/dist/atomic/Collapsible/index.d.ts.map +1 -1
- package/dist/atomic/Collapsible/index.js +1 -61
- package/dist/atomic/ColorPicker/index.d.ts.map +1 -1
- package/dist/atomic/ColorPicker/index.js +1 -196
- package/dist/atomic/Combobox/index.d.ts.map +1 -1
- package/dist/atomic/Combobox/index.js +1 -278
- package/dist/atomic/Command/index.d.ts.map +1 -1
- package/dist/atomic/Command/index.js +1 -115
- package/dist/atomic/CommandDialog/index.d.ts.map +1 -1
- package/dist/atomic/CommandDialog/index.js +1 -34
- package/dist/atomic/ContextMenu/ContextMenuPrimitive.d.ts.map +1 -1
- package/dist/atomic/ContextMenu/ContextMenuPrimitive.js +1 -32
- package/dist/atomic/ContextMenu/index.d.ts.map +1 -1
- package/dist/atomic/ContextMenu/index.js +1 -52
- package/dist/atomic/CopyToClipboard/index.d.ts.map +1 -1
- package/dist/atomic/CopyToClipboard/index.js +1 -61
- package/dist/atomic/DatePicker/index.d.ts +3 -1
- package/dist/atomic/DatePicker/index.d.ts.map +1 -1
- package/dist/atomic/DatePicker/index.js +1 -377
- package/dist/atomic/DateTimePicker/index.d.ts +3 -1
- package/dist/atomic/DateTimePicker/index.d.ts.map +1 -1
- package/dist/atomic/DateTimePicker/index.js +1 -460
- package/dist/atomic/DescriptionList/index.js +1 -30
- package/dist/atomic/Dialog/DialogPrimitive.d.ts.map +1 -1
- package/dist/atomic/Dialog/DialogPrimitive.js +1 -20
- package/dist/atomic/Dialog/index.d.ts.map +1 -1
- package/dist/atomic/Dialog/index.js +1 -63
- package/dist/atomic/DropdownMenu/DropdownMenuPrimitive.d.ts.map +1 -1
- package/dist/atomic/DropdownMenu/DropdownMenuPrimitive.js +1 -34
- package/dist/atomic/DropdownMenu/index.d.ts.map +1 -1
- package/dist/atomic/DropdownMenu/index.js +1 -121
- package/dist/atomic/DualCard/index.d.ts.map +1 -1
- package/dist/atomic/DualCard/index.js +1 -42
- package/dist/atomic/FeatureFallback/index.js +1 -32
- package/dist/atomic/FileButton/index.d.ts.map +1 -1
- package/dist/atomic/FileButton/index.js +1 -35
- package/dist/atomic/Grid/GridArea.js +1 -70
- package/dist/atomic/Grid/index.js +1 -118
- package/dist/atomic/HeroSection/index.d.ts +1 -1
- package/dist/atomic/HeroSection/index.js +1 -138
- package/dist/atomic/HoverCard/HoverCardPrimitive.d.ts.map +1 -1
- package/dist/atomic/HoverCard/HoverCardPrimitive.js +1 -19
- package/dist/atomic/HoverCard/index.d.ts.map +1 -1
- package/dist/atomic/HoverCard/index.js +1 -57
- package/dist/atomic/Icons/Icon.js +1 -37
- package/dist/atomic/Icons/IconBox.js +1 -49
- package/dist/atomic/Icons/Partner/AppleIcon.js +1 -31
- package/dist/atomic/Icons/Partner/DiscordIcon.js +1 -15
- package/dist/atomic/Icons/Partner/FacebookIcon.js +1 -15
- package/dist/atomic/Icons/Partner/GitHubIcon.js +1 -15
- package/dist/atomic/Icons/Partner/GoogleIcon.js +1 -15
- package/dist/atomic/Icons/Partner/InstagramIcon.js +1 -29
- package/dist/atomic/Icons/Partner/LinkedinIcon.js +1 -20
- package/dist/atomic/Icons/Partner/MastodonIcon.js +1 -19
- package/dist/atomic/Icons/Partner/MediumIcon.js +1 -19
- package/dist/atomic/Icons/Partner/MicrosoftIcon.js +1 -15
- package/dist/atomic/Icons/Partner/NotionIcon.js +1 -15
- package/dist/atomic/Icons/Partner/RedditIcon.js +1 -15
- package/dist/atomic/Icons/Partner/SlackIcon.js +1 -15
- package/dist/atomic/Icons/Partner/SpotifyIcon.js +1 -15
- package/dist/atomic/Icons/Partner/SupabaseIcon.js +1 -16
- package/dist/atomic/Icons/Partner/TwitchIcon.js +1 -15
- package/dist/atomic/Icons/Partner/XIcon.js +1 -15
- package/dist/atomic/Icons/Partner/YahooIcon.js +1 -15
- package/dist/atomic/Icons/Partner/YouTubeIcon.js +1 -19
- package/dist/atomic/Icons/Partner/index.js +1 -25
- package/dist/atomic/Icons/Partner/providerIcons.js +1 -98
- package/dist/atomic/Icons/index.js +1 -8
- package/dist/atomic/InfiniteScroll/index.d.ts.map +1 -1
- package/dist/atomic/InfiniteScroll/index.js +1 -53
- package/dist/atomic/Input/index.d.ts.map +1 -1
- package/dist/atomic/Input/index.js +1 -63
- package/dist/atomic/Label/FieldInfo.js +1 -34
- package/dist/atomic/Label/FloatingLabel.js +1 -35
- package/dist/atomic/Label/LabelPrimitive.d.ts.map +1 -1
- package/dist/atomic/Label/LabelPrimitive.js +1 -16
- package/dist/atomic/Label/index.js +1 -23
- package/dist/atomic/List/index.js +1 -48
- package/dist/atomic/NavigationMenu/NavigationMenuPrimitive.d.ts.map +1 -1
- package/dist/atomic/NavigationMenu/NavigationMenuPrimitive.js +1 -28
- package/dist/atomic/NavigationMenu/index.js +1 -72
- package/dist/atomic/Pagination/index.d.ts.map +1 -1
- package/dist/atomic/Pagination/index.js +1 -107
- package/dist/atomic/PasswordInput/index.d.ts.map +1 -1
- package/dist/atomic/PasswordInput/index.js +1 -66
- package/dist/atomic/PinInput/index.d.ts.map +1 -1
- package/dist/atomic/PinInput/index.js +1 -155
- package/dist/atomic/Popover/PopoverPrimitive.d.ts.map +1 -1
- package/dist/atomic/Popover/PopoverPrimitive.js +1 -29
- package/dist/atomic/Popover/index.d.ts.map +1 -1
- package/dist/atomic/Popover/index.js +1 -38
- package/dist/atomic/Portal/PortalPrimitive.d.ts.map +1 -1
- package/dist/atomic/Portal/PortalPrimitive.js +1 -21
- package/dist/atomic/Portal/index.js +1 -10
- package/dist/atomic/PortalButton/index.js +1 -33
- package/dist/atomic/Progress/ProgressPrimitive.d.ts.map +1 -1
- package/dist/atomic/Progress/ProgressPrimitive.js +1 -28
- package/dist/atomic/Progress/index.js +1 -15
- package/dist/atomic/QuantityStepper/index.js +1 -239
- package/dist/atomic/QuantityStepper/quantity.js +1 -35
- package/dist/atomic/RadioGroup/RadioGroupPrimitive.d.ts.map +1 -1
- package/dist/atomic/RadioGroup/RadioGroupPrimitive.js +1 -17
- package/dist/atomic/RadioGroup/index.js +1 -63
- package/dist/atomic/RangeInput/index.d.ts.map +1 -1
- package/dist/atomic/RangeInput/index.js +1 -62
- package/dist/atomic/Rating/index.d.ts +1 -1
- package/dist/atomic/Rating/index.d.ts.map +1 -1
- package/dist/atomic/Rating/index.js +1 -115
- package/dist/atomic/ScrollArea/ScrollAreaPrimitive.d.ts.map +1 -1
- package/dist/atomic/ScrollArea/ScrollAreaPrimitive.js +1 -18
- package/dist/atomic/ScrollArea/index.js +1 -22
- package/dist/atomic/Section/index.d.ts +6 -0
- package/dist/atomic/Section/index.d.ts.map +1 -1
- package/dist/atomic/Section/index.js +1 -119
- package/dist/atomic/Select/SelectPrimitive.d.ts.map +1 -1
- package/dist/atomic/Select/SelectPrimitive.js +1 -27
- package/dist/atomic/Select/index.d.ts.map +1 -1
- package/dist/atomic/Select/index.js +1 -58
- package/dist/atomic/Separator/SeparatorPrimitive.d.ts.map +1 -1
- package/dist/atomic/Separator/SeparatorPrimitive.js +1 -15
- package/dist/atomic/Separator/index.js +1 -47
- package/dist/atomic/Sheet/SheetPrimitive.d.ts.map +1 -1
- package/dist/atomic/Sheet/SheetPrimitive.js +1 -24
- package/dist/atomic/Sheet/index.d.ts.map +1 -1
- package/dist/atomic/Sheet/index.js +1 -66
- package/dist/atomic/Skeleton/index.js +1 -73
- package/dist/atomic/SkipLink/index.d.ts.map +1 -1
- package/dist/atomic/SkipLink/index.js +1 -45
- package/dist/atomic/Slider/SliderPrimitive.d.ts.map +1 -1
- package/dist/atomic/Slider/SliderPrimitive.js +1 -35
- package/dist/atomic/Slider/index.d.ts +11 -2
- package/dist/atomic/Slider/index.d.ts.map +1 -1
- package/dist/atomic/Slider/index.js +1 -59
- package/dist/atomic/Slot/SlotPrimitive.d.ts.map +1 -1
- package/dist/atomic/Slot/SlotPrimitive.js +1 -10
- package/dist/atomic/Slot/index.js +1 -21
- package/dist/atomic/Spinner/index.js +1 -72
- package/dist/atomic/Stack/index.js +1 -84
- package/dist/atomic/Stepper/index.d.ts +1 -1
- package/dist/atomic/Stepper/index.d.ts.map +1 -1
- package/dist/atomic/Stepper/index.js +1 -100
- package/dist/atomic/SwipeAction/index.js +1 -62
- package/dist/atomic/Switch/SwitchPrimitive.d.ts.map +1 -1
- package/dist/atomic/Switch/SwitchPrimitive.js +1 -28
- package/dist/atomic/Switch/index.d.ts.map +1 -1
- package/dist/atomic/Switch/index.js +1 -82
- package/dist/atomic/Table/editing.d.ts.map +1 -1
- package/dist/atomic/Table/editing.js +4 -173
- package/dist/atomic/Table/index.d.ts.map +1 -1
- package/dist/atomic/Table/index.js +3 -419
- package/dist/atomic/Tabs/TabsPrimitive.d.ts.map +1 -1
- package/dist/atomic/Tabs/TabsPrimitive.js +1 -17
- package/dist/atomic/Tabs/index.d.ts +1 -1
- package/dist/atomic/Tabs/index.d.ts.map +1 -1
- package/dist/atomic/Tabs/index.js +1 -124
- package/dist/atomic/Tag/index.d.ts.map +1 -1
- package/dist/atomic/Tag/index.js +1 -60
- package/dist/atomic/Text/index.js +3 -116
- package/dist/atomic/Textarea/index.js +1 -20
- package/dist/atomic/Toaster/Toast.types.js +0 -2
- package/dist/atomic/Toaster/ToastPrimitive.d.ts +1 -1
- package/dist/atomic/Toaster/ToastPrimitive.d.ts.map +1 -1
- package/dist/atomic/Toaster/ToastPrimitive.js +1 -68
- package/dist/atomic/Toaster/index.d.ts.map +1 -1
- package/dist/atomic/Toaster/index.js +1 -30
- package/dist/atomic/Toggle/TogglePrimitive.d.ts.map +1 -1
- package/dist/atomic/Toggle/TogglePrimitive.js +1 -16
- package/dist/atomic/Toggle/index.d.ts +1 -1
- package/dist/atomic/Toggle/index.js +1 -41
- package/dist/atomic/ToggleGroup/ToggleGroupPrimitive.d.ts.map +1 -1
- package/dist/atomic/ToggleGroup/ToggleGroupPrimitive.js +1 -36
- package/dist/atomic/ToggleGroup/index.js +1 -64
- package/dist/atomic/Tooltip/TooltipPrimitive.d.ts.map +1 -1
- package/dist/atomic/Tooltip/TooltipPrimitive.js +1 -20
- package/dist/atomic/Tooltip/index.js +1 -84
- package/dist/atomic/TreeView/index.d.ts.map +1 -1
- package/dist/atomic/TreeView/index.js +1 -316
- package/dist/atomic/VideoPlayer/index.d.ts.map +1 -1
- package/dist/atomic/VideoPlayer/index.js +1 -204
- package/dist/atomic/VisuallyHidden/VisuallyHiddenPrimitive.d.ts.map +1 -1
- package/dist/atomic/VisuallyHidden/VisuallyHiddenPrimitive.js +1 -10
- package/dist/atomic/VisuallyHidden/index.js +1 -4
- package/dist/atomic/index.js +1 -124
- package/dist/hooks/index.js +1 -14
- package/dist/hooks/useClickOutside.d.ts.map +1 -1
- package/dist/hooks/useClickOutside.js +1 -128
- package/dist/hooks/useIntersectionObserver.d.ts.map +1 -1
- package/dist/hooks/useIntersectionObserver.js +1 -222
- package/dist/hooks/useSwipeToClose.d.ts.map +1 -1
- package/dist/hooks/useSwipeToClose.js +1 -280
- package/dist/hooks/useToast.d.ts +1 -1
- package/dist/hooks/useToast.d.ts.map +1 -1
- package/dist/hooks/useToast.js +1 -419
- package/dist/hooks/useViewportVisibility.d.ts.map +1 -1
- package/dist/hooks/useViewportVisibility.js +1 -110
- package/dist/index.js +1 -12
- package/dist/rsc.d.ts +47 -0
- package/dist/rsc.d.ts.map +1 -0
- package/dist/rsc.js +1 -0
- package/dist/styles/componentConstants.js +1 -184
- package/dist/styles/index.css +25 -4
- package/dist/styles/index.js +1 -10
- package/dist/symbol-index.json +1 -1
- package/dist/types.js +0 -2
- package/dist/utils/constants.js +1 -62
- package/dist/utils/cssVars.js +1 -10
- package/dist/utils/date.js +1 -130
- package/dist/utils/eventListener.js +1 -57
- package/dist/utils/helpers.js +1 -58
- package/dist/utils/imageRetry.js +1 -35
- package/dist/utils/index.js +1 -24
- package/dist/utils/initRipple.js +6 -84
- package/dist/utils/intersectionObserver.js +1 -184
- package/dist/utils/isDark.d.ts.map +1 -1
- package/dist/utils/isDark.js +1 -69
- package/dist/utils/linkComponent.d.ts.map +1 -1
- package/dist/utils/linkComponent.js +1 -37
- package/dist/utils/measurement.js +1 -197
- package/dist/utils/responsiveImage.js +1 -45
- package/dist/utils/rtl.js +1 -56
- package/dist/utils/sanitizeHtml.js +1 -43
- package/dist/utils/variants.d.ts +1 -1
- package/dist/utils/variants.js +1 -31
- package/dist/utils/viewportDetection.js +1 -86
- package/guidelines/API_REFERENCE.md +34 -0
- package/guidelines/COMPONENTS.md +1 -1
- package/package.json +5 -1
- package/dist/CarouselContent-5W2E2F2I.js +0 -1
- package/dist/CarouselContent-EELYAALW.js +0 -1
- package/dist/CarouselContent-KLQY6OVW.css +0 -1
- package/dist/CarouselContent-MQPZBYOS.js +0 -1
- package/dist/CodeContent-265AM7HO.css +0 -1
- package/dist/CodeContent-64D4U3NM.js +0 -2
- package/dist/CodeContent-7FWYPAB3.js +0 -2
- package/dist/CodeContent-E3HLW6BW.js +0 -2
- package/dist/CrawlContent-5Z6AVDDW.js +0 -1
- package/dist/MarqueeContent-7BVN7TX3.css +0 -1
- package/dist/MarqueeContent-KAYCOUNM.js +0 -1
- package/dist/atomic/SwipeAction/gesture.d.ts +0 -5
- package/dist/atomic/SwipeAction/gesture.d.ts.map +0 -1
- package/dist/atomic/SwipeAction/gesture.js +0 -10
- package/dist/chunk-3WHSDDVJ.js +0 -1
- package/dist/chunk-CIBFOOWV.js +0 -1
- package/dist/chunk-IFHHPSKN.js +0 -1
- package/dist/chunk-JAV36ZQC.js +0 -3
- package/dist/chunk-O2XFH626.js +0 -1
- package/dist/chunk-SZNEXFUZ.js +0 -3
- package/dist/chunk-UTPWB5VD.js +0 -1
- package/dist/chunk-XK52GUFA.js +0 -3
package/dist/hooks/useToast.js
CHANGED
|
@@ -1,419 +1 @@
|
|
|
1
|
-
|
|
2
|
-
/**
|
|
3
|
-
* @fileoverview Industry-standard toast notification system
|
|
4
|
-
* @description React 19+ compatible, CSR/SSR safe toast implementation
|
|
5
|
-
*
|
|
6
|
-
* **Architecture:**
|
|
7
|
-
* - Singleton pub-sub pattern with module-level state
|
|
8
|
-
* - Zero React Context overhead
|
|
9
|
-
* - Works across component boundaries
|
|
10
|
-
* - Can be called from anywhere (even non-React code)
|
|
11
|
-
*
|
|
12
|
-
* **React 19 Compatibility:**
|
|
13
|
-
* - Uses stable setState functions (no re-subscription needed)
|
|
14
|
-
* - Empty dependency array in useEffect (best practice)
|
|
15
|
-
* - 'use client' directive for Next.js App Router
|
|
16
|
-
*
|
|
17
|
-
* **CSR/SSR Safety:**
|
|
18
|
-
* - Client-only execution via 'use client'
|
|
19
|
-
* - No hydration mismatches (toasts are ephemeral)
|
|
20
|
-
* - Module state resets on navigation (expected behavior)
|
|
21
|
-
*
|
|
22
|
-
* @version 0.1.0
|
|
23
|
-
* @since 0.0.1
|
|
24
|
-
* @author AMBROISE PARK Consulting
|
|
25
|
-
*/
|
|
26
|
-
import { useSyncExternalStore, useMemo } from 'react';
|
|
27
|
-
// ============================================================================
|
|
28
|
-
// CONSTANTS & CONFIGURATION
|
|
29
|
-
// ============================================================================
|
|
30
|
-
const TOAST_LIMIT = 5;
|
|
31
|
-
// Must match the CSS exit animation duration in Toast styles — changing this
|
|
32
|
-
// without updating the CSS will cause toasts to vanish before animation completes
|
|
33
|
-
// or linger after animation ends.
|
|
34
|
-
const TOAST_REMOVE_DELAY = 300;
|
|
35
|
-
// ToastType is imported from Toast.types.ts
|
|
36
|
-
/**
|
|
37
|
-
* Toast duration configuration
|
|
38
|
-
*
|
|
39
|
-
* @version 0.1.0
|
|
40
|
-
* @since 0.0.1
|
|
41
|
-
* @author AMBROISE PARK Consulting
|
|
42
|
-
*/
|
|
43
|
-
export const TOAST_DURATIONS = {
|
|
44
|
-
default: 5000,
|
|
45
|
-
success: 4000,
|
|
46
|
-
error: 7000,
|
|
47
|
-
warning: 6000,
|
|
48
|
-
info: 5000,
|
|
49
|
-
};
|
|
50
|
-
const actionTypes = {
|
|
51
|
-
ADD_TOAST: 'ADD_TOAST',
|
|
52
|
-
UPDATE_TOAST: 'UPDATE_TOAST',
|
|
53
|
-
DISMISS_TOAST: 'DISMISS_TOAST',
|
|
54
|
-
REMOVE_TOAST: 'REMOVE_TOAST',
|
|
55
|
-
PAUSE_TOAST: 'PAUSE_TOAST',
|
|
56
|
-
RESUME_TOAST: 'RESUME_TOAST',
|
|
57
|
-
};
|
|
58
|
-
// ============================================================================
|
|
59
|
-
// TIMER MANAGEMENT
|
|
60
|
-
// ============================================================================
|
|
61
|
-
const toastTimers = new Map();
|
|
62
|
-
const toastPauseTimes = new Map();
|
|
63
|
-
const clearTimer = (toastId) => {
|
|
64
|
-
const timer = toastTimers.get(toastId);
|
|
65
|
-
if (timer) {
|
|
66
|
-
clearTimeout(timer);
|
|
67
|
-
toastTimers.delete(toastId);
|
|
68
|
-
}
|
|
69
|
-
toastPauseTimes.delete(toastId);
|
|
70
|
-
};
|
|
71
|
-
const isToastActive = (toastId) => {
|
|
72
|
-
return getMemoryState().toasts.some((t) => t.id === toastId);
|
|
73
|
-
};
|
|
74
|
-
const dismissToastIfExists = (toastId) => {
|
|
75
|
-
if (isToastActive(toastId)) {
|
|
76
|
-
dispatch({ type: 'DISMISS_TOAST', toastId });
|
|
77
|
-
}
|
|
78
|
-
};
|
|
79
|
-
const startAutoTimer = (toast) => {
|
|
80
|
-
if (toast.duration === 0)
|
|
81
|
-
return; // Persistent toast
|
|
82
|
-
clearTimer(toast.id);
|
|
83
|
-
const timeout = setTimeout(() => {
|
|
84
|
-
dismissToastIfExists(toast.id);
|
|
85
|
-
}, toast.remainingTime);
|
|
86
|
-
toastTimers.set(toast.id, timeout);
|
|
87
|
-
};
|
|
88
|
-
const pauseTimer = (toastId) => {
|
|
89
|
-
const timer = toastTimers.get(toastId);
|
|
90
|
-
if (timer) {
|
|
91
|
-
clearTimeout(timer);
|
|
92
|
-
toastTimers.delete(toastId);
|
|
93
|
-
toastPauseTimes.set(toastId, Date.now());
|
|
94
|
-
}
|
|
95
|
-
};
|
|
96
|
-
const resumeTimer = (toast) => {
|
|
97
|
-
if (toast.duration === 0)
|
|
98
|
-
return; // Persistent toast
|
|
99
|
-
const pauseTime = toastPauseTimes.get(toast.id);
|
|
100
|
-
if (!pauseTime)
|
|
101
|
-
return;
|
|
102
|
-
const elapsed = Date.now() - pauseTime;
|
|
103
|
-
const remaining = Math.max(0, toast.remainingTime - elapsed);
|
|
104
|
-
// Update remaining time
|
|
105
|
-
dispatch({
|
|
106
|
-
type: 'UPDATE_TOAST',
|
|
107
|
-
toast: { id: toast.id, remainingTime: remaining },
|
|
108
|
-
});
|
|
109
|
-
toastPauseTimes.delete(toast.id);
|
|
110
|
-
if (remaining > 0) {
|
|
111
|
-
const timeout = setTimeout(() => {
|
|
112
|
-
dismissToastIfExists(toast.id);
|
|
113
|
-
}, remaining);
|
|
114
|
-
toastTimers.set(toast.id, timeout);
|
|
115
|
-
}
|
|
116
|
-
else {
|
|
117
|
-
dismissToastIfExists(toast.id);
|
|
118
|
-
}
|
|
119
|
-
};
|
|
120
|
-
const addToRemoveQueue = (toastId) => {
|
|
121
|
-
clearTimer(toastId);
|
|
122
|
-
const timeout = setTimeout(() => {
|
|
123
|
-
clearTimer(toastId);
|
|
124
|
-
dispatch({ type: 'REMOVE_TOAST', toastId });
|
|
125
|
-
}, TOAST_REMOVE_DELAY);
|
|
126
|
-
toastTimers.set(toastId, timeout);
|
|
127
|
-
};
|
|
128
|
-
const clearAllTimers = () => {
|
|
129
|
-
toastTimers.forEach((timer) => clearTimeout(timer));
|
|
130
|
-
toastTimers.clear();
|
|
131
|
-
toastPauseTimes.clear();
|
|
132
|
-
};
|
|
133
|
-
// Initialize global state if not exists
|
|
134
|
-
if (typeof window !== 'undefined') {
|
|
135
|
-
if (!window.__DNDEV_TOAST_STATE__) {
|
|
136
|
-
window.__DNDEV_TOAST_STATE__ = { toasts: [] };
|
|
137
|
-
}
|
|
138
|
-
if (!window.__DNDEV_TOAST_LISTENERS__) {
|
|
139
|
-
window.__DNDEV_TOAST_LISTENERS__ = [];
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
// Snapshot cache for useSyncExternalStore referential stability.
|
|
143
|
-
// Only updated when dispatch() actually produces a new state object.
|
|
144
|
-
const SSR_FALLBACK = { toasts: [] };
|
|
145
|
-
let snapshotCache = SSR_FALLBACK;
|
|
146
|
-
// Use global state — guard against SSR (window is undefined on the server)
|
|
147
|
-
const getMemoryState = () => {
|
|
148
|
-
if (typeof window === 'undefined')
|
|
149
|
-
return SSR_FALLBACK;
|
|
150
|
-
const current = window.__DNDEV_TOAST_STATE__ ?? SSR_FALLBACK;
|
|
151
|
-
// Only return a new reference when the underlying state object changed
|
|
152
|
-
if (current !== snapshotCache) {
|
|
153
|
-
snapshotCache = current;
|
|
154
|
-
}
|
|
155
|
-
return snapshotCache;
|
|
156
|
-
};
|
|
157
|
-
// Always return the same array reference from window global — never a new [].
|
|
158
|
-
// This guarantees push/splice in subscribe and forEach in dispatch operate on the same array.
|
|
159
|
-
const getListeners = () => {
|
|
160
|
-
if (typeof window === 'undefined')
|
|
161
|
-
return [];
|
|
162
|
-
if (!window.__DNDEV_TOAST_LISTENERS__) {
|
|
163
|
-
window.__DNDEV_TOAST_LISTENERS__ = [];
|
|
164
|
-
}
|
|
165
|
-
return window.__DNDEV_TOAST_LISTENERS__;
|
|
166
|
-
};
|
|
167
|
-
function dispatch(action) {
|
|
168
|
-
const currentState = getMemoryState();
|
|
169
|
-
const newState = reducer(currentState, action);
|
|
170
|
-
// Update global state
|
|
171
|
-
if (typeof window !== 'undefined') {
|
|
172
|
-
window.__DNDEV_TOAST_STATE__ = newState;
|
|
173
|
-
}
|
|
174
|
-
// Debug logging
|
|
175
|
-
if (process.env.NODE_ENV === 'development') {
|
|
176
|
-
console.log('[Toast] Dispatch:', action.type, 'Listeners:', getListeners().length);
|
|
177
|
-
}
|
|
178
|
-
// Notify all listeners — wrap each call so one throwing listener doesn't break the chain
|
|
179
|
-
getListeners().forEach((listener) => {
|
|
180
|
-
try {
|
|
181
|
-
listener(newState);
|
|
182
|
-
}
|
|
183
|
-
catch (err) {
|
|
184
|
-
if (process.env.NODE_ENV === 'development') {
|
|
185
|
-
console.error('[Toast] Listener threw:', err);
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
});
|
|
189
|
-
}
|
|
190
|
-
/**
|
|
191
|
-
* Toast state reducer
|
|
192
|
-
*
|
|
193
|
-
* @version 0.1.0
|
|
194
|
-
* @since 0.0.1
|
|
195
|
-
* @author AMBROISE PARK Consulting
|
|
196
|
-
*/
|
|
197
|
-
export const reducer = (state, action) => {
|
|
198
|
-
switch (action.type) {
|
|
199
|
-
case 'ADD_TOAST': {
|
|
200
|
-
const newToasts = [action.toast, ...state.toasts];
|
|
201
|
-
const removedToasts = newToasts.slice(TOAST_LIMIT);
|
|
202
|
-
// Clean up timers for removed toasts
|
|
203
|
-
removedToasts.forEach((toast) => pauseTimer(toast.id));
|
|
204
|
-
return {
|
|
205
|
-
...state,
|
|
206
|
-
toasts: newToasts.slice(0, TOAST_LIMIT),
|
|
207
|
-
};
|
|
208
|
-
}
|
|
209
|
-
case 'UPDATE_TOAST':
|
|
210
|
-
return {
|
|
211
|
-
...state,
|
|
212
|
-
toasts: state.toasts.map((t) => t.id === action.toast.id ? { ...t, ...action.toast } : t),
|
|
213
|
-
};
|
|
214
|
-
case 'DISMISS_TOAST': {
|
|
215
|
-
const { toastId } = action;
|
|
216
|
-
if (toastId) {
|
|
217
|
-
// Dismiss specific toast — animate out, then remove
|
|
218
|
-
pauseTimer(toastId);
|
|
219
|
-
if (!state.toasts.some((t) => t.id === toastId)) {
|
|
220
|
-
return state; // Toast doesn't exist
|
|
221
|
-
}
|
|
222
|
-
addToRemoveQueue(toastId);
|
|
223
|
-
return {
|
|
224
|
-
...state,
|
|
225
|
-
toasts: state.toasts.map((t) => t.id === toastId ? { ...t, open: false } : t),
|
|
226
|
-
};
|
|
227
|
-
}
|
|
228
|
-
else {
|
|
229
|
-
// Dismiss all non-persistent toasts
|
|
230
|
-
state.toasts.forEach((toast) => {
|
|
231
|
-
if (toast.duration > 0) {
|
|
232
|
-
pauseTimer(toast.id);
|
|
233
|
-
addToRemoveQueue(toast.id);
|
|
234
|
-
}
|
|
235
|
-
});
|
|
236
|
-
return {
|
|
237
|
-
...state,
|
|
238
|
-
toasts: state.toasts.map((t) => ({
|
|
239
|
-
...t,
|
|
240
|
-
open: t.duration === 0 ? t.open : false,
|
|
241
|
-
})),
|
|
242
|
-
};
|
|
243
|
-
}
|
|
244
|
-
}
|
|
245
|
-
case 'REMOVE_TOAST':
|
|
246
|
-
if (action.toastId === undefined) {
|
|
247
|
-
clearAllTimers();
|
|
248
|
-
return { ...state, toasts: [] };
|
|
249
|
-
}
|
|
250
|
-
return {
|
|
251
|
-
...state,
|
|
252
|
-
toasts: state.toasts.filter((t) => t.id !== action.toastId),
|
|
253
|
-
};
|
|
254
|
-
case 'PAUSE_TOAST': {
|
|
255
|
-
const toast = state.toasts.find((t) => t.id === action.toastId);
|
|
256
|
-
if (!toast || toast.isPaused)
|
|
257
|
-
return state;
|
|
258
|
-
pauseTimer(action.toastId);
|
|
259
|
-
return {
|
|
260
|
-
...state,
|
|
261
|
-
toasts: state.toasts.map((t) => t.id === action.toastId ? { ...t, isPaused: true } : t),
|
|
262
|
-
};
|
|
263
|
-
}
|
|
264
|
-
case 'RESUME_TOAST': {
|
|
265
|
-
const toast = state.toasts.find((t) => t.id === action.toastId);
|
|
266
|
-
if (!toast || !toast.isPaused)
|
|
267
|
-
return state;
|
|
268
|
-
resumeTimer(toast);
|
|
269
|
-
return {
|
|
270
|
-
...state,
|
|
271
|
-
toasts: state.toasts.map((t) => t.id === action.toastId ? { ...t, isPaused: false } : t),
|
|
272
|
-
};
|
|
273
|
-
}
|
|
274
|
-
default:
|
|
275
|
-
return state;
|
|
276
|
-
}
|
|
277
|
-
};
|
|
278
|
-
// ============================================================================
|
|
279
|
-
// TOAST CREATION
|
|
280
|
-
// ============================================================================
|
|
281
|
-
let count = 0;
|
|
282
|
-
const genId = () => (++count % Number.MAX_VALUE).toString();
|
|
283
|
-
const normalizeToastProps = (input, message) => {
|
|
284
|
-
// Type + message: toast('success', 'message')
|
|
285
|
-
if (typeof input === 'string' && typeof message === 'string') {
|
|
286
|
-
const type = input;
|
|
287
|
-
return {
|
|
288
|
-
title: message,
|
|
289
|
-
toastType: type,
|
|
290
|
-
duration: TOAST_DURATIONS[type],
|
|
291
|
-
};
|
|
292
|
-
}
|
|
293
|
-
// Single string: toast('message')
|
|
294
|
-
if (typeof input === 'string' && message === undefined) {
|
|
295
|
-
return {
|
|
296
|
-
title: input,
|
|
297
|
-
toastType: 'default',
|
|
298
|
-
duration: TOAST_DURATIONS.default,
|
|
299
|
-
};
|
|
300
|
-
}
|
|
301
|
-
// Object: toast({ title: '...', type: 'success' })
|
|
302
|
-
return input;
|
|
303
|
-
};
|
|
304
|
-
const createToast = (props) => {
|
|
305
|
-
const id = props.id || genId();
|
|
306
|
-
const type = props.toastType || 'default';
|
|
307
|
-
const duration = props.duration ?? TOAST_DURATIONS[type];
|
|
308
|
-
const { id: _, toastType: __, duration: ___, ...restProps } = props;
|
|
309
|
-
const update = (updateProps) => dispatch({ type: 'UPDATE_TOAST', toast: { ...updateProps, id } });
|
|
310
|
-
const dismiss = () => dispatch({ type: 'DISMISS_TOAST', toastId: id });
|
|
311
|
-
const newToast = {
|
|
312
|
-
...restProps,
|
|
313
|
-
id,
|
|
314
|
-
type,
|
|
315
|
-
duration,
|
|
316
|
-
open: true,
|
|
317
|
-
isPaused: false,
|
|
318
|
-
startTime: Date.now(),
|
|
319
|
-
remainingTime: duration,
|
|
320
|
-
onOpenChange: (open) => {
|
|
321
|
-
props.onOpenChange?.(open);
|
|
322
|
-
if (!open) {
|
|
323
|
-
props.onDismiss?.();
|
|
324
|
-
dismiss();
|
|
325
|
-
}
|
|
326
|
-
},
|
|
327
|
-
};
|
|
328
|
-
dispatch({ type: 'ADD_TOAST', toast: newToast });
|
|
329
|
-
// Start auto-dismiss timer
|
|
330
|
-
if (duration > 0) {
|
|
331
|
-
startAutoTimer(newToast);
|
|
332
|
-
}
|
|
333
|
-
return { id, dismiss, update };
|
|
334
|
-
};
|
|
335
|
-
/**
|
|
336
|
-
* Standalone toast function
|
|
337
|
-
*
|
|
338
|
-
* Can be called from anywhere - even outside React components:
|
|
339
|
-
* - Event handlers
|
|
340
|
-
* - Utility functions
|
|
341
|
-
* - API clients
|
|
342
|
-
* - Error handlers
|
|
343
|
-
*
|
|
344
|
-
* @example
|
|
345
|
-
* // Simple usage
|
|
346
|
-
* toast('Operation successful');
|
|
347
|
-
*
|
|
348
|
-
* @example
|
|
349
|
-
* // With type
|
|
350
|
-
* toast('error', 'Something went wrong');
|
|
351
|
-
*
|
|
352
|
-
* @example
|
|
353
|
-
* // Advanced usage
|
|
354
|
-
* toast({
|
|
355
|
-
* title: 'Success',
|
|
356
|
-
* description: 'Your changes have been saved',
|
|
357
|
-
* type: 'success',
|
|
358
|
-
* duration: 5000
|
|
359
|
-
* });
|
|
360
|
-
*
|
|
361
|
-
* @version 0.1.0
|
|
362
|
-
* @since 0.0.1
|
|
363
|
-
* @author AMBROISE PARK Consulting
|
|
364
|
-
*/
|
|
365
|
-
const toast = (input, message) => {
|
|
366
|
-
if (process.env.NODE_ENV === 'development') {
|
|
367
|
-
console.log('[Toast] toast() called with:', typeof input === 'string' ? input : input);
|
|
368
|
-
}
|
|
369
|
-
const props = normalizeToastProps(input, message);
|
|
370
|
-
return createToast(props);
|
|
371
|
-
};
|
|
372
|
-
// ============================================================================
|
|
373
|
-
// REACT HOOK
|
|
374
|
-
// ============================================================================
|
|
375
|
-
/**
|
|
376
|
-
* React hook for consuming toast state
|
|
377
|
-
*
|
|
378
|
-
* Industry-standard pub-sub pattern:
|
|
379
|
-
* - Subscribe once on mount
|
|
380
|
-
* - Unsubscribe once on unmount
|
|
381
|
-
* - Uses module-level state for singleton behavior
|
|
382
|
-
* - CSR/SSR safe with proper initialization
|
|
383
|
-
*/
|
|
384
|
-
export function useToast() {
|
|
385
|
-
// Use React 19.1 useSyncExternalStore for optimal performance
|
|
386
|
-
const state = useSyncExternalStore(useMemo(() => (callback) => {
|
|
387
|
-
const listeners = getListeners();
|
|
388
|
-
listeners.push(callback);
|
|
389
|
-
return () => {
|
|
390
|
-
const index = listeners.indexOf(callback);
|
|
391
|
-
if (index > -1) {
|
|
392
|
-
listeners.splice(index, 1);
|
|
393
|
-
}
|
|
394
|
-
};
|
|
395
|
-
}, []), () => getMemoryState(), () => getMemoryState() // SSR fallback
|
|
396
|
-
);
|
|
397
|
-
return {
|
|
398
|
-
...state,
|
|
399
|
-
toast,
|
|
400
|
-
dismiss: (toastId) => dispatch({ type: 'DISMISS_TOAST', toastId }),
|
|
401
|
-
pause: (toastId) => dispatch({ type: 'PAUSE_TOAST', toastId }),
|
|
402
|
-
resume: (toastId) => dispatch({ type: 'RESUME_TOAST', toastId }),
|
|
403
|
-
};
|
|
404
|
-
}
|
|
405
|
-
/**
|
|
406
|
-
* React hook for consuming toast state
|
|
407
|
-
*
|
|
408
|
-
* @version 0.1.0
|
|
409
|
-
* @since 0.0.1
|
|
410
|
-
* @author AMBROISE PARK Consulting
|
|
411
|
-
*/
|
|
412
|
-
/**
|
|
413
|
-
* Exported toast function and duration constants
|
|
414
|
-
*
|
|
415
|
-
* @version 0.1.0
|
|
416
|
-
* @since 0.0.1
|
|
417
|
-
* @author AMBROISE PARK Consulting
|
|
418
|
-
*/
|
|
419
|
-
export { toast };
|
|
1
|
+
"use client";import{useSyncExternalStore as g,useMemo as P}from"react";var f=5,w=300,c={default:5e3,success:4e3,error:7e3,warning:6e3,info:5e3};var n=new Map,r=new Map,S=t=>{let e=n.get(t);e&&(clearTimeout(e),n.delete(t)),r.delete(t)},R=t=>d().toasts.some(e=>e.id===t),u=t=>{R(t)&&i({type:"DISMISS_TOAST",toastId:t})},I=t=>{if(t.duration===0)return;S(t.id);let e=setTimeout(()=>{u(t.id)},t.remainingTime);n.set(t.id,e)},p=t=>{let e=n.get(t);e&&(clearTimeout(e),n.delete(t),r.set(t,Date.now()))},N=t=>{if(t.duration===0)return;let e=r.get(t.id);if(!e)return;let o=Date.now()-e,s=Math.max(0,t.remainingTime-o);if(i({type:"UPDATE_TOAST",toast:{id:t.id,remainingTime:s}}),r.delete(t.id),s>0){let T=setTimeout(()=>{u(t.id)},s);n.set(t.id,T)}else u(t.id)},y=t=>{S(t);let e=setTimeout(()=>{S(t),i({type:"REMOVE_TOAST",toastId:t})},w);n.set(t,e)},v=()=>{n.forEach(t=>clearTimeout(t)),n.clear(),r.clear()};typeof window<"u"&&(window.__DNDEV_TOAST_STATE__||(window.__DNDEV_TOAST_STATE__={toasts:[]}),window.__DNDEV_TOAST_LISTENERS__||(window.__DNDEV_TOAST_LISTENERS__=[]));var A={toasts:[]},_=A,d=()=>{if(typeof window>"u")return A;let t=window.__DNDEV_TOAST_STATE__??A;return t!==_&&(_=t),_},O=()=>typeof window>"u"?[]:(window.__DNDEV_TOAST_LISTENERS__||(window.__DNDEV_TOAST_LISTENERS__=[]),window.__DNDEV_TOAST_LISTENERS__);function i(t){let e=d(),o=M(e,t);typeof window<"u"&&(window.__DNDEV_TOAST_STATE__=o),O().forEach(s=>{try{s(o)}catch{}})}var M=(t,e)=>{switch(e.type){case"ADD_TOAST":{let o=[e.toast,...t.toasts];return o.slice(f).forEach(T=>p(T.id)),{...t,toasts:o.slice(0,f)}}case"UPDATE_TOAST":return{...t,toasts:t.toasts.map(o=>o.id===e.toast.id?{...o,...e.toast}:o)};case"DISMISS_TOAST":{let{toastId:o}=e;return o?(p(o),t.toasts.some(s=>s.id===o)?(y(o),{...t,toasts:t.toasts.map(s=>s.id===o?{...s,open:!1}:s)}):t):(t.toasts.forEach(s=>{s.duration>0&&(p(s.id),y(s.id))}),{...t,toasts:t.toasts.map(s=>({...s,open:s.duration===0?s.open:!1}))})}case"REMOVE_TOAST":return e.toastId===void 0?(v(),{...t,toasts:[]}):{...t,toasts:t.toasts.filter(o=>o.id!==e.toastId)};case"PAUSE_TOAST":{let o=t.toasts.find(s=>s.id===e.toastId);return!o||o.isPaused?t:(p(e.toastId),{...t,toasts:t.toasts.map(s=>s.id===e.toastId?{...s,isPaused:!0}:s)})}case"RESUME_TOAST":{let o=t.toasts.find(s=>s.id===e.toastId);return!o||!o.isPaused?t:(N(o),{...t,toasts:t.toasts.map(s=>s.id===e.toastId?{...s,isPaused:!1}:s)})}default:return t}},V=0,h=()=>(++V%Number.MAX_VALUE).toString(),U=(t,e)=>{if(typeof t=="string"&&typeof e=="string"){let o=t;return{title:e,toastType:o,duration:c[o]}}return typeof t=="string"&&e===void 0?{title:t,toastType:"default",duration:c.default}:t},L=t=>{let e=t.id||h(),o=t.toastType||"default",s=t.duration??c[o],{id:T,toastType:x,duration:C,...l}=t,D=a=>i({type:"UPDATE_TOAST",toast:{...a,id:e}}),m=()=>i({type:"DISMISS_TOAST",toastId:e}),E={...l,id:e,type:o,duration:s,open:!0,isPaused:!1,startTime:Date.now(),remainingTime:s,onOpenChange:a=>{t.onOpenChange?.(a),a||(t.onDismiss?.(),m())}};return i({type:"ADD_TOAST",toast:E}),s>0&&I(E),{id:e,dismiss:m,update:D}},b=(t,e)=>{let o=U(t,e);return L(o)};function z(){return{...g(P(()=>e=>{let o=O();return o.push(e),()=>{let s=o.indexOf(e);s>-1&&o.splice(s,1)}},[]),()=>d(),()=>d()),toast:b,dismiss:e=>i({type:"DISMISS_TOAST",toastId:e}),pause:e=>i({type:"PAUSE_TOAST",toastId:e}),resume:e=>i({type:"RESUME_TOAST",toastId:e})}}export{c as TOAST_DURATIONS,M as reducer,b as toast,z as useToast};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useViewportVisibility.d.ts","sourceRoot":"","sources":["../../src/hooks/useViewportVisibility.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"useViewportVisibility.d.ts","sourceRoot":"","sources":["../../src/hooks/useViewportVisibility.ts"],"names":[],"mappings":"AAeA,OAAO,EAEL,KAAK,wBAAwB,EAC7B,KAAK,uBAAuB,EAC7B,MAAM,4BAA4B,CAAC;AAEpC;;;;;;GAMG;AACH,MAAM,WAAW,4BAA6B,SAAQ,wBAAwB;IAC5E;;;OAGG;IACH,oBAAoB,CAAC,EAAE,OAAO,CAAC;CAChC;AAED;;;;;;GAMG;AACH,MAAM,WAAW,2BAA4B,SAAQ,uBAAuB;IAC1E;;OAEG;IACH,GAAG,EAAE,KAAK,CAAC,SAAS,CAAC,cAAc,GAAG,IAAI,CAAC,CAAC;IAE5C;;OAEG;IACH,aAAa,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,OAAO,CAAC;CAC3C;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAsCG;AACH,wBAAgB,qBAAqB,CACnC,OAAO,GAAE,4BAAiC,GACzC,2BAA2B,CAyF7B"}
|
|
@@ -1,110 +1 @@
|
|
|
1
|
-
|
|
2
|
-
/**
|
|
3
|
-
* @fileoverview Viewport Visibility Hook
|
|
4
|
-
* @description React hook for viewport-based visibility detection. Provides visibility state for components like Reveal, StackedCards, and LazyImage.
|
|
5
|
-
*
|
|
6
|
-
* @version 0.1.0
|
|
7
|
-
* @since 0.0.1
|
|
8
|
-
* @author AMBROISE PARK Consulting
|
|
9
|
-
*/
|
|
10
|
-
import { useRef, useEffect, useState, useCallback, useMemo } from 'react';
|
|
11
|
-
import { observeElement } from '../utils/intersectionObserver';
|
|
12
|
-
import { createViewportHandler, } from '../utils/viewportDetection';
|
|
13
|
-
/**
|
|
14
|
-
* Hook for viewport-based visibility detection
|
|
15
|
-
*
|
|
16
|
-
* Provides reusable viewport detection logic for components like:
|
|
17
|
-
* - Reveal (staggered animations)
|
|
18
|
-
* - StackedCards (progressive reveal)
|
|
19
|
-
* - LazyImage (lazy loading)
|
|
20
|
-
* - ScrollTrigger (scroll-based effects)
|
|
21
|
-
*
|
|
22
|
-
* @example Basic usage
|
|
23
|
-
* ```tsx
|
|
24
|
-
* const { ref, isVisible, visibleItems } = useViewportVisibility({
|
|
25
|
-
* threshold: 0.1,
|
|
26
|
-
* trackItems: true
|
|
27
|
-
* });
|
|
28
|
-
*
|
|
29
|
-
* return (
|
|
30
|
-
* <div>
|
|
31
|
-
* {items.map((item, index) => (
|
|
32
|
-
* <div key={index} className={isItemVisible(index) ? 'visible' : 'hidden'}>
|
|
33
|
-
* {item}
|
|
34
|
-
* </div>
|
|
35
|
-
* ))}
|
|
36
|
-
* </div>
|
|
37
|
-
* );
|
|
38
|
-
* ```
|
|
39
|
-
*
|
|
40
|
-
* @example With scroll listener
|
|
41
|
-
* ```tsx
|
|
42
|
-
* const { ref, isVisible, visibleItems } = useViewportVisibility({
|
|
43
|
-
* trackItems: true,
|
|
44
|
-
* enableScrollListener: true
|
|
45
|
-
* });
|
|
46
|
-
* ```
|
|
47
|
-
*
|
|
48
|
-
* @version 0.1.0
|
|
49
|
-
* @since 0.0.1
|
|
50
|
-
* @author AMBROISE PARK Consulting
|
|
51
|
-
*/
|
|
52
|
-
export function useViewportVisibility(options = {}) {
|
|
53
|
-
const { threshold = 0.1, rootMargin = '50px', once = false, trackItems = true, enableScrollListener = true, } = options;
|
|
54
|
-
const containerRef = useRef(null);
|
|
55
|
-
const [isVisible, setIsVisible] = useState(false);
|
|
56
|
-
const [hasTriggered, setHasTriggered] = useState(false);
|
|
57
|
-
const [visibleItems, setVisibleItems] = useState(new Set());
|
|
58
|
-
// Create viewport handler once, recreate only when options change
|
|
59
|
-
const viewportHandler = useMemo(() => createViewportHandler({
|
|
60
|
-
threshold,
|
|
61
|
-
rootMargin,
|
|
62
|
-
once,
|
|
63
|
-
trackItems,
|
|
64
|
-
}), [threshold, rootMargin, once, trackItems]);
|
|
65
|
-
// Intersection observer callback
|
|
66
|
-
const handleIntersection = useCallback((entry) => {
|
|
67
|
-
const handler = viewportHandler;
|
|
68
|
-
const result = handler.handleIntersection(entry, containerRef.current || undefined);
|
|
69
|
-
setIsVisible(result.isVisible);
|
|
70
|
-
setHasTriggered(result.hasTriggered);
|
|
71
|
-
setVisibleItems(result.visibleItems);
|
|
72
|
-
}, [viewportHandler]);
|
|
73
|
-
// Scroll handler for item visibility tracking
|
|
74
|
-
const handleScroll = useCallback(() => {
|
|
75
|
-
if (!containerRef.current || !isVisible)
|
|
76
|
-
return;
|
|
77
|
-
const handler = viewportHandler;
|
|
78
|
-
const result = handler.handleScroll(containerRef.current);
|
|
79
|
-
setVisibleItems(result.visibleItems);
|
|
80
|
-
}, [isVisible, viewportHandler]);
|
|
81
|
-
// Set up intersection observer
|
|
82
|
-
useEffect(() => {
|
|
83
|
-
const element = containerRef.current;
|
|
84
|
-
if (!element)
|
|
85
|
-
return;
|
|
86
|
-
const cleanup = observeElement(element, handleIntersection, {
|
|
87
|
-
threshold,
|
|
88
|
-
rootMargin,
|
|
89
|
-
});
|
|
90
|
-
return cleanup;
|
|
91
|
-
}, [handleIntersection, threshold, rootMargin]);
|
|
92
|
-
// Set up scroll listener for item tracking
|
|
93
|
-
useEffect(() => {
|
|
94
|
-
if (!enableScrollListener || !trackItems || typeof window === 'undefined')
|
|
95
|
-
return;
|
|
96
|
-
window.addEventListener('scroll', handleScroll, { passive: true });
|
|
97
|
-
return () => window.removeEventListener('scroll', handleScroll);
|
|
98
|
-
}, [enableScrollListener, trackItems, handleScroll]);
|
|
99
|
-
// Check if specific item is visible
|
|
100
|
-
const isItemVisible = useCallback((index) => {
|
|
101
|
-
return visibleItems.has(index);
|
|
102
|
-
}, [visibleItems]);
|
|
103
|
-
return {
|
|
104
|
-
ref: containerRef,
|
|
105
|
-
isVisible,
|
|
106
|
-
hasTriggered,
|
|
107
|
-
visibleItems,
|
|
108
|
-
isItemVisible,
|
|
109
|
-
};
|
|
110
|
-
}
|
|
1
|
+
"use client";import{useRef as g,useEffect as w,useState as p,useCallback as b,useMemo as x}from"react";import{observeElement as E}from"../utils/intersectionObserver.js";import{createViewportHandler as S}from"../utils/viewportDetection.js";function H(h={}){let{threshold:n=.1,rootMargin:i="50px",once:d=!1,trackItems:r=!0,enableScrollListener:f=!0}=h,t=g(null),[o,v]=p(!1),[I,y]=p(!1),[l,m]=p(new Set),s=x(()=>S({threshold:n,rootMargin:i,once:d,trackItems:r}),[n,i,d,r]),V=b(e=>{let a=s.handleIntersection(e,t.current||void 0);v(a.isVisible),y(a.hasTriggered),m(a.visibleItems)},[s]),c=b(()=>{if(!t.current||!o)return;let u=s.handleScroll(t.current);m(u.visibleItems)},[o,s]);w(()=>{let e=t.current;return e?E(e,V,{threshold:n,rootMargin:i}):void 0},[V,n,i]),w(()=>{if(!(!f||!r||typeof window>"u"))return window.addEventListener("scroll",c,{passive:!0}),()=>window.removeEventListener("scroll",c)},[f,r,c]);let R=b(e=>l.has(e),[l]);return{ref:t,isVisible:o,hasTriggered:I,visibleItems:l,isItemVisible:R}}export{H as useViewportVisibility};
|