@hanzo/ui 3.0.4 → 3.0.6
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/package.json +4 -2
- package/primitives/drawer.tsx +116 -0
- package/primitives/index.ts +15 -14
- package/primitives/sheet.tsx +1 -1
- package/primitives/sonner.tsx +35 -0
- package/tailwind/colors.tailwind.js +4 -0
- package/primitives/toast.tsx +0 -129
- package/primitives/toaster.tsx +0 -37
- package/primitives/use-toast.ts +0 -192
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hanzo/ui",
|
|
3
|
-
"version": "3.0.
|
|
3
|
+
"version": "3.0.6",
|
|
4
4
|
"description": "Library that contains shared UI primitives, support for a common design system, and other boilerplate support.",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"registry": "https://registry.npmjs.org/",
|
|
@@ -63,9 +63,11 @@
|
|
|
63
63
|
"postcss-selector-parser": "^6.0.13",
|
|
64
64
|
"react-day-picker": "^8.7.1",
|
|
65
65
|
"react-intersection-observer": "^9.7.0",
|
|
66
|
+
"sonner": "^1.2.3",
|
|
66
67
|
"tailwind-merge": "^2.2.0",
|
|
67
68
|
"tailwindcss-animate": "^1.0.6",
|
|
68
|
-
"tailwindcss-interaction-media": "^0.1.0"
|
|
69
|
+
"tailwindcss-interaction-media": "^0.1.0",
|
|
70
|
+
"vaul": "^0.2.0"
|
|
69
71
|
},
|
|
70
72
|
"peerDependencies": {
|
|
71
73
|
"@hookform/resolvers": "^3.3.2",
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
|
|
3
|
+
import * as React from 'react'
|
|
4
|
+
import { Drawer as DrawerPrimitive } from 'vaul'
|
|
5
|
+
|
|
6
|
+
import { cn } from '../util'
|
|
7
|
+
|
|
8
|
+
const Drawer = ({
|
|
9
|
+
shouldScaleBackground = true,
|
|
10
|
+
...props
|
|
11
|
+
}: React.ComponentProps<typeof DrawerPrimitive.Root>) => (
|
|
12
|
+
<DrawerPrimitive.Root
|
|
13
|
+
shouldScaleBackground={shouldScaleBackground}
|
|
14
|
+
{...props}
|
|
15
|
+
/>
|
|
16
|
+
)
|
|
17
|
+
Drawer.displayName = 'Drawer'
|
|
18
|
+
|
|
19
|
+
const DrawerTrigger = DrawerPrimitive.Trigger
|
|
20
|
+
const DrawerPortal = DrawerPrimitive.Portal
|
|
21
|
+
const DrawerClose = DrawerPrimitive.Close
|
|
22
|
+
|
|
23
|
+
const DrawerOverlay = React.forwardRef<
|
|
24
|
+
React.ElementRef<typeof DrawerPrimitive.Overlay>,
|
|
25
|
+
React.ComponentPropsWithoutRef<typeof DrawerPrimitive.Overlay>
|
|
26
|
+
>(({ className, ...props }, ref) => (
|
|
27
|
+
<DrawerPrimitive.Overlay
|
|
28
|
+
ref={ref}
|
|
29
|
+
className={cn('fixed inset-0 z-20 bg-background', className)}
|
|
30
|
+
{...props}
|
|
31
|
+
/>
|
|
32
|
+
))
|
|
33
|
+
DrawerOverlay.displayName = DrawerPrimitive.Overlay.displayName
|
|
34
|
+
|
|
35
|
+
const DrawerContent = React.forwardRef<
|
|
36
|
+
React.ElementRef<typeof DrawerPrimitive.Content>,
|
|
37
|
+
React.ComponentPropsWithoutRef<typeof DrawerPrimitive.Content>
|
|
38
|
+
>(({ className, children, ...props }, ref) => (
|
|
39
|
+
<DrawerPortal>
|
|
40
|
+
<DrawerOverlay />
|
|
41
|
+
<DrawerPrimitive.Content
|
|
42
|
+
ref={ref}
|
|
43
|
+
className={cn(
|
|
44
|
+
'fixed inset-x-0 bottom-0 z-20 mt-24 flex h-auto flex-col rounded-t-[10px] border bg-background',
|
|
45
|
+
className
|
|
46
|
+
)}
|
|
47
|
+
{...props}
|
|
48
|
+
>
|
|
49
|
+
<div className='mx-auto mt-4 h-2 w-[100px] rounded-full bg-level-1' />
|
|
50
|
+
{children}
|
|
51
|
+
</DrawerPrimitive.Content>
|
|
52
|
+
</DrawerPortal>
|
|
53
|
+
))
|
|
54
|
+
DrawerContent.displayName = 'DrawerContent'
|
|
55
|
+
|
|
56
|
+
const DrawerHeader = ({
|
|
57
|
+
className,
|
|
58
|
+
...props
|
|
59
|
+
}: React.HTMLAttributes<HTMLDivElement>) => (
|
|
60
|
+
<div
|
|
61
|
+
className={cn('grid gap-1.5 p-4 text-center sm:text-left', className)}
|
|
62
|
+
{...props}
|
|
63
|
+
/>
|
|
64
|
+
)
|
|
65
|
+
DrawerHeader.displayName = 'DrawerHeader'
|
|
66
|
+
|
|
67
|
+
const DrawerFooter = ({
|
|
68
|
+
className,
|
|
69
|
+
...props
|
|
70
|
+
}: React.HTMLAttributes<HTMLDivElement>) => (
|
|
71
|
+
<div
|
|
72
|
+
className={cn('mt-auto flex flex-col gap-2 p-4', className)}
|
|
73
|
+
{...props}
|
|
74
|
+
/>
|
|
75
|
+
)
|
|
76
|
+
DrawerFooter.displayName = 'DrawerFooter'
|
|
77
|
+
|
|
78
|
+
const DrawerTitle = React.forwardRef<
|
|
79
|
+
React.ElementRef<typeof DrawerPrimitive.Title>,
|
|
80
|
+
React.ComponentPropsWithoutRef<typeof DrawerPrimitive.Title>
|
|
81
|
+
>(({ className, ...props }, ref) => (
|
|
82
|
+
<DrawerPrimitive.Title
|
|
83
|
+
ref={ref}
|
|
84
|
+
className={cn(
|
|
85
|
+
'text-lg font-semibold leading-none tracking-tight',
|
|
86
|
+
className
|
|
87
|
+
)}
|
|
88
|
+
{...props}
|
|
89
|
+
/>
|
|
90
|
+
))
|
|
91
|
+
DrawerTitle.displayName = DrawerPrimitive.Title.displayName
|
|
92
|
+
|
|
93
|
+
const DrawerDescription = React.forwardRef<
|
|
94
|
+
React.ElementRef<typeof DrawerPrimitive.Description>,
|
|
95
|
+
React.ComponentPropsWithoutRef<typeof DrawerPrimitive.Description>
|
|
96
|
+
>(({ className, ...props }, ref) => (
|
|
97
|
+
<DrawerPrimitive.Description
|
|
98
|
+
ref={ref}
|
|
99
|
+
className={cn('text-sm text-muted', className)}
|
|
100
|
+
{...props}
|
|
101
|
+
/>
|
|
102
|
+
))
|
|
103
|
+
DrawerDescription.displayName = DrawerPrimitive.Description.displayName
|
|
104
|
+
|
|
105
|
+
export {
|
|
106
|
+
Drawer,
|
|
107
|
+
DrawerPortal,
|
|
108
|
+
DrawerOverlay,
|
|
109
|
+
DrawerTrigger,
|
|
110
|
+
DrawerClose,
|
|
111
|
+
DrawerContent,
|
|
112
|
+
DrawerHeader,
|
|
113
|
+
DrawerFooter,
|
|
114
|
+
DrawerTitle,
|
|
115
|
+
DrawerDescription,
|
|
116
|
+
}
|
package/primitives/index.ts
CHANGED
|
@@ -25,6 +25,20 @@ export {
|
|
|
25
25
|
DialogDescription,
|
|
26
26
|
} from './dialog'
|
|
27
27
|
|
|
28
|
+
export {
|
|
29
|
+
Drawer,
|
|
30
|
+
DrawerPortal,
|
|
31
|
+
DrawerOverlay,
|
|
32
|
+
DrawerTrigger,
|
|
33
|
+
DrawerClose,
|
|
34
|
+
DrawerContent,
|
|
35
|
+
DrawerHeader,
|
|
36
|
+
DrawerFooter,
|
|
37
|
+
DrawerTitle,
|
|
38
|
+
DrawerDescription,
|
|
39
|
+
} from './drawer'
|
|
40
|
+
|
|
41
|
+
|
|
28
42
|
export {
|
|
29
43
|
useFormField,
|
|
30
44
|
Form,
|
|
@@ -67,18 +81,6 @@ export {
|
|
|
67
81
|
|
|
68
82
|
export { default as BreakpointIndicator } from './breakpoint-indicator'
|
|
69
83
|
|
|
70
|
-
export {
|
|
71
|
-
type ToastProps,
|
|
72
|
-
type ToastActionElement,
|
|
73
|
-
ToastProvider,
|
|
74
|
-
ToastViewport,
|
|
75
|
-
Toast,
|
|
76
|
-
ToastTitle,
|
|
77
|
-
ToastDescription,
|
|
78
|
-
ToastClose,
|
|
79
|
-
ToastAction,
|
|
80
|
-
} from './toast'
|
|
81
|
-
|
|
82
84
|
export {
|
|
83
85
|
Select,
|
|
84
86
|
SelectGroup,
|
|
@@ -149,8 +151,7 @@ export { ToggleGroup, ToggleGroupItem } from './toggle-group'
|
|
|
149
151
|
|
|
150
152
|
export { ScrollArea, ScrollBar } from './scroll-area'
|
|
151
153
|
|
|
152
|
-
export { Toaster } from './
|
|
153
|
-
export { useToast, toast } from './use-toast'
|
|
154
|
+
export { Toaster, toast } from './sonner'
|
|
154
155
|
export { RadioGroup, RadioGroupItem } from './radio-group'
|
|
155
156
|
|
|
156
157
|
export { default as AspectRatio } from './aspect-ratio'
|
package/primitives/sheet.tsx
CHANGED
|
@@ -54,7 +54,7 @@ interface SheetContentProps
|
|
|
54
54
|
VariantProps<typeof sheetVariants> {}
|
|
55
55
|
|
|
56
56
|
const closeUIclx = 'rounded-sm opacity-70 ring-offset-background ' +
|
|
57
|
-
'transition-opacity hover:opacity-100 disabled:pointer-events-none data-[state=open]:bg-
|
|
57
|
+
'transition-opacity hover:opacity-100 disabled:pointer-events-none data-[state=open]:bg-primary'
|
|
58
58
|
|
|
59
59
|
const SheetContent = React.forwardRef<
|
|
60
60
|
React.ElementRef<typeof SheetPrimitive.Content>,
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"use client"
|
|
2
|
+
|
|
3
|
+
import { useTheme } from "next-themes"
|
|
4
|
+
import { Toaster as Sonner } from "sonner"
|
|
5
|
+
import { toast } from "sonner"
|
|
6
|
+
|
|
7
|
+
type ToasterProps = React.ComponentProps<typeof Sonner>
|
|
8
|
+
|
|
9
|
+
const Toaster = ({ ...props }: ToasterProps) => {
|
|
10
|
+
const { theme = "system" } = useTheme()
|
|
11
|
+
|
|
12
|
+
return (
|
|
13
|
+
<Sonner
|
|
14
|
+
theme={theme as ToasterProps["theme"]}
|
|
15
|
+
className="toaster group"
|
|
16
|
+
toastOptions={{
|
|
17
|
+
classNames: {
|
|
18
|
+
toast:
|
|
19
|
+
"group toast group-[.toaster]:bg-background group-[.toaster]:text-foreground group-[.toaster]:border-muted-3 group-[.toaster]:shadow-lg",
|
|
20
|
+
description: "group-[.toast]:text-foreground",
|
|
21
|
+
actionButton:
|
|
22
|
+
"group-[.toast]:bg-primary group-[.toast]:text-muted-3",
|
|
23
|
+
cancelButton:
|
|
24
|
+
"group-[.toast]:bg-level-3 group-[.toast]:text-muted-3",
|
|
25
|
+
},
|
|
26
|
+
}}
|
|
27
|
+
{...props}
|
|
28
|
+
/>
|
|
29
|
+
)
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export {
|
|
33
|
+
toast,
|
|
34
|
+
Toaster
|
|
35
|
+
}
|
|
@@ -33,6 +33,10 @@ export default ({ colors }) => ({
|
|
|
33
33
|
lux: "var(--hz-ui-secondary)", // in case there are two configs
|
|
34
34
|
hover: "var(--hz-ui-secondary-hover)",
|
|
35
35
|
fg: "var(--hz-ui-secondary-fg)",
|
|
36
|
+
'0': 'var(--hz-ui-secondary-0)',
|
|
37
|
+
'1': 'var(--hz-ui-secondary-1)',
|
|
38
|
+
'2': 'var(--hz-ui-secondary-2)',
|
|
39
|
+
'3': 'var(--hz-ui-secondary-3)',
|
|
36
40
|
},
|
|
37
41
|
destructive: {
|
|
38
42
|
DEFAULT: "var(--hz-ui-destructive)",
|
package/primitives/toast.tsx
DELETED
|
@@ -1,129 +0,0 @@
|
|
|
1
|
-
// @ts-nocheck
|
|
2
|
-
'use client'
|
|
3
|
-
import * as React from "react"
|
|
4
|
-
import * as ToastPrimitives from "@radix-ui/react-toast"
|
|
5
|
-
import { cva, type VariantProps } from "class-variance-authority"
|
|
6
|
-
import { X } from "lucide-react"
|
|
7
|
-
|
|
8
|
-
import { cn } from "../util"
|
|
9
|
-
|
|
10
|
-
const ToastProvider = ToastPrimitives.Provider
|
|
11
|
-
|
|
12
|
-
const ToastViewport = React.forwardRef<
|
|
13
|
-
React.ElementRef<typeof ToastPrimitives.Viewport>,
|
|
14
|
-
React.ComponentPropsWithoutRef<typeof ToastPrimitives.Viewport>
|
|
15
|
-
>(({ className, ...props }, ref) => (
|
|
16
|
-
<ToastPrimitives.Viewport
|
|
17
|
-
ref={ref}
|
|
18
|
-
className={cn(
|
|
19
|
-
"fixed top-0 z-[100] flex max-h-screen w-full flex-col-reverse p-4 sm:bottom-0 sm:right-0 sm:top-auto sm:flex-col md:max-w-[420px]",
|
|
20
|
-
className
|
|
21
|
-
)}
|
|
22
|
-
{...props}
|
|
23
|
-
/>
|
|
24
|
-
))
|
|
25
|
-
ToastViewport.displayName = ToastPrimitives.Viewport.displayName
|
|
26
|
-
|
|
27
|
-
const toastVariants = cva(
|
|
28
|
-
"group pointer-events-auto relative flex w-full items-center justify-between space-x-4 overflow-hidden rounded-md border p-6 pr-8 shadow-lg transition-all data-[swipe=cancel]:translate-x-0 data-[swipe=end]:translate-x-[var(--radix-toast-swipe-end-x)] data-[swipe=move]:translate-x-[var(--radix-toast-swipe-move-x)] data-[swipe=move]:transition-none data-[state=open]:animate-in data-[state=closed]:animate-out data-[swipe=end]:animate-out data-[state=closed]:fade-out-80 data-[state=closed]:slide-out-to-right-full data-[state=open]:slide-in-from-top-full data-[state=open]:sm:slide-in-from-bottom-full",
|
|
29
|
-
{
|
|
30
|
-
variants: {
|
|
31
|
-
variant: {
|
|
32
|
-
default: "border bg-background text-foreground",
|
|
33
|
-
destructive:
|
|
34
|
-
"destructive group border-destructive bg-destructive text-destructive-fg",
|
|
35
|
-
},
|
|
36
|
-
},
|
|
37
|
-
defaultVariants: {
|
|
38
|
-
variant: "default",
|
|
39
|
-
},
|
|
40
|
-
}
|
|
41
|
-
)
|
|
42
|
-
|
|
43
|
-
const Toast = React.forwardRef<
|
|
44
|
-
React.ElementRef<typeof ToastPrimitives.Root>,
|
|
45
|
-
React.ComponentPropsWithoutRef<typeof ToastPrimitives.Root> &
|
|
46
|
-
VariantProps<typeof toastVariants>
|
|
47
|
-
>(({ className, variant, ...props }, ref) => {
|
|
48
|
-
return (
|
|
49
|
-
<ToastPrimitives.Root
|
|
50
|
-
ref={ref}
|
|
51
|
-
className={cn(toastVariants({ variant }), className)}
|
|
52
|
-
{...props}
|
|
53
|
-
/>
|
|
54
|
-
)
|
|
55
|
-
})
|
|
56
|
-
Toast.displayName = ToastPrimitives.Root.displayName
|
|
57
|
-
|
|
58
|
-
const ToastAction = React.forwardRef<
|
|
59
|
-
React.ElementRef<typeof ToastPrimitives.Action>,
|
|
60
|
-
React.ComponentPropsWithoutRef<typeof ToastPrimitives.Action>
|
|
61
|
-
>(({ className, ...props }, ref) => (
|
|
62
|
-
<ToastPrimitives.Action
|
|
63
|
-
ref={ref}
|
|
64
|
-
className={cn(
|
|
65
|
-
"inline-flex h-8 shrink-0 items-center justify-center rounded-md border bg-transparent px-3 text-sm font-medium ring-offset-background transition-colors hover:bg-secondary focus:outline-none focus:ring-2 focus:ring-ring focus:ring-offset-2 disabled:pointer-events-none disabled:opacity-50 group-[.destructive]:border-muted-1/40 group-[.destructive]:hover:border-destructive/30 group-[.destructive]:hover:bg-destructive group-[.destructive]:hover:text-destructive-fg group-[.destructive]:focus:ring-destructive",
|
|
66
|
-
className
|
|
67
|
-
)}
|
|
68
|
-
{...props}
|
|
69
|
-
/>
|
|
70
|
-
))
|
|
71
|
-
ToastAction.displayName = ToastPrimitives.Action.displayName
|
|
72
|
-
|
|
73
|
-
const ToastClose = React.forwardRef<
|
|
74
|
-
React.ElementRef<typeof ToastPrimitives.Close>,
|
|
75
|
-
React.ComponentPropsWithoutRef<typeof ToastPrimitives.Close>
|
|
76
|
-
>(({ className, ...props }, ref) => (
|
|
77
|
-
<ToastPrimitives.Close
|
|
78
|
-
ref={ref}
|
|
79
|
-
className={cn(
|
|
80
|
-
"absolute right-2 top-2 rounded-md p-1 text-foreground/50 opacity-0 transition-opacity hover:text-foreground focus:opacity-100 focus:outline-none focus:ring-2 group-hover:opacity-100 group-[.destructive]:text-red-300 group-[.destructive]:hover:text-red-50 group-[.destructive]:focus:ring-red-400 group-[.destructive]:focus:ring-offset-red-600",
|
|
81
|
-
className
|
|
82
|
-
)}
|
|
83
|
-
toast-close=""
|
|
84
|
-
{...props}
|
|
85
|
-
>
|
|
86
|
-
<X className="h-4 w-4" />
|
|
87
|
-
</ToastPrimitives.Close>
|
|
88
|
-
))
|
|
89
|
-
ToastClose.displayName = ToastPrimitives.Close.displayName
|
|
90
|
-
|
|
91
|
-
const ToastTitle = React.forwardRef<
|
|
92
|
-
React.ElementRef<typeof ToastPrimitives.Title>,
|
|
93
|
-
React.ComponentPropsWithoutRef<typeof ToastPrimitives.Title>
|
|
94
|
-
>(({ className, ...props }, ref) => (
|
|
95
|
-
<ToastPrimitives.Title
|
|
96
|
-
ref={ref}
|
|
97
|
-
className={cn("text-sm font-semibold", className)}
|
|
98
|
-
{...props}
|
|
99
|
-
/>
|
|
100
|
-
))
|
|
101
|
-
ToastTitle.displayName = ToastPrimitives.Title.displayName
|
|
102
|
-
|
|
103
|
-
const ToastDescription = React.forwardRef<
|
|
104
|
-
React.ElementRef<typeof ToastPrimitives.Description>,
|
|
105
|
-
React.ComponentPropsWithoutRef<typeof ToastPrimitives.Description>
|
|
106
|
-
>(({ className, ...props }, ref) => (
|
|
107
|
-
<ToastPrimitives.Description
|
|
108
|
-
ref={ref}
|
|
109
|
-
className={cn("text-sm opacity-90", className)}
|
|
110
|
-
{...props}
|
|
111
|
-
/>
|
|
112
|
-
))
|
|
113
|
-
ToastDescription.displayName = ToastPrimitives.Description.displayName
|
|
114
|
-
|
|
115
|
-
type ToastProps = React.ComponentPropsWithoutRef<typeof Toast>
|
|
116
|
-
|
|
117
|
-
type ToastActionElement = React.ReactElement<typeof ToastAction>
|
|
118
|
-
|
|
119
|
-
export {
|
|
120
|
-
type ToastProps,
|
|
121
|
-
type ToastActionElement,
|
|
122
|
-
ToastProvider,
|
|
123
|
-
ToastViewport,
|
|
124
|
-
Toast,
|
|
125
|
-
ToastTitle,
|
|
126
|
-
ToastDescription,
|
|
127
|
-
ToastClose,
|
|
128
|
-
ToastAction,
|
|
129
|
-
}
|
package/primitives/toaster.tsx
DELETED
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
// @ts-nocheck
|
|
2
|
-
"use client"
|
|
3
|
-
|
|
4
|
-
import {
|
|
5
|
-
Toast,
|
|
6
|
-
ToastClose,
|
|
7
|
-
ToastDescription,
|
|
8
|
-
ToastProvider,
|
|
9
|
-
ToastTitle,
|
|
10
|
-
ToastViewport,
|
|
11
|
-
} from "./toast"
|
|
12
|
-
|
|
13
|
-
import { useToast } from "./use-toast"
|
|
14
|
-
|
|
15
|
-
export const Toaster: React.FC = () => {
|
|
16
|
-
const { toasts } = useToast()
|
|
17
|
-
|
|
18
|
-
return (
|
|
19
|
-
<ToastProvider>
|
|
20
|
-
{toasts.map(function ({ id, title, description, action, ...props }) {
|
|
21
|
-
return (
|
|
22
|
-
<Toast key={id} {...props}>
|
|
23
|
-
<div className="grid gap-1">
|
|
24
|
-
{title && <ToastTitle>{title}</ToastTitle>}
|
|
25
|
-
{description && (
|
|
26
|
-
<ToastDescription>{description}</ToastDescription>
|
|
27
|
-
)}
|
|
28
|
-
</div>
|
|
29
|
-
{action}
|
|
30
|
-
<ToastClose />
|
|
31
|
-
</Toast>
|
|
32
|
-
)
|
|
33
|
-
})}
|
|
34
|
-
<ToastViewport />
|
|
35
|
-
</ToastProvider>
|
|
36
|
-
)
|
|
37
|
-
}
|
package/primitives/use-toast.ts
DELETED
|
@@ -1,192 +0,0 @@
|
|
|
1
|
-
// Inspired by react-hot-toast library
|
|
2
|
-
import * as React from "react"
|
|
3
|
-
|
|
4
|
-
import type {
|
|
5
|
-
ToastActionElement,
|
|
6
|
-
ToastProps,
|
|
7
|
-
} from "./toast"
|
|
8
|
-
|
|
9
|
-
const TOAST_LIMIT = 1
|
|
10
|
-
const TOAST_REMOVE_DELAY = 1000000
|
|
11
|
-
|
|
12
|
-
type ToasterToast = ToastProps & {
|
|
13
|
-
id: string
|
|
14
|
-
title?: React.ReactNode
|
|
15
|
-
description?: React.ReactNode
|
|
16
|
-
action?: ToastActionElement
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
const actionTypes = {
|
|
20
|
-
ADD_TOAST: "ADD_TOAST",
|
|
21
|
-
UPDATE_TOAST: "UPDATE_TOAST",
|
|
22
|
-
DISMISS_TOAST: "DISMISS_TOAST",
|
|
23
|
-
REMOVE_TOAST: "REMOVE_TOAST",
|
|
24
|
-
} as const
|
|
25
|
-
|
|
26
|
-
let count = 0
|
|
27
|
-
|
|
28
|
-
function genId() {
|
|
29
|
-
count = (count + 1) % Number.MAX_VALUE
|
|
30
|
-
return count.toString()
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
type ActionType = typeof actionTypes
|
|
34
|
-
|
|
35
|
-
type Action =
|
|
36
|
-
| {
|
|
37
|
-
type: ActionType["ADD_TOAST"]
|
|
38
|
-
toast: ToasterToast
|
|
39
|
-
}
|
|
40
|
-
| {
|
|
41
|
-
type: ActionType["UPDATE_TOAST"]
|
|
42
|
-
toast: Partial<ToasterToast>
|
|
43
|
-
}
|
|
44
|
-
| {
|
|
45
|
-
type: ActionType["DISMISS_TOAST"]
|
|
46
|
-
toastId?: ToasterToast["id"]
|
|
47
|
-
}
|
|
48
|
-
| {
|
|
49
|
-
type: ActionType["REMOVE_TOAST"]
|
|
50
|
-
toastId?: ToasterToast["id"]
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
interface State {
|
|
54
|
-
toasts: ToasterToast[]
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
const toastTimeouts = new Map<string, ReturnType<typeof setTimeout>>()
|
|
58
|
-
|
|
59
|
-
const addToRemoveQueue = (toastId: string) => {
|
|
60
|
-
if (toastTimeouts.has(toastId)) {
|
|
61
|
-
return
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
const timeout = setTimeout(() => {
|
|
65
|
-
toastTimeouts.delete(toastId)
|
|
66
|
-
dispatch({
|
|
67
|
-
type: "REMOVE_TOAST",
|
|
68
|
-
toastId: toastId,
|
|
69
|
-
})
|
|
70
|
-
}, TOAST_REMOVE_DELAY)
|
|
71
|
-
|
|
72
|
-
toastTimeouts.set(toastId, timeout)
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
const reducer = (state: State, action: Action): State => {
|
|
76
|
-
switch (action.type) {
|
|
77
|
-
case "ADD_TOAST":
|
|
78
|
-
return {
|
|
79
|
-
...state,
|
|
80
|
-
toasts: [action.toast, ...state.toasts].slice(0, TOAST_LIMIT),
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
case "UPDATE_TOAST":
|
|
84
|
-
return {
|
|
85
|
-
...state,
|
|
86
|
-
toasts: state.toasts.map((t) =>
|
|
87
|
-
t.id === action.toast.id ? { ...t, ...action.toast } : t
|
|
88
|
-
),
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
case "DISMISS_TOAST": {
|
|
92
|
-
const { toastId } = action
|
|
93
|
-
|
|
94
|
-
// ! Side effects ! - This could be extracted into a dismissToast() action,
|
|
95
|
-
// but I'll keep it here for simplicity
|
|
96
|
-
if (toastId) {
|
|
97
|
-
addToRemoveQueue(toastId)
|
|
98
|
-
} else {
|
|
99
|
-
state.toasts.forEach((toast) => {
|
|
100
|
-
addToRemoveQueue(toast.id)
|
|
101
|
-
})
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
return {
|
|
105
|
-
...state,
|
|
106
|
-
toasts: state.toasts.map((t) =>
|
|
107
|
-
t.id === toastId || toastId === undefined
|
|
108
|
-
? {
|
|
109
|
-
...t,
|
|
110
|
-
open: false,
|
|
111
|
-
}
|
|
112
|
-
: t
|
|
113
|
-
),
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
case "REMOVE_TOAST":
|
|
117
|
-
if (action.toastId === undefined) {
|
|
118
|
-
return {
|
|
119
|
-
...state,
|
|
120
|
-
toasts: [],
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
return {
|
|
124
|
-
...state,
|
|
125
|
-
toasts: state.toasts.filter((t) => t.id !== action.toastId),
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
const listeners: Array<(state: State) => void> = []
|
|
131
|
-
|
|
132
|
-
let memoryState: State = { toasts: [] }
|
|
133
|
-
|
|
134
|
-
function dispatch(action: Action) {
|
|
135
|
-
memoryState = reducer(memoryState, action)
|
|
136
|
-
listeners.forEach((listener) => {
|
|
137
|
-
listener(memoryState)
|
|
138
|
-
})
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
type Toast = Omit<ToasterToast, "id">
|
|
142
|
-
|
|
143
|
-
function toast({ ...props }: Toast) {
|
|
144
|
-
const id = genId()
|
|
145
|
-
|
|
146
|
-
const update = (props: ToasterToast) =>
|
|
147
|
-
dispatch({
|
|
148
|
-
type: "UPDATE_TOAST",
|
|
149
|
-
toast: { ...props, id },
|
|
150
|
-
})
|
|
151
|
-
const dismiss = () => dispatch({ type: "DISMISS_TOAST", toastId: id })
|
|
152
|
-
|
|
153
|
-
dispatch({
|
|
154
|
-
type: "ADD_TOAST",
|
|
155
|
-
toast: {
|
|
156
|
-
...props,
|
|
157
|
-
id,
|
|
158
|
-
open: true,
|
|
159
|
-
onOpenChange: (open: any) => {
|
|
160
|
-
if (!open) dismiss()
|
|
161
|
-
},
|
|
162
|
-
},
|
|
163
|
-
})
|
|
164
|
-
|
|
165
|
-
return {
|
|
166
|
-
id: id,
|
|
167
|
-
dismiss,
|
|
168
|
-
update,
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
function useToast() {
|
|
173
|
-
const [state, setState] = React.useState<State>(memoryState)
|
|
174
|
-
|
|
175
|
-
React.useEffect(() => {
|
|
176
|
-
listeners.push(setState)
|
|
177
|
-
return () => {
|
|
178
|
-
const index = listeners.indexOf(setState)
|
|
179
|
-
if (index > -1) {
|
|
180
|
-
listeners.splice(index, 1)
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
}, [state])
|
|
184
|
-
|
|
185
|
-
return {
|
|
186
|
-
...state,
|
|
187
|
-
toast,
|
|
188
|
-
dismiss: (toastId?: string) => dispatch({ type: "DISMISS_TOAST", toastId }),
|
|
189
|
-
}
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
export { useToast, toast }
|