@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,104 @@
1
+ import {
2
+ compact,
3
+ createToastMachine
4
+ } from "./chunk-YVNTR4MK.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-KHVXQ4ZE.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,9 @@
1
+ // src/toast.anatomy.ts
2
+ import { createAnatomy } from "@zag-js/anatomy";
3
+ var anatomy = createAnatomy("toast").parts("group", "root", "title", "description", "closeTrigger");
4
+ var parts = anatomy.build();
5
+
6
+ export {
7
+ anatomy,
8
+ parts
9
+ };
@@ -0,0 +1,116 @@
1
+ import {
2
+ parts
3
+ } from "./chunk-GQHI2OMI.mjs";
4
+ import {
5
+ dom
6
+ } from "./chunk-KHVXQ4ZE.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,75 @@
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
+ createEmitter: (ctx, target) => {
30
+ const win = dom2.getWin(ctx);
31
+ return function emit(evt, detail, options) {
32
+ const { bubbles = true, cancelable, composed = true } = options != null ? options : {};
33
+ const eventName = `zag:${evt}`;
34
+ const init = { bubbles, cancelable, composed, detail };
35
+ const event = new win.CustomEvent(eventName, init);
36
+ target.dispatchEvent(event);
37
+ };
38
+ },
39
+ createListener: (target) => {
40
+ return function listen(evt, handler) {
41
+ const eventName = `zag:${evt}`;
42
+ const listener = (e) => handler(e);
43
+ target.addEventListener(eventName, listener);
44
+ return () => target.removeEventListener(eventName, listener);
45
+ };
46
+ }
47
+ };
48
+ return {
49
+ ...dom2,
50
+ ...helpers
51
+ };
52
+ }
53
+
54
+ // src/toast.dom.ts
55
+ var dom = defineDomHelpers({
56
+ getGroupId: (placement) => `toast-group:${placement}`,
57
+ getRootId: (ctx) => `toast:${ctx.id}`,
58
+ getTitleId: (ctx) => `toast:${ctx.id}:title`,
59
+ getDescriptionId: (ctx) => `toast:${ctx.id}:description`,
60
+ getCloseTriggerId: (ctx) => `toast${ctx.id}:close`,
61
+ getPortalId: (ctx) => `toast-portal:${ctx.id}`,
62
+ getPortalEl: (ctx) => dom.getDoc(ctx).getElementById(dom.getPortalId(ctx)),
63
+ createPortalEl: (ctx) => {
64
+ const existing = dom.getPortalEl(ctx);
65
+ if (existing)
66
+ return existing;
67
+ const portal = dom.getDoc(ctx).createElement("toast-portal");
68
+ portal.id = dom.getPortalId(ctx);
69
+ return portal;
70
+ }
71
+ });
72
+
73
+ export {
74
+ dom
75
+ };
@@ -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
+ };