@zag-js/toast 0.2.6 → 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,36 @@
|
|
|
1
|
+
import { Placement, MachineContext, GroupMachineContext } from './toast.types.js';
|
|
2
|
+
import '@zag-js/core';
|
|
3
|
+
import '@zag-js/types';
|
|
4
|
+
|
|
5
|
+
declare const dom: {
|
|
6
|
+
getRootNode: (ctx: {
|
|
7
|
+
getRootNode?: (() => Node | Document | ShadowRoot) | undefined;
|
|
8
|
+
}) => Document | ShadowRoot;
|
|
9
|
+
getDoc: (ctx: {
|
|
10
|
+
getRootNode?: (() => Node | Document | ShadowRoot) | undefined;
|
|
11
|
+
}) => Document;
|
|
12
|
+
getWin: (ctx: {
|
|
13
|
+
getRootNode?: (() => Node | Document | ShadowRoot) | undefined;
|
|
14
|
+
}) => Window & typeof globalThis;
|
|
15
|
+
getActiveElement: (ctx: {
|
|
16
|
+
getRootNode?: (() => Node | Document | ShadowRoot) | undefined;
|
|
17
|
+
}) => HTMLElement | null;
|
|
18
|
+
getById: <T = HTMLElement>(ctx: {
|
|
19
|
+
getRootNode?: (() => Node | Document | ShadowRoot) | undefined;
|
|
20
|
+
}, id: string) => T | null;
|
|
21
|
+
createEmitter: (ctx: {
|
|
22
|
+
getRootNode?: (() => Node | Document | ShadowRoot) | undefined;
|
|
23
|
+
}, target: HTMLElement) => (evt: string, detail: Record<string, any>, options?: EventInit | undefined) => void;
|
|
24
|
+
createListener: (target: HTMLElement) => <T_1 = any>(evt: string, handler: (e: CustomEvent<T_1>) => void) => () => void;
|
|
25
|
+
} & {
|
|
26
|
+
getGroupId: (placement: Placement) => string;
|
|
27
|
+
getRootId: (ctx: MachineContext) => string;
|
|
28
|
+
getTitleId: (ctx: MachineContext) => string;
|
|
29
|
+
getDescriptionId: (ctx: MachineContext) => string;
|
|
30
|
+
getCloseTriggerId: (ctx: MachineContext) => string;
|
|
31
|
+
getPortalId: (ctx: GroupMachineContext) => string;
|
|
32
|
+
getPortalEl: (ctx: GroupMachineContext) => HTMLElement | null;
|
|
33
|
+
createPortalEl: (ctx: GroupMachineContext) => HTMLElement;
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
export { dom };
|
|
@@ -0,0 +1,101 @@
|
|
|
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.dom.ts
|
|
21
|
+
var toast_dom_exports = {};
|
|
22
|
+
__export(toast_dom_exports, {
|
|
23
|
+
dom: () => dom
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(toast_dom_exports);
|
|
26
|
+
|
|
27
|
+
// ../../utilities/dom/src/query.ts
|
|
28
|
+
function isDocument(el) {
|
|
29
|
+
return el.nodeType === Node.DOCUMENT_NODE;
|
|
30
|
+
}
|
|
31
|
+
function isWindow(value) {
|
|
32
|
+
return (value == null ? void 0 : value.toString()) === "[object Window]";
|
|
33
|
+
}
|
|
34
|
+
function getDocument(el) {
|
|
35
|
+
var _a;
|
|
36
|
+
if (isWindow(el))
|
|
37
|
+
return el.document;
|
|
38
|
+
if (isDocument(el))
|
|
39
|
+
return el;
|
|
40
|
+
return (_a = el == null ? void 0 : el.ownerDocument) != null ? _a : document;
|
|
41
|
+
}
|
|
42
|
+
function defineDomHelpers(helpers) {
|
|
43
|
+
const dom2 = {
|
|
44
|
+
getRootNode: (ctx) => {
|
|
45
|
+
var _a, _b;
|
|
46
|
+
return (_b = (_a = ctx.getRootNode) == null ? void 0 : _a.call(ctx)) != null ? _b : document;
|
|
47
|
+
},
|
|
48
|
+
getDoc: (ctx) => getDocument(dom2.getRootNode(ctx)),
|
|
49
|
+
getWin: (ctx) => {
|
|
50
|
+
var _a;
|
|
51
|
+
return (_a = dom2.getDoc(ctx).defaultView) != null ? _a : window;
|
|
52
|
+
},
|
|
53
|
+
getActiveElement: (ctx) => dom2.getDoc(ctx).activeElement,
|
|
54
|
+
getById: (ctx, id) => dom2.getRootNode(ctx).getElementById(id),
|
|
55
|
+
createEmitter: (ctx, target) => {
|
|
56
|
+
const win = dom2.getWin(ctx);
|
|
57
|
+
return function emit(evt, detail, options) {
|
|
58
|
+
const { bubbles = true, cancelable, composed = true } = options != null ? options : {};
|
|
59
|
+
const eventName = `zag:${evt}`;
|
|
60
|
+
const init = { bubbles, cancelable, composed, detail };
|
|
61
|
+
const event = new win.CustomEvent(eventName, init);
|
|
62
|
+
target.dispatchEvent(event);
|
|
63
|
+
};
|
|
64
|
+
},
|
|
65
|
+
createListener: (target) => {
|
|
66
|
+
return function listen(evt, handler) {
|
|
67
|
+
const eventName = `zag:${evt}`;
|
|
68
|
+
const listener = (e) => handler(e);
|
|
69
|
+
target.addEventListener(eventName, listener);
|
|
70
|
+
return () => target.removeEventListener(eventName, listener);
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
return {
|
|
75
|
+
...dom2,
|
|
76
|
+
...helpers
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// src/toast.dom.ts
|
|
81
|
+
var dom = defineDomHelpers({
|
|
82
|
+
getGroupId: (placement) => `toast-group:${placement}`,
|
|
83
|
+
getRootId: (ctx) => `toast:${ctx.id}`,
|
|
84
|
+
getTitleId: (ctx) => `toast:${ctx.id}:title`,
|
|
85
|
+
getDescriptionId: (ctx) => `toast:${ctx.id}:description`,
|
|
86
|
+
getCloseTriggerId: (ctx) => `toast${ctx.id}:close`,
|
|
87
|
+
getPortalId: (ctx) => `toast-portal:${ctx.id}`,
|
|
88
|
+
getPortalEl: (ctx) => dom.getDoc(ctx).getElementById(dom.getPortalId(ctx)),
|
|
89
|
+
createPortalEl: (ctx) => {
|
|
90
|
+
const existing = dom.getPortalEl(ctx);
|
|
91
|
+
if (existing)
|
|
92
|
+
return existing;
|
|
93
|
+
const portal = dom.getDoc(ctx).createElement("toast-portal");
|
|
94
|
+
portal.id = dom.getPortalId(ctx);
|
|
95
|
+
return portal;
|
|
96
|
+
}
|
|
97
|
+
});
|
|
98
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
99
|
+
0 && (module.exports = {
|
|
100
|
+
dom
|
|
101
|
+
});
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import * as _zag_js_core from '@zag-js/core';
|
|
2
|
+
import { Options, MachineContext, MachineState } from './toast.types.js';
|
|
3
|
+
import '@zag-js/types';
|
|
4
|
+
|
|
5
|
+
declare function createToastMachine(options?: Options): _zag_js_core.Machine<MachineContext, MachineState, _zag_js_core.StateMachine.AnyEventObject>;
|
|
6
|
+
|
|
7
|
+
export { createToastMachine };
|
|
@@ -0,0 +1,305 @@
|
|
|
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.machine.ts
|
|
21
|
+
var toast_machine_exports = {};
|
|
22
|
+
__export(toast_machine_exports, {
|
|
23
|
+
createToastMachine: () => createToastMachine
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(toast_machine_exports);
|
|
26
|
+
var import_core = require("@zag-js/core");
|
|
27
|
+
|
|
28
|
+
// ../../utilities/core/src/functions.ts
|
|
29
|
+
var runIfFn = (v, ...a) => {
|
|
30
|
+
const res = typeof v === "function" ? v(...a) : v;
|
|
31
|
+
return res != null ? res : void 0;
|
|
32
|
+
};
|
|
33
|
+
var cast = (v) => v;
|
|
34
|
+
|
|
35
|
+
// ../../utilities/core/src/guard.ts
|
|
36
|
+
var isArray = (v) => Array.isArray(v);
|
|
37
|
+
var isObject = (v) => !(v == null || typeof v !== "object" || isArray(v));
|
|
38
|
+
var hasProp = (obj, prop) => Object.prototype.hasOwnProperty.call(obj, prop);
|
|
39
|
+
|
|
40
|
+
// ../../utilities/core/src/object.ts
|
|
41
|
+
function compact(obj) {
|
|
42
|
+
if (obj === void 0)
|
|
43
|
+
return obj;
|
|
44
|
+
return Object.fromEntries(
|
|
45
|
+
Object.entries(obj).filter(([, value]) => value !== void 0).map(([key, value]) => [key, isObject(value) ? compact(value) : value])
|
|
46
|
+
);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// ../../utilities/dom/src/query.ts
|
|
50
|
+
function isDocument(el) {
|
|
51
|
+
return el.nodeType === Node.DOCUMENT_NODE;
|
|
52
|
+
}
|
|
53
|
+
function isWindow(value) {
|
|
54
|
+
return (value == null ? void 0 : value.toString()) === "[object Window]";
|
|
55
|
+
}
|
|
56
|
+
function getDocument(el) {
|
|
57
|
+
var _a;
|
|
58
|
+
if (isWindow(el))
|
|
59
|
+
return el.document;
|
|
60
|
+
if (isDocument(el))
|
|
61
|
+
return el;
|
|
62
|
+
return (_a = el == null ? void 0 : el.ownerDocument) != null ? _a : document;
|
|
63
|
+
}
|
|
64
|
+
function defineDomHelpers(helpers) {
|
|
65
|
+
const dom2 = {
|
|
66
|
+
getRootNode: (ctx) => {
|
|
67
|
+
var _a, _b;
|
|
68
|
+
return (_b = (_a = ctx.getRootNode) == null ? void 0 : _a.call(ctx)) != null ? _b : document;
|
|
69
|
+
},
|
|
70
|
+
getDoc: (ctx) => getDocument(dom2.getRootNode(ctx)),
|
|
71
|
+
getWin: (ctx) => {
|
|
72
|
+
var _a;
|
|
73
|
+
return (_a = dom2.getDoc(ctx).defaultView) != null ? _a : window;
|
|
74
|
+
},
|
|
75
|
+
getActiveElement: (ctx) => dom2.getDoc(ctx).activeElement,
|
|
76
|
+
getById: (ctx, id) => dom2.getRootNode(ctx).getElementById(id),
|
|
77
|
+
createEmitter: (ctx, target) => {
|
|
78
|
+
const win = dom2.getWin(ctx);
|
|
79
|
+
return function emit(evt, detail, options) {
|
|
80
|
+
const { bubbles = true, cancelable, composed = true } = options != null ? options : {};
|
|
81
|
+
const eventName = `zag:${evt}`;
|
|
82
|
+
const init = { bubbles, cancelable, composed, detail };
|
|
83
|
+
const event = new win.CustomEvent(eventName, init);
|
|
84
|
+
target.dispatchEvent(event);
|
|
85
|
+
};
|
|
86
|
+
},
|
|
87
|
+
createListener: (target) => {
|
|
88
|
+
return function listen(evt, handler) {
|
|
89
|
+
const eventName = `zag:${evt}`;
|
|
90
|
+
const listener = (e) => handler(e);
|
|
91
|
+
target.addEventListener(eventName, listener);
|
|
92
|
+
return () => target.removeEventListener(eventName, listener);
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
};
|
|
96
|
+
return {
|
|
97
|
+
...dom2,
|
|
98
|
+
...helpers
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// ../../utilities/dom/src/listener.ts
|
|
103
|
+
var isRef = (v) => hasProp(v, "current");
|
|
104
|
+
function addDomEvent(target, eventName, handler, options) {
|
|
105
|
+
const node = isRef(target) ? target.current : runIfFn(target);
|
|
106
|
+
node == null ? void 0 : node.addEventListener(eventName, handler, options);
|
|
107
|
+
return () => {
|
|
108
|
+
node == null ? void 0 : node.removeEventListener(eventName, handler, options);
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// ../../utilities/dom/src/visibility-event.ts
|
|
113
|
+
function trackDocumentVisibility(_doc, callback) {
|
|
114
|
+
const doc = cast(_doc);
|
|
115
|
+
return addDomEvent(doc, "visibilitychange", () => {
|
|
116
|
+
const hidden = doc.hidden || doc.msHidden || doc.webkitHidden;
|
|
117
|
+
callback(!!hidden);
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// src/toast.dom.ts
|
|
122
|
+
var dom = defineDomHelpers({
|
|
123
|
+
getGroupId: (placement) => `toast-group:${placement}`,
|
|
124
|
+
getRootId: (ctx) => `toast:${ctx.id}`,
|
|
125
|
+
getTitleId: (ctx) => `toast:${ctx.id}:title`,
|
|
126
|
+
getDescriptionId: (ctx) => `toast:${ctx.id}:description`,
|
|
127
|
+
getCloseTriggerId: (ctx) => `toast${ctx.id}:close`,
|
|
128
|
+
getPortalId: (ctx) => `toast-portal:${ctx.id}`,
|
|
129
|
+
getPortalEl: (ctx) => dom.getDoc(ctx).getElementById(dom.getPortalId(ctx)),
|
|
130
|
+
createPortalEl: (ctx) => {
|
|
131
|
+
const existing = dom.getPortalEl(ctx);
|
|
132
|
+
if (existing)
|
|
133
|
+
return existing;
|
|
134
|
+
const portal = dom.getDoc(ctx).createElement("toast-portal");
|
|
135
|
+
portal.id = dom.getPortalId(ctx);
|
|
136
|
+
return portal;
|
|
137
|
+
}
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
// src/toast.utils.ts
|
|
141
|
+
var defaultTimeouts = {
|
|
142
|
+
info: 5e3,
|
|
143
|
+
error: 5e3,
|
|
144
|
+
success: 2e3,
|
|
145
|
+
loading: Infinity,
|
|
146
|
+
custom: 5e3
|
|
147
|
+
};
|
|
148
|
+
function getToastDuration(duration, type) {
|
|
149
|
+
return duration != null ? duration : defaultTimeouts[type];
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
// src/toast.machine.ts
|
|
153
|
+
var { not, and, or } = import_core.guards;
|
|
154
|
+
function createToastMachine(options = {}) {
|
|
155
|
+
const { type = "info", duration, id = "toast", placement = "bottom", removeDelay = 0, ...restProps } = options;
|
|
156
|
+
const ctx = compact(restProps);
|
|
157
|
+
const computedDuration = getToastDuration(duration, type);
|
|
158
|
+
return (0, import_core.createMachine)(
|
|
159
|
+
{
|
|
160
|
+
id,
|
|
161
|
+
entry: "invokeOnOpen",
|
|
162
|
+
initial: type === "loading" ? "persist" : "active",
|
|
163
|
+
context: {
|
|
164
|
+
id,
|
|
165
|
+
type,
|
|
166
|
+
remaining: computedDuration,
|
|
167
|
+
duration: computedDuration,
|
|
168
|
+
removeDelay,
|
|
169
|
+
createdAt: Date.now(),
|
|
170
|
+
placement,
|
|
171
|
+
...ctx
|
|
172
|
+
},
|
|
173
|
+
on: {
|
|
174
|
+
UPDATE: [
|
|
175
|
+
{
|
|
176
|
+
guard: and("hasTypeChanged", "isChangingToLoading"),
|
|
177
|
+
target: "persist",
|
|
178
|
+
actions: ["setContext", "invokeOnUpdate"]
|
|
179
|
+
},
|
|
180
|
+
{
|
|
181
|
+
guard: or("hasDurationChanged", "hasTypeChanged"),
|
|
182
|
+
target: "active:temp",
|
|
183
|
+
actions: ["setContext", "invokeOnUpdate"]
|
|
184
|
+
},
|
|
185
|
+
{
|
|
186
|
+
actions: ["setContext", "invokeOnUpdate"]
|
|
187
|
+
}
|
|
188
|
+
]
|
|
189
|
+
},
|
|
190
|
+
states: {
|
|
191
|
+
"active:temp": {
|
|
192
|
+
tags: ["visible", "updating"],
|
|
193
|
+
after: {
|
|
194
|
+
0: "active"
|
|
195
|
+
}
|
|
196
|
+
},
|
|
197
|
+
persist: {
|
|
198
|
+
tags: ["visible", "paused"],
|
|
199
|
+
activities: "trackDocumentVisibility",
|
|
200
|
+
on: {
|
|
201
|
+
RESUME: {
|
|
202
|
+
guard: not("isLoadingType"),
|
|
203
|
+
target: "active",
|
|
204
|
+
actions: ["setCreatedAt"]
|
|
205
|
+
},
|
|
206
|
+
DISMISS: "dismissing"
|
|
207
|
+
}
|
|
208
|
+
},
|
|
209
|
+
active: {
|
|
210
|
+
tags: ["visible"],
|
|
211
|
+
activities: "trackDocumentVisibility",
|
|
212
|
+
after: {
|
|
213
|
+
VISIBLE_DURATION: "dismissing"
|
|
214
|
+
},
|
|
215
|
+
on: {
|
|
216
|
+
DISMISS: "dismissing",
|
|
217
|
+
PAUSE: {
|
|
218
|
+
target: "persist",
|
|
219
|
+
actions: "setRemainingDuration"
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
},
|
|
223
|
+
dismissing: {
|
|
224
|
+
entry: "invokeOnClosing",
|
|
225
|
+
after: {
|
|
226
|
+
REMOVE_DELAY: {
|
|
227
|
+
target: "inactive",
|
|
228
|
+
actions: "notifyParentToRemove"
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
},
|
|
232
|
+
inactive: {
|
|
233
|
+
entry: "invokeOnClose",
|
|
234
|
+
type: "final"
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
},
|
|
238
|
+
{
|
|
239
|
+
activities: {
|
|
240
|
+
trackDocumentVisibility(ctx2, _evt, { send }) {
|
|
241
|
+
if (!ctx2.pauseOnPageIdle)
|
|
242
|
+
return;
|
|
243
|
+
return trackDocumentVisibility(dom.getDoc(ctx2), function(hidden) {
|
|
244
|
+
send(hidden ? "PAUSE" : "RESUME");
|
|
245
|
+
});
|
|
246
|
+
}
|
|
247
|
+
},
|
|
248
|
+
guards: {
|
|
249
|
+
isChangingToLoading: (_, evt) => {
|
|
250
|
+
var _a;
|
|
251
|
+
return ((_a = evt.toast) == null ? void 0 : _a.type) === "loading";
|
|
252
|
+
},
|
|
253
|
+
isLoadingType: (ctx2) => ctx2.type === "loading",
|
|
254
|
+
hasTypeChanged: (ctx2, evt) => {
|
|
255
|
+
var _a;
|
|
256
|
+
return ((_a = evt.toast) == null ? void 0 : _a.type) !== ctx2.type;
|
|
257
|
+
},
|
|
258
|
+
hasDurationChanged: (ctx2, evt) => {
|
|
259
|
+
var _a;
|
|
260
|
+
return ((_a = evt.toast) == null ? void 0 : _a.duration) !== ctx2.duration;
|
|
261
|
+
}
|
|
262
|
+
},
|
|
263
|
+
delays: {
|
|
264
|
+
VISIBLE_DURATION: (ctx2) => ctx2.remaining,
|
|
265
|
+
REMOVE_DELAY: (ctx2) => ctx2.removeDelay
|
|
266
|
+
},
|
|
267
|
+
actions: {
|
|
268
|
+
setRemainingDuration(ctx2) {
|
|
269
|
+
ctx2.remaining -= Date.now() - ctx2.createdAt;
|
|
270
|
+
},
|
|
271
|
+
setCreatedAt(ctx2) {
|
|
272
|
+
ctx2.createdAt = Date.now();
|
|
273
|
+
},
|
|
274
|
+
notifyParentToRemove(_ctx, _evt, { self }) {
|
|
275
|
+
self.sendParent({ type: "REMOVE_TOAST", id: self.id });
|
|
276
|
+
},
|
|
277
|
+
invokeOnClosing(ctx2) {
|
|
278
|
+
var _a;
|
|
279
|
+
(_a = ctx2.onClosing) == null ? void 0 : _a.call(ctx2);
|
|
280
|
+
},
|
|
281
|
+
invokeOnClose(ctx2) {
|
|
282
|
+
var _a;
|
|
283
|
+
(_a = ctx2.onClose) == null ? void 0 : _a.call(ctx2);
|
|
284
|
+
},
|
|
285
|
+
invokeOnOpen(ctx2) {
|
|
286
|
+
var _a;
|
|
287
|
+
(_a = ctx2.onOpen) == null ? void 0 : _a.call(ctx2);
|
|
288
|
+
},
|
|
289
|
+
invokeOnUpdate(ctx2) {
|
|
290
|
+
var _a;
|
|
291
|
+
(_a = ctx2.onUpdate) == null ? void 0 : _a.call(ctx2);
|
|
292
|
+
},
|
|
293
|
+
setContext(ctx2, evt) {
|
|
294
|
+
const { duration: duration2, type: type2 } = evt.toast;
|
|
295
|
+
const time = getToastDuration(duration2, type2);
|
|
296
|
+
Object.assign(ctx2, { ...evt.toast, duration: time, remaining: time });
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
);
|
|
301
|
+
}
|
|
302
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
303
|
+
0 && (module.exports = {
|
|
304
|
+
createToastMachine
|
|
305
|
+
});
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
import { StateMachine, Machine } from '@zag-js/core';
|
|
2
|
+
import { RootProperties, CommonProperties, Direction, RequiredBy, DirectionProperty, Context } from '@zag-js/types';
|
|
3
|
+
|
|
4
|
+
type Type = "success" | "error" | "loading" | "info" | "custom";
|
|
5
|
+
type Placement = "top-start" | "top" | "top-end" | "bottom-start" | "bottom" | "bottom-end";
|
|
6
|
+
type SharedContext = {
|
|
7
|
+
/**
|
|
8
|
+
* Whether to pause toast when the user leaves the browser tab
|
|
9
|
+
*/
|
|
10
|
+
pauseOnPageIdle?: boolean;
|
|
11
|
+
/**
|
|
12
|
+
* Whether to pause the toast when interacted with
|
|
13
|
+
*/
|
|
14
|
+
pauseOnInteraction?: boolean;
|
|
15
|
+
};
|
|
16
|
+
type ToastOptions = {
|
|
17
|
+
/**
|
|
18
|
+
* The unique id of the toast
|
|
19
|
+
*/
|
|
20
|
+
id: string;
|
|
21
|
+
/**
|
|
22
|
+
* The type of the toast
|
|
23
|
+
*/
|
|
24
|
+
type: Type;
|
|
25
|
+
/**
|
|
26
|
+
* The placement of the toast
|
|
27
|
+
*/
|
|
28
|
+
placement: Placement;
|
|
29
|
+
/**
|
|
30
|
+
* The message of the toast
|
|
31
|
+
*/
|
|
32
|
+
title?: string;
|
|
33
|
+
/**
|
|
34
|
+
* The description of the toast
|
|
35
|
+
*/
|
|
36
|
+
description?: string;
|
|
37
|
+
/**
|
|
38
|
+
* The duration the toast will be visible
|
|
39
|
+
*/
|
|
40
|
+
duration: number;
|
|
41
|
+
/**
|
|
42
|
+
* Custom function to render the toast element.
|
|
43
|
+
*/
|
|
44
|
+
render?: (options: RenderOptions) => any;
|
|
45
|
+
/**
|
|
46
|
+
* The duration for the toast to kept alive before it is removed.
|
|
47
|
+
* Useful for exit transitions.
|
|
48
|
+
*/
|
|
49
|
+
removeDelay?: number;
|
|
50
|
+
/**
|
|
51
|
+
* Function called when the toast has been closed and removed
|
|
52
|
+
*/
|
|
53
|
+
onClose?: VoidFunction;
|
|
54
|
+
/**
|
|
55
|
+
* Function called when the toast is leaving
|
|
56
|
+
*/
|
|
57
|
+
onClosing?: VoidFunction;
|
|
58
|
+
/**
|
|
59
|
+
* Function called when the toast is shown
|
|
60
|
+
*/
|
|
61
|
+
onOpen?: VoidFunction;
|
|
62
|
+
/**
|
|
63
|
+
* Function called when the toast is updated
|
|
64
|
+
*/
|
|
65
|
+
onUpdate?: VoidFunction;
|
|
66
|
+
};
|
|
67
|
+
type Options = Partial<ToastOptions>;
|
|
68
|
+
type RenderOptions = Omit<ToastOptions, "render"> & {
|
|
69
|
+
dismiss(): void;
|
|
70
|
+
};
|
|
71
|
+
type MachineContext = SharedContext & RootProperties & CommonProperties & Omit<ToastOptions, "removeDelay"> & {
|
|
72
|
+
/**
|
|
73
|
+
* The duration for the toast to kept alive before it is removed.
|
|
74
|
+
* Useful for exit transitions.
|
|
75
|
+
*/
|
|
76
|
+
removeDelay: number;
|
|
77
|
+
/**
|
|
78
|
+
* The document's text/writing direction.
|
|
79
|
+
*/
|
|
80
|
+
dir?: Direction;
|
|
81
|
+
/**
|
|
82
|
+
* The time the toast was created
|
|
83
|
+
*/
|
|
84
|
+
createdAt: number;
|
|
85
|
+
/**
|
|
86
|
+
* The time left before the toast is removed
|
|
87
|
+
*/
|
|
88
|
+
remaining: number;
|
|
89
|
+
};
|
|
90
|
+
type MachineState = {
|
|
91
|
+
value: "active" | "active:temp" | "dismissing" | "inactive" | "persist";
|
|
92
|
+
tags: "visible" | "paused" | "updating";
|
|
93
|
+
};
|
|
94
|
+
type State = StateMachine.State<MachineContext, MachineState>;
|
|
95
|
+
type Send = StateMachine.Send;
|
|
96
|
+
type Service = Machine<MachineContext, MachineState>;
|
|
97
|
+
type GroupPublicContext = SharedContext & DirectionProperty & CommonProperties & {
|
|
98
|
+
/**
|
|
99
|
+
* The gutter or spacing between toasts
|
|
100
|
+
*/
|
|
101
|
+
gutter: string;
|
|
102
|
+
/**
|
|
103
|
+
* The z-index applied to each toast group
|
|
104
|
+
*/
|
|
105
|
+
zIndex: number;
|
|
106
|
+
/**
|
|
107
|
+
* The maximum number of toasts that can be shown at once
|
|
108
|
+
*/
|
|
109
|
+
max: number;
|
|
110
|
+
/**
|
|
111
|
+
* The offset from the safe environment edge of the viewport
|
|
112
|
+
*/
|
|
113
|
+
offsets: string | Record<"left" | "right" | "bottom" | "top", string>;
|
|
114
|
+
};
|
|
115
|
+
type UserDefinedGroupContext = RequiredBy<GroupPublicContext, "id">;
|
|
116
|
+
type GroupComputedContext = Readonly<{
|
|
117
|
+
/**
|
|
118
|
+
* @computed
|
|
119
|
+
* The total number of toasts in the group
|
|
120
|
+
*/
|
|
121
|
+
readonly count: number;
|
|
122
|
+
}>;
|
|
123
|
+
type GroupPrivateContext = Context<{}>;
|
|
124
|
+
type GroupMachineContext = GroupPublicContext & GroupComputedContext & GroupPrivateContext;
|
|
125
|
+
type GroupState = StateMachine.State<GroupMachineContext>;
|
|
126
|
+
type GroupSend = (event: StateMachine.Event<StateMachine.AnyEventObject>) => void;
|
|
127
|
+
type MaybeFunction<Value, Args> = Value | ((arg: Args) => Value);
|
|
128
|
+
type PromiseOptions<Value> = {
|
|
129
|
+
loading: ToastOptions;
|
|
130
|
+
success: MaybeFunction<ToastOptions, Value>;
|
|
131
|
+
error: MaybeFunction<ToastOptions, Error>;
|
|
132
|
+
};
|
|
133
|
+
type GroupProps = {
|
|
134
|
+
placement: Placement;
|
|
135
|
+
label?: string;
|
|
136
|
+
};
|
|
137
|
+
type Toaster = {
|
|
138
|
+
count: number;
|
|
139
|
+
isVisible(id: string): boolean;
|
|
140
|
+
upsert(options: ToastOptions): string | undefined;
|
|
141
|
+
create(options: ToastOptions): string | undefined;
|
|
142
|
+
success(options: ToastOptions): string | undefined;
|
|
143
|
+
error(options: ToastOptions): string | undefined;
|
|
144
|
+
loading(options: ToastOptions): string | undefined;
|
|
145
|
+
dismiss(id?: string | undefined): void;
|
|
146
|
+
remove(id?: string | undefined): void;
|
|
147
|
+
promise<T>(promise: Promise<T>, options: PromiseOptions<T>, shared?: ToastOptions): Promise<T>;
|
|
148
|
+
};
|
|
149
|
+
|
|
150
|
+
export { GroupMachineContext, GroupProps, GroupSend, GroupState, MachineContext, MachineState, Options, Placement, PromiseOptions, RenderOptions, Send, Service, State, ToastOptions, Toaster, Type, UserDefinedGroupContext };
|
|
@@ -0,0 +1,18 @@
|
|
|
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);
|
|
File without changes
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { Style } from '@zag-js/types';
|
|
2
|
+
import { Service, Placement, Type, MachineContext, GroupMachineContext } from './toast.types.js';
|
|
3
|
+
import '@zag-js/core';
|
|
4
|
+
|
|
5
|
+
declare function getToastsByPlacement(toasts: Service[]): Partial<Record<Placement, Service[]>>;
|
|
6
|
+
declare const defaultTimeouts: Record<Type, number>;
|
|
7
|
+
declare function getToastDuration(duration: number | undefined, type: MachineContext["type"]): number;
|
|
8
|
+
declare function getGroupPlacementStyle(ctx: GroupMachineContext, placement: Placement): Style;
|
|
9
|
+
|
|
10
|
+
export { defaultTimeouts, getGroupPlacementStyle, getToastDuration, getToastsByPlacement };
|