@zag-js/toast 0.10.2 → 0.10.4
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/index.d.ts +10 -15
- package/dist/index.js +21 -660
- package/dist/index.mjs +10 -29
- package/dist/toast-group.connect.d.ts +4 -8
- package/dist/toast-group.connect.js +19 -107
- package/dist/toast-group.connect.mjs +171 -11
- package/dist/toast-group.machine.d.ts +3 -7
- package/dist/toast-group.machine.js +12 -202
- package/dist/toast-group.machine.mjs +95 -9
- package/dist/toast.anatomy.d.ts +3 -6
- package/dist/toast.anatomy.js +10 -33
- package/dist/toast.anatomy.mjs +6 -8
- package/dist/toast.connect.d.ts +3 -7
- package/dist/toast.connect.js +16 -54
- package/dist/toast.connect.mjs +137 -8
- package/dist/toast.dom.d.ts +7 -12
- package/dist/toast.dom.js +8 -30
- package/dist/toast.dom.mjs +12 -6
- package/dist/toast.machine.d.ts +3 -7
- package/dist/toast.machine.js +16 -61
- package/dist/toast.machine.mjs +145 -8
- package/dist/toast.types.d.ts +20 -22
- package/dist/toast.utils.d.ts +6 -10
- package/dist/toast.utils.js +10 -36
- package/dist/toast.utils.mjs +59 -11
- package/package.json +8 -13
- package/dist/chunk-6TPPYHFP.mjs +0 -182
- package/dist/chunk-DBPKSADW.mjs +0 -66
- package/dist/chunk-DUZD3NLM.mjs +0 -14
- package/dist/chunk-EQTV3BNM.mjs +0 -152
- package/dist/chunk-GQHI2OMI.mjs +0 -9
- package/dist/chunk-MUJMFD4Y.mjs +0 -144
- package/dist/chunk-TCF4OLIQ.mjs +0 -100
- package/dist/toast.types.js +0 -18
- package/dist/toast.types.mjs +0 -0
package/dist/toast.machine.mjs
CHANGED
|
@@ -1,8 +1,145 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
} from
|
|
4
|
-
import
|
|
5
|
-
import
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
}
|
|
1
|
+
import { createMachine, guards } from '@zag-js/core';
|
|
2
|
+
import { addDomEvent } from '@zag-js/dom-event';
|
|
3
|
+
import { compact } from '@zag-js/utils';
|
|
4
|
+
import { dom } from './toast.dom.mjs';
|
|
5
|
+
import { getToastDuration } from './toast.utils.mjs';
|
|
6
|
+
|
|
7
|
+
const { not, and, or } = guards;
|
|
8
|
+
function createToastMachine(options = {}) {
|
|
9
|
+
const { type = "info", duration, id = "toast", placement = "bottom", removeDelay = 0, ...restProps } = options;
|
|
10
|
+
const ctx = compact(restProps);
|
|
11
|
+
const computedDuration = getToastDuration(duration, type);
|
|
12
|
+
return createMachine(
|
|
13
|
+
{
|
|
14
|
+
id,
|
|
15
|
+
entry: "invokeOnOpen",
|
|
16
|
+
initial: type === "loading" ? "persist" : "active",
|
|
17
|
+
context: {
|
|
18
|
+
id,
|
|
19
|
+
type,
|
|
20
|
+
remaining: computedDuration,
|
|
21
|
+
duration: computedDuration,
|
|
22
|
+
removeDelay,
|
|
23
|
+
createdAt: Date.now(),
|
|
24
|
+
placement,
|
|
25
|
+
...ctx
|
|
26
|
+
},
|
|
27
|
+
on: {
|
|
28
|
+
UPDATE: [
|
|
29
|
+
{
|
|
30
|
+
guard: and("hasTypeChanged", "isChangingToLoading"),
|
|
31
|
+
target: "persist",
|
|
32
|
+
actions: ["setContext", "invokeOnUpdate"]
|
|
33
|
+
},
|
|
34
|
+
{
|
|
35
|
+
guard: or("hasDurationChanged", "hasTypeChanged"),
|
|
36
|
+
target: "active:temp",
|
|
37
|
+
actions: ["setContext", "invokeOnUpdate"]
|
|
38
|
+
},
|
|
39
|
+
{
|
|
40
|
+
actions: ["setContext", "invokeOnUpdate"]
|
|
41
|
+
}
|
|
42
|
+
]
|
|
43
|
+
},
|
|
44
|
+
states: {
|
|
45
|
+
"active:temp": {
|
|
46
|
+
tags: ["visible", "updating"],
|
|
47
|
+
after: {
|
|
48
|
+
0: "active"
|
|
49
|
+
}
|
|
50
|
+
},
|
|
51
|
+
persist: {
|
|
52
|
+
tags: ["visible", "paused"],
|
|
53
|
+
activities: "trackDocumentVisibility",
|
|
54
|
+
on: {
|
|
55
|
+
RESUME: {
|
|
56
|
+
guard: not("isLoadingType"),
|
|
57
|
+
target: "active",
|
|
58
|
+
actions: ["setCreatedAt"]
|
|
59
|
+
},
|
|
60
|
+
DISMISS: "dismissing"
|
|
61
|
+
}
|
|
62
|
+
},
|
|
63
|
+
active: {
|
|
64
|
+
tags: ["visible"],
|
|
65
|
+
activities: "trackDocumentVisibility",
|
|
66
|
+
after: {
|
|
67
|
+
VISIBLE_DURATION: "dismissing"
|
|
68
|
+
},
|
|
69
|
+
on: {
|
|
70
|
+
DISMISS: "dismissing",
|
|
71
|
+
PAUSE: {
|
|
72
|
+
target: "persist",
|
|
73
|
+
actions: "setRemainingDuration"
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
},
|
|
77
|
+
dismissing: {
|
|
78
|
+
entry: "invokeOnClosing",
|
|
79
|
+
after: {
|
|
80
|
+
REMOVE_DELAY: {
|
|
81
|
+
target: "inactive",
|
|
82
|
+
actions: "notifyParentToRemove"
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
},
|
|
86
|
+
inactive: {
|
|
87
|
+
entry: "invokeOnClose",
|
|
88
|
+
type: "final"
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
},
|
|
92
|
+
{
|
|
93
|
+
activities: {
|
|
94
|
+
trackDocumentVisibility(ctx2, _evt, { send }) {
|
|
95
|
+
if (!ctx2.pauseOnPageIdle)
|
|
96
|
+
return;
|
|
97
|
+
const doc = dom.getDoc(ctx2);
|
|
98
|
+
return addDomEvent(doc, "visibilitychange", () => {
|
|
99
|
+
send(doc.visibilityState === "hidden" ? "PAUSE" : "RESUME");
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
},
|
|
103
|
+
guards: {
|
|
104
|
+
isChangingToLoading: (_, evt) => evt.toast?.type === "loading",
|
|
105
|
+
isLoadingType: (ctx2) => ctx2.type === "loading",
|
|
106
|
+
hasTypeChanged: (ctx2, evt) => evt.toast?.type !== ctx2.type,
|
|
107
|
+
hasDurationChanged: (ctx2, evt) => evt.toast?.duration !== ctx2.duration
|
|
108
|
+
},
|
|
109
|
+
delays: {
|
|
110
|
+
VISIBLE_DURATION: (ctx2) => ctx2.remaining,
|
|
111
|
+
REMOVE_DELAY: (ctx2) => ctx2.removeDelay
|
|
112
|
+
},
|
|
113
|
+
actions: {
|
|
114
|
+
setRemainingDuration(ctx2) {
|
|
115
|
+
ctx2.remaining -= Date.now() - ctx2.createdAt;
|
|
116
|
+
},
|
|
117
|
+
setCreatedAt(ctx2) {
|
|
118
|
+
ctx2.createdAt = Date.now();
|
|
119
|
+
},
|
|
120
|
+
notifyParentToRemove(_ctx, _evt, { self }) {
|
|
121
|
+
self.sendParent({ type: "REMOVE_TOAST", id: self.id });
|
|
122
|
+
},
|
|
123
|
+
invokeOnClosing(ctx2) {
|
|
124
|
+
ctx2.onClosing?.();
|
|
125
|
+
},
|
|
126
|
+
invokeOnClose(ctx2) {
|
|
127
|
+
ctx2.onClose?.();
|
|
128
|
+
},
|
|
129
|
+
invokeOnOpen(ctx2) {
|
|
130
|
+
ctx2.onOpen?.();
|
|
131
|
+
},
|
|
132
|
+
invokeOnUpdate(ctx2) {
|
|
133
|
+
ctx2.onUpdate?.();
|
|
134
|
+
},
|
|
135
|
+
setContext(ctx2, evt) {
|
|
136
|
+
const { duration: duration2, type: type2 } = evt.toast;
|
|
137
|
+
const time = getToastDuration(duration2, type2);
|
|
138
|
+
Object.assign(ctx2, { ...evt.toast, duration: time, remaining: time });
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
export { createToastMachine };
|
package/dist/toast.types.d.ts
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
type
|
|
5
|
-
type Placement = "top-start" | "top" | "top-end" | "bottom-start" | "bottom" | "bottom-end";
|
|
1
|
+
import type { Machine, StateMachine as S } from "@zag-js/core";
|
|
2
|
+
import type { CommonProperties, Context, Direction, DirectionProperty, RequiredBy, RootProperties } from "@zag-js/types";
|
|
3
|
+
export type Type = "success" | "error" | "loading" | "info" | "custom";
|
|
4
|
+
export type Placement = "top-start" | "top" | "top-end" | "bottom-start" | "bottom" | "bottom-end";
|
|
6
5
|
type SharedContext = {
|
|
7
6
|
/**
|
|
8
7
|
* Whether to pause toast when the user leaves the browser tab
|
|
@@ -13,7 +12,7 @@ type SharedContext = {
|
|
|
13
12
|
*/
|
|
14
13
|
pauseOnInteraction?: boolean;
|
|
15
14
|
};
|
|
16
|
-
type ToastOptions = {
|
|
15
|
+
export type ToastOptions = {
|
|
17
16
|
/**
|
|
18
17
|
* The unique id of the toast
|
|
19
18
|
*/
|
|
@@ -64,11 +63,11 @@ type ToastOptions = {
|
|
|
64
63
|
*/
|
|
65
64
|
onUpdate?: VoidFunction;
|
|
66
65
|
};
|
|
67
|
-
type Options = Partial<ToastOptions>;
|
|
68
|
-
type RenderOptions = Omit<ToastOptions, "render"> & {
|
|
66
|
+
export type Options = Partial<ToastOptions>;
|
|
67
|
+
export type RenderOptions = Omit<ToastOptions, "render"> & {
|
|
69
68
|
dismiss(): void;
|
|
70
69
|
};
|
|
71
|
-
type MachineContext = SharedContext & RootProperties & CommonProperties & Omit<ToastOptions, "removeDelay"> & {
|
|
70
|
+
export type MachineContext = SharedContext & RootProperties & CommonProperties & Omit<ToastOptions, "removeDelay"> & {
|
|
72
71
|
/**
|
|
73
72
|
* The duration for the toast to kept alive before it is removed.
|
|
74
73
|
* Useful for exit transitions.
|
|
@@ -87,13 +86,13 @@ type MachineContext = SharedContext & RootProperties & CommonProperties & Omit<T
|
|
|
87
86
|
*/
|
|
88
87
|
remaining: number;
|
|
89
88
|
};
|
|
90
|
-
type MachineState = {
|
|
89
|
+
export type MachineState = {
|
|
91
90
|
value: "active" | "active:temp" | "dismissing" | "inactive" | "persist";
|
|
92
91
|
tags: "visible" | "paused" | "updating";
|
|
93
92
|
};
|
|
94
|
-
type State =
|
|
95
|
-
type Send =
|
|
96
|
-
type Service = Machine<MachineContext, MachineState>;
|
|
93
|
+
export type State = S.State<MachineContext, MachineState>;
|
|
94
|
+
export type Send = S.Send;
|
|
95
|
+
export type Service = Machine<MachineContext, MachineState>;
|
|
97
96
|
type GroupPublicContext = SharedContext & DirectionProperty & CommonProperties & {
|
|
98
97
|
/**
|
|
99
98
|
* The gutter or spacing between toasts
|
|
@@ -112,7 +111,7 @@ type GroupPublicContext = SharedContext & DirectionProperty & CommonProperties &
|
|
|
112
111
|
*/
|
|
113
112
|
offsets: string | Record<"left" | "right" | "bottom" | "top", string>;
|
|
114
113
|
};
|
|
115
|
-
type UserDefinedGroupContext = RequiredBy<GroupPublicContext, "id">;
|
|
114
|
+
export type UserDefinedGroupContext = RequiredBy<GroupPublicContext, "id">;
|
|
116
115
|
type GroupComputedContext = Readonly<{
|
|
117
116
|
/**
|
|
118
117
|
* @computed
|
|
@@ -121,20 +120,20 @@ type GroupComputedContext = Readonly<{
|
|
|
121
120
|
readonly count: number;
|
|
122
121
|
}>;
|
|
123
122
|
type GroupPrivateContext = Context<{}>;
|
|
124
|
-
type GroupMachineContext = GroupPublicContext & GroupComputedContext & GroupPrivateContext;
|
|
125
|
-
type GroupState =
|
|
126
|
-
type GroupSend = (event:
|
|
123
|
+
export type GroupMachineContext = GroupPublicContext & GroupComputedContext & GroupPrivateContext;
|
|
124
|
+
export type GroupState = S.State<GroupMachineContext>;
|
|
125
|
+
export type GroupSend = (event: S.Event<S.AnyEventObject>) => void;
|
|
127
126
|
type MaybeFunction<Value, Args> = Value | ((arg: Args) => Value);
|
|
128
|
-
type PromiseOptions<Value> = {
|
|
127
|
+
export type PromiseOptions<Value> = {
|
|
129
128
|
loading: ToastOptions;
|
|
130
129
|
success: MaybeFunction<ToastOptions, Value>;
|
|
131
130
|
error: MaybeFunction<ToastOptions, Error>;
|
|
132
131
|
};
|
|
133
|
-
type GroupProps = {
|
|
132
|
+
export type GroupProps = {
|
|
134
133
|
placement: Placement;
|
|
135
134
|
label?: string;
|
|
136
135
|
};
|
|
137
|
-
type Toaster = {
|
|
136
|
+
export type Toaster = {
|
|
138
137
|
count: number;
|
|
139
138
|
isVisible(id: string): boolean;
|
|
140
139
|
upsert(options: ToastOptions): string | undefined;
|
|
@@ -146,5 +145,4 @@ type Toaster = {
|
|
|
146
145
|
remove(id?: string | undefined): void;
|
|
147
146
|
promise<T>(promise: Promise<T>, options: PromiseOptions<T>, shared?: ToastOptions): Promise<T>;
|
|
148
147
|
};
|
|
149
|
-
|
|
150
|
-
export { GroupMachineContext, GroupProps, GroupSend, GroupState, MachineContext, MachineState, Options, Placement, PromiseOptions, RenderOptions, Send, Service, State, ToastOptions, Toaster, Type, UserDefinedGroupContext };
|
|
148
|
+
export {};
|
package/dist/toast.utils.d.ts
CHANGED
|
@@ -1,10 +1,6 @@
|
|
|
1
|
-
import { Style } from
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
declare function
|
|
6
|
-
declare
|
|
7
|
-
declare function getToastDuration(duration: number | undefined, type: MachineContext["type"]): number;
|
|
8
|
-
declare function getGroupPlacementStyle(ctx: GroupMachineContext, placement: Placement): Style;
|
|
9
|
-
|
|
10
|
-
export { defaultTimeouts, getGroupPlacementStyle, getToastDuration, getToastsByPlacement };
|
|
1
|
+
import type { Style } from "@zag-js/types";
|
|
2
|
+
import type { GroupMachineContext, MachineContext, Placement, Service, Type } from "./toast.types";
|
|
3
|
+
export declare function getToastsByPlacement(toasts: Service[]): Partial<Record<Placement, Service[]>>;
|
|
4
|
+
export declare const defaultTimeouts: Record<Type, number>;
|
|
5
|
+
export declare function getToastDuration(duration: number | undefined, type: MachineContext["type"]): number;
|
|
6
|
+
export declare function getGroupPlacementStyle(ctx: GroupMachineContext, placement: Placement): Style;
|
package/dist/toast.utils.js
CHANGED
|
@@ -1,41 +1,17 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __export = (target, all) => {
|
|
7
|
-
for (var name in all)
|
|
8
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
-
};
|
|
10
|
-
var __copyProps = (to, from, except, desc) => {
|
|
11
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
-
for (let key of __getOwnPropNames(from))
|
|
13
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
-
}
|
|
16
|
-
return to;
|
|
17
|
-
};
|
|
18
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
19
4
|
|
|
20
|
-
// src/toast.utils.ts
|
|
21
|
-
var toast_utils_exports = {};
|
|
22
|
-
__export(toast_utils_exports, {
|
|
23
|
-
defaultTimeouts: () => defaultTimeouts,
|
|
24
|
-
getGroupPlacementStyle: () => getGroupPlacementStyle,
|
|
25
|
-
getToastDuration: () => getToastDuration,
|
|
26
|
-
getToastsByPlacement: () => getToastsByPlacement
|
|
27
|
-
});
|
|
28
|
-
module.exports = __toCommonJS(toast_utils_exports);
|
|
29
5
|
function getToastsByPlacement(toasts) {
|
|
30
6
|
const result = {};
|
|
31
7
|
for (const toast of toasts) {
|
|
32
8
|
const placement = toast.state.context.placement;
|
|
33
|
-
result[placement]
|
|
9
|
+
result[placement] ||= [];
|
|
34
10
|
result[placement].push(toast);
|
|
35
11
|
}
|
|
36
12
|
return result;
|
|
37
13
|
}
|
|
38
|
-
|
|
14
|
+
const defaultTimeouts = {
|
|
39
15
|
info: 5e3,
|
|
40
16
|
error: 5e3,
|
|
41
17
|
success: 2e3,
|
|
@@ -84,10 +60,8 @@ function getGroupPlacementStyle(ctx, placement) {
|
|
|
84
60
|
}
|
|
85
61
|
return styles;
|
|
86
62
|
}
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
getToastsByPlacement
|
|
93
|
-
});
|
|
63
|
+
|
|
64
|
+
exports.defaultTimeouts = defaultTimeouts;
|
|
65
|
+
exports.getGroupPlacementStyle = getGroupPlacementStyle;
|
|
66
|
+
exports.getToastDuration = getToastDuration;
|
|
67
|
+
exports.getToastsByPlacement = getToastsByPlacement;
|
package/dist/toast.utils.mjs
CHANGED
|
@@ -1,12 +1,60 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
1
|
+
function getToastsByPlacement(toasts) {
|
|
2
|
+
const result = {};
|
|
3
|
+
for (const toast of toasts) {
|
|
4
|
+
const placement = toast.state.context.placement;
|
|
5
|
+
result[placement] ||= [];
|
|
6
|
+
result[placement].push(toast);
|
|
7
|
+
}
|
|
8
|
+
return result;
|
|
9
|
+
}
|
|
10
|
+
const defaultTimeouts = {
|
|
11
|
+
info: 5e3,
|
|
12
|
+
error: 5e3,
|
|
13
|
+
success: 2e3,
|
|
14
|
+
loading: Infinity,
|
|
15
|
+
custom: 5e3
|
|
12
16
|
};
|
|
17
|
+
function getToastDuration(duration, type) {
|
|
18
|
+
return duration ?? defaultTimeouts[type];
|
|
19
|
+
}
|
|
20
|
+
function getGroupPlacementStyle(ctx, placement) {
|
|
21
|
+
const offset = ctx.offsets;
|
|
22
|
+
const computedOffset = typeof offset === "string" ? { left: offset, right: offset, bottom: offset, top: offset } : offset;
|
|
23
|
+
const rtl = ctx.dir === "rtl";
|
|
24
|
+
const computedPlacement = placement.replace("-start", rtl ? "-right" : "-left").replace("-end", rtl ? "-left" : "-right");
|
|
25
|
+
const isRighty = computedPlacement.includes("right");
|
|
26
|
+
const isLefty = computedPlacement.includes("left");
|
|
27
|
+
const styles = {
|
|
28
|
+
position: "fixed",
|
|
29
|
+
pointerEvents: ctx.count > 0 ? void 0 : "none",
|
|
30
|
+
display: "flex",
|
|
31
|
+
flexDirection: "column",
|
|
32
|
+
"--toast-gutter": ctx.gutter,
|
|
33
|
+
zIndex: ctx.zIndex
|
|
34
|
+
};
|
|
35
|
+
let alignItems = "center";
|
|
36
|
+
if (isRighty)
|
|
37
|
+
alignItems = "flex-end";
|
|
38
|
+
if (isLefty)
|
|
39
|
+
alignItems = "flex-start";
|
|
40
|
+
styles.alignItems = alignItems;
|
|
41
|
+
if (computedPlacement.includes("top")) {
|
|
42
|
+
const offset2 = computedOffset.top;
|
|
43
|
+
styles.top = `calc(env(safe-area-inset-top, 0px) + ${offset2})`;
|
|
44
|
+
}
|
|
45
|
+
if (computedPlacement.includes("bottom")) {
|
|
46
|
+
const offset2 = computedOffset.bottom;
|
|
47
|
+
styles.bottom = `calc(env(safe-area-inset-bottom, 0px) + ${offset2})`;
|
|
48
|
+
}
|
|
49
|
+
if (!computedPlacement.includes("left")) {
|
|
50
|
+
const offset2 = computedOffset.right;
|
|
51
|
+
styles.right = `calc(env(safe-area-inset-right, 0px) + ${offset2})`;
|
|
52
|
+
}
|
|
53
|
+
if (!computedPlacement.includes("right")) {
|
|
54
|
+
const offset2 = computedOffset.left;
|
|
55
|
+
styles.left = `calc(env(safe-area-inset-left, 0px) + ${offset2})`;
|
|
56
|
+
}
|
|
57
|
+
return styles;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export { defaultTimeouts, getGroupPlacementStyle, getToastDuration, getToastsByPlacement };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zag-js/toast",
|
|
3
|
-
"version": "0.10.
|
|
3
|
+
"version": "0.10.4",
|
|
4
4
|
"description": "Core logic for the toast widget implemented as a state machine",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"js",
|
|
@@ -27,12 +27,12 @@
|
|
|
27
27
|
"url": "https://github.com/chakra-ui/zag/issues"
|
|
28
28
|
},
|
|
29
29
|
"dependencies": {
|
|
30
|
-
"@zag-js/anatomy": "0.10.
|
|
31
|
-
"@zag-js/core": "0.10.
|
|
32
|
-
"@zag-js/dom-query": "0.10.
|
|
33
|
-
"@zag-js/dom-event": "0.10.
|
|
34
|
-
"@zag-js/utils": "0.10.
|
|
35
|
-
"@zag-js/types": "0.10.
|
|
30
|
+
"@zag-js/anatomy": "0.10.4",
|
|
31
|
+
"@zag-js/core": "0.10.4",
|
|
32
|
+
"@zag-js/dom-query": "0.10.4",
|
|
33
|
+
"@zag-js/dom-event": "0.10.4",
|
|
34
|
+
"@zag-js/utils": "0.10.4",
|
|
35
|
+
"@zag-js/types": "0.10.4"
|
|
36
36
|
},
|
|
37
37
|
"devDependencies": {
|
|
38
38
|
"clean-package": "2.2.0"
|
|
@@ -50,13 +50,8 @@
|
|
|
50
50
|
"./package.json": "./package.json"
|
|
51
51
|
},
|
|
52
52
|
"scripts": {
|
|
53
|
-
"build
|
|
54
|
-
"start": "pnpm build --watch",
|
|
55
|
-
"build": "tsup src --dts",
|
|
56
|
-
"test": "jest --config ../../../jest.config.js --rootDir . --passWithNoTests",
|
|
53
|
+
"build": "vite build -c ../../../vite.config.ts",
|
|
57
54
|
"lint": "eslint src --ext .ts,.tsx",
|
|
58
|
-
"test-ci": "pnpm test --ci --runInBand",
|
|
59
|
-
"test-watch": "pnpm test --watch -u",
|
|
60
55
|
"typecheck": "tsc --noEmit"
|
|
61
56
|
}
|
|
62
57
|
}
|
package/dist/chunk-6TPPYHFP.mjs
DELETED
|
@@ -1,182 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
parts
|
|
3
|
-
} from "./chunk-GQHI2OMI.mjs";
|
|
4
|
-
import {
|
|
5
|
-
getGroupPlacementStyle,
|
|
6
|
-
getToastsByPlacement
|
|
7
|
-
} from "./chunk-DBPKSADW.mjs";
|
|
8
|
-
import {
|
|
9
|
-
dom
|
|
10
|
-
} from "./chunk-DUZD3NLM.mjs";
|
|
11
|
-
|
|
12
|
-
// src/toast-group.connect.ts
|
|
13
|
-
import { subscribe } from "@zag-js/core";
|
|
14
|
-
import { runIfFn, uuid } from "@zag-js/utils";
|
|
15
|
-
var toaster = {};
|
|
16
|
-
function groupConnect(state, send, normalize) {
|
|
17
|
-
const group = {
|
|
18
|
-
/**
|
|
19
|
-
* The total number of toasts
|
|
20
|
-
*/
|
|
21
|
-
count: state.context.count,
|
|
22
|
-
/**
|
|
23
|
-
* The active toasts
|
|
24
|
-
*/
|
|
25
|
-
toasts: state.context.toasts,
|
|
26
|
-
/**
|
|
27
|
-
* The active toasts by placement
|
|
28
|
-
*/
|
|
29
|
-
toastsByPlacement: getToastsByPlacement(state.context.toasts),
|
|
30
|
-
/**
|
|
31
|
-
* Returns whether the toast id is visible
|
|
32
|
-
*/
|
|
33
|
-
isVisible(id) {
|
|
34
|
-
if (!state.context.toasts.length)
|
|
35
|
-
return false;
|
|
36
|
-
return !!state.context.toasts.find((toast) => toast.id == id);
|
|
37
|
-
},
|
|
38
|
-
/**
|
|
39
|
-
* Function to create a toast.
|
|
40
|
-
*/
|
|
41
|
-
create(options) {
|
|
42
|
-
const uid = `toast:${uuid()}`;
|
|
43
|
-
const id = options.id ? options.id : uid;
|
|
44
|
-
if (group.isVisible(id))
|
|
45
|
-
return;
|
|
46
|
-
send({ type: "ADD_TOAST", toast: { ...options, id } });
|
|
47
|
-
return id;
|
|
48
|
-
},
|
|
49
|
-
/**
|
|
50
|
-
* Function to create or update a toast.
|
|
51
|
-
*/
|
|
52
|
-
upsert(options) {
|
|
53
|
-
const { id } = options;
|
|
54
|
-
const isVisible = id ? group.isVisible(id) : false;
|
|
55
|
-
if (isVisible && id != null) {
|
|
56
|
-
return group.update(id, options);
|
|
57
|
-
} else {
|
|
58
|
-
return group.create(options);
|
|
59
|
-
}
|
|
60
|
-
},
|
|
61
|
-
/**
|
|
62
|
-
* Function to dismiss a toast by id.
|
|
63
|
-
* If no id is provided, all toasts will be dismissed.
|
|
64
|
-
*/
|
|
65
|
-
dismiss(id) {
|
|
66
|
-
if (id == null) {
|
|
67
|
-
send("DISMISS_ALL");
|
|
68
|
-
} else if (group.isVisible(id)) {
|
|
69
|
-
send({ type: "DISMISS_TOAST", id });
|
|
70
|
-
}
|
|
71
|
-
},
|
|
72
|
-
/**
|
|
73
|
-
* Function to remove a toast by id.
|
|
74
|
-
* If no id is provided, all toasts will be removed.
|
|
75
|
-
*/
|
|
76
|
-
remove(id) {
|
|
77
|
-
if (id == null) {
|
|
78
|
-
send("REMOVE_ALL");
|
|
79
|
-
} else if (group.isVisible(id)) {
|
|
80
|
-
send({ type: "REMOVE_TOAST", id });
|
|
81
|
-
}
|
|
82
|
-
},
|
|
83
|
-
/**
|
|
84
|
-
* Function to dismiss all toasts by placement.
|
|
85
|
-
*/
|
|
86
|
-
dismissByPlacement(placement) {
|
|
87
|
-
const toasts = group.toastsByPlacement[placement];
|
|
88
|
-
if (toasts) {
|
|
89
|
-
toasts.forEach((toast) => group.dismiss(toast.id));
|
|
90
|
-
}
|
|
91
|
-
},
|
|
92
|
-
/**
|
|
93
|
-
* Function to update a toast's options by id.
|
|
94
|
-
*/
|
|
95
|
-
update(id, options) {
|
|
96
|
-
if (!group.isVisible(id))
|
|
97
|
-
return;
|
|
98
|
-
send({ type: "UPDATE_TOAST", id, toast: options });
|
|
99
|
-
return id;
|
|
100
|
-
},
|
|
101
|
-
/**
|
|
102
|
-
* Function to create a loading toast.
|
|
103
|
-
*/
|
|
104
|
-
loading(options) {
|
|
105
|
-
options.type = "loading";
|
|
106
|
-
return group.upsert(options);
|
|
107
|
-
},
|
|
108
|
-
/**
|
|
109
|
-
* Function to create a success toast.
|
|
110
|
-
*/
|
|
111
|
-
success(options) {
|
|
112
|
-
options.type = "success";
|
|
113
|
-
return group.upsert(options);
|
|
114
|
-
},
|
|
115
|
-
/**
|
|
116
|
-
* Function to create an error toast.
|
|
117
|
-
*/
|
|
118
|
-
error(options) {
|
|
119
|
-
options.type = "error";
|
|
120
|
-
return group.upsert(options);
|
|
121
|
-
},
|
|
122
|
-
/**
|
|
123
|
-
* Function to create a toast from a promise.
|
|
124
|
-
* - When the promise resolves, the toast will be updated with the success options.
|
|
125
|
-
* - When the promise rejects, the toast will be updated with the error options.
|
|
126
|
-
*/
|
|
127
|
-
promise(promise, options, shared = {}) {
|
|
128
|
-
const id = group.loading({ ...shared, ...options.loading });
|
|
129
|
-
promise.then((response) => {
|
|
130
|
-
const successOptions = runIfFn(options.success, response);
|
|
131
|
-
group.success({ ...shared, ...successOptions, id });
|
|
132
|
-
}).catch((error) => {
|
|
133
|
-
const errorOptions = runIfFn(options.error, error);
|
|
134
|
-
group.error({ ...shared, ...errorOptions, id });
|
|
135
|
-
});
|
|
136
|
-
return promise;
|
|
137
|
-
},
|
|
138
|
-
/**
|
|
139
|
-
* Function to pause a toast by id.
|
|
140
|
-
*/
|
|
141
|
-
pause(id) {
|
|
142
|
-
if (id == null) {
|
|
143
|
-
send("PAUSE_ALL");
|
|
144
|
-
} else if (group.isVisible(id)) {
|
|
145
|
-
send({ type: "PAUSE_TOAST", id });
|
|
146
|
-
}
|
|
147
|
-
},
|
|
148
|
-
/**
|
|
149
|
-
* Function to resume a toast by id.
|
|
150
|
-
*/
|
|
151
|
-
resume(id) {
|
|
152
|
-
if (id == null) {
|
|
153
|
-
send("RESUME_ALL");
|
|
154
|
-
} else if (group.isVisible(id)) {
|
|
155
|
-
send({ type: "RESUME_TOAST", id });
|
|
156
|
-
}
|
|
157
|
-
},
|
|
158
|
-
getGroupProps(options) {
|
|
159
|
-
const { placement, label = "Notifications" } = options;
|
|
160
|
-
return normalize.element({
|
|
161
|
-
...parts.group.attrs,
|
|
162
|
-
tabIndex: -1,
|
|
163
|
-
"aria-label": label,
|
|
164
|
-
id: dom.getGroupId(placement),
|
|
165
|
-
"data-placement": placement,
|
|
166
|
-
"aria-live": "polite",
|
|
167
|
-
role: "region",
|
|
168
|
-
style: getGroupPlacementStyle(state.context, placement)
|
|
169
|
-
});
|
|
170
|
-
},
|
|
171
|
-
subscribe(fn) {
|
|
172
|
-
return subscribe(state.context.toasts, () => fn(state.context.toasts));
|
|
173
|
-
}
|
|
174
|
-
};
|
|
175
|
-
Object.assign(toaster, group);
|
|
176
|
-
return group;
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
export {
|
|
180
|
-
toaster,
|
|
181
|
-
groupConnect
|
|
182
|
-
};
|