@zag-js/toast 0.10.5 → 0.11.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.
@@ -1,145 +0,0 @@
1
- import { createMachine, guards } from '@zag-js/core';
2
- import { addDomEvent } from '@zag-js/dom-event';
3
- import { compact } from '@zag-js/utils';
4
- import { dom } from './toast.dom.mjs';
5
- import { getToastDuration } from './toast.utils.mjs';
6
-
7
- const { not, and, or } = guards;
8
- function createToastMachine(options = {}) {
9
- const { type = "info", duration, id = "toast", placement = "bottom", removeDelay = 0, ...restProps } = options;
10
- const ctx = compact(restProps);
11
- const computedDuration = getToastDuration(duration, type);
12
- return createMachine(
13
- {
14
- id,
15
- entry: "invokeOnOpen",
16
- initial: type === "loading" ? "persist" : "active",
17
- context: {
18
- id,
19
- type,
20
- remaining: computedDuration,
21
- duration: computedDuration,
22
- removeDelay,
23
- createdAt: Date.now(),
24
- placement,
25
- ...ctx
26
- },
27
- on: {
28
- UPDATE: [
29
- {
30
- guard: and("hasTypeChanged", "isChangingToLoading"),
31
- target: "persist",
32
- actions: ["setContext", "invokeOnUpdate"]
33
- },
34
- {
35
- guard: or("hasDurationChanged", "hasTypeChanged"),
36
- target: "active:temp",
37
- actions: ["setContext", "invokeOnUpdate"]
38
- },
39
- {
40
- actions: ["setContext", "invokeOnUpdate"]
41
- }
42
- ]
43
- },
44
- states: {
45
- "active:temp": {
46
- tags: ["visible", "updating"],
47
- after: {
48
- 0: "active"
49
- }
50
- },
51
- persist: {
52
- tags: ["visible", "paused"],
53
- activities: "trackDocumentVisibility",
54
- on: {
55
- RESUME: {
56
- guard: not("isLoadingType"),
57
- target: "active",
58
- actions: ["setCreatedAt"]
59
- },
60
- DISMISS: "dismissing"
61
- }
62
- },
63
- active: {
64
- tags: ["visible"],
65
- activities: "trackDocumentVisibility",
66
- after: {
67
- VISIBLE_DURATION: "dismissing"
68
- },
69
- on: {
70
- DISMISS: "dismissing",
71
- PAUSE: {
72
- target: "persist",
73
- actions: "setRemainingDuration"
74
- }
75
- }
76
- },
77
- dismissing: {
78
- entry: "invokeOnClosing",
79
- after: {
80
- REMOVE_DELAY: {
81
- target: "inactive",
82
- actions: "notifyParentToRemove"
83
- }
84
- }
85
- },
86
- inactive: {
87
- entry: "invokeOnClose",
88
- type: "final"
89
- }
90
- }
91
- },
92
- {
93
- activities: {
94
- trackDocumentVisibility(ctx2, _evt, { send }) {
95
- if (!ctx2.pauseOnPageIdle)
96
- return;
97
- const doc = dom.getDoc(ctx2);
98
- return addDomEvent(doc, "visibilitychange", () => {
99
- send(doc.visibilityState === "hidden" ? "PAUSE" : "RESUME");
100
- });
101
- }
102
- },
103
- guards: {
104
- isChangingToLoading: (_, evt) => evt.toast?.type === "loading",
105
- isLoadingType: (ctx2) => ctx2.type === "loading",
106
- hasTypeChanged: (ctx2, evt) => evt.toast?.type !== ctx2.type,
107
- hasDurationChanged: (ctx2, evt) => evt.toast?.duration !== ctx2.duration
108
- },
109
- delays: {
110
- VISIBLE_DURATION: (ctx2) => ctx2.remaining,
111
- REMOVE_DELAY: (ctx2) => ctx2.removeDelay
112
- },
113
- actions: {
114
- setRemainingDuration(ctx2) {
115
- ctx2.remaining -= Date.now() - ctx2.createdAt;
116
- },
117
- setCreatedAt(ctx2) {
118
- ctx2.createdAt = Date.now();
119
- },
120
- notifyParentToRemove(_ctx, _evt, { self }) {
121
- self.sendParent({ type: "REMOVE_TOAST", id: self.id });
122
- },
123
- invokeOnClosing(ctx2) {
124
- ctx2.onClosing?.();
125
- },
126
- invokeOnClose(ctx2) {
127
- ctx2.onClose?.();
128
- },
129
- invokeOnOpen(ctx2) {
130
- ctx2.onOpen?.();
131
- },
132
- invokeOnUpdate(ctx2) {
133
- ctx2.onUpdate?.();
134
- },
135
- setContext(ctx2, evt) {
136
- const { duration: duration2, type: type2 } = evt.toast;
137
- const time = getToastDuration(duration2, type2);
138
- Object.assign(ctx2, { ...evt.toast, duration: time, remaining: time });
139
- }
140
- }
141
- }
142
- );
143
- }
144
-
145
- export { createToastMachine };
@@ -1,148 +0,0 @@
1
- import type { Machine, StateMachine as S } from "@zag-js/core";
2
- import type { CommonProperties, Context, Direction, DirectionProperty, RequiredBy, RootProperties } from "@zag-js/types";
3
- export type Type = "success" | "error" | "loading" | "info" | "custom";
4
- export type Placement = "top-start" | "top" | "top-end" | "bottom-start" | "bottom" | "bottom-end";
5
- type SharedContext = {
6
- /**
7
- * Whether to pause toast when the user leaves the browser tab
8
- */
9
- pauseOnPageIdle?: boolean;
10
- /**
11
- * Whether to pause the toast when interacted with
12
- */
13
- pauseOnInteraction?: boolean;
14
- };
15
- export type ToastOptions = {
16
- /**
17
- * The unique id of the toast
18
- */
19
- id: string;
20
- /**
21
- * The type of the toast
22
- */
23
- type: Type;
24
- /**
25
- * The placement of the toast
26
- */
27
- placement: Placement;
28
- /**
29
- * The message of the toast
30
- */
31
- title?: string;
32
- /**
33
- * The description of the toast
34
- */
35
- description?: string;
36
- /**
37
- * The duration the toast will be visible
38
- */
39
- duration: number;
40
- /**
41
- * Custom function to render the toast element.
42
- */
43
- render?: (options: RenderOptions) => any;
44
- /**
45
- * The duration for the toast to kept alive before it is removed.
46
- * Useful for exit transitions.
47
- */
48
- removeDelay?: number;
49
- /**
50
- * Function called when the toast has been closed and removed
51
- */
52
- onClose?: VoidFunction;
53
- /**
54
- * Function called when the toast is leaving
55
- */
56
- onClosing?: VoidFunction;
57
- /**
58
- * Function called when the toast is shown
59
- */
60
- onOpen?: VoidFunction;
61
- /**
62
- * Function called when the toast is updated
63
- */
64
- onUpdate?: VoidFunction;
65
- };
66
- export type Options = Partial<ToastOptions>;
67
- export type RenderOptions = Omit<ToastOptions, "render"> & {
68
- dismiss(): void;
69
- };
70
- export type MachineContext = SharedContext & RootProperties & CommonProperties & Omit<ToastOptions, "removeDelay"> & {
71
- /**
72
- * The duration for the toast to kept alive before it is removed.
73
- * Useful for exit transitions.
74
- */
75
- removeDelay: number;
76
- /**
77
- * The document's text/writing direction.
78
- */
79
- dir?: Direction;
80
- /**
81
- * The time the toast was created
82
- */
83
- createdAt: number;
84
- /**
85
- * The time left before the toast is removed
86
- */
87
- remaining: number;
88
- };
89
- export type MachineState = {
90
- value: "active" | "active:temp" | "dismissing" | "inactive" | "persist";
91
- tags: "visible" | "paused" | "updating";
92
- };
93
- export type State = S.State<MachineContext, MachineState>;
94
- export type Send = S.Send;
95
- export type Service = Machine<MachineContext, MachineState>;
96
- type GroupPublicContext = SharedContext & DirectionProperty & CommonProperties & {
97
- /**
98
- * The gutter or spacing between toasts
99
- */
100
- gutter: string;
101
- /**
102
- * The z-index applied to each toast group
103
- */
104
- zIndex: number;
105
- /**
106
- * The maximum number of toasts that can be shown at once
107
- */
108
- max: number;
109
- /**
110
- * The offset from the safe environment edge of the viewport
111
- */
112
- offsets: string | Record<"left" | "right" | "bottom" | "top", string>;
113
- };
114
- export type UserDefinedGroupContext = RequiredBy<GroupPublicContext, "id">;
115
- type GroupComputedContext = Readonly<{
116
- /**
117
- * @computed
118
- * The total number of toasts in the group
119
- */
120
- readonly count: number;
121
- }>;
122
- type GroupPrivateContext = Context<{}>;
123
- export type GroupMachineContext = GroupPublicContext & GroupComputedContext & GroupPrivateContext;
124
- export type GroupState = S.State<GroupMachineContext>;
125
- export type GroupSend = (event: S.Event<S.AnyEventObject>) => void;
126
- type MaybeFunction<Value, Args> = Value | ((arg: Args) => Value);
127
- export type PromiseOptions<Value> = {
128
- loading: ToastOptions;
129
- success: MaybeFunction<ToastOptions, Value>;
130
- error: MaybeFunction<ToastOptions, Error>;
131
- };
132
- export type GroupProps = {
133
- placement: Placement;
134
- label?: string;
135
- };
136
- export type Toaster = {
137
- count: number;
138
- isVisible(id: string): boolean;
139
- upsert(options: ToastOptions): string | undefined;
140
- create(options: ToastOptions): string | undefined;
141
- success(options: ToastOptions): string | undefined;
142
- error(options: ToastOptions): string | undefined;
143
- loading(options: ToastOptions): string | undefined;
144
- dismiss(id?: string | undefined): void;
145
- remove(id?: string | undefined): void;
146
- promise<T>(promise: Promise<T>, options: PromiseOptions<T>, shared?: ToastOptions): Promise<T>;
147
- };
148
- export {};
@@ -1,6 +0,0 @@
1
- import type { Style } from "@zag-js/types";
2
- import type { GroupMachineContext, MachineContext, Placement, Service, Type } from "./toast.types";
3
- export declare function getToastsByPlacement(toasts: Service[]): Partial<Record<Placement, Service[]>>;
4
- export declare const defaultTimeouts: Record<Type, number>;
5
- export declare function getToastDuration(duration: number | undefined, type: MachineContext["type"]): number;
6
- export declare function getGroupPlacementStyle(ctx: GroupMachineContext, placement: Placement): Style;
@@ -1,67 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4
-
5
- function getToastsByPlacement(toasts) {
6
- const result = {};
7
- for (const toast of toasts) {
8
- const placement = toast.state.context.placement;
9
- result[placement] ||= [];
10
- result[placement].push(toast);
11
- }
12
- return result;
13
- }
14
- const defaultTimeouts = {
15
- info: 5e3,
16
- error: 5e3,
17
- success: 2e3,
18
- loading: Infinity,
19
- custom: 5e3
20
- };
21
- function getToastDuration(duration, type) {
22
- return duration ?? defaultTimeouts[type];
23
- }
24
- function getGroupPlacementStyle(ctx, placement) {
25
- const offset = ctx.offsets;
26
- const computedOffset = typeof offset === "string" ? { left: offset, right: offset, bottom: offset, top: offset } : offset;
27
- const rtl = ctx.dir === "rtl";
28
- const computedPlacement = placement.replace("-start", rtl ? "-right" : "-left").replace("-end", rtl ? "-left" : "-right");
29
- const isRighty = computedPlacement.includes("right");
30
- const isLefty = computedPlacement.includes("left");
31
- const styles = {
32
- position: "fixed",
33
- pointerEvents: ctx.count > 0 ? void 0 : "none",
34
- display: "flex",
35
- flexDirection: "column",
36
- "--toast-gutter": ctx.gutter,
37
- zIndex: ctx.zIndex
38
- };
39
- let alignItems = "center";
40
- if (isRighty)
41
- alignItems = "flex-end";
42
- if (isLefty)
43
- alignItems = "flex-start";
44
- styles.alignItems = alignItems;
45
- if (computedPlacement.includes("top")) {
46
- const offset2 = computedOffset.top;
47
- styles.top = `calc(env(safe-area-inset-top, 0px) + ${offset2})`;
48
- }
49
- if (computedPlacement.includes("bottom")) {
50
- const offset2 = computedOffset.bottom;
51
- styles.bottom = `calc(env(safe-area-inset-bottom, 0px) + ${offset2})`;
52
- }
53
- if (!computedPlacement.includes("left")) {
54
- const offset2 = computedOffset.right;
55
- styles.right = `calc(env(safe-area-inset-right, 0px) + ${offset2})`;
56
- }
57
- if (!computedPlacement.includes("right")) {
58
- const offset2 = computedOffset.left;
59
- styles.left = `calc(env(safe-area-inset-left, 0px) + ${offset2})`;
60
- }
61
- return styles;
62
- }
63
-
64
- exports.defaultTimeouts = defaultTimeouts;
65
- exports.getGroupPlacementStyle = getGroupPlacementStyle;
66
- exports.getToastDuration = getToastDuration;
67
- exports.getToastsByPlacement = getToastsByPlacement;
@@ -1,60 +0,0 @@
1
- function getToastsByPlacement(toasts) {
2
- const result = {};
3
- for (const toast of toasts) {
4
- const placement = toast.state.context.placement;
5
- result[placement] ||= [];
6
- result[placement].push(toast);
7
- }
8
- return result;
9
- }
10
- const defaultTimeouts = {
11
- info: 5e3,
12
- error: 5e3,
13
- success: 2e3,
14
- loading: Infinity,
15
- custom: 5e3
16
- };
17
- function getToastDuration(duration, type) {
18
- return duration ?? defaultTimeouts[type];
19
- }
20
- function getGroupPlacementStyle(ctx, placement) {
21
- const offset = ctx.offsets;
22
- const computedOffset = typeof offset === "string" ? { left: offset, right: offset, bottom: offset, top: offset } : offset;
23
- const rtl = ctx.dir === "rtl";
24
- const computedPlacement = placement.replace("-start", rtl ? "-right" : "-left").replace("-end", rtl ? "-left" : "-right");
25
- const isRighty = computedPlacement.includes("right");
26
- const isLefty = computedPlacement.includes("left");
27
- const styles = {
28
- position: "fixed",
29
- pointerEvents: ctx.count > 0 ? void 0 : "none",
30
- display: "flex",
31
- flexDirection: "column",
32
- "--toast-gutter": ctx.gutter,
33
- zIndex: ctx.zIndex
34
- };
35
- let alignItems = "center";
36
- if (isRighty)
37
- alignItems = "flex-end";
38
- if (isLefty)
39
- alignItems = "flex-start";
40
- styles.alignItems = alignItems;
41
- if (computedPlacement.includes("top")) {
42
- const offset2 = computedOffset.top;
43
- styles.top = `calc(env(safe-area-inset-top, 0px) + ${offset2})`;
44
- }
45
- if (computedPlacement.includes("bottom")) {
46
- const offset2 = computedOffset.bottom;
47
- styles.bottom = `calc(env(safe-area-inset-bottom, 0px) + ${offset2})`;
48
- }
49
- if (!computedPlacement.includes("left")) {
50
- const offset2 = computedOffset.right;
51
- styles.right = `calc(env(safe-area-inset-right, 0px) + ${offset2})`;
52
- }
53
- if (!computedPlacement.includes("right")) {
54
- const offset2 = computedOffset.left;
55
- styles.left = `calc(env(safe-area-inset-left, 0px) + ${offset2})`;
56
- }
57
- return styles;
58
- }
59
-
60
- export { defaultTimeouts, getGroupPlacementStyle, getToastDuration, getToastsByPlacement };