@zag-js/toast 2.0.0-next.2 → 2.0.0-next.4

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 (45) hide show
  1. package/dist/index.d.mts +10 -17
  2. package/dist/index.d.ts +10 -17
  3. package/dist/index.js +18 -45
  4. package/dist/index.mjs +5 -11
  5. package/dist/toast-group.connect.d.mts +4 -8
  6. package/dist/toast-group.connect.d.ts +4 -8
  7. package/dist/toast-group.connect.js +82 -103
  8. package/dist/toast-group.connect.mjs +77 -67
  9. package/dist/toast-group.machine.d.mts +2 -8
  10. package/dist/toast-group.machine.d.ts +2 -8
  11. package/dist/toast-group.machine.js +295 -290
  12. package/dist/toast-group.machine.mjs +290 -254
  13. package/dist/toast.anatomy.d.mts +2 -6
  14. package/dist/toast.anatomy.d.ts +2 -6
  15. package/dist/toast.anatomy.js +8 -40
  16. package/dist/toast.anatomy.mjs +4 -14
  17. package/dist/toast.connect.d.mts +4 -8
  18. package/dist/toast.connect.d.ts +4 -8
  19. package/dist/toast.connect.js +176 -160
  20. package/dist/toast.connect.mjs +171 -124
  21. package/dist/toast.dom.d.mts +21 -14
  22. package/dist/toast.dom.d.ts +21 -14
  23. package/dist/toast.dom.js +50 -48
  24. package/dist/toast.dom.mjs +35 -17
  25. package/dist/toast.machine.d.mts +2 -8
  26. package/dist/toast.machine.d.ts +2 -8
  27. package/dist/toast.machine.js +404 -288
  28. package/dist/toast.machine.mjs +400 -253
  29. package/dist/toast.store.d.mts +2 -8
  30. package/dist/toast.store.d.ts +2 -8
  31. package/dist/toast.store.js +345 -263
  32. package/dist/toast.store.mjs +341 -238
  33. package/dist/toast.types.d.mts +460 -427
  34. package/dist/toast.types.d.ts +460 -427
  35. package/dist/toast.types.js +4 -18
  36. package/dist/toast.types.mjs +1 -0
  37. package/dist/toast.utils.d.mts +10 -13
  38. package/dist/toast.utils.d.ts +10 -13
  39. package/dist/toast.utils.js +185 -198
  40. package/dist/toast.utils.mjs +175 -168
  41. package/dist/utils/swipe.d.mts +16 -0
  42. package/dist/utils/swipe.d.ts +16 -0
  43. package/dist/utils/swipe.js +100 -0
  44. package/dist/utils/swipe.mjs +93 -0
  45. package/package.json +8 -8
@@ -1,18 +1,4 @@
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 __copyProps = (to, from, except, desc) => {
7
- if (from && typeof from === "object" || typeof from === "function") {
8
- for (let key of __getOwnPropNames(from))
9
- if (!__hasOwnProp.call(to, key) && key !== except)
10
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
11
- }
12
- return to;
13
- };
14
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
15
-
16
- // src/toast.types.ts
17
- var toast_types_exports = {};
18
- module.exports = __toCommonJS(toast_types_exports);
1
+ Object.defineProperties(exports, {
2
+ __esModule: { value: true },
3
+ [Symbol.toStringTag]: { value: "Module" }
4
+ });
@@ -0,0 +1 @@
1
+ export {};
@@ -1,13 +1,10 @@
1
- import { Service } from '@zag-js/core';
2
- import { Style } from '@zag-js/types';
3
- import { Type, ToastSchema, ToastGroupService, Placement } from './toast.types.mjs';
4
- import '@zag-js/dom-query';
5
-
6
- declare const defaultTimeouts: Record<Type, number>;
7
- declare function getToastDuration(duration: number | undefined, type: Type): number;
8
- declare function getGroupPlacementStyle(service: ToastGroupService, placement: Placement): Style;
9
- declare function getPlacementStyle(service: Service<ToastSchema>, visible: boolean): Style;
10
- declare function getGhostBeforeStyle(service: Service<ToastSchema>, visible: boolean): Style;
11
- declare function getGhostAfterStyle(): Style;
12
-
13
- export { defaultTimeouts, getGhostAfterStyle, getGhostBeforeStyle, getGroupPlacementStyle, getPlacementStyle, getToastDuration };
1
+ import { Placement, ToastGroupSchema, ToastGroupService, ToastHeight, ToastSchema, Type } from "./toast.types.mjs";
2
+ import { Service } from "@zag-js/core";
3
+ import { Style } from "@zag-js/types";
4
+ export declare const defaultTimeouts: Record<Type, number>;
5
+ export declare function getToastDuration(duration: number | undefined, type: Type): number;
6
+ export declare function getGroupPlacementStyle(service: ToastGroupService, placement: Placement): Style;
7
+ export declare function getPlacementStyle(service: Service<ToastSchema>, visible: boolean): Style;
8
+ export declare function getGhostBeforeStyle(service: Service<ToastSchema>, visible: boolean): Style;
9
+ export declare function getGhostAfterStyle(): Style;
10
+ export declare function setHeight(parent: Service<ToastGroupSchema>, item: ToastHeight): void;
@@ -1,13 +1,10 @@
1
- import { Service } from '@zag-js/core';
2
- import { Style } from '@zag-js/types';
3
- import { Type, ToastSchema, ToastGroupService, Placement } from './toast.types.js';
4
- import '@zag-js/dom-query';
5
-
6
- declare const defaultTimeouts: Record<Type, number>;
7
- declare function getToastDuration(duration: number | undefined, type: Type): number;
8
- declare function getGroupPlacementStyle(service: ToastGroupService, placement: Placement): Style;
9
- declare function getPlacementStyle(service: Service<ToastSchema>, visible: boolean): Style;
10
- declare function getGhostBeforeStyle(service: Service<ToastSchema>, visible: boolean): Style;
11
- declare function getGhostAfterStyle(): Style;
12
-
13
- export { defaultTimeouts, getGhostAfterStyle, getGhostBeforeStyle, getGroupPlacementStyle, getPlacementStyle, getToastDuration };
1
+ import { Placement, ToastGroupSchema, ToastGroupService, ToastHeight, ToastSchema, Type } from "./toast.types.js";
2
+ import { Style } from "@zag-js/types";
3
+ import { Service } from "@zag-js/core";
4
+ export declare const defaultTimeouts: Record<Type, number>;
5
+ export declare function getToastDuration(duration: number | undefined, type: Type): number;
6
+ export declare function getGroupPlacementStyle(service: ToastGroupService, placement: Placement): Style;
7
+ export declare function getPlacementStyle(service: Service<ToastSchema>, visible: boolean): Style;
8
+ export declare function getGhostBeforeStyle(service: Service<ToastSchema>, visible: boolean): Style;
9
+ export declare function getGhostAfterStyle(): Style;
10
+ export declare function setHeight(parent: Service<ToastGroupSchema>, item: ToastHeight): void;
@@ -1,210 +1,197 @@
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.utils.ts
21
- var toast_utils_exports = {};
22
- __export(toast_utils_exports, {
23
- defaultTimeouts: () => defaultTimeouts,
24
- getGhostAfterStyle: () => getGhostAfterStyle,
25
- getGhostBeforeStyle: () => getGhostBeforeStyle,
26
- getGroupPlacementStyle: () => getGroupPlacementStyle,
27
- getPlacementStyle: () => getPlacementStyle,
28
- getToastDuration: () => getToastDuration
1
+ Object.defineProperties(exports, {
2
+ __esModule: { value: true },
3
+ [Symbol.toStringTag]: { value: "Module" }
29
4
  });
30
- module.exports = __toCommonJS(toast_utils_exports);
31
- var import_dom_query = require("@zag-js/dom-query");
32
- var defaultTimeouts = {
33
- info: 5e3,
34
- error: 5e3,
35
- success: 2e3,
36
- loading: Infinity,
37
- warning: 5e3,
38
- DEFAULT: 5e3
5
+ let _zag_js_utils = require("@zag-js/utils");
6
+ // src/toast.utils.ts
7
+ const defaultTimeouts = {
8
+ info: 5e3,
9
+ error: 5e3,
10
+ success: 2e3,
11
+ loading: 1e400,
12
+ warning: 5e3,
13
+ DEFAULT: 5e3
39
14
  };
40
15
  function getToastDuration(duration, type) {
41
- return duration ?? defaultTimeouts[type] ?? defaultTimeouts.DEFAULT;
16
+ return duration ?? defaultTimeouts[type] ?? defaultTimeouts.DEFAULT;
42
17
  }
43
- var getOffsets = (offsets) => typeof offsets === "string" ? { left: offsets, right: offsets, bottom: offsets, top: offsets } : offsets;
18
+ const getOffsets = (offsets) => typeof offsets === "string" ? {
19
+ left: offsets,
20
+ right: offsets,
21
+ bottom: offsets,
22
+ top: offsets
23
+ } : offsets;
44
24
  function getGroupPlacementStyle(service, placement) {
45
- const { prop, computed, context } = service;
46
- const { offsets, gap } = prop("store").attrs;
47
- const heights = context.get("heights");
48
- const computedOffset = getOffsets(offsets);
49
- const rtl = prop("dir") === "rtl";
50
- const computedPlacement = placement.replace("-start", rtl ? "-right" : "-left").replace("-end", rtl ? "-left" : "-right");
51
- const isRighty = computedPlacement.includes("right");
52
- const isLefty = computedPlacement.includes("left");
53
- const styles = {
54
- position: "fixed",
55
- pointerEvents: computed("count") > 0 ? void 0 : "none",
56
- display: "flex",
57
- flexDirection: "column",
58
- "--gap": `${gap}px`,
59
- "--first-height": `${heights[0]?.height || 0}px`,
60
- "--viewport-offset-left": computedOffset.left,
61
- "--viewport-offset-right": computedOffset.right,
62
- "--viewport-offset-top": computedOffset.top,
63
- "--viewport-offset-bottom": computedOffset.bottom,
64
- zIndex: import_dom_query.MAX_Z_INDEX
65
- };
66
- let alignItems = "center";
67
- if (isRighty) alignItems = "flex-end";
68
- if (isLefty) alignItems = "flex-start";
69
- styles.alignItems = alignItems;
70
- if (computedPlacement.includes("top")) {
71
- const offset = computedOffset.top;
72
- styles.top = `max(env(safe-area-inset-top, 0px), ${offset})`;
73
- }
74
- if (computedPlacement.includes("bottom")) {
75
- const offset = computedOffset.bottom;
76
- styles.bottom = `max(env(safe-area-inset-bottom, 0px), ${offset})`;
77
- }
78
- if (!computedPlacement.includes("left")) {
79
- const offset = computedOffset.right;
80
- styles.insetInlineEnd = `calc(env(safe-area-inset-right, 0px) + ${offset})`;
81
- }
82
- if (!computedPlacement.includes("right")) {
83
- const offset = computedOffset.left;
84
- styles.insetInlineStart = `calc(env(safe-area-inset-left, 0px) + ${offset})`;
85
- }
86
- return styles;
25
+ const { prop, computed, context } = service;
26
+ const { offsets, gap } = prop("store").attrs;
27
+ const heights = context.get("heights");
28
+ const computedOffset = getOffsets(offsets);
29
+ const rtl = prop("dir") === "rtl";
30
+ const computedPlacement = placement.replace("-start", rtl ? "-right" : "-left").replace("-end", rtl ? "-left" : "-right");
31
+ const isRighty = computedPlacement.includes("right");
32
+ const isLefty = computedPlacement.includes("left");
33
+ const styles = {
34
+ position: "fixed",
35
+ pointerEvents: computed("count") > 0 ? void 0 : "none",
36
+ display: "flex",
37
+ flexDirection: "column",
38
+ "--gap": (0, _zag_js_utils.toPx)(gap),
39
+ "--first-height": (0, _zag_js_utils.toPx)(heights[0]?.height || 0),
40
+ "--viewport-offset-left": computedOffset.left,
41
+ "--viewport-offset-right": computedOffset.right,
42
+ "--viewport-offset-top": computedOffset.top,
43
+ "--viewport-offset-bottom": computedOffset.bottom
44
+ };
45
+ let alignItems = "center";
46
+ if (isRighty) alignItems = "flex-end";
47
+ if (isLefty) alignItems = "flex-start";
48
+ styles.alignItems = alignItems;
49
+ if (computedPlacement.includes("top")) styles.top = `max(env(safe-area-inset-top, 0px), ${computedOffset.top})`;
50
+ if (computedPlacement.includes("bottom")) styles.bottom = `max(env(safe-area-inset-bottom, 0px), ${computedOffset.bottom})`;
51
+ if (!computedPlacement.includes("left")) styles.insetInlineEnd = `calc(env(safe-area-inset-right, 0px) + ${computedOffset.right})`;
52
+ if (!computedPlacement.includes("right")) styles.insetInlineStart = `calc(env(safe-area-inset-left, 0px) + ${computedOffset.left})`;
53
+ return styles;
87
54
  }
88
55
  function getPlacementStyle(service, visible) {
89
- const { prop, context, computed } = service;
90
- const parent = prop("parent");
91
- const placement = parent.computed("placement");
92
- const { gap } = parent.prop("store").attrs;
93
- const [side] = placement.split("-");
94
- const mounted = context.get("mounted");
95
- const remainingTime = context.get("remainingTime");
96
- const height = computed("height");
97
- const frontmost = computed("frontmost");
98
- const sibling = !frontmost;
99
- const overlap = !prop("stacked");
100
- const stacked = prop("stacked");
101
- const type = prop("type");
102
- const duration = type === "loading" ? Number.MAX_SAFE_INTEGER : remainingTime;
103
- const offset = computed("heightIndex") * gap + computed("heightBefore");
104
- const styles = {
105
- position: "absolute",
106
- pointerEvents: "auto",
107
- "--opacity": "0",
108
- "--remove-delay": `${prop("removeDelay")}ms`,
109
- "--duration": `${duration}ms`,
110
- "--initial-height": `${height}px`,
111
- "--offset": `${offset}px`,
112
- "--index": prop("index"),
113
- "--z-index": computed("zIndex"),
114
- "--lift-amount": "calc(var(--lift) * var(--gap))",
115
- "--y": "100%",
116
- "--x": "0"
117
- };
118
- const assign = (overrides) => Object.assign(styles, overrides);
119
- if (side === "top") {
120
- assign({
121
- top: "0",
122
- "--sign": "-1",
123
- "--y": "-100%",
124
- "--lift": "1"
125
- });
126
- } else if (side === "bottom") {
127
- assign({
128
- bottom: "0",
129
- "--sign": "1",
130
- "--y": "100%",
131
- "--lift": "-1"
132
- });
133
- }
134
- if (mounted) {
135
- assign({
136
- "--y": "0",
137
- "--opacity": "1"
138
- });
139
- if (stacked) {
140
- assign({
141
- "--y": "calc(var(--lift) * var(--offset))",
142
- "--height": "var(--initial-height)"
143
- });
144
- }
145
- }
146
- if (!visible) {
147
- assign({
148
- "--opacity": "0",
149
- pointerEvents: "none"
150
- });
151
- }
152
- if (sibling && overlap) {
153
- assign({
154
- "--base-scale": "var(--index) * 0.05 + 1",
155
- "--y": "calc(var(--lift-amount) * var(--index))",
156
- "--scale": "calc(-1 * var(--base-scale))",
157
- "--height": "var(--first-height)"
158
- });
159
- if (!visible) {
160
- assign({
161
- "--y": "calc(var(--sign) * 40%)"
162
- });
163
- }
164
- }
165
- if (sibling && stacked && !visible) {
166
- assign({
167
- "--y": "calc(var(--lift) * var(--offset) + var(--lift) * -100%)"
168
- });
169
- }
170
- if (frontmost && !visible) {
171
- assign({
172
- "--y": "calc(var(--lift) * -100%)"
173
- });
174
- }
175
- return styles;
56
+ const { prop, context, computed } = service;
57
+ const parent = prop("parent");
58
+ const placement = parent.computed("placement");
59
+ const { gap } = parent.prop("store").attrs;
60
+ const [side] = placement.split("-");
61
+ const mounted = context.get("mounted");
62
+ const height = computed("height");
63
+ const frontmost = computed("frontmost");
64
+ const sibling = !frontmost;
65
+ const stacked = prop("parent").state.matches("stack");
66
+ const overlap = !stacked;
67
+ const type = prop("type");
68
+ const totalDuration = getToastDuration(prop("duration"), type);
69
+ const duration = type === "loading" || !Number.isFinite(totalDuration) ? Number.MAX_SAFE_INTEGER : totalDuration;
70
+ const offset = computed("heightIndex") * gap + computed("heightBefore");
71
+ const swipeOffset = context.get("swipeOffset");
72
+ const swipedOut = !visible ? context.get("swipeDirection") : null;
73
+ const exiting = !visible && !swipedOut;
74
+ const styles = {
75
+ position: "absolute",
76
+ pointerEvents: "auto",
77
+ "--opacity": "0",
78
+ "--remove-delay": `${prop("removeDelay")}ms`,
79
+ "--duration": `${duration}ms`,
80
+ "--initial-height": (0, _zag_js_utils.toPx)(height),
81
+ "--offset": (0, _zag_js_utils.toPx)(offset),
82
+ "--index": prop("index"),
83
+ "--z-index": computed("zIndex"),
84
+ "--lift-amount": "calc(var(--lift) * var(--gap))",
85
+ "--y": "100%",
86
+ "--x": "0px",
87
+ "--swipe-x": (0, _zag_js_utils.toPx)(swipeOffset.x),
88
+ "--swipe-y": (0, _zag_js_utils.toPx)(swipeOffset.y)
89
+ };
90
+ const assign = (overrides) => Object.assign(styles, overrides);
91
+ if (side === "top") assign({
92
+ top: "0",
93
+ "--sign": "-1",
94
+ "--y": "-100%",
95
+ "--lift": "1"
96
+ });
97
+ else if (side === "bottom") assign({
98
+ bottom: "0",
99
+ "--sign": "1",
100
+ "--y": "100%",
101
+ "--lift": "-1"
102
+ });
103
+ if (mounted) {
104
+ assign({
105
+ "--y": "0",
106
+ "--opacity": "1"
107
+ });
108
+ if (stacked) assign({
109
+ "--y": "calc(var(--lift) * var(--offset))",
110
+ "--height": "var(--initial-height)"
111
+ });
112
+ }
113
+ if (!visible) assign({
114
+ "--opacity": "0",
115
+ pointerEvents: "none"
116
+ });
117
+ if (sibling && overlap) {
118
+ assign({
119
+ "--base-scale": "var(--index) * 0.05 + 1",
120
+ "--y": "calc(var(--lift-amount) * var(--index))",
121
+ "--scale": "calc(-1 * var(--base-scale))",
122
+ "--height": "var(--first-height)"
123
+ });
124
+ if (exiting) assign({ "--y": "calc(var(--sign) * 40%)" });
125
+ }
126
+ if (sibling && stacked && exiting) assign({ "--y": "calc(var(--lift) * var(--offset) + var(--lift) * -100%)" });
127
+ if (frontmost && exiting) assign({ "--y": "calc(var(--lift) * -100%)" });
128
+ const [throwX, throwY] = swipedOut ? swipeThrow[swipedOut] : ["0px", "0px"];
129
+ assign({
130
+ "--x": `calc(${styles["--x"]} + var(--swipe-x) + ${throwX})`,
131
+ "--y": `calc(${styles["--y"]} + var(--swipe-y) + ${throwY})`
132
+ });
133
+ return styles;
176
134
  }
135
+ const swipeThrow = {
136
+ up: ["0px", "-100%"],
137
+ down: ["0px", "100%"],
138
+ left: ["-100%", "0px"],
139
+ right: ["100%", "0px"]
140
+ };
177
141
  function getGhostBeforeStyle(service, visible) {
178
- const { computed } = service;
179
- const styles = {
180
- position: "absolute",
181
- inset: "0",
182
- scale: "1 2",
183
- pointerEvents: visible ? "none" : "auto"
184
- };
185
- const assign = (overrides) => Object.assign(styles, overrides);
186
- if (computed("frontmost") && !visible) {
187
- assign({
188
- height: "calc(var(--initial-height) + 80%)"
189
- });
190
- }
191
- return styles;
142
+ const { computed } = service;
143
+ const styles = {
144
+ position: "absolute",
145
+ inset: "0",
146
+ scale: "1 2",
147
+ pointerEvents: visible ? "none" : "auto"
148
+ };
149
+ const assign = (overrides) => Object.assign(styles, overrides);
150
+ if (computed("frontmost") && !visible) assign({ height: "calc(var(--initial-height) + 80%)" });
151
+ return styles;
192
152
  }
193
153
  function getGhostAfterStyle() {
194
- return {
195
- position: "absolute",
196
- left: "0",
197
- height: "calc(var(--gap) + 2px)",
198
- bottom: "100%",
199
- width: "100%"
200
- };
154
+ return {
155
+ position: "absolute",
156
+ left: "0",
157
+ height: "calc(var(--gap) + 2px)",
158
+ bottom: "100%",
159
+ width: "100%"
160
+ };
201
161
  }
202
- // Annotate the CommonJS export names for ESM import in node:
203
- 0 && (module.exports = {
204
- defaultTimeouts,
205
- getGhostAfterStyle,
206
- getGhostBeforeStyle,
207
- getGroupPlacementStyle,
208
- getPlacementStyle,
209
- getToastDuration
210
- });
162
+ function setHeight(parent, item) {
163
+ const { id, height } = item;
164
+ parent.context.set("heights", (prev) => {
165
+ const existing = prev.find((i) => i.id === id);
166
+ if (existing) {
167
+ if (existing.height === height) return prev;
168
+ return prev.map((i) => i.id === id ? {
169
+ ...i,
170
+ height
171
+ } : i);
172
+ }
173
+ const toasts = parent.context.get("toasts");
174
+ const rank = (id) => toasts.findIndex((t) => t.id === id);
175
+ const index = rank(id);
176
+ let insertAt = prev.findIndex((i) => {
177
+ const r = rank(i.id);
178
+ return r === -1 || r > index;
179
+ });
180
+ if (insertAt === -1) insertAt = prev.length;
181
+ return [
182
+ ...prev.slice(0, insertAt),
183
+ {
184
+ id,
185
+ height
186
+ },
187
+ ...prev.slice(insertAt)
188
+ ];
189
+ });
190
+ }
191
+ exports.defaultTimeouts = defaultTimeouts;
192
+ exports.getGhostAfterStyle = getGhostAfterStyle;
193
+ exports.getGhostBeforeStyle = getGhostBeforeStyle;
194
+ exports.getGroupPlacementStyle = getGroupPlacementStyle;
195
+ exports.getPlacementStyle = getPlacementStyle;
196
+ exports.getToastDuration = getToastDuration;
197
+ exports.setHeight = setHeight;