@medway-ui/native 1.0.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/README.md +153 -0
- package/lib/commonjs/components/Alert.js +206 -0
- package/lib/commonjs/components/Alert.js.map +1 -0
- package/lib/commonjs/components/AlertDialog.js +273 -0
- package/lib/commonjs/components/AlertDialog.js.map +1 -0
- package/lib/commonjs/components/Badge.js +94 -0
- package/lib/commonjs/components/Badge.js.map +1 -0
- package/lib/commonjs/components/Button.js +181 -0
- package/lib/commonjs/components/Button.js.map +1 -0
- package/lib/commonjs/components/Checkbox.js +91 -0
- package/lib/commonjs/components/Checkbox.js.map +1 -0
- package/lib/commonjs/components/Dialog.js +164 -0
- package/lib/commonjs/components/Dialog.js.map +1 -0
- package/lib/commonjs/components/Input.js +364 -0
- package/lib/commonjs/components/Input.js.map +1 -0
- package/lib/commonjs/components/Label.js +90 -0
- package/lib/commonjs/components/Label.js.map +1 -0
- package/lib/commonjs/components/Loading.js +49 -0
- package/lib/commonjs/components/Loading.js.map +1 -0
- package/lib/commonjs/components/Popover.js +54 -0
- package/lib/commonjs/components/Popover.js.map +1 -0
- package/lib/commonjs/components/RadioGroup.js +72 -0
- package/lib/commonjs/components/RadioGroup.js.map +1 -0
- package/lib/commonjs/components/Select.js +234 -0
- package/lib/commonjs/components/Select.js.map +1 -0
- package/lib/commonjs/components/Separator.js +22 -0
- package/lib/commonjs/components/Separator.js.map +1 -0
- package/lib/commonjs/components/Sheet.js +148 -0
- package/lib/commonjs/components/Sheet.js.map +1 -0
- package/lib/commonjs/components/Switch.js +99 -0
- package/lib/commonjs/components/Switch.js.map +1 -0
- package/lib/commonjs/components/Tag.js +132 -0
- package/lib/commonjs/components/Tag.js.map +1 -0
- package/lib/commonjs/components/Toast.js +244 -0
- package/lib/commonjs/components/Toast.js.map +1 -0
- package/lib/commonjs/components/Tooltip.js +54 -0
- package/lib/commonjs/components/Tooltip.js.map +1 -0
- package/lib/commonjs/hooks/use-toast.js +136 -0
- package/lib/commonjs/hooks/use-toast.js.map +1 -0
- package/lib/commonjs/index.js +667 -0
- package/lib/commonjs/index.js.map +1 -0
- package/lib/commonjs/lib/utils.js +22 -0
- package/lib/commonjs/lib/utils.js.map +1 -0
- package/lib/commonjs/nativewind-env.d.js +3 -0
- package/lib/commonjs/nativewind-env.d.js.map +1 -0
- package/lib/module/components/Alert.js +195 -0
- package/lib/module/components/Alert.js.map +1 -0
- package/lib/module/components/AlertDialog.js +257 -0
- package/lib/module/components/AlertDialog.js.map +1 -0
- package/lib/module/components/Badge.js +90 -0
- package/lib/module/components/Badge.js.map +1 -0
- package/lib/module/components/Button.js +177 -0
- package/lib/module/components/Button.js.map +1 -0
- package/lib/module/components/Checkbox.js +86 -0
- package/lib/module/components/Checkbox.js.map +1 -0
- package/lib/module/components/Dialog.js +150 -0
- package/lib/module/components/Dialog.js.map +1 -0
- package/lib/module/components/Input.js +349 -0
- package/lib/module/components/Input.js.map +1 -0
- package/lib/module/components/Label.js +86 -0
- package/lib/module/components/Label.js.map +1 -0
- package/lib/module/components/Loading.js +45 -0
- package/lib/module/components/Loading.js.map +1 -0
- package/lib/module/components/Popover.js +47 -0
- package/lib/module/components/Popover.js.map +1 -0
- package/lib/module/components/RadioGroup.js +66 -0
- package/lib/module/components/RadioGroup.js.map +1 -0
- package/lib/module/components/Select.js +217 -0
- package/lib/module/components/Select.js.map +1 -0
- package/lib/module/components/Separator.js +19 -0
- package/lib/module/components/Separator.js.map +1 -0
- package/lib/module/components/Sheet.js +136 -0
- package/lib/module/components/Sheet.js.map +1 -0
- package/lib/module/components/Switch.js +94 -0
- package/lib/module/components/Switch.js.map +1 -0
- package/lib/module/components/Tag.js +128 -0
- package/lib/module/components/Tag.js.map +1 -0
- package/lib/module/components/Toast.js +233 -0
- package/lib/module/components/Toast.js.map +1 -0
- package/lib/module/components/Tooltip.js +47 -0
- package/lib/module/components/Tooltip.js.map +1 -0
- package/lib/module/hooks/use-toast.js +130 -0
- package/lib/module/hooks/use-toast.js.map +1 -0
- package/lib/module/index.js +41 -0
- package/lib/module/index.js.map +1 -0
- package/lib/module/lib/utils.js +18 -0
- package/lib/module/lib/utils.js.map +1 -0
- package/lib/module/nativewind-env.d.js +3 -0
- package/lib/module/nativewind-env.d.js.map +1 -0
- package/lib/module/package.json +1 -0
- package/lib/typescript/components/Alert.d.ts +36 -0
- package/lib/typescript/components/Alert.d.ts.map +1 -0
- package/lib/typescript/components/AlertDialog.d.ts +44 -0
- package/lib/typescript/components/AlertDialog.d.ts.map +1 -0
- package/lib/typescript/components/Badge.d.ts +17 -0
- package/lib/typescript/components/Badge.d.ts.map +1 -0
- package/lib/typescript/components/Button.d.ts +24 -0
- package/lib/typescript/components/Button.d.ts.map +1 -0
- package/lib/typescript/components/Checkbox.d.ts +20 -0
- package/lib/typescript/components/Checkbox.d.ts.map +1 -0
- package/lib/typescript/components/Dialog.d.ts +35 -0
- package/lib/typescript/components/Dialog.d.ts.map +1 -0
- package/lib/typescript/components/Input.d.ts +57 -0
- package/lib/typescript/components/Input.d.ts.map +1 -0
- package/lib/typescript/components/Label.d.ts +21 -0
- package/lib/typescript/components/Label.d.ts.map +1 -0
- package/lib/typescript/components/Loading.d.ts +9 -0
- package/lib/typescript/components/Loading.d.ts.map +1 -0
- package/lib/typescript/components/Popover.d.ts +11 -0
- package/lib/typescript/components/Popover.d.ts.map +1 -0
- package/lib/typescript/components/RadioGroup.d.ts +13 -0
- package/lib/typescript/components/RadioGroup.d.ts.map +1 -0
- package/lib/typescript/components/Select.d.ts +37 -0
- package/lib/typescript/components/Select.d.ts.map +1 -0
- package/lib/typescript/components/Separator.d.ts +9 -0
- package/lib/typescript/components/Separator.d.ts.map +1 -0
- package/lib/typescript/components/Sheet.d.ts +31 -0
- package/lib/typescript/components/Sheet.d.ts.map +1 -0
- package/lib/typescript/components/Switch.d.ts +18 -0
- package/lib/typescript/components/Switch.d.ts.map +1 -0
- package/lib/typescript/components/Tag.d.ts +17 -0
- package/lib/typescript/components/Tag.d.ts.map +1 -0
- package/lib/typescript/components/Toast.d.ts +42 -0
- package/lib/typescript/components/Toast.d.ts.map +1 -0
- package/lib/typescript/components/Tooltip.d.ts +14 -0
- package/lib/typescript/components/Tooltip.d.ts.map +1 -0
- package/lib/typescript/hooks/use-toast.d.ts +46 -0
- package/lib/typescript/hooks/use-toast.d.ts.map +1 -0
- package/lib/typescript/index.d.ts +38 -0
- package/lib/typescript/index.d.ts.map +1 -0
- package/lib/typescript/lib/utils.d.ts +3 -0
- package/lib/typescript/lib/utils.d.ts.map +1 -0
- package/package.json +111 -0
- package/src/components/Alert.tsx +185 -0
- package/src/components/AlertDialog.tsx +277 -0
- package/src/components/Badge.tsx +113 -0
- package/src/components/Button.tsx +224 -0
- package/src/components/Checkbox.tsx +106 -0
- package/src/components/Dialog.tsx +175 -0
- package/src/components/Input.tsx +439 -0
- package/src/components/Label.tsx +95 -0
- package/src/components/Loading.tsx +54 -0
- package/src/components/Popover.tsx +46 -0
- package/src/components/RadioGroup.tsx +62 -0
- package/src/components/Select.tsx +246 -0
- package/src/components/Separator.tsx +24 -0
- package/src/components/Sheet.tsx +165 -0
- package/src/components/Switch.tsx +138 -0
- package/src/components/Tag.tsx +116 -0
- package/src/components/Toast.tsx +265 -0
- package/src/components/Tooltip.tsx +49 -0
- package/src/hooks/use-toast.ts +144 -0
- package/src/index.ts +193 -0
- package/src/lib/utils.ts +63 -0
- package/src/nativewind-env.d.ts +1 -0
|
@@ -0,0 +1,265 @@
|
|
|
1
|
+
import { useState, useEffect, type ReactElement, type ReactNode } from "react";
|
|
2
|
+
import { View, Text, Pressable, type ViewProps } from "react-native";
|
|
3
|
+
import {
|
|
4
|
+
Info,
|
|
5
|
+
CircleCheck,
|
|
6
|
+
TriangleWarning,
|
|
7
|
+
OctagonWarning,
|
|
8
|
+
CloseMd,
|
|
9
|
+
} from "@medway-ui/icons/native";
|
|
10
|
+
import { cva } from "class-variance-authority";
|
|
11
|
+
import Animated, {
|
|
12
|
+
useSharedValue,
|
|
13
|
+
useAnimatedStyle,
|
|
14
|
+
withTiming,
|
|
15
|
+
withDelay,
|
|
16
|
+
runOnJS,
|
|
17
|
+
} from "react-native-reanimated";
|
|
18
|
+
import { cn } from "@/lib/utils";
|
|
19
|
+
import { Button, type ButtonProps } from "@/components/Button";
|
|
20
|
+
import { useToast } from "@/hooks/use-toast";
|
|
21
|
+
|
|
22
|
+
type ToastVariant = "primary" | "secondary" | "success" | "warning" | "error";
|
|
23
|
+
type ToastActionVariant = ToastVariant;
|
|
24
|
+
|
|
25
|
+
type ToastActionElement = ReactElement<typeof ToastAction>;
|
|
26
|
+
|
|
27
|
+
interface ToastProps extends ViewProps {
|
|
28
|
+
variant?: ToastVariant;
|
|
29
|
+
showIcon?: boolean;
|
|
30
|
+
duration?: number;
|
|
31
|
+
open?: boolean;
|
|
32
|
+
onOpenChange?: (open: boolean) => void;
|
|
33
|
+
children?: ReactNode;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
interface ToastActionProps extends Omit<ButtonProps, "variant"> {
|
|
37
|
+
variant?: ToastActionVariant;
|
|
38
|
+
altText?: string;
|
|
39
|
+
onPress?: () => void;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
interface ToastCloseProps extends Omit<ViewProps, "children"> {
|
|
43
|
+
onPress?: () => void;
|
|
44
|
+
label?: string;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const toastVariants = cva(
|
|
48
|
+
"flex-row items-center gap-3 w-full rounded-md bg-surface-elevated p-3 shadow-lg",
|
|
49
|
+
{
|
|
50
|
+
variants: {
|
|
51
|
+
variant: {
|
|
52
|
+
primary: "border-l-4 border-primary-foreground",
|
|
53
|
+
secondary: "border-l-4 border-secondary-foreground",
|
|
54
|
+
success: "border-l-4 border-feedback-success-foreground",
|
|
55
|
+
warning: "border-l-4 border-feedback-warning-foreground",
|
|
56
|
+
error: "border-l-4 border-feedback-error-foreground",
|
|
57
|
+
},
|
|
58
|
+
},
|
|
59
|
+
defaultVariants: { variant: "primary" },
|
|
60
|
+
}
|
|
61
|
+
);
|
|
62
|
+
|
|
63
|
+
const toastIconVariants = cva("shrink-0", {
|
|
64
|
+
variants: {
|
|
65
|
+
variant: {
|
|
66
|
+
primary: "text-primary-foreground",
|
|
67
|
+
secondary: "text-secondary-foreground",
|
|
68
|
+
success: "text-feedback-success-foreground",
|
|
69
|
+
warning: "text-feedback-warning-foreground",
|
|
70
|
+
error: "text-feedback-error-foreground",
|
|
71
|
+
},
|
|
72
|
+
},
|
|
73
|
+
defaultVariants: { variant: "primary" },
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
const toastActionButtonVariantMap: Record<ToastActionVariant, ButtonProps["variant"]> = {
|
|
77
|
+
primary: "regular-soft",
|
|
78
|
+
secondary: "emphasis-soft",
|
|
79
|
+
success: "success-soft",
|
|
80
|
+
warning: "warning-soft",
|
|
81
|
+
error: "error-soft",
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
const variantIcons: Record<ToastVariant, typeof Info> = {
|
|
85
|
+
primary: Info,
|
|
86
|
+
secondary: Info,
|
|
87
|
+
success: CircleCheck,
|
|
88
|
+
warning: TriangleWarning,
|
|
89
|
+
error: OctagonWarning,
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
function ToastItem({
|
|
93
|
+
className,
|
|
94
|
+
variant = "primary",
|
|
95
|
+
showIcon = true,
|
|
96
|
+
open = true,
|
|
97
|
+
onOpenChange,
|
|
98
|
+
children,
|
|
99
|
+
...props
|
|
100
|
+
}: ToastProps) {
|
|
101
|
+
const [visible, setVisible] = useState(open);
|
|
102
|
+
const translateY = useSharedValue(open ? 0 : -80);
|
|
103
|
+
const opacity = useSharedValue(open ? 1 : 0);
|
|
104
|
+
|
|
105
|
+
const animatedStyle = useAnimatedStyle(() => ({
|
|
106
|
+
transform: [{ translateY: translateY.value }],
|
|
107
|
+
opacity: opacity.value,
|
|
108
|
+
}));
|
|
109
|
+
|
|
110
|
+
useEffect(() => {
|
|
111
|
+
if (open) {
|
|
112
|
+
setVisible(true);
|
|
113
|
+
translateY.value = withTiming(0, { duration: 300 });
|
|
114
|
+
opacity.value = withTiming(1, { duration: 300 });
|
|
115
|
+
} else {
|
|
116
|
+
translateY.value = withDelay(50, withTiming(-80, { duration: 200 }));
|
|
117
|
+
opacity.value = withTiming(0, { duration: 200 }, (finished) => {
|
|
118
|
+
if (finished) runOnJS(setVisible)(false);
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
}, [open]);
|
|
122
|
+
|
|
123
|
+
if (!visible) return null;
|
|
124
|
+
|
|
125
|
+
const IconComponent = variantIcons[variant];
|
|
126
|
+
|
|
127
|
+
return (
|
|
128
|
+
<Animated.View style={animatedStyle}>
|
|
129
|
+
<View className={cn(toastVariants({ variant }), className)} {...props}>
|
|
130
|
+
{showIcon && (
|
|
131
|
+
<IconComponent size={20} className={cn(toastIconVariants({ variant }))} />
|
|
132
|
+
)}
|
|
133
|
+
<View className="flex-1 flex-row items-center justify-between gap-3">
|
|
134
|
+
{children}
|
|
135
|
+
</View>
|
|
136
|
+
<Pressable
|
|
137
|
+
onPress={() => onOpenChange?.(false)}
|
|
138
|
+
className="rounded-pill p-1 active:bg-gray-background-soft"
|
|
139
|
+
>
|
|
140
|
+
<CloseMd size={16} className="text-gray-foreground" />
|
|
141
|
+
</Pressable>
|
|
142
|
+
</View>
|
|
143
|
+
</Animated.View>
|
|
144
|
+
);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
function Toast({
|
|
148
|
+
variant = "primary",
|
|
149
|
+
showIcon = true,
|
|
150
|
+
duration,
|
|
151
|
+
open,
|
|
152
|
+
onOpenChange,
|
|
153
|
+
children,
|
|
154
|
+
...props
|
|
155
|
+
}: ToastProps) {
|
|
156
|
+
return (
|
|
157
|
+
<ToastItem
|
|
158
|
+
variant={variant}
|
|
159
|
+
showIcon={showIcon}
|
|
160
|
+
duration={duration}
|
|
161
|
+
open={open}
|
|
162
|
+
onOpenChange={onOpenChange}
|
|
163
|
+
{...props}
|
|
164
|
+
>
|
|
165
|
+
{children}
|
|
166
|
+
</ToastItem>
|
|
167
|
+
);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
function ToastAction({
|
|
171
|
+
className,
|
|
172
|
+
variant = "primary",
|
|
173
|
+
size = "sm",
|
|
174
|
+
altText,
|
|
175
|
+
children,
|
|
176
|
+
...props
|
|
177
|
+
}: ToastActionProps) {
|
|
178
|
+
return (
|
|
179
|
+
<Button
|
|
180
|
+
size={size}
|
|
181
|
+
variant={toastActionButtonVariantMap[variant]}
|
|
182
|
+
className={cn(className)}
|
|
183
|
+
{...props}
|
|
184
|
+
>
|
|
185
|
+
{children as ReactNode}
|
|
186
|
+
</Button>
|
|
187
|
+
);
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
function ToastTitle({ className, children }: ViewProps & { children?: ReactNode }) {
|
|
191
|
+
return (
|
|
192
|
+
<Text className={cn("text-md font-medium text-content-title flex-1", className)}>
|
|
193
|
+
{children}
|
|
194
|
+
</Text>
|
|
195
|
+
);
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
function ToastDescription({ className, children }: ViewProps & { children?: ReactNode }) {
|
|
199
|
+
return (
|
|
200
|
+
<Text className={cn("text-xs text-content-paragraph", className)}>
|
|
201
|
+
{children}
|
|
202
|
+
</Text>
|
|
203
|
+
);
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
function ToastViewport({ className, children, ...props }: ViewProps & { children?: ReactNode }) {
|
|
207
|
+
return (
|
|
208
|
+
<View
|
|
209
|
+
pointerEvents="box-none"
|
|
210
|
+
className={cn("absolute top-0 left-0 right-0 z-50 px-4 pt-12 gap-2", className)}
|
|
211
|
+
{...props}
|
|
212
|
+
>
|
|
213
|
+
{children}
|
|
214
|
+
</View>
|
|
215
|
+
);
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
function ToastClose({ className, onPress, label = "Close", ...props }: ToastCloseProps) {
|
|
219
|
+
return (
|
|
220
|
+
<Pressable
|
|
221
|
+
onPress={onPress}
|
|
222
|
+
accessibilityLabel={label}
|
|
223
|
+
className={cn("rounded-pill p-1 active:bg-gray-background-soft", className)}
|
|
224
|
+
{...props}
|
|
225
|
+
>
|
|
226
|
+
<CloseMd size={16} className="text-gray-foreground" />
|
|
227
|
+
</Pressable>
|
|
228
|
+
);
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
function ToastProvider() {
|
|
232
|
+
const { toasts, dismiss } = useToast();
|
|
233
|
+
|
|
234
|
+
return (
|
|
235
|
+
<ToastViewport>
|
|
236
|
+
{toasts.map(({ id, title, description, action, open = true, variant, showIcon }) => (
|
|
237
|
+
<Toast
|
|
238
|
+
key={id}
|
|
239
|
+
variant={variant}
|
|
240
|
+
showIcon={showIcon}
|
|
241
|
+
open={open}
|
|
242
|
+
onOpenChange={(isOpen) => !isOpen && dismiss(id)}
|
|
243
|
+
>
|
|
244
|
+
<View className="flex-col gap-0.5">
|
|
245
|
+
{title && <ToastTitle>{title}</ToastTitle>}
|
|
246
|
+
{description && <ToastDescription>{description}</ToastDescription>}
|
|
247
|
+
</View>
|
|
248
|
+
{action}
|
|
249
|
+
</Toast>
|
|
250
|
+
))}
|
|
251
|
+
</ToastViewport>
|
|
252
|
+
);
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
export {
|
|
256
|
+
Toast,
|
|
257
|
+
ToastAction,
|
|
258
|
+
ToastClose,
|
|
259
|
+
ToastDescription,
|
|
260
|
+
ToastProvider,
|
|
261
|
+
ToastTitle,
|
|
262
|
+
ToastViewport,
|
|
263
|
+
toastVariants,
|
|
264
|
+
};
|
|
265
|
+
export type { ToastActionElement, ToastProps };
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { type ReactNode } from "react";
|
|
2
|
+
import { Text } from "react-native";
|
|
3
|
+
import * as TooltipPrimitive from "@rn-primitives/tooltip";
|
|
4
|
+
import { cn } from "@/lib/utils";
|
|
5
|
+
|
|
6
|
+
interface TooltipContentProps extends TooltipPrimitive.ContentProps {
|
|
7
|
+
sideOffset?: number;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
function TooltipProvider({ children }: { children?: ReactNode }) {
|
|
11
|
+
return <>{children}</>;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function Tooltip({ ...props }: TooltipPrimitive.RootProps) {
|
|
15
|
+
return <TooltipPrimitive.Root {...props} />;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function TooltipTrigger({ ...props }: TooltipPrimitive.TriggerProps) {
|
|
19
|
+
return <TooltipPrimitive.Trigger {...props} />;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function TooltipContent({
|
|
23
|
+
className,
|
|
24
|
+
sideOffset = 0,
|
|
25
|
+
children,
|
|
26
|
+
...props
|
|
27
|
+
}: TooltipContentProps) {
|
|
28
|
+
return (
|
|
29
|
+
<TooltipPrimitive.Portal>
|
|
30
|
+
<TooltipPrimitive.Content
|
|
31
|
+
sideOffset={sideOffset}
|
|
32
|
+
className={cn(
|
|
33
|
+
"z-50 rounded-md bg-surface-elevated px-3 py-2 shadow-md",
|
|
34
|
+
className
|
|
35
|
+
)}
|
|
36
|
+
{...props}
|
|
37
|
+
>
|
|
38
|
+
{typeof children === "string" ? (
|
|
39
|
+
<Text className="text-sm font-medium text-gray-foreground-emphasis">{children}</Text>
|
|
40
|
+
) : (
|
|
41
|
+
children
|
|
42
|
+
)}
|
|
43
|
+
</TooltipPrimitive.Content>
|
|
44
|
+
</TooltipPrimitive.Portal>
|
|
45
|
+
);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger };
|
|
49
|
+
export type { TooltipContentProps };
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
import { useState, useEffect, type ReactNode } from "react";
|
|
2
|
+
|
|
3
|
+
import type { ToastActionElement, ToastProps } from "@/components/Toast";
|
|
4
|
+
|
|
5
|
+
const TOAST_LIMIT = 3;
|
|
6
|
+
const TOAST_REMOVE_DELAY = 5000;
|
|
7
|
+
|
|
8
|
+
type ToasterToast = ToastProps & {
|
|
9
|
+
id: string;
|
|
10
|
+
title?: ReactNode;
|
|
11
|
+
description?: ReactNode;
|
|
12
|
+
action?: ToastActionElement;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
const actionTypes = {
|
|
16
|
+
ADD_TOAST: "ADD_TOAST",
|
|
17
|
+
UPDATE_TOAST: "UPDATE_TOAST",
|
|
18
|
+
DISMISS_TOAST: "DISMISS_TOAST",
|
|
19
|
+
REMOVE_TOAST: "REMOVE_TOAST",
|
|
20
|
+
} as const;
|
|
21
|
+
|
|
22
|
+
let count = 0;
|
|
23
|
+
|
|
24
|
+
function genId() {
|
|
25
|
+
count = (count + 1) % Number.MAX_SAFE_INTEGER;
|
|
26
|
+
return count.toString();
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
type ActionType = typeof actionTypes;
|
|
30
|
+
|
|
31
|
+
type Action =
|
|
32
|
+
| { type: ActionType["ADD_TOAST"]; toast: ToasterToast }
|
|
33
|
+
| { type: ActionType["UPDATE_TOAST"]; toast: Partial<ToasterToast> }
|
|
34
|
+
| { type: ActionType["DISMISS_TOAST"]; toastId?: ToasterToast["id"] }
|
|
35
|
+
| { type: ActionType["REMOVE_TOAST"]; toastId?: ToasterToast["id"] };
|
|
36
|
+
|
|
37
|
+
interface State {
|
|
38
|
+
toasts: ToasterToast[];
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const toastTimeouts = new Map<string, ReturnType<typeof setTimeout>>();
|
|
42
|
+
|
|
43
|
+
const addToRemoveQueue = (toastId: string) => {
|
|
44
|
+
if (toastTimeouts.has(toastId)) return;
|
|
45
|
+
|
|
46
|
+
const timeout = setTimeout(() => {
|
|
47
|
+
toastTimeouts.delete(toastId);
|
|
48
|
+
dispatch({ type: actionTypes.REMOVE_TOAST, toastId });
|
|
49
|
+
}, TOAST_REMOVE_DELAY);
|
|
50
|
+
|
|
51
|
+
toastTimeouts.set(toastId, timeout);
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
export const reducer = (state: State, action: Action): State => {
|
|
55
|
+
switch (action.type) {
|
|
56
|
+
case actionTypes.ADD_TOAST:
|
|
57
|
+
return {
|
|
58
|
+
...state,
|
|
59
|
+
toasts: [action.toast, ...state.toasts].slice(0, TOAST_LIMIT),
|
|
60
|
+
};
|
|
61
|
+
case actionTypes.UPDATE_TOAST:
|
|
62
|
+
return {
|
|
63
|
+
...state,
|
|
64
|
+
toasts: state.toasts.map((t) =>
|
|
65
|
+
t.id === action.toast.id ? { ...t, ...action.toast } : t
|
|
66
|
+
),
|
|
67
|
+
};
|
|
68
|
+
case actionTypes.DISMISS_TOAST: {
|
|
69
|
+
const { toastId } = action;
|
|
70
|
+
if (toastId) {
|
|
71
|
+
addToRemoveQueue(toastId);
|
|
72
|
+
} else {
|
|
73
|
+
state.toasts.forEach((t) => addToRemoveQueue(t.id));
|
|
74
|
+
}
|
|
75
|
+
return {
|
|
76
|
+
...state,
|
|
77
|
+
toasts: state.toasts.map((t) =>
|
|
78
|
+
t.id === toastId || toastId === undefined ? { ...t, open: false } : t
|
|
79
|
+
),
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
case actionTypes.REMOVE_TOAST:
|
|
83
|
+
if (action.toastId === undefined) return { ...state, toasts: [] };
|
|
84
|
+
return {
|
|
85
|
+
...state,
|
|
86
|
+
toasts: state.toasts.filter((t) => t.id !== action.toastId),
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
const listeners: Array<(state: State) => void> = [];
|
|
92
|
+
let memoryState: State = { toasts: [] };
|
|
93
|
+
|
|
94
|
+
function dispatch(action: Action) {
|
|
95
|
+
memoryState = reducer(memoryState, action);
|
|
96
|
+
[...listeners].forEach((listener) => listener(memoryState));
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
type Toast = Omit<ToasterToast, "id">;
|
|
100
|
+
|
|
101
|
+
function toast(props: Toast) {
|
|
102
|
+
const id = genId();
|
|
103
|
+
|
|
104
|
+
const update = (p: ToasterToast) =>
|
|
105
|
+
dispatch({ type: actionTypes.UPDATE_TOAST, toast: { ...p, id } });
|
|
106
|
+
const dismiss = () =>
|
|
107
|
+
dispatch({ type: actionTypes.DISMISS_TOAST, toastId: id });
|
|
108
|
+
|
|
109
|
+
dispatch({
|
|
110
|
+
type: actionTypes.ADD_TOAST,
|
|
111
|
+
toast: {
|
|
112
|
+
...props,
|
|
113
|
+
id,
|
|
114
|
+
open: true,
|
|
115
|
+
onOpenChange: (open) => {
|
|
116
|
+
if (!open) dismiss();
|
|
117
|
+
},
|
|
118
|
+
},
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
return { id, dismiss, update };
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
function useToast() {
|
|
125
|
+
const [state, setState] = useState<State>(memoryState);
|
|
126
|
+
|
|
127
|
+
useEffect(() => {
|
|
128
|
+
listeners.push(setState);
|
|
129
|
+
return () => {
|
|
130
|
+
const index = listeners.indexOf(setState);
|
|
131
|
+
if (index > -1) listeners.splice(index, 1);
|
|
132
|
+
};
|
|
133
|
+
}, []);
|
|
134
|
+
|
|
135
|
+
return {
|
|
136
|
+
...state,
|
|
137
|
+
toast,
|
|
138
|
+
dismiss: (toastId?: string) =>
|
|
139
|
+
dispatch({ type: actionTypes.DISMISS_TOAST, toastId }),
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
export { useToast, toast };
|
|
144
|
+
export type { ToasterToast };
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
// Alert
|
|
2
|
+
export {
|
|
3
|
+
Alert,
|
|
4
|
+
AlertActions,
|
|
5
|
+
AlertButton,
|
|
6
|
+
alertButtonVariants,
|
|
7
|
+
AlertContainer,
|
|
8
|
+
AlertContent,
|
|
9
|
+
AlertDescription,
|
|
10
|
+
AlertIcon,
|
|
11
|
+
AlertTitle,
|
|
12
|
+
alertVariants,
|
|
13
|
+
} from "@/components/Alert";
|
|
14
|
+
export type { AlertButtonProps, AlertProps } from "@/components/Alert";
|
|
15
|
+
|
|
16
|
+
// Badge
|
|
17
|
+
export { Badge, badgeVariants } from "@/components/Badge";
|
|
18
|
+
export type { BadgeAnimation, BadgeProps } from "@/components/Badge";
|
|
19
|
+
|
|
20
|
+
// Button
|
|
21
|
+
export { Button, buttonVariants, buttonTextVariants } from "@/components/Button";
|
|
22
|
+
export type { ButtonProps } from "@/components/Button";
|
|
23
|
+
|
|
24
|
+
// Checkbox
|
|
25
|
+
export { Checkbox, checkboxVariants } from "@/components/Checkbox";
|
|
26
|
+
export type { CheckboxProps } from "@/components/Checkbox";
|
|
27
|
+
|
|
28
|
+
// Input
|
|
29
|
+
export {
|
|
30
|
+
Input,
|
|
31
|
+
InputAdornment,
|
|
32
|
+
applyInputIconSize,
|
|
33
|
+
InputEndIcon,
|
|
34
|
+
InputField,
|
|
35
|
+
InputLabel,
|
|
36
|
+
InputNumeric,
|
|
37
|
+
InputPassword,
|
|
38
|
+
InputStartIcon,
|
|
39
|
+
InputText,
|
|
40
|
+
InputWrapper,
|
|
41
|
+
inputVariants,
|
|
42
|
+
} from "@/components/Input";
|
|
43
|
+
export type {
|
|
44
|
+
InputLabelProps,
|
|
45
|
+
InputNumericProps,
|
|
46
|
+
InputPasswordProps,
|
|
47
|
+
InputProps,
|
|
48
|
+
InputTextProps,
|
|
49
|
+
InputType,
|
|
50
|
+
} from "@/components/Input";
|
|
51
|
+
|
|
52
|
+
// Label
|
|
53
|
+
export { Label, labelVariants, labelTextVariants } from "@/components/Label";
|
|
54
|
+
export type { LabelProps } from "@/components/Label";
|
|
55
|
+
|
|
56
|
+
// Loading
|
|
57
|
+
export { Loading } from "@/components/Loading";
|
|
58
|
+
export type { LoadingProps } from "@/components/Loading";
|
|
59
|
+
|
|
60
|
+
// RadioGroup
|
|
61
|
+
export { RadioGroup, RadioGroupItem, radioGroupVariants } from "@/components/RadioGroup";
|
|
62
|
+
export type { RadioGroupItemProps } from "@/components/RadioGroup";
|
|
63
|
+
|
|
64
|
+
// Select
|
|
65
|
+
export {
|
|
66
|
+
Select,
|
|
67
|
+
SelectContent,
|
|
68
|
+
SelectGroup,
|
|
69
|
+
SelectHeader,
|
|
70
|
+
SelectItem,
|
|
71
|
+
SelectLabel,
|
|
72
|
+
SelectScrollDownButton,
|
|
73
|
+
SelectScrollUpButton,
|
|
74
|
+
SelectSeparator,
|
|
75
|
+
SelectText,
|
|
76
|
+
SelectTrigger,
|
|
77
|
+
SelectValue,
|
|
78
|
+
SelectWrapper,
|
|
79
|
+
selectVariants,
|
|
80
|
+
} from "@/components/Select";
|
|
81
|
+
export type { SelectProps, SelectItemProps, SelectSize } from "@/components/Select";
|
|
82
|
+
|
|
83
|
+
// Separator
|
|
84
|
+
export { Separator } from "@/components/Separator";
|
|
85
|
+
export type { SeparatorProps } from "@/components/Separator";
|
|
86
|
+
|
|
87
|
+
// Switch
|
|
88
|
+
export { Switch, switchVariants } from "@/components/Switch";
|
|
89
|
+
export type { SwitchProps } from "@/components/Switch";
|
|
90
|
+
|
|
91
|
+
// AlertDialog
|
|
92
|
+
export {
|
|
93
|
+
AlertDialog,
|
|
94
|
+
AlertDialogAction,
|
|
95
|
+
AlertDialogCancel,
|
|
96
|
+
AlertDialogContent,
|
|
97
|
+
AlertDialogDescription,
|
|
98
|
+
AlertDialogFooter,
|
|
99
|
+
AlertDialogHeader,
|
|
100
|
+
AlertDialogIcon,
|
|
101
|
+
AlertDialogOverlay,
|
|
102
|
+
AlertDialogPortal,
|
|
103
|
+
AlertDialogTag,
|
|
104
|
+
AlertDialogTitle,
|
|
105
|
+
AlertDialogTrigger,
|
|
106
|
+
} from "@/components/AlertDialog";
|
|
107
|
+
export type {
|
|
108
|
+
AlertDialogActionProps,
|
|
109
|
+
AlertDialogCancelProps,
|
|
110
|
+
AlertDialogContentProps,
|
|
111
|
+
AlertDialogIconProps,
|
|
112
|
+
AlertDialogProps,
|
|
113
|
+
AlertDialogTagProps,
|
|
114
|
+
AlertDialogVariant,
|
|
115
|
+
} from "@/components/AlertDialog";
|
|
116
|
+
|
|
117
|
+
// Dialog
|
|
118
|
+
export {
|
|
119
|
+
Dialog,
|
|
120
|
+
DialogClose,
|
|
121
|
+
DialogContainer,
|
|
122
|
+
DialogContent,
|
|
123
|
+
DialogDescription,
|
|
124
|
+
DialogFooter,
|
|
125
|
+
DialogHeader,
|
|
126
|
+
DialogOverlay,
|
|
127
|
+
DialogPortal,
|
|
128
|
+
DialogTitle,
|
|
129
|
+
DialogTrigger,
|
|
130
|
+
} from "@/components/Dialog";
|
|
131
|
+
export type {
|
|
132
|
+
DialogContainerProps,
|
|
133
|
+
DialogFooterProps,
|
|
134
|
+
DialogHeaderProps,
|
|
135
|
+
DialogTitleProps,
|
|
136
|
+
} from "@/components/Dialog";
|
|
137
|
+
|
|
138
|
+
// Popover
|
|
139
|
+
export {
|
|
140
|
+
Popover,
|
|
141
|
+
PopoverClose,
|
|
142
|
+
PopoverContent,
|
|
143
|
+
PopoverTrigger,
|
|
144
|
+
} from "@/components/Popover";
|
|
145
|
+
export type { PopoverContentProps } from "@/components/Popover";
|
|
146
|
+
|
|
147
|
+
// Sheet
|
|
148
|
+
export {
|
|
149
|
+
Sheet,
|
|
150
|
+
SheetClose,
|
|
151
|
+
SheetContainer,
|
|
152
|
+
SheetContent,
|
|
153
|
+
SheetDescription,
|
|
154
|
+
SheetFooter,
|
|
155
|
+
SheetHeader,
|
|
156
|
+
SheetTitle,
|
|
157
|
+
SheetTrigger,
|
|
158
|
+
} from "@/components/Sheet";
|
|
159
|
+
export type {
|
|
160
|
+
SheetContainerProps,
|
|
161
|
+
SheetFooterProps,
|
|
162
|
+
SheetHeaderProps,
|
|
163
|
+
SheetSide,
|
|
164
|
+
} from "@/components/Sheet";
|
|
165
|
+
|
|
166
|
+
// Tag
|
|
167
|
+
export { Tag, tagVariants, tagTextVariants } from "@/components/Tag";
|
|
168
|
+
export type { TagProps } from "@/components/Tag";
|
|
169
|
+
|
|
170
|
+
// Toast
|
|
171
|
+
export {
|
|
172
|
+
Toast,
|
|
173
|
+
ToastAction,
|
|
174
|
+
ToastClose,
|
|
175
|
+
ToastDescription,
|
|
176
|
+
ToastProvider,
|
|
177
|
+
ToastTitle,
|
|
178
|
+
ToastViewport,
|
|
179
|
+
toastVariants,
|
|
180
|
+
} from "@/components/Toast";
|
|
181
|
+
export type { ToastActionElement, ToastProps } from "@/components/Toast";
|
|
182
|
+
|
|
183
|
+
// Tooltip
|
|
184
|
+
export {
|
|
185
|
+
Tooltip,
|
|
186
|
+
TooltipContent,
|
|
187
|
+
TooltipProvider,
|
|
188
|
+
TooltipTrigger,
|
|
189
|
+
} from "@/components/Tooltip";
|
|
190
|
+
export type { TooltipContentProps } from "@/components/Tooltip";
|
|
191
|
+
|
|
192
|
+
// Hooks
|
|
193
|
+
export { useToast, toast } from "@/hooks/use-toast";
|
package/src/lib/utils.ts
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { clsx, type ClassValue } from "clsx";
|
|
2
|
+
import { extendTailwindMerge } from "tailwind-merge";
|
|
3
|
+
|
|
4
|
+
const customTwMerge = extendTailwindMerge({
|
|
5
|
+
extend: {
|
|
6
|
+
classGroups: {
|
|
7
|
+
"font-size": [
|
|
8
|
+
"text-xxs",
|
|
9
|
+
"text-xs",
|
|
10
|
+
"text-sm",
|
|
11
|
+
"text-base",
|
|
12
|
+
"text-md",
|
|
13
|
+
"text-lg",
|
|
14
|
+
"text-xl",
|
|
15
|
+
"text-title-xs",
|
|
16
|
+
"text-title-sm",
|
|
17
|
+
"text-title-md",
|
|
18
|
+
"text-title-lg",
|
|
19
|
+
"text-title-xl",
|
|
20
|
+
"text-title-xxl",
|
|
21
|
+
"text-display-sm",
|
|
22
|
+
"text-display-md",
|
|
23
|
+
"text-display-lg",
|
|
24
|
+
],
|
|
25
|
+
leading: [
|
|
26
|
+
"leading-xxs",
|
|
27
|
+
"leading-xs",
|
|
28
|
+
"leading-sm",
|
|
29
|
+
"leading-md",
|
|
30
|
+
"leading-lg",
|
|
31
|
+
"leading-title-xs",
|
|
32
|
+
"leading-title-sm",
|
|
33
|
+
"leading-title-md",
|
|
34
|
+
"leading-title-lg",
|
|
35
|
+
"leading-title-xl",
|
|
36
|
+
"leading-title-xxl",
|
|
37
|
+
"leading-display-sm",
|
|
38
|
+
"leading-display-md",
|
|
39
|
+
"leading-display-lg",
|
|
40
|
+
],
|
|
41
|
+
"font-weight": [
|
|
42
|
+
"font-regular",
|
|
43
|
+
"font-medium",
|
|
44
|
+
"font-semibold",
|
|
45
|
+
"font-bold",
|
|
46
|
+
"font-extrabold",
|
|
47
|
+
],
|
|
48
|
+
rounded: [
|
|
49
|
+
"rounded-none",
|
|
50
|
+
"rounded-sm",
|
|
51
|
+
"rounded-md",
|
|
52
|
+
"rounded-lg",
|
|
53
|
+
"rounded-pill",
|
|
54
|
+
"rounded-circular",
|
|
55
|
+
"rounded-full",
|
|
56
|
+
],
|
|
57
|
+
},
|
|
58
|
+
},
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
export function cn(...inputs: ClassValue[]) {
|
|
62
|
+
return customTwMerge(clsx(inputs));
|
|
63
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
/// <reference types="nativewind/types" />
|