@zag-js/toast 1.34.1 → 1.35.1

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.
Files changed (41) hide show
  1. package/dist/index.d.mts +11 -365
  2. package/dist/index.d.ts +11 -365
  3. package/dist/index.js +40 -1084
  4. package/dist/index.mjs +13 -1080
  5. package/dist/toast-group.connect.d.mts +8 -0
  6. package/dist/toast-group.connect.d.ts +8 -0
  7. package/dist/toast-group.connect.js +98 -0
  8. package/dist/toast-group.connect.mjs +63 -0
  9. package/dist/toast-group.machine.d.mts +8 -0
  10. package/dist/toast-group.machine.d.ts +8 -0
  11. package/dist/toast-group.machine.js +292 -0
  12. package/dist/toast-group.machine.mjs +257 -0
  13. package/dist/toast.anatomy.d.mts +6 -0
  14. package/dist/toast.anatomy.d.ts +6 -0
  15. package/dist/toast.anatomy.js +41 -0
  16. package/dist/toast.anatomy.mjs +15 -0
  17. package/dist/toast.connect.d.mts +8 -0
  18. package/dist/toast.connect.d.ts +8 -0
  19. package/dist/toast.connect.js +155 -0
  20. package/dist/toast.connect.mjs +120 -0
  21. package/dist/toast.dom.d.mts +14 -0
  22. package/dist/toast.dom.d.ts +14 -0
  23. package/dist/toast.dom.js +48 -0
  24. package/dist/toast.dom.mjs +17 -0
  25. package/dist/toast.machine.d.mts +8 -0
  26. package/dist/toast.machine.d.ts +8 -0
  27. package/dist/toast.machine.js +291 -0
  28. package/dist/toast.machine.mjs +256 -0
  29. package/dist/toast.store.d.mts +8 -0
  30. package/dist/toast.store.d.ts +8 -0
  31. package/dist/toast.store.js +249 -0
  32. package/dist/toast.store.mjs +224 -0
  33. package/dist/toast.types.d.mts +371 -0
  34. package/dist/toast.types.d.ts +371 -0
  35. package/dist/toast.types.js +18 -0
  36. package/dist/toast.types.mjs +0 -0
  37. package/dist/toast.utils.d.mts +13 -0
  38. package/dist/toast.utils.d.ts +13 -0
  39. package/dist/toast.utils.js +209 -0
  40. package/dist/toast.utils.mjs +179 -0
  41. package/package.json +18 -8
@@ -0,0 +1,8 @@
1
+ import { Service } from '@zag-js/core';
2
+ import { PropTypes, NormalizeProps } from '@zag-js/types';
3
+ import { ToastGroupSchema, ToastGroupApi } from './toast.types.mjs';
4
+ import '@zag-js/dom-query';
5
+
6
+ declare function groupConnect<T extends PropTypes, O = any>(service: Service<ToastGroupSchema>, normalize: NormalizeProps<T>): ToastGroupApi<T, O>;
7
+
8
+ export { groupConnect };
@@ -0,0 +1,8 @@
1
+ import { Service } from '@zag-js/core';
2
+ import { PropTypes, NormalizeProps } from '@zag-js/types';
3
+ import { ToastGroupSchema, ToastGroupApi } from './toast.types.js';
4
+ import '@zag-js/dom-query';
5
+
6
+ declare function groupConnect<T extends PropTypes, O = any>(service: Service<ToastGroupSchema>, normalize: NormalizeProps<T>): ToastGroupApi<T, O>;
7
+
8
+ export { groupConnect };
@@ -0,0 +1,98 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+
30
+ // src/toast-group.connect.ts
31
+ var toast_group_connect_exports = {};
32
+ __export(toast_group_connect_exports, {
33
+ groupConnect: () => groupConnect
34
+ });
35
+ module.exports = __toCommonJS(toast_group_connect_exports);
36
+ var import_dom_query = require("@zag-js/dom-query");
37
+ var import_toast = require("./toast.anatomy.cjs");
38
+ var dom = __toESM(require("./toast.dom.cjs"));
39
+ var import_toast2 = require("./toast.utils.cjs");
40
+ function groupConnect(service, normalize) {
41
+ const { context, prop, send, refs, computed } = service;
42
+ return {
43
+ getCount() {
44
+ return context.get("toasts").length;
45
+ },
46
+ getToasts() {
47
+ return context.get("toasts");
48
+ },
49
+ getGroupProps(options = {}) {
50
+ const { label = "Notifications" } = options;
51
+ const { hotkey } = prop("store").attrs;
52
+ const hotkeyLabel = hotkey.join("+").replace(/Key/g, "").replace(/Digit/g, "");
53
+ const placement = computed("placement");
54
+ const [side, align = "center"] = placement.split("-");
55
+ return normalize.element({
56
+ ...import_toast.parts.group.attrs,
57
+ dir: prop("dir"),
58
+ tabIndex: -1,
59
+ "aria-label": `${placement} ${label} ${hotkeyLabel}`,
60
+ id: dom.getRegionId(placement),
61
+ "data-placement": placement,
62
+ "data-side": side,
63
+ "data-align": align,
64
+ "aria-live": "polite",
65
+ role: "region",
66
+ style: (0, import_toast2.getGroupPlacementStyle)(service, placement),
67
+ onMouseEnter() {
68
+ if (refs.get("ignoreMouseTimer").isActive()) return;
69
+ send({ type: "REGION.POINTER_ENTER", placement });
70
+ },
71
+ onMouseMove() {
72
+ if (refs.get("ignoreMouseTimer").isActive()) return;
73
+ send({ type: "REGION.POINTER_ENTER", placement });
74
+ },
75
+ onMouseLeave() {
76
+ if (refs.get("ignoreMouseTimer").isActive()) return;
77
+ send({ type: "REGION.POINTER_LEAVE", placement });
78
+ },
79
+ onFocus(event) {
80
+ send({ type: "REGION.FOCUS", target: event.relatedTarget });
81
+ },
82
+ onBlur(event) {
83
+ if (refs.get("isFocusWithin") && !(0, import_dom_query.contains)(event.currentTarget, event.relatedTarget)) {
84
+ queueMicrotask(() => send({ type: "REGION.BLUR" }));
85
+ }
86
+ }
87
+ });
88
+ },
89
+ subscribe(fn) {
90
+ const store = prop("store");
91
+ return store.subscribe(() => fn(context.get("toasts")));
92
+ }
93
+ };
94
+ }
95
+ // Annotate the CommonJS export names for ESM import in node:
96
+ 0 && (module.exports = {
97
+ groupConnect
98
+ });
@@ -0,0 +1,63 @@
1
+ // src/toast-group.connect.ts
2
+ import { contains } from "@zag-js/dom-query";
3
+ import { parts } from "./toast.anatomy.mjs";
4
+ import * as dom from "./toast.dom.mjs";
5
+ import { getGroupPlacementStyle } from "./toast.utils.mjs";
6
+ function groupConnect(service, normalize) {
7
+ const { context, prop, send, refs, computed } = service;
8
+ return {
9
+ getCount() {
10
+ return context.get("toasts").length;
11
+ },
12
+ getToasts() {
13
+ return context.get("toasts");
14
+ },
15
+ getGroupProps(options = {}) {
16
+ const { label = "Notifications" } = options;
17
+ const { hotkey } = prop("store").attrs;
18
+ const hotkeyLabel = hotkey.join("+").replace(/Key/g, "").replace(/Digit/g, "");
19
+ const placement = computed("placement");
20
+ const [side, align = "center"] = placement.split("-");
21
+ return normalize.element({
22
+ ...parts.group.attrs,
23
+ dir: prop("dir"),
24
+ tabIndex: -1,
25
+ "aria-label": `${placement} ${label} ${hotkeyLabel}`,
26
+ id: dom.getRegionId(placement),
27
+ "data-placement": placement,
28
+ "data-side": side,
29
+ "data-align": align,
30
+ "aria-live": "polite",
31
+ role: "region",
32
+ style: getGroupPlacementStyle(service, placement),
33
+ onMouseEnter() {
34
+ if (refs.get("ignoreMouseTimer").isActive()) return;
35
+ send({ type: "REGION.POINTER_ENTER", placement });
36
+ },
37
+ onMouseMove() {
38
+ if (refs.get("ignoreMouseTimer").isActive()) return;
39
+ send({ type: "REGION.POINTER_ENTER", placement });
40
+ },
41
+ onMouseLeave() {
42
+ if (refs.get("ignoreMouseTimer").isActive()) return;
43
+ send({ type: "REGION.POINTER_LEAVE", placement });
44
+ },
45
+ onFocus(event) {
46
+ send({ type: "REGION.FOCUS", target: event.relatedTarget });
47
+ },
48
+ onBlur(event) {
49
+ if (refs.get("isFocusWithin") && !contains(event.currentTarget, event.relatedTarget)) {
50
+ queueMicrotask(() => send({ type: "REGION.BLUR" }));
51
+ }
52
+ }
53
+ });
54
+ },
55
+ subscribe(fn) {
56
+ const store = prop("store");
57
+ return store.subscribe(() => fn(context.get("toasts")));
58
+ }
59
+ };
60
+ }
61
+ export {
62
+ groupConnect
63
+ };
@@ -0,0 +1,8 @@
1
+ import * as _zag_js_core from '@zag-js/core';
2
+ import { ToastGroupSchema } from './toast.types.mjs';
3
+ import '@zag-js/types';
4
+ import '@zag-js/dom-query';
5
+
6
+ declare const groupMachine: _zag_js_core.Machine<ToastGroupSchema>;
7
+
8
+ export { groupMachine };
@@ -0,0 +1,8 @@
1
+ import * as _zag_js_core from '@zag-js/core';
2
+ import { ToastGroupSchema } from './toast.types.js';
3
+ import '@zag-js/types';
4
+ import '@zag-js/dom-query';
5
+
6
+ declare const groupMachine: _zag_js_core.Machine<ToastGroupSchema>;
7
+
8
+ export { groupMachine };
@@ -0,0 +1,292 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+
30
+ // src/toast-group.machine.ts
31
+ var toast_group_machine_exports = {};
32
+ __export(toast_group_machine_exports, {
33
+ groupMachine: () => groupMachine
34
+ });
35
+ module.exports = __toCommonJS(toast_group_machine_exports);
36
+ var import_core = require("@zag-js/core");
37
+ var import_dismissable = require("@zag-js/dismissable");
38
+ var import_dom_query = require("@zag-js/dom-query");
39
+ var import_utils = require("@zag-js/utils");
40
+ var dom = __toESM(require("./toast.dom.cjs"));
41
+ var { guards, createMachine } = (0, import_core.setup)();
42
+ var { and } = guards;
43
+ var groupMachine = createMachine({
44
+ props({ props }) {
45
+ return {
46
+ dir: "ltr",
47
+ id: (0, import_utils.uuid)(),
48
+ ...props,
49
+ store: props.store
50
+ };
51
+ },
52
+ initialState({ prop }) {
53
+ return prop("store").attrs.overlap ? "overlap" : "stack";
54
+ },
55
+ refs() {
56
+ return {
57
+ lastFocusedEl: null,
58
+ isFocusWithin: false,
59
+ isPointerWithin: false,
60
+ ignoreMouseTimer: import_dom_query.AnimationFrame.create(),
61
+ dismissableCleanup: void 0
62
+ };
63
+ },
64
+ context({ bindable }) {
65
+ return {
66
+ toasts: bindable(() => ({
67
+ defaultValue: [],
68
+ sync: true,
69
+ hash: (toasts) => toasts.map((t) => t.id).join(",")
70
+ })),
71
+ heights: bindable(() => ({
72
+ defaultValue: [],
73
+ sync: true
74
+ }))
75
+ };
76
+ },
77
+ computed: {
78
+ count: ({ context }) => context.get("toasts").length,
79
+ overlap: ({ prop }) => prop("store").attrs.overlap,
80
+ placement: ({ prop }) => prop("store").attrs.placement
81
+ },
82
+ effects: ["subscribeToStore", "trackDocumentVisibility", "trackHotKeyPress"],
83
+ watch({ track, context, action }) {
84
+ track([() => context.hash("toasts")], () => {
85
+ queueMicrotask(() => {
86
+ action(["collapsedIfEmpty", "setDismissableBranch"]);
87
+ });
88
+ });
89
+ },
90
+ exit: ["clearDismissableBranch", "clearLastFocusedEl", "clearMouseEventTimer"],
91
+ on: {
92
+ "DOC.HOTKEY": {
93
+ actions: ["focusRegionEl"]
94
+ },
95
+ "REGION.BLUR": [
96
+ {
97
+ guard: and("isOverlapping", "isPointerOut"),
98
+ target: "overlap",
99
+ actions: ["collapseToasts", "resumeToasts", "restoreFocusIfPointerOut"]
100
+ },
101
+ {
102
+ guard: "isPointerOut",
103
+ target: "stack",
104
+ actions: ["resumeToasts", "restoreFocusIfPointerOut"]
105
+ },
106
+ {
107
+ actions: ["clearFocusWithin"]
108
+ }
109
+ ],
110
+ "TOAST.REMOVE": {
111
+ actions: ["removeToast", "removeHeight", "ignoreMouseEventsTemporarily"]
112
+ },
113
+ "TOAST.PAUSE": {
114
+ actions: ["pauseToasts"]
115
+ }
116
+ },
117
+ states: {
118
+ stack: {
119
+ on: {
120
+ "REGION.POINTER_LEAVE": [
121
+ {
122
+ guard: "isOverlapping",
123
+ target: "overlap",
124
+ actions: ["clearPointerWithin", "resumeToasts", "collapseToasts"]
125
+ },
126
+ {
127
+ actions: ["clearPointerWithin", "resumeToasts"]
128
+ }
129
+ ],
130
+ "REGION.OVERLAP": {
131
+ target: "overlap",
132
+ actions: ["collapseToasts"]
133
+ },
134
+ "REGION.FOCUS": {
135
+ actions: ["setLastFocusedEl", "pauseToasts"]
136
+ },
137
+ "REGION.POINTER_ENTER": {
138
+ actions: ["setPointerWithin", "pauseToasts"]
139
+ }
140
+ }
141
+ },
142
+ overlap: {
143
+ on: {
144
+ "REGION.STACK": {
145
+ target: "stack",
146
+ actions: ["expandToasts"]
147
+ },
148
+ "REGION.POINTER_ENTER": {
149
+ target: "stack",
150
+ actions: ["setPointerWithin", "pauseToasts", "expandToasts"]
151
+ },
152
+ "REGION.FOCUS": {
153
+ target: "stack",
154
+ actions: ["setLastFocusedEl", "pauseToasts", "expandToasts"]
155
+ }
156
+ }
157
+ }
158
+ },
159
+ implementations: {
160
+ guards: {
161
+ isOverlapping: ({ computed }) => computed("overlap"),
162
+ isPointerOut: ({ refs }) => !refs.get("isPointerWithin")
163
+ },
164
+ effects: {
165
+ subscribeToStore({ context, prop }) {
166
+ const store = prop("store");
167
+ context.set("toasts", store.getVisibleToasts());
168
+ return store.subscribe((toast) => {
169
+ if (toast.dismiss) {
170
+ context.set("toasts", (prev) => prev.filter((t) => t.id !== toast.id));
171
+ return;
172
+ }
173
+ context.set("toasts", (prev) => {
174
+ const index = prev.findIndex((t) => t.id === toast.id);
175
+ if (index !== -1) {
176
+ return [...prev.slice(0, index), { ...prev[index], ...toast }, ...prev.slice(index + 1)];
177
+ }
178
+ return [toast, ...prev];
179
+ });
180
+ });
181
+ },
182
+ trackHotKeyPress({ prop, send }) {
183
+ const handleKeyDown = (event) => {
184
+ const { hotkey } = prop("store").attrs;
185
+ const isHotkeyPressed = hotkey.every((key) => event[key] || event.code === key);
186
+ if (!isHotkeyPressed) return;
187
+ send({ type: "DOC.HOTKEY" });
188
+ };
189
+ return (0, import_dom_query.addDomEvent)(document, "keydown", handleKeyDown, { capture: true });
190
+ },
191
+ trackDocumentVisibility({ prop, send, scope }) {
192
+ const { pauseOnPageIdle } = prop("store").attrs;
193
+ if (!pauseOnPageIdle) return;
194
+ const doc = scope.getDoc();
195
+ return (0, import_dom_query.addDomEvent)(doc, "visibilitychange", () => {
196
+ const isHidden = doc.visibilityState === "hidden";
197
+ send({ type: isHidden ? "PAUSE_ALL" : "RESUME_ALL" });
198
+ });
199
+ }
200
+ },
201
+ actions: {
202
+ setDismissableBranch({ refs, context, computed, scope }) {
203
+ const toasts = context.get("toasts");
204
+ const placement = computed("placement");
205
+ const hasToasts = toasts.length > 0;
206
+ if (!hasToasts) {
207
+ refs.get("dismissableCleanup")?.();
208
+ return;
209
+ }
210
+ if (hasToasts && refs.get("dismissableCleanup")) {
211
+ return;
212
+ }
213
+ const groupEl = () => dom.getRegionEl(scope, placement);
214
+ const cleanup = (0, import_dismissable.trackDismissableBranch)(groupEl, { defer: true });
215
+ refs.set("dismissableCleanup", cleanup);
216
+ },
217
+ clearDismissableBranch({ refs }) {
218
+ refs.get("dismissableCleanup")?.();
219
+ },
220
+ focusRegionEl({ scope, computed }) {
221
+ queueMicrotask(() => {
222
+ dom.getRegionEl(scope, computed("placement"))?.focus();
223
+ });
224
+ },
225
+ pauseToasts({ prop }) {
226
+ prop("store").pause();
227
+ },
228
+ resumeToasts({ prop }) {
229
+ prop("store").resume();
230
+ },
231
+ expandToasts({ prop }) {
232
+ prop("store").expand();
233
+ },
234
+ collapseToasts({ prop }) {
235
+ prop("store").collapse();
236
+ },
237
+ removeToast({ prop, event }) {
238
+ prop("store").remove(event.id);
239
+ },
240
+ removeHeight({ event, context }) {
241
+ if (event?.id == null) return;
242
+ queueMicrotask(() => {
243
+ context.set("heights", (heights) => heights.filter((height) => height.id !== event.id));
244
+ });
245
+ },
246
+ collapsedIfEmpty({ send, computed }) {
247
+ if (!computed("overlap") || computed("count") > 1) return;
248
+ send({ type: "REGION.OVERLAP" });
249
+ },
250
+ setLastFocusedEl({ refs, event }) {
251
+ if (refs.get("isFocusWithin") || !event.target) return;
252
+ refs.set("isFocusWithin", true);
253
+ refs.set("lastFocusedEl", event.target);
254
+ },
255
+ restoreFocusIfPointerOut({ refs }) {
256
+ if (!refs.get("lastFocusedEl") || refs.get("isPointerWithin")) return;
257
+ refs.get("lastFocusedEl")?.focus({ preventScroll: true });
258
+ refs.set("lastFocusedEl", null);
259
+ refs.set("isFocusWithin", false);
260
+ },
261
+ setPointerWithin({ refs }) {
262
+ refs.set("isPointerWithin", true);
263
+ },
264
+ clearPointerWithin({ refs }) {
265
+ refs.set("isPointerWithin", false);
266
+ if (refs.get("lastFocusedEl") && !refs.get("isFocusWithin")) {
267
+ refs.get("lastFocusedEl")?.focus({ preventScroll: true });
268
+ refs.set("lastFocusedEl", null);
269
+ }
270
+ },
271
+ clearFocusWithin({ refs }) {
272
+ refs.set("isFocusWithin", false);
273
+ },
274
+ clearLastFocusedEl({ refs }) {
275
+ if (!refs.get("lastFocusedEl")) return;
276
+ refs.get("lastFocusedEl")?.focus({ preventScroll: true });
277
+ refs.set("lastFocusedEl", null);
278
+ refs.set("isFocusWithin", false);
279
+ },
280
+ ignoreMouseEventsTemporarily({ refs }) {
281
+ refs.get("ignoreMouseTimer").request();
282
+ },
283
+ clearMouseEventTimer({ refs }) {
284
+ refs.get("ignoreMouseTimer").cancel();
285
+ }
286
+ }
287
+ }
288
+ });
289
+ // Annotate the CommonJS export names for ESM import in node:
290
+ 0 && (module.exports = {
291
+ groupMachine
292
+ });