@zag-js/toast 0.2.5 → 0.2.7

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.
@@ -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-KHVXQ4ZE.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,19 @@
1
+ // ../../utilities/core/src/functions.ts
2
+ var runIfFn = (v, ...a) => {
3
+ const res = typeof v === "function" ? v(...a) : v;
4
+ return res != null ? res : void 0;
5
+ };
6
+ var cast = (v) => v;
7
+ var uuid = /* @__PURE__ */ (() => {
8
+ let id = 0;
9
+ return () => {
10
+ id++;
11
+ return id.toString(36);
12
+ };
13
+ })();
14
+
15
+ export {
16
+ runIfFn,
17
+ cast,
18
+ uuid
19
+ };