@zag-js/toast 0.1.9 → 0.1.12

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/index.js CHANGED
@@ -1,37 +1,8 @@
1
1
  "use strict";
2
2
  var __defProp = Object.defineProperty;
3
- var __defProps = Object.defineProperties;
4
3
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
- var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
6
4
  var __getOwnPropNames = Object.getOwnPropertyNames;
7
- var __getOwnPropSymbols = Object.getOwnPropertySymbols;
8
5
  var __hasOwnProp = Object.prototype.hasOwnProperty;
9
- var __propIsEnum = Object.prototype.propertyIsEnumerable;
10
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
11
- var __spreadValues = (a, b) => {
12
- for (var prop in b || (b = {}))
13
- if (__hasOwnProp.call(b, prop))
14
- __defNormalProp(a, prop, b[prop]);
15
- if (__getOwnPropSymbols)
16
- for (var prop of __getOwnPropSymbols(b)) {
17
- if (__propIsEnum.call(b, prop))
18
- __defNormalProp(a, prop, b[prop]);
19
- }
20
- return a;
21
- };
22
- var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
23
- var __objRest = (source, exclude) => {
24
- var target = {};
25
- for (var prop in source)
26
- if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
27
- target[prop] = source[prop];
28
- if (source != null && __getOwnPropSymbols)
29
- for (var prop of __getOwnPropSymbols(source)) {
30
- if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
31
- target[prop] = source[prop];
32
- }
33
- return target;
34
- };
35
6
  var __export = (target, all) => {
36
7
  for (var name in all)
37
8
  __defProp(target, name, { get: all[name], enumerable: true });
@@ -63,11 +34,40 @@ var dataAttr = (guard) => {
63
34
  var MAX_Z_INDEX = 2147483647;
64
35
  var runIfFn = (v, ...a) => {
65
36
  const res = typeof v === "function" ? v(...a) : v;
66
- return res != null ? res : void 0;
37
+ return res ?? void 0;
67
38
  };
68
39
  var cast = (v) => v;
69
40
  var hasProp = (obj, prop) => Object.prototype.hasOwnProperty.call(obj, prop);
70
41
  var isDom = () => typeof window !== "undefined";
42
+ function isDocument(el) {
43
+ return el.nodeType === Node.DOCUMENT_NODE;
44
+ }
45
+ function isWindow(value) {
46
+ return (value == null ? void 0 : value.toString()) === "[object Window]";
47
+ }
48
+ function getDocument(el) {
49
+ if (isWindow(el))
50
+ return el.document;
51
+ if (isDocument(el))
52
+ return el;
53
+ return (el == null ? void 0 : el.ownerDocument) ?? document;
54
+ }
55
+ function defineDomHelpers(helpers) {
56
+ const dom2 = {
57
+ getRootNode: (ctx) => {
58
+ var _a;
59
+ return ((_a = ctx.getRootNode) == null ? void 0 : _a.call(ctx)) ?? document;
60
+ },
61
+ getDoc: (ctx) => getDocument(dom2.getRootNode(ctx)),
62
+ getWin: (ctx) => dom2.getDoc(ctx).defaultView ?? window,
63
+ getActiveElement: (ctx) => dom2.getDoc(ctx).activeElement,
64
+ getById: (ctx, id) => dom2.getRootNode(ctx).getElementById(id)
65
+ };
66
+ return {
67
+ ...dom2,
68
+ ...helpers
69
+ };
70
+ }
71
71
  var isRef = (v) => hasProp(v, "current");
72
72
  function addDomEvent(target, eventName, handler, options) {
73
73
  const node = isRef(target) ? target.current : runIfFn(target);
@@ -87,7 +87,7 @@ function trackDocumentVisibility(_doc, callback) {
87
87
  // ../../utilities/core/dist/index.mjs
88
88
  var runIfFn2 = (v, ...a) => {
89
89
  const res = typeof v === "function" ? v(...a) : v;
90
- return res != null ? res : void 0;
90
+ return res ?? void 0;
91
91
  };
92
92
  var uuid = /* @__PURE__ */ (() => {
93
93
  let id = 0;
@@ -99,7 +99,7 @@ var uuid = /* @__PURE__ */ (() => {
99
99
  function warn(...a) {
100
100
  const m = a.length === 1 ? a[0] : a[1];
101
101
  const c = a.length === 2 ? a[0] : true;
102
- if (c && void 0 !== "production") {
102
+ if (c && process.env.NODE_ENV !== "production") {
103
103
  console.warn(m);
104
104
  }
105
105
  }
@@ -108,20 +108,13 @@ function warn(...a) {
108
108
  var import_core = require("@zag-js/core");
109
109
 
110
110
  // src/toast.dom.ts
111
- var dom = {
112
- getDoc: (ctx) => {
113
- var _a;
114
- return (_a = ctx.doc) != null ? _a : document;
115
- },
116
- getRootNode: (ctx) => {
117
- var _a;
118
- return (_a = ctx.rootNode) != null ? _a : dom.getDoc(ctx);
119
- },
111
+ var dom = defineDomHelpers({
120
112
  getGroupId: (placement) => `toast-group:${placement}`,
121
113
  getContainerId: (ctx) => `toast:${ctx.id}`,
122
114
  getTitleId: (ctx) => `toast-title:${ctx.id}`,
115
+ getDescriptionId: (ctx) => `toast-description:${ctx.id}`,
123
116
  getCloseButtonId: (ctx) => `toast-close-button:${ctx.id}`,
124
- getPortalId: (ctx) => `toast-portal:${ctx.uid}`,
117
+ getPortalId: (ctx) => `toast-portal:${ctx.id}`,
125
118
  getPortalEl: (ctx) => dom.getDoc(ctx).getElementById(dom.getPortalId(ctx)),
126
119
  createPortalEl: (ctx) => {
127
120
  const existing = dom.getPortalEl(ctx);
@@ -131,7 +124,7 @@ var dom = {
131
124
  portal.id = dom.getPortalId(ctx);
132
125
  return portal;
133
126
  }
134
- };
127
+ });
135
128
 
136
129
  // src/toast.utils.ts
137
130
  function getToastsByPlacement(toasts) {
@@ -151,7 +144,7 @@ var defaultTimeouts = {
151
144
  custom: 5e3
152
145
  };
153
146
  function getToastDuration(duration, type) {
154
- return duration != null ? duration : defaultTimeouts[type];
147
+ return duration ?? defaultTimeouts[type];
155
148
  }
156
149
  function getGroupPlacementStyle(ctx, placement) {
157
150
  const offset = ctx.offsets;
@@ -210,7 +203,7 @@ function groupConnect(state, send, normalize) {
210
203
  const id = options.id ? options.id : uid;
211
204
  if (group2.isVisible(id))
212
205
  return;
213
- send({ type: "ADD_TOAST", toast: __spreadProps(__spreadValues({}, options), { id }) });
206
+ send({ type: "ADD_TOAST", toast: { ...options, id } });
214
207
  return id;
215
208
  },
216
209
  upsert(options) {
@@ -261,13 +254,13 @@ function groupConnect(state, send, normalize) {
261
254
  return group2.upsert(options);
262
255
  },
263
256
  promise(promise, options, shared = {}) {
264
- const id = group2.loading(__spreadValues(__spreadValues({}, shared), options.loading));
257
+ const id = group2.loading({ ...shared, ...options.loading });
265
258
  promise.then((response) => {
266
259
  const successOptions = runIfFn2(options.success, response);
267
- group2.success(__spreadProps(__spreadValues(__spreadValues({}, shared), successOptions), { id }));
260
+ group2.success({ ...shared, ...successOptions, id });
268
261
  }).catch((error) => {
269
262
  const errorOptions = runIfFn2(options.error, error);
270
- group2.error(__spreadProps(__spreadValues(__spreadValues({}, shared), errorOptions), { id }));
263
+ group2.error({ ...shared, ...errorOptions, id });
271
264
  });
272
265
  return promise;
273
266
  },
@@ -323,178 +316,174 @@ var import_core3 = require("@zag-js/core");
323
316
  var import_core2 = require("@zag-js/core");
324
317
  var { not, and, or } = import_core2.guards;
325
318
  function createToastMachine(options = {}) {
326
- const _a = options, { type = "info", duration, id = "toast", placement = "bottom", removeDelay = 500 } = _a, rest = __objRest(_a, ["type", "duration", "id", "placement", "removeDelay"]);
319
+ const { type = "info", duration, id = "toast", placement = "bottom", removeDelay = 500, ...rest } = options;
327
320
  const __duration = getToastDuration(duration, type);
328
- return (0, import_core2.createMachine)({
329
- id,
330
- entry: "invokeOnOpen",
331
- initial: type === "loading" ? "persist" : "active",
332
- context: __spreadValues({
321
+ return (0, import_core2.createMachine)(
322
+ {
333
323
  id,
334
- type,
335
- remaining: __duration,
336
- duration: __duration,
337
- removeDelay,
338
- createdAt: Date.now(),
339
- placement
340
- }, rest),
341
- on: {
342
- UPDATE: [
343
- {
344
- guard: and("hasTypeChanged", "isChangingToLoading"),
345
- target: "persist",
346
- actions: ["setContext", "invokeOnUpdate"]
347
- },
348
- {
349
- guard: or("hasDurationChanged", "hasTypeChanged"),
350
- target: "active:temp",
351
- actions: ["setContext", "invokeOnUpdate"]
352
- },
353
- {
354
- actions: ["setContext", "invokeOnUpdate"]
355
- }
356
- ]
357
- },
358
- states: {
359
- "active:temp": {
360
- tags: ["visible", "updating"],
361
- after: {
362
- 0: "active"
363
- }
324
+ entry: "invokeOnOpen",
325
+ initial: type === "loading" ? "persist" : "active",
326
+ context: {
327
+ id,
328
+ type,
329
+ remaining: __duration,
330
+ duration: __duration,
331
+ removeDelay,
332
+ createdAt: Date.now(),
333
+ placement,
334
+ ...rest
364
335
  },
365
- persist: {
366
- tags: ["visible", "paused"],
367
- activities: "trackDocumentVisibility",
368
- on: {
369
- RESUME: {
370
- guard: not("isLoadingType"),
371
- target: "active",
372
- actions: ["setCreatedAt"]
336
+ on: {
337
+ UPDATE: [
338
+ {
339
+ guard: and("hasTypeChanged", "isChangingToLoading"),
340
+ target: "persist",
341
+ actions: ["setContext", "invokeOnUpdate"]
373
342
  },
374
- DISMISS: "dismissing"
375
- }
343
+ {
344
+ guard: or("hasDurationChanged", "hasTypeChanged"),
345
+ target: "active:temp",
346
+ actions: ["setContext", "invokeOnUpdate"]
347
+ },
348
+ {
349
+ actions: ["setContext", "invokeOnUpdate"]
350
+ }
351
+ ]
376
352
  },
377
- active: {
378
- tags: ["visible"],
379
- activities: "trackDocumentVisibility",
380
- after: {
381
- VISIBLE_DURATION: "dismissing"
353
+ states: {
354
+ "active:temp": {
355
+ tags: ["visible", "updating"],
356
+ after: {
357
+ 0: "active"
358
+ }
382
359
  },
383
- on: {
384
- DISMISS: "dismissing",
385
- PAUSE: {
386
- target: "persist",
387
- actions: "setRemainingDuration"
360
+ persist: {
361
+ tags: ["visible", "paused"],
362
+ activities: "trackDocumentVisibility",
363
+ on: {
364
+ RESUME: {
365
+ guard: not("isLoadingType"),
366
+ target: "active",
367
+ actions: ["setCreatedAt"]
368
+ },
369
+ DISMISS: "dismissing"
388
370
  }
389
- }
390
- },
391
- dismissing: {
392
- entry: "invokeOnClosing",
393
- after: {
394
- REMOVE_DELAY: {
395
- target: "inactive",
396
- actions: "notifyParentToRemove"
371
+ },
372
+ active: {
373
+ tags: ["visible"],
374
+ activities: "trackDocumentVisibility",
375
+ after: {
376
+ VISIBLE_DURATION: "dismissing"
377
+ },
378
+ on: {
379
+ DISMISS: "dismissing",
380
+ PAUSE: {
381
+ target: "persist",
382
+ actions: "setRemainingDuration"
383
+ }
397
384
  }
385
+ },
386
+ dismissing: {
387
+ entry: "invokeOnClosing",
388
+ after: {
389
+ REMOVE_DELAY: {
390
+ target: "inactive",
391
+ actions: "notifyParentToRemove"
392
+ }
393
+ }
394
+ },
395
+ inactive: {
396
+ entry: "invokeOnClose",
397
+ type: "final"
398
398
  }
399
- },
400
- inactive: {
401
- entry: "invokeOnClose",
402
- type: "final"
403
- }
404
- }
405
- }, {
406
- activities: {
407
- trackDocumentVisibility(ctx, _evt, { send }) {
408
- if (!ctx.pauseOnPageIdle)
409
- return;
410
- return trackDocumentVisibility(dom.getDoc(ctx), function(hidden) {
411
- send(hidden ? "PAUSE" : "RESUME");
412
- });
413
399
  }
414
400
  },
415
- guards: {
416
- isChangingToLoading: (_, evt) => {
417
- var _a2;
418
- return ((_a2 = evt.toast) == null ? void 0 : _a2.type) === "loading";
419
- },
420
- isLoadingType: (ctx) => ctx.type === "loading",
421
- hasTypeChanged: (ctx, evt) => {
422
- var _a2;
423
- return ((_a2 = evt.toast) == null ? void 0 : _a2.type) !== ctx.type;
424
- },
425
- hasDurationChanged: (ctx, evt) => {
426
- var _a2;
427
- return ((_a2 = evt.toast) == null ? void 0 : _a2.duration) !== ctx.duration;
428
- }
429
- },
430
- delays: {
431
- VISIBLE_DURATION: (ctx) => ctx.remaining,
432
- REMOVE_DELAY: (ctx) => ctx.removeDelay
433
- },
434
- actions: {
435
- setRemainingDuration(ctx) {
436
- ctx.remaining -= Date.now() - ctx.createdAt;
437
- },
438
- setCreatedAt(ctx) {
439
- ctx.createdAt = Date.now();
440
- },
441
- notifyParentToRemove(_ctx, _evt, { self }) {
442
- self.sendParent({ type: "REMOVE_TOAST", id: self.id });
443
- },
444
- invokeOnClosing(ctx) {
445
- var _a2;
446
- (_a2 = ctx.onClosing) == null ? void 0 : _a2.call(ctx);
447
- },
448
- invokeOnClose(ctx) {
449
- var _a2;
450
- (_a2 = ctx.onClose) == null ? void 0 : _a2.call(ctx);
401
+ {
402
+ activities: {
403
+ trackDocumentVisibility(ctx, _evt, { send }) {
404
+ if (!ctx.pauseOnPageIdle)
405
+ return;
406
+ return trackDocumentVisibility(dom.getDoc(ctx), function(hidden) {
407
+ send(hidden ? "PAUSE" : "RESUME");
408
+ });
409
+ }
451
410
  },
452
- invokeOnOpen(ctx) {
453
- var _a2;
454
- (_a2 = ctx.onOpen) == null ? void 0 : _a2.call(ctx);
411
+ guards: {
412
+ isChangingToLoading: (_, evt) => {
413
+ var _a;
414
+ return ((_a = evt.toast) == null ? void 0 : _a.type) === "loading";
415
+ },
416
+ isLoadingType: (ctx) => ctx.type === "loading",
417
+ hasTypeChanged: (ctx, evt) => {
418
+ var _a;
419
+ return ((_a = evt.toast) == null ? void 0 : _a.type) !== ctx.type;
420
+ },
421
+ hasDurationChanged: (ctx, evt) => {
422
+ var _a;
423
+ return ((_a = evt.toast) == null ? void 0 : _a.duration) !== ctx.duration;
424
+ }
455
425
  },
456
- invokeOnUpdate(ctx) {
457
- var _a2;
458
- (_a2 = ctx.onUpdate) == null ? void 0 : _a2.call(ctx);
426
+ delays: {
427
+ VISIBLE_DURATION: (ctx) => ctx.remaining,
428
+ REMOVE_DELAY: (ctx) => ctx.removeDelay
459
429
  },
460
- setContext(ctx, evt) {
461
- const { duration: duration2, type: type2 } = evt.toast;
462
- const time = getToastDuration(duration2, type2);
463
- Object.assign(ctx, __spreadProps(__spreadValues({}, evt.toast), { duration: time, remaining: time }));
430
+ actions: {
431
+ setRemainingDuration(ctx) {
432
+ ctx.remaining -= Date.now() - ctx.createdAt;
433
+ },
434
+ setCreatedAt(ctx) {
435
+ ctx.createdAt = Date.now();
436
+ },
437
+ notifyParentToRemove(_ctx, _evt, { self }) {
438
+ self.sendParent({ type: "REMOVE_TOAST", id: self.id });
439
+ },
440
+ invokeOnClosing(ctx) {
441
+ var _a;
442
+ (_a = ctx.onClosing) == null ? void 0 : _a.call(ctx);
443
+ },
444
+ invokeOnClose(ctx) {
445
+ var _a;
446
+ (_a = ctx.onClose) == null ? void 0 : _a.call(ctx);
447
+ },
448
+ invokeOnOpen(ctx) {
449
+ var _a;
450
+ (_a = ctx.onOpen) == null ? void 0 : _a.call(ctx);
451
+ },
452
+ invokeOnUpdate(ctx) {
453
+ var _a;
454
+ (_a = ctx.onUpdate) == null ? void 0 : _a.call(ctx);
455
+ },
456
+ setContext(ctx, evt) {
457
+ const { duration: duration2, type: type2 } = evt.toast;
458
+ const time = getToastDuration(duration2, type2);
459
+ Object.assign(ctx, { ...evt.toast, duration: time, remaining: time });
460
+ }
464
461
  }
465
462
  }
466
- });
463
+ );
467
464
  }
468
465
 
469
466
  // src/toast-group.machine.ts
470
- function groupMachine(ctx = {}) {
467
+ function groupMachine(ctx) {
471
468
  return (0, import_core3.createMachine)({
472
469
  id: "toaster",
473
470
  initial: "active",
474
- context: __spreadValues({
471
+ context: {
475
472
  dir: "ltr",
476
473
  max: Number.MAX_SAFE_INTEGER,
477
- uid: "",
478
474
  toasts: [],
479
475
  gutter: "1rem",
480
476
  zIndex: MAX_Z_INDEX,
481
477
  pauseOnPageIdle: false,
482
478
  pauseOnInteraction: true,
483
- offsets: { left: "0px", right: "0px", top: "0px", bottom: "0px" }
484
- }, ctx),
479
+ offsets: { left: "0px", right: "0px", top: "0px", bottom: "0px" },
480
+ ...ctx
481
+ },
485
482
  computed: {
486
483
  count: (ctx2) => ctx2.toasts.length
487
484
  },
488
485
  on: {
489
- SETUP: {
490
- actions: (ctx2, evt) => {
491
- ctx2.uid = evt.id;
492
- if (evt.doc)
493
- ctx2.doc = (0, import_core3.ref)(evt.doc);
494
- if (evt.root)
495
- ctx2.rootNode = (0, import_core3.ref)(evt.root);
496
- }
497
- },
486
+ SETUP: {},
498
487
  PAUSE_TOAST: {
499
488
  actions: (_ctx, evt, { self }) => {
500
489
  self.sendChild("PAUSE", evt.id);
@@ -518,13 +507,13 @@ function groupMachine(ctx = {}) {
518
507
  ADD_TOAST: {
519
508
  guard: (ctx2) => ctx2.toasts.length < ctx2.max,
520
509
  actions: (ctx2, evt, { self }) => {
521
- var _a;
522
- const options = __spreadProps(__spreadValues({}, evt.toast), {
510
+ const options = {
511
+ ...evt.toast,
523
512
  pauseOnPageIdle: ctx2.pauseOnPageIdle,
524
513
  pauseOnInteraction: ctx2.pauseOnInteraction,
525
514
  dir: ctx2.dir,
526
- doc: (0, import_core3.ref)((_a = ctx2.doc) != null ? _a : document)
527
- });
515
+ getRootNode: ctx2.getRootNode
516
+ };
528
517
  const toast = createToastMachine(options);
529
518
  const actor = self.spawn(toast);
530
519
  ctx2.toasts.push(actor);
@@ -575,6 +564,7 @@ function connect(state, send, normalize) {
575
564
  return {
576
565
  type: state.context.type,
577
566
  title: state.context.title,
567
+ description: state.context.description,
578
568
  placement,
579
569
  isVisible,
580
570
  isPaused,
@@ -647,6 +637,10 @@ function connect(state, send, normalize) {
647
637
  "data-part": "title",
648
638
  id: dom.getTitleId(state.context)
649
639
  }),
640
+ descriptionProps: normalize.element({
641
+ "data-part": "description",
642
+ id: dom.getDescriptionId(state.context)
643
+ }),
650
644
  closeButtonProps: normalize.button({
651
645
  id: dom.getCloseButtonId(state.context),
652
646
  "data-part": "close-button",
@@ -685,3 +679,10 @@ function api() {
685
679
  return toaster;
686
680
  }
687
681
  }
682
+ // Annotate the CommonJS export names for ESM import in node:
683
+ 0 && (module.exports = {
684
+ api,
685
+ connect,
686
+ createMachine,
687
+ group
688
+ });