@mip-ui/toast 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/components/toast.d.ts +16 -0
- package/dist/components/toast.d.ts.map +1 -0
- package/dist/components/toast.js +33 -0
- package/dist/components/toaster.d.ts +2 -0
- package/dist/components/toaster.d.ts.map +1 -0
- package/dist/components/toaster.js +9 -0
- package/dist/hooks/use-toast.d.ts +45 -0
- package/dist/hooks/use-toast.d.ts.map +1 -0
- package/dist/hooks/use-toast.js +128 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +3 -0
- package/package.json +26 -0
@@ -0,0 +1,16 @@
|
|
1
|
+
import * as React from "react";
|
2
|
+
import * as ToastPrimitives from "@radix-ui/react-toast";
|
3
|
+
import { type VariantProps } from "class-variance-authority";
|
4
|
+
declare const ToastProvider: React.FC<ToastPrimitives.ToastProviderProps>;
|
5
|
+
declare const ToastViewport: React.ForwardRefExoticComponent<Omit<ToastPrimitives.ToastViewportProps & React.RefAttributes<HTMLOListElement>, "ref"> & React.RefAttributes<HTMLOListElement>>;
|
6
|
+
declare const Toast: React.ForwardRefExoticComponent<Omit<ToastPrimitives.ToastProps & React.RefAttributes<HTMLLIElement>, "ref"> & VariantProps<(props?: ({
|
7
|
+
variant?: "default" | "destructive" | null | undefined;
|
8
|
+
} & import("class-variance-authority/dist/types").ClassProp) | undefined) => string> & React.RefAttributes<HTMLLIElement>>;
|
9
|
+
declare const ToastAction: React.ForwardRefExoticComponent<Omit<ToastPrimitives.ToastActionProps & React.RefAttributes<HTMLButtonElement>, "ref"> & React.RefAttributes<HTMLButtonElement>>;
|
10
|
+
declare const ToastClose: React.ForwardRefExoticComponent<Omit<ToastPrimitives.ToastCloseProps & React.RefAttributes<HTMLButtonElement>, "ref"> & React.RefAttributes<HTMLButtonElement>>;
|
11
|
+
declare const ToastTitle: React.ForwardRefExoticComponent<Omit<ToastPrimitives.ToastTitleProps & React.RefAttributes<HTMLDivElement>, "ref"> & React.RefAttributes<HTMLDivElement>>;
|
12
|
+
declare const ToastDescription: React.ForwardRefExoticComponent<Omit<ToastPrimitives.ToastDescriptionProps & React.RefAttributes<HTMLDivElement>, "ref"> & React.RefAttributes<HTMLDivElement>>;
|
13
|
+
type ToastProps = React.ComponentPropsWithoutRef<typeof Toast>;
|
14
|
+
type ToastActionElement = React.ReactElement<typeof ToastAction>;
|
15
|
+
export { type ToastProps, type ToastActionElement, ToastProvider, ToastViewport, Toast, ToastTitle, ToastDescription, ToastClose, ToastAction, };
|
16
|
+
//# sourceMappingURL=toast.d.ts.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"toast.d.ts","sourceRoot":"","sources":["../../src/components/toast.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAC;AAC/B,OAAO,KAAK,eAAe,MAAM,uBAAuB,CAAC;AACzD,OAAO,EAAO,KAAK,YAAY,EAAE,MAAM,0BAA0B,CAAC;AAKlE,QAAA,MAAM,aAAa,8CAA2B,CAAC;AAE/C,QAAA,MAAM,aAAa,kKAYjB,CAAC;AAmBH,QAAA,MAAM,KAAK;;0HAYT,CAAC;AAGH,QAAA,MAAM,WAAW,kKAYf,CAAC;AAGH,QAAA,MAAM,UAAU,iKAed,CAAC;AAGH,QAAA,MAAM,UAAU,2JASd,CAAC;AAGH,QAAA,MAAM,gBAAgB,iKASpB,CAAC;AAGH,KAAK,UAAU,GAAG,KAAK,CAAC,wBAAwB,CAAC,OAAO,KAAK,CAAC,CAAC;AAE/D,KAAK,kBAAkB,GAAG,KAAK,CAAC,YAAY,CAAC,OAAO,WAAW,CAAC,CAAC;AAEjE,OAAO,EACL,KAAK,UAAU,EACf,KAAK,kBAAkB,EACvB,aAAa,EACb,aAAa,EACb,KAAK,EACL,UAAU,EACV,gBAAgB,EAChB,UAAU,EACV,WAAW,GACZ,CAAC"}
|
@@ -0,0 +1,33 @@
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
2
|
+
import * as React from "react";
|
3
|
+
import * as ToastPrimitives from "@radix-ui/react-toast";
|
4
|
+
import { cva } from "class-variance-authority";
|
5
|
+
import { X } from "lucide-react";
|
6
|
+
import { cn } from "@mip-ui/lib";
|
7
|
+
const ToastProvider = ToastPrimitives.Provider;
|
8
|
+
const ToastViewport = React.forwardRef(({ className, ...props }, ref) => (_jsx(ToastPrimitives.Viewport, { ref: ref, className: cn("fixed top-0 z-[100] flex max-h-screen w-full flex-col-reverse p-4 sm:top-auto sm:right-0 sm:bottom-0 sm:flex-col md:max-w-[420px]", className), ...props })));
|
9
|
+
ToastViewport.displayName = ToastPrimitives.Viewport.displayName;
|
10
|
+
const toastVariants = cva("group pointer-events-auto relative flex w-full items-center justify-between space-x-2 overflow-hidden rounded-md border p-4 pr-6 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", {
|
11
|
+
variants: {
|
12
|
+
variant: {
|
13
|
+
default: "border bg-background text-foreground",
|
14
|
+
destructive: "destructive group border-destructive bg-destructive text-destructive-foreground",
|
15
|
+
},
|
16
|
+
},
|
17
|
+
defaultVariants: {
|
18
|
+
variant: "default",
|
19
|
+
},
|
20
|
+
});
|
21
|
+
const Toast = React.forwardRef(({ className, variant, ...props }, ref) => {
|
22
|
+
return (_jsx(ToastPrimitives.Root, { ref: ref, className: cn(toastVariants({ variant }), className), ...props }));
|
23
|
+
});
|
24
|
+
Toast.displayName = ToastPrimitives.Root.displayName;
|
25
|
+
const ToastAction = React.forwardRef(({ className, ...props }, ref) => (_jsx(ToastPrimitives.Action, { ref: ref, className: cn("hover:bg-secondary focus:ring-ring group-[.destructive]:border-muted/40 group-[.destructive]:hover:border-destructive/30 group-[.destructive]:hover:bg-destructive group-[.destructive]:hover:text-destructive-foreground group-[.destructive]:focus:ring-destructive inline-flex h-8 shrink-0 items-center justify-center rounded-md border bg-transparent px-3 text-sm font-medium transition-colors focus:ring-1 focus:outline-none disabled:pointer-events-none disabled:opacity-50", className), ...props })));
|
26
|
+
ToastAction.displayName = ToastPrimitives.Action.displayName;
|
27
|
+
const ToastClose = React.forwardRef(({ className, ...props }, ref) => (_jsx(ToastPrimitives.Close, { ref: ref, className: cn("text-foreground/50 hover:text-foreground absolute top-1 right-1 rounded-md p-1 opacity-0 transition-opacity group-hover:opacity-100 group-[.destructive]:text-red-300 group-[.destructive]:hover:text-red-50 focus:opacity-100 focus:ring-1 focus:outline-none group-[.destructive]:focus:ring-red-400 group-[.destructive]:focus:ring-offset-red-600", className), "toast-close": "", ...props, children: _jsx(X, { className: "h-4 w-4" }) })));
|
28
|
+
ToastClose.displayName = ToastPrimitives.Close.displayName;
|
29
|
+
const ToastTitle = React.forwardRef(({ className, ...props }, ref) => (_jsx(ToastPrimitives.Title, { ref: ref, className: cn("text-sm font-semibold [&+div]:text-xs", className), ...props })));
|
30
|
+
ToastTitle.displayName = ToastPrimitives.Title.displayName;
|
31
|
+
const ToastDescription = React.forwardRef(({ className, ...props }, ref) => (_jsx(ToastPrimitives.Description, { ref: ref, className: cn("text-sm opacity-90", className), ...props })));
|
32
|
+
ToastDescription.displayName = ToastPrimitives.Description.displayName;
|
33
|
+
export { ToastProvider, ToastViewport, Toast, ToastTitle, ToastDescription, ToastClose, ToastAction, };
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"toaster.d.ts","sourceRoot":"","sources":["../../src/components/toaster.tsx"],"names":[],"mappings":"AAUA,wBAAgB,OAAO,4CAsBtB"}
|
@@ -0,0 +1,9 @@
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
2
|
+
import { Toast, ToastClose, ToastDescription, ToastProvider, ToastTitle, ToastViewport, } from "./toast";
|
3
|
+
import { useToast } from "../hooks/use-toast";
|
4
|
+
export function Toaster() {
|
5
|
+
const { toasts } = useToast();
|
6
|
+
return (_jsxs(ToastProvider, { swipeDirection: "down", children: [toasts.map(function ({ id, title, description, action, ...props }) {
|
7
|
+
return (_jsxs(Toast, { ...props, children: [_jsxs("div", { className: "grid gap-1", children: [title && _jsx(ToastTitle, { children: title }), description && (_jsx(ToastDescription, { children: description }))] }), action, _jsx(ToastClose, {})] }, id));
|
8
|
+
}), _jsx(ToastViewport, {})] }));
|
9
|
+
}
|
@@ -0,0 +1,45 @@
|
|
1
|
+
import * as React from "react";
|
2
|
+
import type { ToastActionElement, ToastProps } from "../components/toast";
|
3
|
+
type ToasterToast = ToastProps & {
|
4
|
+
id: string;
|
5
|
+
title?: React.ReactNode;
|
6
|
+
description?: React.ReactNode;
|
7
|
+
action?: ToastActionElement;
|
8
|
+
};
|
9
|
+
declare const actionTypes: {
|
10
|
+
readonly ADD_TOAST: "ADD_TOAST";
|
11
|
+
readonly UPDATE_TOAST: "UPDATE_TOAST";
|
12
|
+
readonly DISMISS_TOAST: "DISMISS_TOAST";
|
13
|
+
readonly REMOVE_TOAST: "REMOVE_TOAST";
|
14
|
+
};
|
15
|
+
type ActionType = typeof actionTypes;
|
16
|
+
type Action = {
|
17
|
+
type: ActionType["ADD_TOAST"];
|
18
|
+
toast: ToasterToast;
|
19
|
+
} | {
|
20
|
+
type: ActionType["UPDATE_TOAST"];
|
21
|
+
toast: Partial<ToasterToast>;
|
22
|
+
} | {
|
23
|
+
type: ActionType["DISMISS_TOAST"];
|
24
|
+
toastId?: ToasterToast["id"];
|
25
|
+
} | {
|
26
|
+
type: ActionType["REMOVE_TOAST"];
|
27
|
+
toastId?: ToasterToast["id"];
|
28
|
+
};
|
29
|
+
interface State {
|
30
|
+
toasts: ToasterToast[];
|
31
|
+
}
|
32
|
+
export declare const reducer: (state: State, action: Action) => State;
|
33
|
+
type Toast = Omit<ToasterToast, "id">;
|
34
|
+
declare function toast({ ...props }: Toast): {
|
35
|
+
id: string;
|
36
|
+
dismiss: () => void;
|
37
|
+
update: (props: ToasterToast) => void;
|
38
|
+
};
|
39
|
+
declare function useToast(): {
|
40
|
+
toast: typeof toast;
|
41
|
+
dismiss: (toastId?: string) => void;
|
42
|
+
toasts: ToasterToast[];
|
43
|
+
};
|
44
|
+
export { useToast, toast };
|
45
|
+
//# sourceMappingURL=use-toast.d.ts.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"use-toast.d.ts","sourceRoot":"","sources":["../../src/hooks/use-toast.tsx"],"names":[],"mappings":"AACA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAC;AAE/B,OAAO,KAAK,EAAE,kBAAkB,EAAE,UAAU,EAAE,MAAM,qBAAqB,CAAC;AAK1E,KAAK,YAAY,GAAG,UAAU,GAAG;IAC/B,EAAE,EAAE,MAAM,CAAC;IACX,KAAK,CAAC,EAAE,KAAK,CAAC,SAAS,CAAC;IACxB,WAAW,CAAC,EAAE,KAAK,CAAC,SAAS,CAAC;IAC9B,MAAM,CAAC,EAAE,kBAAkB,CAAC;CAC7B,CAAC;AAGF,QAAA,MAAM,WAAW;;;;;CAKP,CAAC;AASX,KAAK,UAAU,GAAG,OAAO,WAAW,CAAC;AAErC,KAAK,MAAM,GACP;IACE,IAAI,EAAE,UAAU,CAAC,WAAW,CAAC,CAAC;IAC9B,KAAK,EAAE,YAAY,CAAC;CACrB,GACD;IACE,IAAI,EAAE,UAAU,CAAC,cAAc,CAAC,CAAC;IACjC,KAAK,EAAE,OAAO,CAAC,YAAY,CAAC,CAAC;CAC9B,GACD;IACE,IAAI,EAAE,UAAU,CAAC,eAAe,CAAC,CAAC;IAClC,OAAO,CAAC,EAAE,YAAY,CAAC,IAAI,CAAC,CAAC;CAC9B,GACD;IACE,IAAI,EAAE,UAAU,CAAC,cAAc,CAAC,CAAC;IACjC,OAAO,CAAC,EAAE,YAAY,CAAC,IAAI,CAAC,CAAC;CAC9B,CAAC;AAEN,UAAU,KAAK;IACb,MAAM,EAAE,YAAY,EAAE,CAAC;CACxB;AAoBD,eAAO,MAAM,OAAO,UAAW,KAAK,UAAU,MAAM,KAAG,KAqDtD,CAAC;AAaF,KAAK,KAAK,GAAG,IAAI,CAAC,YAAY,EAAE,IAAI,CAAC,CAAC;AAEtC,iBAAS,KAAK,CAAC,EAAE,GAAG,KAAK,EAAE,EAAE,KAAK;;;oBAGT,YAAY;EAwBpC;AAED,iBAAS,QAAQ;;wBAgBO,MAAM;YAtIpB,YAAY,EAAE;EAwIvB;AAED,OAAO,EAAE,QAAQ,EAAE,KAAK,EAAE,CAAC"}
|
@@ -0,0 +1,128 @@
|
|
1
|
+
// Inspired by react-hot-toast library
|
2
|
+
import * as React from "react";
|
3
|
+
const TOAST_LIMIT = 1;
|
4
|
+
const TOAST_REMOVE_DELAY = 1000000;
|
5
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
6
|
+
const actionTypes = {
|
7
|
+
ADD_TOAST: "ADD_TOAST",
|
8
|
+
UPDATE_TOAST: "UPDATE_TOAST",
|
9
|
+
DISMISS_TOAST: "DISMISS_TOAST",
|
10
|
+
REMOVE_TOAST: "REMOVE_TOAST",
|
11
|
+
};
|
12
|
+
let count = 0;
|
13
|
+
function genId() {
|
14
|
+
count = (count + 1) % Number.MAX_SAFE_INTEGER;
|
15
|
+
return count.toString();
|
16
|
+
}
|
17
|
+
const toastTimeouts = new Map();
|
18
|
+
const addToRemoveQueue = (toastId) => {
|
19
|
+
if (toastTimeouts.has(toastId)) {
|
20
|
+
return;
|
21
|
+
}
|
22
|
+
const timeout = setTimeout(() => {
|
23
|
+
toastTimeouts.delete(toastId);
|
24
|
+
dispatch({
|
25
|
+
type: "REMOVE_TOAST",
|
26
|
+
toastId: toastId,
|
27
|
+
});
|
28
|
+
}, TOAST_REMOVE_DELAY);
|
29
|
+
toastTimeouts.set(toastId, timeout);
|
30
|
+
};
|
31
|
+
export const reducer = (state, action) => {
|
32
|
+
switch (action.type) {
|
33
|
+
case "ADD_TOAST":
|
34
|
+
return {
|
35
|
+
...state,
|
36
|
+
toasts: [action.toast, ...state.toasts].slice(0, TOAST_LIMIT),
|
37
|
+
};
|
38
|
+
case "UPDATE_TOAST":
|
39
|
+
return {
|
40
|
+
...state,
|
41
|
+
toasts: state.toasts.map((t) => t.id === action.toast.id ? { ...t, ...action.toast } : t),
|
42
|
+
};
|
43
|
+
case "DISMISS_TOAST": {
|
44
|
+
const { toastId } = action;
|
45
|
+
// ! Side effects ! - This could be extracted into a dismissToast() action,
|
46
|
+
// but I'll keep it here for simplicity
|
47
|
+
if (toastId) {
|
48
|
+
addToRemoveQueue(toastId);
|
49
|
+
}
|
50
|
+
else {
|
51
|
+
state.toasts.forEach((toast) => {
|
52
|
+
addToRemoveQueue(toast.id);
|
53
|
+
});
|
54
|
+
}
|
55
|
+
return {
|
56
|
+
...state,
|
57
|
+
toasts: state.toasts.map((t) => t.id === toastId || toastId === undefined
|
58
|
+
? {
|
59
|
+
...t,
|
60
|
+
open: false,
|
61
|
+
}
|
62
|
+
: t),
|
63
|
+
};
|
64
|
+
}
|
65
|
+
case "REMOVE_TOAST":
|
66
|
+
if (action.toastId === undefined) {
|
67
|
+
return {
|
68
|
+
...state,
|
69
|
+
toasts: [],
|
70
|
+
};
|
71
|
+
}
|
72
|
+
return {
|
73
|
+
...state,
|
74
|
+
toasts: state.toasts.filter((t) => t.id !== action.toastId),
|
75
|
+
};
|
76
|
+
}
|
77
|
+
};
|
78
|
+
const listeners = [];
|
79
|
+
let memoryState = { toasts: [] };
|
80
|
+
function dispatch(action) {
|
81
|
+
memoryState = reducer(memoryState, action);
|
82
|
+
listeners.forEach((listener) => {
|
83
|
+
listener(memoryState);
|
84
|
+
});
|
85
|
+
}
|
86
|
+
function toast({ ...props }) {
|
87
|
+
const id = genId();
|
88
|
+
const update = (props) => dispatch({
|
89
|
+
type: "UPDATE_TOAST",
|
90
|
+
toast: { ...props, id },
|
91
|
+
});
|
92
|
+
const dismiss = () => dispatch({ type: "DISMISS_TOAST", toastId: id });
|
93
|
+
dispatch({
|
94
|
+
type: "ADD_TOAST",
|
95
|
+
toast: {
|
96
|
+
...props,
|
97
|
+
id,
|
98
|
+
open: true,
|
99
|
+
onOpenChange: (open) => {
|
100
|
+
if (!open)
|
101
|
+
dismiss();
|
102
|
+
},
|
103
|
+
},
|
104
|
+
});
|
105
|
+
return {
|
106
|
+
id: id,
|
107
|
+
dismiss,
|
108
|
+
update,
|
109
|
+
};
|
110
|
+
}
|
111
|
+
function useToast() {
|
112
|
+
const [state, setState] = React.useState(memoryState);
|
113
|
+
React.useEffect(() => {
|
114
|
+
listeners.push(setState);
|
115
|
+
return () => {
|
116
|
+
const index = listeners.indexOf(setState);
|
117
|
+
if (index > -1) {
|
118
|
+
listeners.splice(index, 1);
|
119
|
+
}
|
120
|
+
};
|
121
|
+
}, [state]);
|
122
|
+
return {
|
123
|
+
...state,
|
124
|
+
toast,
|
125
|
+
dismiss: (toastId) => dispatch({ type: "DISMISS_TOAST", toastId }),
|
126
|
+
};
|
127
|
+
}
|
128
|
+
export { useToast, toast };
|
package/dist/index.d.ts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,sBAAsB,CAAC;AACrC,cAAc,oBAAoB,CAAC;AACnC,cAAc,mBAAmB,CAAC"}
|
package/dist/index.js
ADDED
package/package.json
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
{
|
2
|
+
"name": "@mip-ui/toast",
|
3
|
+
"version": "0.1.0",
|
4
|
+
"main": "dist/index.js",
|
5
|
+
"module": "dist/index.js",
|
6
|
+
"types": "dist/index.d.ts",
|
7
|
+
"exports": {
|
8
|
+
".": {
|
9
|
+
"import": "./dist/index.js",
|
10
|
+
"require": "./dist/index.js",
|
11
|
+
"types": "./dist/index.d.ts"
|
12
|
+
}
|
13
|
+
},
|
14
|
+
"publishConfig": {
|
15
|
+
"access": "public"
|
16
|
+
},
|
17
|
+
"files": [
|
18
|
+
"dist"
|
19
|
+
],
|
20
|
+
"dependencies": {
|
21
|
+
"@radix-ui/react-toast": "^1.2.11"
|
22
|
+
},
|
23
|
+
"scripts": {
|
24
|
+
"build": "tsc -b"
|
25
|
+
}
|
26
|
+
}
|