@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.
- package/dist/chunk-2E56V7D3.mjs +104 -0
- package/dist/chunk-6MVNQK7B.mjs +144 -0
- package/dist/chunk-GQHI2OMI.mjs +9 -0
- package/dist/chunk-IEPKBUZZ.mjs +116 -0
- package/dist/chunk-KHVXQ4ZE.mjs +75 -0
- package/dist/chunk-LOJTIJID.mjs +66 -0
- package/dist/chunk-YVNTR4MK.mjs +202 -0
- package/dist/chunk-YVYADWQR.mjs +19 -0
- package/dist/index.d.ts +11 -1043
- package/dist/index.js +44 -32
- package/dist/index.mjs +23 -656
- package/dist/toast-group.connect.d.ts +28 -0
- package/dist/toast-group.connect.js +296 -0
- package/dist/toast-group.connect.mjs +12 -0
- package/dist/toast-group.machine.d.ts +7 -0
- package/dist/toast-group.machine.js +401 -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 +212 -0
- package/dist/toast.connect.mjs +8 -0
- package/dist/toast.dom.d.ts +36 -0
- package/dist/toast.dom.js +101 -0
- package/dist/toast.dom.mjs +6 -0
- package/dist/toast.machine.d.ts +7 -0
- package/dist/toast.machine.js +305 -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,28 @@
|
|
|
1
|
+
import { Toaster, GroupState, GroupSend, Service, Placement, Options, PromiseOptions, GroupProps, GroupMachineContext } from './toast.types.js';
|
|
2
|
+
import { PropTypes, NormalizeProps } from '@zag-js/types';
|
|
3
|
+
import '@zag-js/core';
|
|
4
|
+
|
|
5
|
+
declare let toaster: Toaster;
|
|
6
|
+
declare function groupConnect<T extends PropTypes>(state: GroupState, send: GroupSend, normalize: NormalizeProps<T>): {
|
|
7
|
+
count: number;
|
|
8
|
+
toasts: Service[];
|
|
9
|
+
toastsByPlacement: Partial<Record<Placement, Service[]>>;
|
|
10
|
+
isVisible(id: string): boolean;
|
|
11
|
+
create(options: Options): string | undefined;
|
|
12
|
+
upsert(options: Options): string | undefined;
|
|
13
|
+
dismiss(id?: string): void;
|
|
14
|
+
remove(id?: string): void;
|
|
15
|
+
dismissByPlacement(placement: Placement): void;
|
|
16
|
+
update(id: string, options: Options): string | undefined;
|
|
17
|
+
loading(options: Options): string | undefined;
|
|
18
|
+
success(options: Options): string | undefined;
|
|
19
|
+
error(options: Options): string | undefined;
|
|
20
|
+
promise<T_1>(promise: Promise<T_1>, options: PromiseOptions<T_1>, shared?: Options): Promise<T_1>;
|
|
21
|
+
pause(id?: string): void;
|
|
22
|
+
resume(id?: string): void;
|
|
23
|
+
getGroupProps(options: GroupProps): T["element"];
|
|
24
|
+
createPortal(): HTMLElement;
|
|
25
|
+
subscribe(fn: (toasts: GroupMachineContext["toasts"]) => void): () => void;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
export { groupConnect, toaster };
|
|
@@ -0,0 +1,296 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
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);
|
|
19
|
+
|
|
20
|
+
// src/toast-group.connect.ts
|
|
21
|
+
var toast_group_connect_exports = {};
|
|
22
|
+
__export(toast_group_connect_exports, {
|
|
23
|
+
groupConnect: () => groupConnect,
|
|
24
|
+
toaster: () => toaster
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(toast_group_connect_exports);
|
|
27
|
+
var import_core = require("@zag-js/core");
|
|
28
|
+
|
|
29
|
+
// ../../utilities/core/src/functions.ts
|
|
30
|
+
var runIfFn = (v, ...a) => {
|
|
31
|
+
const res = typeof v === "function" ? v(...a) : v;
|
|
32
|
+
return res != null ? res : void 0;
|
|
33
|
+
};
|
|
34
|
+
var uuid = /* @__PURE__ */ (() => {
|
|
35
|
+
let id = 0;
|
|
36
|
+
return () => {
|
|
37
|
+
id++;
|
|
38
|
+
return id.toString(36);
|
|
39
|
+
};
|
|
40
|
+
})();
|
|
41
|
+
|
|
42
|
+
// src/toast.anatomy.ts
|
|
43
|
+
var import_anatomy = require("@zag-js/anatomy");
|
|
44
|
+
var anatomy = (0, import_anatomy.createAnatomy)("toast").parts("group", "root", "title", "description", "closeTrigger");
|
|
45
|
+
var parts = anatomy.build();
|
|
46
|
+
|
|
47
|
+
// ../../utilities/dom/src/query.ts
|
|
48
|
+
function isDocument(el) {
|
|
49
|
+
return el.nodeType === Node.DOCUMENT_NODE;
|
|
50
|
+
}
|
|
51
|
+
function isWindow(value) {
|
|
52
|
+
return (value == null ? void 0 : value.toString()) === "[object Window]";
|
|
53
|
+
}
|
|
54
|
+
function getDocument(el) {
|
|
55
|
+
var _a;
|
|
56
|
+
if (isWindow(el))
|
|
57
|
+
return el.document;
|
|
58
|
+
if (isDocument(el))
|
|
59
|
+
return el;
|
|
60
|
+
return (_a = el == null ? void 0 : el.ownerDocument) != null ? _a : document;
|
|
61
|
+
}
|
|
62
|
+
function defineDomHelpers(helpers) {
|
|
63
|
+
const dom2 = {
|
|
64
|
+
getRootNode: (ctx) => {
|
|
65
|
+
var _a, _b;
|
|
66
|
+
return (_b = (_a = ctx.getRootNode) == null ? void 0 : _a.call(ctx)) != null ? _b : document;
|
|
67
|
+
},
|
|
68
|
+
getDoc: (ctx) => getDocument(dom2.getRootNode(ctx)),
|
|
69
|
+
getWin: (ctx) => {
|
|
70
|
+
var _a;
|
|
71
|
+
return (_a = dom2.getDoc(ctx).defaultView) != null ? _a : window;
|
|
72
|
+
},
|
|
73
|
+
getActiveElement: (ctx) => dom2.getDoc(ctx).activeElement,
|
|
74
|
+
getById: (ctx, id) => dom2.getRootNode(ctx).getElementById(id),
|
|
75
|
+
createEmitter: (ctx, target) => {
|
|
76
|
+
const win = dom2.getWin(ctx);
|
|
77
|
+
return function emit(evt, detail, options) {
|
|
78
|
+
const { bubbles = true, cancelable, composed = true } = options != null ? options : {};
|
|
79
|
+
const eventName = `zag:${evt}`;
|
|
80
|
+
const init = { bubbles, cancelable, composed, detail };
|
|
81
|
+
const event = new win.CustomEvent(eventName, init);
|
|
82
|
+
target.dispatchEvent(event);
|
|
83
|
+
};
|
|
84
|
+
},
|
|
85
|
+
createListener: (target) => {
|
|
86
|
+
return function listen(evt, handler) {
|
|
87
|
+
const eventName = `zag:${evt}`;
|
|
88
|
+
const listener = (e) => handler(e);
|
|
89
|
+
target.addEventListener(eventName, listener);
|
|
90
|
+
return () => target.removeEventListener(eventName, listener);
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
return {
|
|
95
|
+
...dom2,
|
|
96
|
+
...helpers
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// src/toast.dom.ts
|
|
101
|
+
var dom = defineDomHelpers({
|
|
102
|
+
getGroupId: (placement) => `toast-group:${placement}`,
|
|
103
|
+
getRootId: (ctx) => `toast:${ctx.id}`,
|
|
104
|
+
getTitleId: (ctx) => `toast:${ctx.id}:title`,
|
|
105
|
+
getDescriptionId: (ctx) => `toast:${ctx.id}:description`,
|
|
106
|
+
getCloseTriggerId: (ctx) => `toast${ctx.id}:close`,
|
|
107
|
+
getPortalId: (ctx) => `toast-portal:${ctx.id}`,
|
|
108
|
+
getPortalEl: (ctx) => dom.getDoc(ctx).getElementById(dom.getPortalId(ctx)),
|
|
109
|
+
createPortalEl: (ctx) => {
|
|
110
|
+
const existing = dom.getPortalEl(ctx);
|
|
111
|
+
if (existing)
|
|
112
|
+
return existing;
|
|
113
|
+
const portal = dom.getDoc(ctx).createElement("toast-portal");
|
|
114
|
+
portal.id = dom.getPortalId(ctx);
|
|
115
|
+
return portal;
|
|
116
|
+
}
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
// src/toast.utils.ts
|
|
120
|
+
function getToastsByPlacement(toasts) {
|
|
121
|
+
const result = {};
|
|
122
|
+
for (const toast of toasts) {
|
|
123
|
+
const placement = toast.state.context.placement;
|
|
124
|
+
result[placement] || (result[placement] = []);
|
|
125
|
+
result[placement].push(toast);
|
|
126
|
+
}
|
|
127
|
+
return result;
|
|
128
|
+
}
|
|
129
|
+
function getGroupPlacementStyle(ctx, placement) {
|
|
130
|
+
const offset = ctx.offsets;
|
|
131
|
+
const __offset = typeof offset === "string" ? { left: offset, right: offset, bottom: offset, top: offset } : offset;
|
|
132
|
+
const rtl = ctx.dir === "rtl";
|
|
133
|
+
const __placement = placement.replace("-start", rtl ? "-right" : "-left").replace("-end", rtl ? "-left" : "-right");
|
|
134
|
+
const isRighty = __placement.includes("right");
|
|
135
|
+
const isLefty = __placement.includes("left");
|
|
136
|
+
const styles = {
|
|
137
|
+
position: "fixed",
|
|
138
|
+
pointerEvents: ctx.count > 0 ? void 0 : "none",
|
|
139
|
+
display: "flex",
|
|
140
|
+
flexDirection: "column",
|
|
141
|
+
"--toast-gutter": ctx.gutter,
|
|
142
|
+
zIndex: ctx.zIndex
|
|
143
|
+
};
|
|
144
|
+
let alignItems = "center";
|
|
145
|
+
if (isRighty)
|
|
146
|
+
alignItems = "flex-end";
|
|
147
|
+
if (isLefty)
|
|
148
|
+
alignItems = "flex-start";
|
|
149
|
+
styles.alignItems = alignItems;
|
|
150
|
+
if (__placement.includes("top")) {
|
|
151
|
+
const offset2 = __offset.top;
|
|
152
|
+
styles.top = `calc(env(safe-area-inset-top, 0px) + ${offset2})`;
|
|
153
|
+
}
|
|
154
|
+
if (__placement.includes("bottom")) {
|
|
155
|
+
const offset2 = __offset.bottom;
|
|
156
|
+
styles.bottom = `calc(env(safe-area-inset-bottom, 0px) + ${offset2})`;
|
|
157
|
+
}
|
|
158
|
+
if (!__placement.includes("left")) {
|
|
159
|
+
const offset2 = __offset.right;
|
|
160
|
+
styles.right = `calc(env(safe-area-inset-right, 0px) + ${offset2})`;
|
|
161
|
+
}
|
|
162
|
+
if (!__placement.includes("right")) {
|
|
163
|
+
const offset2 = __offset.left;
|
|
164
|
+
styles.left = `calc(env(safe-area-inset-left, 0px) + ${offset2})`;
|
|
165
|
+
}
|
|
166
|
+
return styles;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
// src/toast-group.connect.ts
|
|
170
|
+
var toaster = {};
|
|
171
|
+
function groupConnect(state, send, normalize) {
|
|
172
|
+
const group = {
|
|
173
|
+
count: state.context.count,
|
|
174
|
+
toasts: state.context.toasts,
|
|
175
|
+
toastsByPlacement: getToastsByPlacement(state.context.toasts),
|
|
176
|
+
isVisible(id) {
|
|
177
|
+
if (!state.context.toasts.length)
|
|
178
|
+
return false;
|
|
179
|
+
return !!state.context.toasts.find((toast) => toast.id == id);
|
|
180
|
+
},
|
|
181
|
+
create(options) {
|
|
182
|
+
const uid = `toast:${uuid()}`;
|
|
183
|
+
const id = options.id ? options.id : uid;
|
|
184
|
+
if (group.isVisible(id))
|
|
185
|
+
return;
|
|
186
|
+
send({ type: "ADD_TOAST", toast: { ...options, id } });
|
|
187
|
+
return id;
|
|
188
|
+
},
|
|
189
|
+
upsert(options) {
|
|
190
|
+
const { id } = options;
|
|
191
|
+
const isVisible = id ? group.isVisible(id) : false;
|
|
192
|
+
if (isVisible && id != null) {
|
|
193
|
+
return group.update(id, options);
|
|
194
|
+
} else {
|
|
195
|
+
return group.create(options);
|
|
196
|
+
}
|
|
197
|
+
},
|
|
198
|
+
dismiss(id) {
|
|
199
|
+
if (id == null) {
|
|
200
|
+
send("DISMISS_ALL");
|
|
201
|
+
} else if (group.isVisible(id)) {
|
|
202
|
+
send({ type: "DISMISS_TOAST", id });
|
|
203
|
+
}
|
|
204
|
+
},
|
|
205
|
+
remove(id) {
|
|
206
|
+
if (id == null) {
|
|
207
|
+
send("REMOVE_ALL");
|
|
208
|
+
} else if (group.isVisible(id)) {
|
|
209
|
+
send({ type: "REMOVE_TOAST", id });
|
|
210
|
+
}
|
|
211
|
+
},
|
|
212
|
+
dismissByPlacement(placement) {
|
|
213
|
+
const toasts = group.toastsByPlacement[placement];
|
|
214
|
+
if (toasts) {
|
|
215
|
+
toasts.forEach((toast) => group.dismiss(toast.id));
|
|
216
|
+
}
|
|
217
|
+
},
|
|
218
|
+
update(id, options) {
|
|
219
|
+
if (!group.isVisible(id))
|
|
220
|
+
return;
|
|
221
|
+
send({ type: "UPDATE_TOAST", id, toast: options });
|
|
222
|
+
return id;
|
|
223
|
+
},
|
|
224
|
+
loading(options) {
|
|
225
|
+
options.type = "loading";
|
|
226
|
+
return group.upsert(options);
|
|
227
|
+
},
|
|
228
|
+
success(options) {
|
|
229
|
+
options.type = "success";
|
|
230
|
+
return group.upsert(options);
|
|
231
|
+
},
|
|
232
|
+
error(options) {
|
|
233
|
+
options.type = "error";
|
|
234
|
+
return group.upsert(options);
|
|
235
|
+
},
|
|
236
|
+
promise(promise, options, shared = {}) {
|
|
237
|
+
const id = group.loading({ ...shared, ...options.loading });
|
|
238
|
+
promise.then((response) => {
|
|
239
|
+
const successOptions = runIfFn(options.success, response);
|
|
240
|
+
group.success({ ...shared, ...successOptions, id });
|
|
241
|
+
}).catch((error) => {
|
|
242
|
+
const errorOptions = runIfFn(options.error, error);
|
|
243
|
+
group.error({ ...shared, ...errorOptions, id });
|
|
244
|
+
});
|
|
245
|
+
return promise;
|
|
246
|
+
},
|
|
247
|
+
pause(id) {
|
|
248
|
+
if (id == null) {
|
|
249
|
+
send("PAUSE_ALL");
|
|
250
|
+
} else if (group.isVisible(id)) {
|
|
251
|
+
send({ type: "PAUSE_TOAST", id });
|
|
252
|
+
}
|
|
253
|
+
},
|
|
254
|
+
resume(id) {
|
|
255
|
+
if (id == null) {
|
|
256
|
+
send("RESUME_ALL");
|
|
257
|
+
} else if (group.isVisible(id)) {
|
|
258
|
+
send({ type: "RESUME_TOAST", id });
|
|
259
|
+
}
|
|
260
|
+
},
|
|
261
|
+
getGroupProps(options) {
|
|
262
|
+
const { placement, label = "Notifications" } = options;
|
|
263
|
+
return normalize.element({
|
|
264
|
+
...parts.group.attrs,
|
|
265
|
+
tabIndex: -1,
|
|
266
|
+
"aria-label": label,
|
|
267
|
+
id: dom.getGroupId(placement),
|
|
268
|
+
"data-placement": placement,
|
|
269
|
+
"aria-live": "polite",
|
|
270
|
+
role: "region",
|
|
271
|
+
style: getGroupPlacementStyle(state.context, placement)
|
|
272
|
+
});
|
|
273
|
+
},
|
|
274
|
+
createPortal() {
|
|
275
|
+
const doc = dom.getDoc(state.context);
|
|
276
|
+
const exist = dom.getPortalEl(state.context);
|
|
277
|
+
if (exist)
|
|
278
|
+
return exist;
|
|
279
|
+
const portal = dom.createPortalEl(state.context);
|
|
280
|
+
doc.body.appendChild(portal);
|
|
281
|
+
return portal;
|
|
282
|
+
},
|
|
283
|
+
subscribe(fn) {
|
|
284
|
+
return (0, import_core.subscribe)(state.context.toasts, () => fn(state.context.toasts));
|
|
285
|
+
}
|
|
286
|
+
};
|
|
287
|
+
if (!state.matches("unknown")) {
|
|
288
|
+
Object.assign(toaster, group);
|
|
289
|
+
}
|
|
290
|
+
return group;
|
|
291
|
+
}
|
|
292
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
293
|
+
0 && (module.exports = {
|
|
294
|
+
groupConnect,
|
|
295
|
+
toaster
|
|
296
|
+
});
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import * as _zag_js_core from '@zag-js/core';
|
|
2
|
+
import { UserDefinedGroupContext, GroupMachineContext } from './toast.types.js';
|
|
3
|
+
import '@zag-js/types';
|
|
4
|
+
|
|
5
|
+
declare function groupMachine(userContext: UserDefinedGroupContext): _zag_js_core.Machine<GroupMachineContext, _zag_js_core.StateMachine.StateSchema, _zag_js_core.StateMachine.AnyEventObject>;
|
|
6
|
+
|
|
7
|
+
export { groupMachine };
|