@zag-js/toast 0.2.6 → 0.2.8
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/chunk-EMY6MMVS.mjs +116 -0
- package/dist/chunk-GQHI2OMI.mjs +9 -0
- package/dist/chunk-KJLVUGML.mjs +202 -0
- package/dist/chunk-LOJTIJID.mjs +66 -0
- package/dist/chunk-LOTG5P4K.mjs +104 -0
- package/dist/chunk-MCVNSXDU.mjs +144 -0
- package/dist/chunk-SMQXX7JA.mjs +57 -0
- package/dist/chunk-YVYADWQR.mjs +19 -0
- package/dist/index.d.ts +11 -1043
- package/dist/index.js +45 -51
- package/dist/index.mjs +23 -656
- package/dist/toast-group.connect.d.ts +28 -0
- package/dist/toast-group.connect.js +278 -0
- package/dist/toast-group.connect.mjs +12 -0
- package/dist/toast-group.machine.d.ts +7 -0
- package/dist/toast-group.machine.js +383 -0
- package/dist/toast-group.machine.mjs +10 -0
- package/dist/toast.anatomy.d.ts +6 -0
- package/dist/toast.anatomy.js +34 -0
- package/dist/toast.anatomy.mjs +8 -0
- package/dist/toast.connect.d.ts +23 -0
- package/dist/toast.connect.js +194 -0
- package/dist/toast.connect.mjs +8 -0
- package/dist/toast.dom.d.ts +32 -0
- package/dist/toast.dom.js +83 -0
- package/dist/toast.dom.mjs +6 -0
- package/dist/toast.machine.d.ts +7 -0
- package/dist/toast.machine.js +287 -0
- package/dist/toast.machine.mjs +9 -0
- package/dist/toast.types.d.ts +150 -0
- package/dist/toast.types.js +18 -0
- package/dist/toast.types.mjs +0 -0
- package/dist/toast.utils.d.ts +10 -0
- package/dist/toast.utils.js +93 -0
- package/dist/toast.utils.mjs +12 -0
- package/package.json +21 -11
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import {
|
|
2
|
+
parts
|
|
3
|
+
} from "./chunk-GQHI2OMI.mjs";
|
|
4
|
+
import {
|
|
5
|
+
dom
|
|
6
|
+
} from "./chunk-SMQXX7JA.mjs";
|
|
7
|
+
|
|
8
|
+
// ../../utilities/dom/src/attrs.ts
|
|
9
|
+
var dataAttr = (guard) => {
|
|
10
|
+
return guard ? "" : void 0;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
// src/toast.connect.ts
|
|
14
|
+
function connect(state, send, normalize) {
|
|
15
|
+
const isVisible = state.hasTag("visible");
|
|
16
|
+
const isPaused = state.hasTag("paused");
|
|
17
|
+
const pauseOnInteraction = state.context.pauseOnInteraction;
|
|
18
|
+
const placement = state.context.placement;
|
|
19
|
+
return {
|
|
20
|
+
type: state.context.type,
|
|
21
|
+
title: state.context.title,
|
|
22
|
+
description: state.context.description,
|
|
23
|
+
placement,
|
|
24
|
+
isVisible,
|
|
25
|
+
isPaused,
|
|
26
|
+
isRtl: state.context.dir === "rtl",
|
|
27
|
+
pause() {
|
|
28
|
+
send("PAUSE");
|
|
29
|
+
},
|
|
30
|
+
resume() {
|
|
31
|
+
send("RESUME");
|
|
32
|
+
},
|
|
33
|
+
dismiss() {
|
|
34
|
+
send("DISMISS");
|
|
35
|
+
},
|
|
36
|
+
rootProps: normalize.element({
|
|
37
|
+
...parts.root.attrs,
|
|
38
|
+
dir: state.context.dir,
|
|
39
|
+
id: dom.getRootId(state.context),
|
|
40
|
+
"data-open": dataAttr(isVisible),
|
|
41
|
+
"data-type": state.context.type,
|
|
42
|
+
"data-placement": placement,
|
|
43
|
+
role: "status",
|
|
44
|
+
"aria-atomic": "true",
|
|
45
|
+
tabIndex: 0,
|
|
46
|
+
style: {
|
|
47
|
+
position: "relative",
|
|
48
|
+
pointerEvents: "auto",
|
|
49
|
+
margin: "calc(var(--toast-gutter) / 2)",
|
|
50
|
+
"--remove-delay": `${state.context.removeDelay}ms`,
|
|
51
|
+
"--duration": `${state.context.duration}ms`
|
|
52
|
+
},
|
|
53
|
+
onKeyDown(event) {
|
|
54
|
+
if (event.key == "Escape") {
|
|
55
|
+
send("DISMISS");
|
|
56
|
+
event.preventDefault();
|
|
57
|
+
}
|
|
58
|
+
},
|
|
59
|
+
onFocus() {
|
|
60
|
+
if (pauseOnInteraction) {
|
|
61
|
+
send("PAUSE");
|
|
62
|
+
}
|
|
63
|
+
},
|
|
64
|
+
onBlur() {
|
|
65
|
+
if (pauseOnInteraction) {
|
|
66
|
+
send("RESUME");
|
|
67
|
+
}
|
|
68
|
+
},
|
|
69
|
+
onPointerEnter() {
|
|
70
|
+
if (pauseOnInteraction) {
|
|
71
|
+
send("PAUSE");
|
|
72
|
+
}
|
|
73
|
+
},
|
|
74
|
+
onPointerLeave() {
|
|
75
|
+
if (pauseOnInteraction) {
|
|
76
|
+
send("RESUME");
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}),
|
|
80
|
+
titleProps: normalize.element({
|
|
81
|
+
...parts.title.attrs,
|
|
82
|
+
id: dom.getTitleId(state.context)
|
|
83
|
+
}),
|
|
84
|
+
descriptionProps: normalize.element({
|
|
85
|
+
...parts.description.attrs,
|
|
86
|
+
id: dom.getDescriptionId(state.context)
|
|
87
|
+
}),
|
|
88
|
+
closeTriggerProps: normalize.button({
|
|
89
|
+
id: dom.getCloseTriggerId(state.context),
|
|
90
|
+
...parts.closeTrigger.attrs,
|
|
91
|
+
type: "button",
|
|
92
|
+
"aria-label": "Dismiss notification",
|
|
93
|
+
onClick() {
|
|
94
|
+
send("DISMISS");
|
|
95
|
+
}
|
|
96
|
+
}),
|
|
97
|
+
render() {
|
|
98
|
+
var _a, _b;
|
|
99
|
+
return (_b = (_a = state.context).render) == null ? void 0 : _b.call(_a, {
|
|
100
|
+
id: state.context.id,
|
|
101
|
+
type: state.context.type,
|
|
102
|
+
duration: state.context.duration,
|
|
103
|
+
title: state.context.title,
|
|
104
|
+
placement: state.context.placement,
|
|
105
|
+
description: state.context.description,
|
|
106
|
+
dismiss() {
|
|
107
|
+
send("DISMISS");
|
|
108
|
+
}
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
export {
|
|
115
|
+
connect
|
|
116
|
+
};
|
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
import {
|
|
2
|
+
cast,
|
|
3
|
+
runIfFn
|
|
4
|
+
} from "./chunk-YVYADWQR.mjs";
|
|
5
|
+
import {
|
|
6
|
+
getToastDuration
|
|
7
|
+
} from "./chunk-LOJTIJID.mjs";
|
|
8
|
+
import {
|
|
9
|
+
dom
|
|
10
|
+
} from "./chunk-SMQXX7JA.mjs";
|
|
11
|
+
|
|
12
|
+
// src/toast.machine.ts
|
|
13
|
+
import { createMachine, guards } from "@zag-js/core";
|
|
14
|
+
|
|
15
|
+
// ../../utilities/core/src/guard.ts
|
|
16
|
+
var isArray = (v) => Array.isArray(v);
|
|
17
|
+
var isObject = (v) => !(v == null || typeof v !== "object" || isArray(v));
|
|
18
|
+
var hasProp = (obj, prop) => Object.prototype.hasOwnProperty.call(obj, prop);
|
|
19
|
+
|
|
20
|
+
// ../../utilities/core/src/object.ts
|
|
21
|
+
function compact(obj) {
|
|
22
|
+
if (obj === void 0)
|
|
23
|
+
return obj;
|
|
24
|
+
return Object.fromEntries(
|
|
25
|
+
Object.entries(obj).filter(([, value]) => value !== void 0).map(([key, value]) => [key, isObject(value) ? compact(value) : value])
|
|
26
|
+
);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// ../../utilities/dom/src/listener.ts
|
|
30
|
+
var isRef = (v) => hasProp(v, "current");
|
|
31
|
+
function addDomEvent(target, eventName, handler, options) {
|
|
32
|
+
const node = isRef(target) ? target.current : runIfFn(target);
|
|
33
|
+
node == null ? void 0 : node.addEventListener(eventName, handler, options);
|
|
34
|
+
return () => {
|
|
35
|
+
node == null ? void 0 : node.removeEventListener(eventName, handler, options);
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// ../../utilities/dom/src/visibility-event.ts
|
|
40
|
+
function trackDocumentVisibility(_doc, callback) {
|
|
41
|
+
const doc = cast(_doc);
|
|
42
|
+
return addDomEvent(doc, "visibilitychange", () => {
|
|
43
|
+
const hidden = doc.hidden || doc.msHidden || doc.webkitHidden;
|
|
44
|
+
callback(!!hidden);
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// src/toast.machine.ts
|
|
49
|
+
var { not, and, or } = guards;
|
|
50
|
+
function createToastMachine(options = {}) {
|
|
51
|
+
const { type = "info", duration, id = "toast", placement = "bottom", removeDelay = 0, ...restProps } = options;
|
|
52
|
+
const ctx = compact(restProps);
|
|
53
|
+
const computedDuration = getToastDuration(duration, type);
|
|
54
|
+
return createMachine(
|
|
55
|
+
{
|
|
56
|
+
id,
|
|
57
|
+
entry: "invokeOnOpen",
|
|
58
|
+
initial: type === "loading" ? "persist" : "active",
|
|
59
|
+
context: {
|
|
60
|
+
id,
|
|
61
|
+
type,
|
|
62
|
+
remaining: computedDuration,
|
|
63
|
+
duration: computedDuration,
|
|
64
|
+
removeDelay,
|
|
65
|
+
createdAt: Date.now(),
|
|
66
|
+
placement,
|
|
67
|
+
...ctx
|
|
68
|
+
},
|
|
69
|
+
on: {
|
|
70
|
+
UPDATE: [
|
|
71
|
+
{
|
|
72
|
+
guard: and("hasTypeChanged", "isChangingToLoading"),
|
|
73
|
+
target: "persist",
|
|
74
|
+
actions: ["setContext", "invokeOnUpdate"]
|
|
75
|
+
},
|
|
76
|
+
{
|
|
77
|
+
guard: or("hasDurationChanged", "hasTypeChanged"),
|
|
78
|
+
target: "active:temp",
|
|
79
|
+
actions: ["setContext", "invokeOnUpdate"]
|
|
80
|
+
},
|
|
81
|
+
{
|
|
82
|
+
actions: ["setContext", "invokeOnUpdate"]
|
|
83
|
+
}
|
|
84
|
+
]
|
|
85
|
+
},
|
|
86
|
+
states: {
|
|
87
|
+
"active:temp": {
|
|
88
|
+
tags: ["visible", "updating"],
|
|
89
|
+
after: {
|
|
90
|
+
0: "active"
|
|
91
|
+
}
|
|
92
|
+
},
|
|
93
|
+
persist: {
|
|
94
|
+
tags: ["visible", "paused"],
|
|
95
|
+
activities: "trackDocumentVisibility",
|
|
96
|
+
on: {
|
|
97
|
+
RESUME: {
|
|
98
|
+
guard: not("isLoadingType"),
|
|
99
|
+
target: "active",
|
|
100
|
+
actions: ["setCreatedAt"]
|
|
101
|
+
},
|
|
102
|
+
DISMISS: "dismissing"
|
|
103
|
+
}
|
|
104
|
+
},
|
|
105
|
+
active: {
|
|
106
|
+
tags: ["visible"],
|
|
107
|
+
activities: "trackDocumentVisibility",
|
|
108
|
+
after: {
|
|
109
|
+
VISIBLE_DURATION: "dismissing"
|
|
110
|
+
},
|
|
111
|
+
on: {
|
|
112
|
+
DISMISS: "dismissing",
|
|
113
|
+
PAUSE: {
|
|
114
|
+
target: "persist",
|
|
115
|
+
actions: "setRemainingDuration"
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
},
|
|
119
|
+
dismissing: {
|
|
120
|
+
entry: "invokeOnClosing",
|
|
121
|
+
after: {
|
|
122
|
+
REMOVE_DELAY: {
|
|
123
|
+
target: "inactive",
|
|
124
|
+
actions: "notifyParentToRemove"
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
},
|
|
128
|
+
inactive: {
|
|
129
|
+
entry: "invokeOnClose",
|
|
130
|
+
type: "final"
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
},
|
|
134
|
+
{
|
|
135
|
+
activities: {
|
|
136
|
+
trackDocumentVisibility(ctx2, _evt, { send }) {
|
|
137
|
+
if (!ctx2.pauseOnPageIdle)
|
|
138
|
+
return;
|
|
139
|
+
return trackDocumentVisibility(dom.getDoc(ctx2), function(hidden) {
|
|
140
|
+
send(hidden ? "PAUSE" : "RESUME");
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
},
|
|
144
|
+
guards: {
|
|
145
|
+
isChangingToLoading: (_, evt) => {
|
|
146
|
+
var _a;
|
|
147
|
+
return ((_a = evt.toast) == null ? void 0 : _a.type) === "loading";
|
|
148
|
+
},
|
|
149
|
+
isLoadingType: (ctx2) => ctx2.type === "loading",
|
|
150
|
+
hasTypeChanged: (ctx2, evt) => {
|
|
151
|
+
var _a;
|
|
152
|
+
return ((_a = evt.toast) == null ? void 0 : _a.type) !== ctx2.type;
|
|
153
|
+
},
|
|
154
|
+
hasDurationChanged: (ctx2, evt) => {
|
|
155
|
+
var _a;
|
|
156
|
+
return ((_a = evt.toast) == null ? void 0 : _a.duration) !== ctx2.duration;
|
|
157
|
+
}
|
|
158
|
+
},
|
|
159
|
+
delays: {
|
|
160
|
+
VISIBLE_DURATION: (ctx2) => ctx2.remaining,
|
|
161
|
+
REMOVE_DELAY: (ctx2) => ctx2.removeDelay
|
|
162
|
+
},
|
|
163
|
+
actions: {
|
|
164
|
+
setRemainingDuration(ctx2) {
|
|
165
|
+
ctx2.remaining -= Date.now() - ctx2.createdAt;
|
|
166
|
+
},
|
|
167
|
+
setCreatedAt(ctx2) {
|
|
168
|
+
ctx2.createdAt = Date.now();
|
|
169
|
+
},
|
|
170
|
+
notifyParentToRemove(_ctx, _evt, { self }) {
|
|
171
|
+
self.sendParent({ type: "REMOVE_TOAST", id: self.id });
|
|
172
|
+
},
|
|
173
|
+
invokeOnClosing(ctx2) {
|
|
174
|
+
var _a;
|
|
175
|
+
(_a = ctx2.onClosing) == null ? void 0 : _a.call(ctx2);
|
|
176
|
+
},
|
|
177
|
+
invokeOnClose(ctx2) {
|
|
178
|
+
var _a;
|
|
179
|
+
(_a = ctx2.onClose) == null ? void 0 : _a.call(ctx2);
|
|
180
|
+
},
|
|
181
|
+
invokeOnOpen(ctx2) {
|
|
182
|
+
var _a;
|
|
183
|
+
(_a = ctx2.onOpen) == null ? void 0 : _a.call(ctx2);
|
|
184
|
+
},
|
|
185
|
+
invokeOnUpdate(ctx2) {
|
|
186
|
+
var _a;
|
|
187
|
+
(_a = ctx2.onUpdate) == null ? void 0 : _a.call(ctx2);
|
|
188
|
+
},
|
|
189
|
+
setContext(ctx2, evt) {
|
|
190
|
+
const { duration: duration2, type: type2 } = evt.toast;
|
|
191
|
+
const time = getToastDuration(duration2, type2);
|
|
192
|
+
Object.assign(ctx2, { ...evt.toast, duration: time, remaining: time });
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
);
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
export {
|
|
200
|
+
compact,
|
|
201
|
+
createToastMachine
|
|
202
|
+
};
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
// src/toast.utils.ts
|
|
2
|
+
function getToastsByPlacement(toasts) {
|
|
3
|
+
const result = {};
|
|
4
|
+
for (const toast of toasts) {
|
|
5
|
+
const placement = toast.state.context.placement;
|
|
6
|
+
result[placement] || (result[placement] = []);
|
|
7
|
+
result[placement].push(toast);
|
|
8
|
+
}
|
|
9
|
+
return result;
|
|
10
|
+
}
|
|
11
|
+
var defaultTimeouts = {
|
|
12
|
+
info: 5e3,
|
|
13
|
+
error: 5e3,
|
|
14
|
+
success: 2e3,
|
|
15
|
+
loading: Infinity,
|
|
16
|
+
custom: 5e3
|
|
17
|
+
};
|
|
18
|
+
function getToastDuration(duration, type) {
|
|
19
|
+
return duration != null ? duration : defaultTimeouts[type];
|
|
20
|
+
}
|
|
21
|
+
function getGroupPlacementStyle(ctx, placement) {
|
|
22
|
+
const offset = ctx.offsets;
|
|
23
|
+
const __offset = typeof offset === "string" ? { left: offset, right: offset, bottom: offset, top: offset } : offset;
|
|
24
|
+
const rtl = ctx.dir === "rtl";
|
|
25
|
+
const __placement = placement.replace("-start", rtl ? "-right" : "-left").replace("-end", rtl ? "-left" : "-right");
|
|
26
|
+
const isRighty = __placement.includes("right");
|
|
27
|
+
const isLefty = __placement.includes("left");
|
|
28
|
+
const styles = {
|
|
29
|
+
position: "fixed",
|
|
30
|
+
pointerEvents: ctx.count > 0 ? void 0 : "none",
|
|
31
|
+
display: "flex",
|
|
32
|
+
flexDirection: "column",
|
|
33
|
+
"--toast-gutter": ctx.gutter,
|
|
34
|
+
zIndex: ctx.zIndex
|
|
35
|
+
};
|
|
36
|
+
let alignItems = "center";
|
|
37
|
+
if (isRighty)
|
|
38
|
+
alignItems = "flex-end";
|
|
39
|
+
if (isLefty)
|
|
40
|
+
alignItems = "flex-start";
|
|
41
|
+
styles.alignItems = alignItems;
|
|
42
|
+
if (__placement.includes("top")) {
|
|
43
|
+
const offset2 = __offset.top;
|
|
44
|
+
styles.top = `calc(env(safe-area-inset-top, 0px) + ${offset2})`;
|
|
45
|
+
}
|
|
46
|
+
if (__placement.includes("bottom")) {
|
|
47
|
+
const offset2 = __offset.bottom;
|
|
48
|
+
styles.bottom = `calc(env(safe-area-inset-bottom, 0px) + ${offset2})`;
|
|
49
|
+
}
|
|
50
|
+
if (!__placement.includes("left")) {
|
|
51
|
+
const offset2 = __offset.right;
|
|
52
|
+
styles.right = `calc(env(safe-area-inset-right, 0px) + ${offset2})`;
|
|
53
|
+
}
|
|
54
|
+
if (!__placement.includes("right")) {
|
|
55
|
+
const offset2 = __offset.left;
|
|
56
|
+
styles.left = `calc(env(safe-area-inset-left, 0px) + ${offset2})`;
|
|
57
|
+
}
|
|
58
|
+
return styles;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export {
|
|
62
|
+
getToastsByPlacement,
|
|
63
|
+
defaultTimeouts,
|
|
64
|
+
getToastDuration,
|
|
65
|
+
getGroupPlacementStyle
|
|
66
|
+
};
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import {
|
|
2
|
+
compact,
|
|
3
|
+
createToastMachine
|
|
4
|
+
} from "./chunk-KJLVUGML.mjs";
|
|
5
|
+
|
|
6
|
+
// src/toast-group.machine.ts
|
|
7
|
+
import { createMachine } from "@zag-js/core";
|
|
8
|
+
|
|
9
|
+
// ../../utilities/dom/src/constants.ts
|
|
10
|
+
var MAX_Z_INDEX = 2147483647;
|
|
11
|
+
|
|
12
|
+
// src/toast-group.machine.ts
|
|
13
|
+
function groupMachine(userContext) {
|
|
14
|
+
const ctx = compact(userContext);
|
|
15
|
+
return createMachine({
|
|
16
|
+
id: "toaster",
|
|
17
|
+
initial: "active",
|
|
18
|
+
context: {
|
|
19
|
+
dir: "ltr",
|
|
20
|
+
max: Number.MAX_SAFE_INTEGER,
|
|
21
|
+
toasts: [],
|
|
22
|
+
gutter: "1rem",
|
|
23
|
+
zIndex: MAX_Z_INDEX,
|
|
24
|
+
pauseOnPageIdle: false,
|
|
25
|
+
pauseOnInteraction: true,
|
|
26
|
+
offsets: { left: "0px", right: "0px", top: "0px", bottom: "0px" },
|
|
27
|
+
...ctx
|
|
28
|
+
},
|
|
29
|
+
computed: {
|
|
30
|
+
count: (ctx2) => ctx2.toasts.length
|
|
31
|
+
},
|
|
32
|
+
on: {
|
|
33
|
+
SETUP: {},
|
|
34
|
+
PAUSE_TOAST: {
|
|
35
|
+
actions: (_ctx, evt, { self }) => {
|
|
36
|
+
self.sendChild("PAUSE", evt.id);
|
|
37
|
+
}
|
|
38
|
+
},
|
|
39
|
+
PAUSE_ALL: {
|
|
40
|
+
actions: (ctx2) => {
|
|
41
|
+
ctx2.toasts.forEach((toast) => toast.send("PAUSE"));
|
|
42
|
+
}
|
|
43
|
+
},
|
|
44
|
+
RESUME_TOAST: {
|
|
45
|
+
actions: (_ctx, evt, { self }) => {
|
|
46
|
+
self.sendChild("RESUME", evt.id);
|
|
47
|
+
}
|
|
48
|
+
},
|
|
49
|
+
RESUME_ALL: {
|
|
50
|
+
actions: (ctx2) => {
|
|
51
|
+
ctx2.toasts.forEach((toast) => toast.send("RESUME"));
|
|
52
|
+
}
|
|
53
|
+
},
|
|
54
|
+
ADD_TOAST: {
|
|
55
|
+
guard: (ctx2) => ctx2.toasts.length < ctx2.max,
|
|
56
|
+
actions: (ctx2, evt, { self }) => {
|
|
57
|
+
const options = {
|
|
58
|
+
...evt.toast,
|
|
59
|
+
pauseOnPageIdle: ctx2.pauseOnPageIdle,
|
|
60
|
+
pauseOnInteraction: ctx2.pauseOnInteraction,
|
|
61
|
+
dir: ctx2.dir,
|
|
62
|
+
getRootNode: ctx2.getRootNode
|
|
63
|
+
};
|
|
64
|
+
const toast = createToastMachine(options);
|
|
65
|
+
const actor = self.spawn(toast);
|
|
66
|
+
ctx2.toasts.push(actor);
|
|
67
|
+
}
|
|
68
|
+
},
|
|
69
|
+
UPDATE_TOAST: {
|
|
70
|
+
actions: (_ctx, evt, { self }) => {
|
|
71
|
+
self.sendChild({ type: "UPDATE", toast: evt.toast }, evt.id);
|
|
72
|
+
}
|
|
73
|
+
},
|
|
74
|
+
DISMISS_TOAST: {
|
|
75
|
+
actions: (_ctx, evt, { self }) => {
|
|
76
|
+
self.sendChild("DISMISS", evt.id);
|
|
77
|
+
}
|
|
78
|
+
},
|
|
79
|
+
DISMISS_ALL: {
|
|
80
|
+
actions: (ctx2) => {
|
|
81
|
+
ctx2.toasts.forEach((toast) => toast.send("DISMISS"));
|
|
82
|
+
}
|
|
83
|
+
},
|
|
84
|
+
REMOVE_TOAST: {
|
|
85
|
+
actions: (ctx2, evt, { self }) => {
|
|
86
|
+
self.stopChild(evt.id);
|
|
87
|
+
const index = ctx2.toasts.findIndex((toast) => toast.id === evt.id);
|
|
88
|
+
ctx2.toasts.splice(index, 1);
|
|
89
|
+
}
|
|
90
|
+
},
|
|
91
|
+
REMOVE_ALL: {
|
|
92
|
+
actions: (ctx2, _evt, { self }) => {
|
|
93
|
+
ctx2.toasts.forEach((toast) => self.stopChild(toast.id));
|
|
94
|
+
while (ctx2.toasts.length)
|
|
95
|
+
ctx2.toasts.pop();
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export {
|
|
103
|
+
groupMachine
|
|
104
|
+
};
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
import {
|
|
2
|
+
parts
|
|
3
|
+
} from "./chunk-GQHI2OMI.mjs";
|
|
4
|
+
import {
|
|
5
|
+
runIfFn,
|
|
6
|
+
uuid
|
|
7
|
+
} from "./chunk-YVYADWQR.mjs";
|
|
8
|
+
import {
|
|
9
|
+
getGroupPlacementStyle,
|
|
10
|
+
getToastsByPlacement
|
|
11
|
+
} from "./chunk-LOJTIJID.mjs";
|
|
12
|
+
import {
|
|
13
|
+
dom
|
|
14
|
+
} from "./chunk-SMQXX7JA.mjs";
|
|
15
|
+
|
|
16
|
+
// src/toast-group.connect.ts
|
|
17
|
+
import { subscribe } from "@zag-js/core";
|
|
18
|
+
var toaster = {};
|
|
19
|
+
function groupConnect(state, send, normalize) {
|
|
20
|
+
const group = {
|
|
21
|
+
count: state.context.count,
|
|
22
|
+
toasts: state.context.toasts,
|
|
23
|
+
toastsByPlacement: getToastsByPlacement(state.context.toasts),
|
|
24
|
+
isVisible(id) {
|
|
25
|
+
if (!state.context.toasts.length)
|
|
26
|
+
return false;
|
|
27
|
+
return !!state.context.toasts.find((toast) => toast.id == id);
|
|
28
|
+
},
|
|
29
|
+
create(options) {
|
|
30
|
+
const uid = `toast:${uuid()}`;
|
|
31
|
+
const id = options.id ? options.id : uid;
|
|
32
|
+
if (group.isVisible(id))
|
|
33
|
+
return;
|
|
34
|
+
send({ type: "ADD_TOAST", toast: { ...options, id } });
|
|
35
|
+
return id;
|
|
36
|
+
},
|
|
37
|
+
upsert(options) {
|
|
38
|
+
const { id } = options;
|
|
39
|
+
const isVisible = id ? group.isVisible(id) : false;
|
|
40
|
+
if (isVisible && id != null) {
|
|
41
|
+
return group.update(id, options);
|
|
42
|
+
} else {
|
|
43
|
+
return group.create(options);
|
|
44
|
+
}
|
|
45
|
+
},
|
|
46
|
+
dismiss(id) {
|
|
47
|
+
if (id == null) {
|
|
48
|
+
send("DISMISS_ALL");
|
|
49
|
+
} else if (group.isVisible(id)) {
|
|
50
|
+
send({ type: "DISMISS_TOAST", id });
|
|
51
|
+
}
|
|
52
|
+
},
|
|
53
|
+
remove(id) {
|
|
54
|
+
if (id == null) {
|
|
55
|
+
send("REMOVE_ALL");
|
|
56
|
+
} else if (group.isVisible(id)) {
|
|
57
|
+
send({ type: "REMOVE_TOAST", id });
|
|
58
|
+
}
|
|
59
|
+
},
|
|
60
|
+
dismissByPlacement(placement) {
|
|
61
|
+
const toasts = group.toastsByPlacement[placement];
|
|
62
|
+
if (toasts) {
|
|
63
|
+
toasts.forEach((toast) => group.dismiss(toast.id));
|
|
64
|
+
}
|
|
65
|
+
},
|
|
66
|
+
update(id, options) {
|
|
67
|
+
if (!group.isVisible(id))
|
|
68
|
+
return;
|
|
69
|
+
send({ type: "UPDATE_TOAST", id, toast: options });
|
|
70
|
+
return id;
|
|
71
|
+
},
|
|
72
|
+
loading(options) {
|
|
73
|
+
options.type = "loading";
|
|
74
|
+
return group.upsert(options);
|
|
75
|
+
},
|
|
76
|
+
success(options) {
|
|
77
|
+
options.type = "success";
|
|
78
|
+
return group.upsert(options);
|
|
79
|
+
},
|
|
80
|
+
error(options) {
|
|
81
|
+
options.type = "error";
|
|
82
|
+
return group.upsert(options);
|
|
83
|
+
},
|
|
84
|
+
promise(promise, options, shared = {}) {
|
|
85
|
+
const id = group.loading({ ...shared, ...options.loading });
|
|
86
|
+
promise.then((response) => {
|
|
87
|
+
const successOptions = runIfFn(options.success, response);
|
|
88
|
+
group.success({ ...shared, ...successOptions, id });
|
|
89
|
+
}).catch((error) => {
|
|
90
|
+
const errorOptions = runIfFn(options.error, error);
|
|
91
|
+
group.error({ ...shared, ...errorOptions, id });
|
|
92
|
+
});
|
|
93
|
+
return promise;
|
|
94
|
+
},
|
|
95
|
+
pause(id) {
|
|
96
|
+
if (id == null) {
|
|
97
|
+
send("PAUSE_ALL");
|
|
98
|
+
} else if (group.isVisible(id)) {
|
|
99
|
+
send({ type: "PAUSE_TOAST", id });
|
|
100
|
+
}
|
|
101
|
+
},
|
|
102
|
+
resume(id) {
|
|
103
|
+
if (id == null) {
|
|
104
|
+
send("RESUME_ALL");
|
|
105
|
+
} else if (group.isVisible(id)) {
|
|
106
|
+
send({ type: "RESUME_TOAST", id });
|
|
107
|
+
}
|
|
108
|
+
},
|
|
109
|
+
getGroupProps(options) {
|
|
110
|
+
const { placement, label = "Notifications" } = options;
|
|
111
|
+
return normalize.element({
|
|
112
|
+
...parts.group.attrs,
|
|
113
|
+
tabIndex: -1,
|
|
114
|
+
"aria-label": label,
|
|
115
|
+
id: dom.getGroupId(placement),
|
|
116
|
+
"data-placement": placement,
|
|
117
|
+
"aria-live": "polite",
|
|
118
|
+
role: "region",
|
|
119
|
+
style: getGroupPlacementStyle(state.context, placement)
|
|
120
|
+
});
|
|
121
|
+
},
|
|
122
|
+
createPortal() {
|
|
123
|
+
const doc = dom.getDoc(state.context);
|
|
124
|
+
const exist = dom.getPortalEl(state.context);
|
|
125
|
+
if (exist)
|
|
126
|
+
return exist;
|
|
127
|
+
const portal = dom.createPortalEl(state.context);
|
|
128
|
+
doc.body.appendChild(portal);
|
|
129
|
+
return portal;
|
|
130
|
+
},
|
|
131
|
+
subscribe(fn) {
|
|
132
|
+
return subscribe(state.context.toasts, () => fn(state.context.toasts));
|
|
133
|
+
}
|
|
134
|
+
};
|
|
135
|
+
if (!state.matches("unknown")) {
|
|
136
|
+
Object.assign(toaster, group);
|
|
137
|
+
}
|
|
138
|
+
return group;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
export {
|
|
142
|
+
toaster,
|
|
143
|
+
groupConnect
|
|
144
|
+
};
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
// ../../utilities/dom/src/query.ts
|
|
2
|
+
function isDocument(el) {
|
|
3
|
+
return el.nodeType === Node.DOCUMENT_NODE;
|
|
4
|
+
}
|
|
5
|
+
function isWindow(value) {
|
|
6
|
+
return (value == null ? void 0 : value.toString()) === "[object Window]";
|
|
7
|
+
}
|
|
8
|
+
function getDocument(el) {
|
|
9
|
+
var _a;
|
|
10
|
+
if (isWindow(el))
|
|
11
|
+
return el.document;
|
|
12
|
+
if (isDocument(el))
|
|
13
|
+
return el;
|
|
14
|
+
return (_a = el == null ? void 0 : el.ownerDocument) != null ? _a : document;
|
|
15
|
+
}
|
|
16
|
+
function defineDomHelpers(helpers) {
|
|
17
|
+
const dom2 = {
|
|
18
|
+
getRootNode: (ctx) => {
|
|
19
|
+
var _a, _b;
|
|
20
|
+
return (_b = (_a = ctx.getRootNode) == null ? void 0 : _a.call(ctx)) != null ? _b : document;
|
|
21
|
+
},
|
|
22
|
+
getDoc: (ctx) => getDocument(dom2.getRootNode(ctx)),
|
|
23
|
+
getWin: (ctx) => {
|
|
24
|
+
var _a;
|
|
25
|
+
return (_a = dom2.getDoc(ctx).defaultView) != null ? _a : window;
|
|
26
|
+
},
|
|
27
|
+
getActiveElement: (ctx) => dom2.getDoc(ctx).activeElement,
|
|
28
|
+
getById: (ctx, id) => dom2.getRootNode(ctx).getElementById(id)
|
|
29
|
+
};
|
|
30
|
+
return {
|
|
31
|
+
...dom2,
|
|
32
|
+
...helpers
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// src/toast.dom.ts
|
|
37
|
+
var dom = defineDomHelpers({
|
|
38
|
+
getGroupId: (placement) => `toast-group:${placement}`,
|
|
39
|
+
getRootId: (ctx) => `toast:${ctx.id}`,
|
|
40
|
+
getTitleId: (ctx) => `toast:${ctx.id}:title`,
|
|
41
|
+
getDescriptionId: (ctx) => `toast:${ctx.id}:description`,
|
|
42
|
+
getCloseTriggerId: (ctx) => `toast${ctx.id}:close`,
|
|
43
|
+
getPortalId: (ctx) => `toast-portal:${ctx.id}`,
|
|
44
|
+
getPortalEl: (ctx) => dom.getDoc(ctx).getElementById(dom.getPortalId(ctx)),
|
|
45
|
+
createPortalEl: (ctx) => {
|
|
46
|
+
const existing = dom.getPortalEl(ctx);
|
|
47
|
+
if (existing)
|
|
48
|
+
return existing;
|
|
49
|
+
const portal = dom.getDoc(ctx).createElement("toast-portal");
|
|
50
|
+
portal.id = dom.getPortalId(ctx);
|
|
51
|
+
return portal;
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
export {
|
|
56
|
+
dom
|
|
57
|
+
};
|