@zag-js/toast 0.1.16 → 0.2.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.
package/dist/index.d.ts CHANGED
@@ -170,7 +170,7 @@ declare function groupConnect<T extends PropTypes>(state: GroupState, send: Grou
170
170
  subscribe(fn: (toasts: GroupMachineContext["toasts"]) => void): () => void;
171
171
  };
172
172
 
173
- declare function groupMachine(ctx: UserDefinedGroupContext): _zag_js_core.Machine<GroupMachineContext, _zag_js_core.StateMachine.StateSchema, _zag_js_core.StateMachine.AnyEventObject>;
173
+ declare function groupMachine(userContext: UserDefinedGroupContext): _zag_js_core.Machine<GroupMachineContext, _zag_js_core.StateMachine.StateSchema, _zag_js_core.StateMachine.AnyEventObject>;
174
174
 
175
175
  declare function createToastMachine(options?: Options): _zag_js_core.Machine<MachineContext, MachineState, _zag_js_core.StateMachine.AnyEventObject>;
176
176
 
package/dist/index.js CHANGED
@@ -34,7 +34,7 @@ var dataAttr = (guard) => {
34
34
  var MAX_Z_INDEX = 2147483647;
35
35
  var runIfFn = (v, ...a) => {
36
36
  const res = typeof v === "function" ? v(...a) : v;
37
- return res ?? void 0;
37
+ return res != null ? res : void 0;
38
38
  };
39
39
  var cast = (v) => v;
40
40
  var hasProp = (obj, prop) => Object.prototype.hasOwnProperty.call(obj, prop);
@@ -46,22 +46,44 @@ function isWindow(value) {
46
46
  return (value == null ? void 0 : value.toString()) === "[object Window]";
47
47
  }
48
48
  function getDocument(el) {
49
+ var _a;
49
50
  if (isWindow(el))
50
51
  return el.document;
51
52
  if (isDocument(el))
52
53
  return el;
53
- return (el == null ? void 0 : el.ownerDocument) ?? document;
54
+ return (_a = el == null ? void 0 : el.ownerDocument) != null ? _a : document;
54
55
  }
55
56
  function defineDomHelpers(helpers) {
56
57
  const dom2 = {
57
58
  getRootNode: (ctx) => {
58
- var _a;
59
- return ((_a = ctx.getRootNode) == null ? void 0 : _a.call(ctx)) ?? document;
59
+ var _a, _b;
60
+ return (_b = (_a = ctx.getRootNode) == null ? void 0 : _a.call(ctx)) != null ? _b : document;
60
61
  },
61
62
  getDoc: (ctx) => getDocument(dom2.getRootNode(ctx)),
62
- getWin: (ctx) => dom2.getDoc(ctx).defaultView ?? window,
63
+ getWin: (ctx) => {
64
+ var _a;
65
+ return (_a = dom2.getDoc(ctx).defaultView) != null ? _a : window;
66
+ },
63
67
  getActiveElement: (ctx) => dom2.getDoc(ctx).activeElement,
64
- getById: (ctx, id) => dom2.getRootNode(ctx).getElementById(id)
68
+ getById: (ctx, id) => dom2.getRootNode(ctx).getElementById(id),
69
+ createEmitter: (ctx, target) => {
70
+ const win = dom2.getWin(ctx);
71
+ return function emit(evt, detail, options) {
72
+ const { bubbles = true, cancelable, composed = true } = options != null ? options : {};
73
+ const eventName = `zag:${evt}`;
74
+ const init = { bubbles, cancelable, composed, detail };
75
+ const event = new win.CustomEvent(eventName, init);
76
+ target.dispatchEvent(event);
77
+ };
78
+ },
79
+ createListener: (target) => {
80
+ return function listen(evt, handler) {
81
+ const eventName = `zag:${evt}`;
82
+ const listener = (e) => handler(e);
83
+ target.addEventListener(eventName, listener);
84
+ return () => target.removeEventListener(eventName, listener);
85
+ };
86
+ }
65
87
  };
66
88
  return {
67
89
  ...dom2,
@@ -87,7 +109,7 @@ function trackDocumentVisibility(_doc, callback) {
87
109
  // ../../utilities/core/dist/index.mjs
88
110
  var runIfFn2 = (v, ...a) => {
89
111
  const res = typeof v === "function" ? v(...a) : v;
90
- return res ?? void 0;
112
+ return res != null ? res : void 0;
91
113
  };
92
114
  var uuid = /* @__PURE__ */ (() => {
93
115
  let id = 0;
@@ -96,6 +118,15 @@ var uuid = /* @__PURE__ */ (() => {
96
118
  return id.toString(36);
97
119
  };
98
120
  })();
121
+ var isArray = (v) => Array.isArray(v);
122
+ var isObject = (v) => !(v == null || typeof v !== "object" || isArray(v));
123
+ function compact(obj) {
124
+ if (obj === void 0)
125
+ return obj;
126
+ return Object.fromEntries(
127
+ Object.entries(obj).filter(([, value]) => value !== void 0).map(([key, value]) => [key, isObject(value) ? compact(value) : value])
128
+ );
129
+ }
99
130
  function warn(...a) {
100
131
  const m = a.length === 1 ? a[0] : a[1];
101
132
  const c = a.length === 2 ? a[0] : true;
@@ -144,7 +175,7 @@ var defaultTimeouts = {
144
175
  custom: 5e3
145
176
  };
146
177
  function getToastDuration(duration, type) {
147
- return duration ?? defaultTimeouts[type];
178
+ return duration != null ? duration : defaultTimeouts[type];
148
179
  }
149
180
  function getGroupPlacementStyle(ctx, placement) {
150
181
  const offset = ctx.offsets;
@@ -316,8 +347,9 @@ var import_core3 = require("@zag-js/core");
316
347
  var import_core2 = require("@zag-js/core");
317
348
  var { not, and, or } = import_core2.guards;
318
349
  function createToastMachine(options = {}) {
319
- const { type = "info", duration, id = "toast", placement = "bottom", removeDelay = 500, ...rest } = options;
320
- const __duration = getToastDuration(duration, type);
350
+ const { type = "info", duration, id = "toast", placement = "bottom", removeDelay = 500, ...restProps } = options;
351
+ const ctx = compact(restProps);
352
+ const computedDuration = getToastDuration(duration, type);
321
353
  return (0, import_core2.createMachine)(
322
354
  {
323
355
  id,
@@ -326,12 +358,12 @@ function createToastMachine(options = {}) {
326
358
  context: {
327
359
  id,
328
360
  type,
329
- remaining: __duration,
330
- duration: __duration,
361
+ remaining: computedDuration,
362
+ duration: computedDuration,
331
363
  removeDelay,
332
364
  createdAt: Date.now(),
333
365
  placement,
334
- ...rest
366
+ ...ctx
335
367
  },
336
368
  on: {
337
369
  UPDATE: [
@@ -400,10 +432,10 @@ function createToastMachine(options = {}) {
400
432
  },
401
433
  {
402
434
  activities: {
403
- trackDocumentVisibility(ctx, _evt, { send }) {
404
- if (!ctx.pauseOnPageIdle)
435
+ trackDocumentVisibility(ctx2, _evt, { send }) {
436
+ if (!ctx2.pauseOnPageIdle)
405
437
  return;
406
- return trackDocumentVisibility(dom.getDoc(ctx), function(hidden) {
438
+ return trackDocumentVisibility(dom.getDoc(ctx2), function(hidden) {
407
439
  send(hidden ? "PAUSE" : "RESUME");
408
440
  });
409
441
  }
@@ -413,50 +445,50 @@ function createToastMachine(options = {}) {
413
445
  var _a;
414
446
  return ((_a = evt.toast) == null ? void 0 : _a.type) === "loading";
415
447
  },
416
- isLoadingType: (ctx) => ctx.type === "loading",
417
- hasTypeChanged: (ctx, evt) => {
448
+ isLoadingType: (ctx2) => ctx2.type === "loading",
449
+ hasTypeChanged: (ctx2, evt) => {
418
450
  var _a;
419
- return ((_a = evt.toast) == null ? void 0 : _a.type) !== ctx.type;
451
+ return ((_a = evt.toast) == null ? void 0 : _a.type) !== ctx2.type;
420
452
  },
421
- hasDurationChanged: (ctx, evt) => {
453
+ hasDurationChanged: (ctx2, evt) => {
422
454
  var _a;
423
- return ((_a = evt.toast) == null ? void 0 : _a.duration) !== ctx.duration;
455
+ return ((_a = evt.toast) == null ? void 0 : _a.duration) !== ctx2.duration;
424
456
  }
425
457
  },
426
458
  delays: {
427
- VISIBLE_DURATION: (ctx) => ctx.remaining,
428
- REMOVE_DELAY: (ctx) => ctx.removeDelay
459
+ VISIBLE_DURATION: (ctx2) => ctx2.remaining,
460
+ REMOVE_DELAY: (ctx2) => ctx2.removeDelay
429
461
  },
430
462
  actions: {
431
- setRemainingDuration(ctx) {
432
- ctx.remaining -= Date.now() - ctx.createdAt;
463
+ setRemainingDuration(ctx2) {
464
+ ctx2.remaining -= Date.now() - ctx2.createdAt;
433
465
  },
434
- setCreatedAt(ctx) {
435
- ctx.createdAt = Date.now();
466
+ setCreatedAt(ctx2) {
467
+ ctx2.createdAt = Date.now();
436
468
  },
437
469
  notifyParentToRemove(_ctx, _evt, { self }) {
438
470
  self.sendParent({ type: "REMOVE_TOAST", id: self.id });
439
471
  },
440
- invokeOnClosing(ctx) {
472
+ invokeOnClosing(ctx2) {
441
473
  var _a;
442
- (_a = ctx.onClosing) == null ? void 0 : _a.call(ctx);
474
+ (_a = ctx2.onClosing) == null ? void 0 : _a.call(ctx2);
443
475
  },
444
- invokeOnClose(ctx) {
476
+ invokeOnClose(ctx2) {
445
477
  var _a;
446
- (_a = ctx.onClose) == null ? void 0 : _a.call(ctx);
478
+ (_a = ctx2.onClose) == null ? void 0 : _a.call(ctx2);
447
479
  },
448
- invokeOnOpen(ctx) {
480
+ invokeOnOpen(ctx2) {
449
481
  var _a;
450
- (_a = ctx.onOpen) == null ? void 0 : _a.call(ctx);
482
+ (_a = ctx2.onOpen) == null ? void 0 : _a.call(ctx2);
451
483
  },
452
- invokeOnUpdate(ctx) {
484
+ invokeOnUpdate(ctx2) {
453
485
  var _a;
454
- (_a = ctx.onUpdate) == null ? void 0 : _a.call(ctx);
486
+ (_a = ctx2.onUpdate) == null ? void 0 : _a.call(ctx2);
455
487
  },
456
- setContext(ctx, evt) {
488
+ setContext(ctx2, evt) {
457
489
  const { duration: duration2, type: type2 } = evt.toast;
458
490
  const time = getToastDuration(duration2, type2);
459
- Object.assign(ctx, { ...evt.toast, duration: time, remaining: time });
491
+ Object.assign(ctx2, { ...evt.toast, duration: time, remaining: time });
460
492
  }
461
493
  }
462
494
  }
@@ -464,7 +496,8 @@ function createToastMachine(options = {}) {
464
496
  }
465
497
 
466
498
  // src/toast-group.machine.ts
467
- function groupMachine(ctx) {
499
+ function groupMachine(userContext) {
500
+ const ctx = compact(userContext);
468
501
  return (0, import_core3.createMachine)({
469
502
  id: "toaster",
470
503
  initial: "active",
package/dist/index.mjs CHANGED
@@ -5,7 +5,7 @@ var dataAttr = (guard) => {
5
5
  var MAX_Z_INDEX = 2147483647;
6
6
  var runIfFn = (v, ...a) => {
7
7
  const res = typeof v === "function" ? v(...a) : v;
8
- return res ?? void 0;
8
+ return res != null ? res : void 0;
9
9
  };
10
10
  var cast = (v) => v;
11
11
  var hasProp = (obj, prop) => Object.prototype.hasOwnProperty.call(obj, prop);
@@ -17,22 +17,44 @@ function isWindow(value) {
17
17
  return (value == null ? void 0 : value.toString()) === "[object Window]";
18
18
  }
19
19
  function getDocument(el) {
20
+ var _a;
20
21
  if (isWindow(el))
21
22
  return el.document;
22
23
  if (isDocument(el))
23
24
  return el;
24
- return (el == null ? void 0 : el.ownerDocument) ?? document;
25
+ return (_a = el == null ? void 0 : el.ownerDocument) != null ? _a : document;
25
26
  }
26
27
  function defineDomHelpers(helpers) {
27
28
  const dom2 = {
28
29
  getRootNode: (ctx) => {
29
- var _a;
30
- return ((_a = ctx.getRootNode) == null ? void 0 : _a.call(ctx)) ?? document;
30
+ var _a, _b;
31
+ return (_b = (_a = ctx.getRootNode) == null ? void 0 : _a.call(ctx)) != null ? _b : document;
31
32
  },
32
33
  getDoc: (ctx) => getDocument(dom2.getRootNode(ctx)),
33
- getWin: (ctx) => dom2.getDoc(ctx).defaultView ?? window,
34
+ getWin: (ctx) => {
35
+ var _a;
36
+ return (_a = dom2.getDoc(ctx).defaultView) != null ? _a : window;
37
+ },
34
38
  getActiveElement: (ctx) => dom2.getDoc(ctx).activeElement,
35
- getById: (ctx, id) => dom2.getRootNode(ctx).getElementById(id)
39
+ getById: (ctx, id) => dom2.getRootNode(ctx).getElementById(id),
40
+ createEmitter: (ctx, target) => {
41
+ const win = dom2.getWin(ctx);
42
+ return function emit(evt, detail, options) {
43
+ const { bubbles = true, cancelable, composed = true } = options != null ? options : {};
44
+ const eventName = `zag:${evt}`;
45
+ const init = { bubbles, cancelable, composed, detail };
46
+ const event = new win.CustomEvent(eventName, init);
47
+ target.dispatchEvent(event);
48
+ };
49
+ },
50
+ createListener: (target) => {
51
+ return function listen(evt, handler) {
52
+ const eventName = `zag:${evt}`;
53
+ const listener = (e) => handler(e);
54
+ target.addEventListener(eventName, listener);
55
+ return () => target.removeEventListener(eventName, listener);
56
+ };
57
+ }
36
58
  };
37
59
  return {
38
60
  ...dom2,
@@ -58,7 +80,7 @@ function trackDocumentVisibility(_doc, callback) {
58
80
  // ../../utilities/core/dist/index.mjs
59
81
  var runIfFn2 = (v, ...a) => {
60
82
  const res = typeof v === "function" ? v(...a) : v;
61
- return res ?? void 0;
83
+ return res != null ? res : void 0;
62
84
  };
63
85
  var uuid = /* @__PURE__ */ (() => {
64
86
  let id = 0;
@@ -67,6 +89,15 @@ var uuid = /* @__PURE__ */ (() => {
67
89
  return id.toString(36);
68
90
  };
69
91
  })();
92
+ var isArray = (v) => Array.isArray(v);
93
+ var isObject = (v) => !(v == null || typeof v !== "object" || isArray(v));
94
+ function compact(obj) {
95
+ if (obj === void 0)
96
+ return obj;
97
+ return Object.fromEntries(
98
+ Object.entries(obj).filter(([, value]) => value !== void 0).map(([key, value]) => [key, isObject(value) ? compact(value) : value])
99
+ );
100
+ }
70
101
  function warn(...a) {
71
102
  const m = a.length === 1 ? a[0] : a[1];
72
103
  const c = a.length === 2 ? a[0] : true;
@@ -115,7 +146,7 @@ var defaultTimeouts = {
115
146
  custom: 5e3
116
147
  };
117
148
  function getToastDuration(duration, type) {
118
- return duration ?? defaultTimeouts[type];
149
+ return duration != null ? duration : defaultTimeouts[type];
119
150
  }
120
151
  function getGroupPlacementStyle(ctx, placement) {
121
152
  const offset = ctx.offsets;
@@ -287,8 +318,9 @@ import { createMachine as createMachine2 } from "@zag-js/core";
287
318
  import { createMachine, guards } from "@zag-js/core";
288
319
  var { not, and, or } = guards;
289
320
  function createToastMachine(options = {}) {
290
- const { type = "info", duration, id = "toast", placement = "bottom", removeDelay = 500, ...rest } = options;
291
- const __duration = getToastDuration(duration, type);
321
+ const { type = "info", duration, id = "toast", placement = "bottom", removeDelay = 500, ...restProps } = options;
322
+ const ctx = compact(restProps);
323
+ const computedDuration = getToastDuration(duration, type);
292
324
  return createMachine(
293
325
  {
294
326
  id,
@@ -297,12 +329,12 @@ function createToastMachine(options = {}) {
297
329
  context: {
298
330
  id,
299
331
  type,
300
- remaining: __duration,
301
- duration: __duration,
332
+ remaining: computedDuration,
333
+ duration: computedDuration,
302
334
  removeDelay,
303
335
  createdAt: Date.now(),
304
336
  placement,
305
- ...rest
337
+ ...ctx
306
338
  },
307
339
  on: {
308
340
  UPDATE: [
@@ -371,10 +403,10 @@ function createToastMachine(options = {}) {
371
403
  },
372
404
  {
373
405
  activities: {
374
- trackDocumentVisibility(ctx, _evt, { send }) {
375
- if (!ctx.pauseOnPageIdle)
406
+ trackDocumentVisibility(ctx2, _evt, { send }) {
407
+ if (!ctx2.pauseOnPageIdle)
376
408
  return;
377
- return trackDocumentVisibility(dom.getDoc(ctx), function(hidden) {
409
+ return trackDocumentVisibility(dom.getDoc(ctx2), function(hidden) {
378
410
  send(hidden ? "PAUSE" : "RESUME");
379
411
  });
380
412
  }
@@ -384,50 +416,50 @@ function createToastMachine(options = {}) {
384
416
  var _a;
385
417
  return ((_a = evt.toast) == null ? void 0 : _a.type) === "loading";
386
418
  },
387
- isLoadingType: (ctx) => ctx.type === "loading",
388
- hasTypeChanged: (ctx, evt) => {
419
+ isLoadingType: (ctx2) => ctx2.type === "loading",
420
+ hasTypeChanged: (ctx2, evt) => {
389
421
  var _a;
390
- return ((_a = evt.toast) == null ? void 0 : _a.type) !== ctx.type;
422
+ return ((_a = evt.toast) == null ? void 0 : _a.type) !== ctx2.type;
391
423
  },
392
- hasDurationChanged: (ctx, evt) => {
424
+ hasDurationChanged: (ctx2, evt) => {
393
425
  var _a;
394
- return ((_a = evt.toast) == null ? void 0 : _a.duration) !== ctx.duration;
426
+ return ((_a = evt.toast) == null ? void 0 : _a.duration) !== ctx2.duration;
395
427
  }
396
428
  },
397
429
  delays: {
398
- VISIBLE_DURATION: (ctx) => ctx.remaining,
399
- REMOVE_DELAY: (ctx) => ctx.removeDelay
430
+ VISIBLE_DURATION: (ctx2) => ctx2.remaining,
431
+ REMOVE_DELAY: (ctx2) => ctx2.removeDelay
400
432
  },
401
433
  actions: {
402
- setRemainingDuration(ctx) {
403
- ctx.remaining -= Date.now() - ctx.createdAt;
434
+ setRemainingDuration(ctx2) {
435
+ ctx2.remaining -= Date.now() - ctx2.createdAt;
404
436
  },
405
- setCreatedAt(ctx) {
406
- ctx.createdAt = Date.now();
437
+ setCreatedAt(ctx2) {
438
+ ctx2.createdAt = Date.now();
407
439
  },
408
440
  notifyParentToRemove(_ctx, _evt, { self }) {
409
441
  self.sendParent({ type: "REMOVE_TOAST", id: self.id });
410
442
  },
411
- invokeOnClosing(ctx) {
443
+ invokeOnClosing(ctx2) {
412
444
  var _a;
413
- (_a = ctx.onClosing) == null ? void 0 : _a.call(ctx);
445
+ (_a = ctx2.onClosing) == null ? void 0 : _a.call(ctx2);
414
446
  },
415
- invokeOnClose(ctx) {
447
+ invokeOnClose(ctx2) {
416
448
  var _a;
417
- (_a = ctx.onClose) == null ? void 0 : _a.call(ctx);
449
+ (_a = ctx2.onClose) == null ? void 0 : _a.call(ctx2);
418
450
  },
419
- invokeOnOpen(ctx) {
451
+ invokeOnOpen(ctx2) {
420
452
  var _a;
421
- (_a = ctx.onOpen) == null ? void 0 : _a.call(ctx);
453
+ (_a = ctx2.onOpen) == null ? void 0 : _a.call(ctx2);
422
454
  },
423
- invokeOnUpdate(ctx) {
455
+ invokeOnUpdate(ctx2) {
424
456
  var _a;
425
- (_a = ctx.onUpdate) == null ? void 0 : _a.call(ctx);
457
+ (_a = ctx2.onUpdate) == null ? void 0 : _a.call(ctx2);
426
458
  },
427
- setContext(ctx, evt) {
459
+ setContext(ctx2, evt) {
428
460
  const { duration: duration2, type: type2 } = evt.toast;
429
461
  const time = getToastDuration(duration2, type2);
430
- Object.assign(ctx, { ...evt.toast, duration: time, remaining: time });
462
+ Object.assign(ctx2, { ...evt.toast, duration: time, remaining: time });
431
463
  }
432
464
  }
433
465
  }
@@ -435,7 +467,8 @@ function createToastMachine(options = {}) {
435
467
  }
436
468
 
437
469
  // src/toast-group.machine.ts
438
- function groupMachine(ctx) {
470
+ function groupMachine(userContext) {
471
+ const ctx = compact(userContext);
439
472
  return createMachine2({
440
473
  id: "toaster",
441
474
  initial: "active",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zag-js/toast",
3
- "version": "0.1.16",
3
+ "version": "0.2.1",
4
4
  "description": "Core logic for the toast widget implemented as a state machine",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
@@ -29,12 +29,12 @@
29
29
  "url": "https://github.com/chakra-ui/zag/issues"
30
30
  },
31
31
  "dependencies": {
32
- "@zag-js/core": "0.1.12",
33
- "@zag-js/types": "0.2.7"
32
+ "@zag-js/core": "0.2.1",
33
+ "@zag-js/types": "0.3.0"
34
34
  },
35
35
  "devDependencies": {
36
- "@zag-js/dom-utils": "0.1.13",
37
- "@zag-js/utils": "0.1.6"
36
+ "@zag-js/dom-utils": "0.2.0",
37
+ "@zag-js/utils": "0.3.0"
38
38
  },
39
39
  "scripts": {
40
40
  "build-fast": "tsup src/index.ts --format=esm,cjs",