@zag-js/toast 0.45.0 → 0.46.0
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.mts +86 -72
- package/dist/index.d.ts +86 -72
- package/dist/index.js +565 -165
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +568 -168
- package/dist/index.mjs.map +1 -1
- package/package.json +8 -7
- package/src/index.ts +5 -3
- package/src/toast-group.connect.ts +62 -28
- package/src/toast-group.machine.ts +284 -67
- package/src/toast.anatomy.ts +10 -1
- package/src/toast.connect.ts +42 -32
- package/src/toast.dom.ts +5 -2
- package/src/toast.machine.ts +99 -41
- package/src/toast.types.ts +188 -108
- package/src/toast.utils.ts +134 -14
package/src/toast.utils.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
|
+
import { MAX_Z_INDEX } from "@zag-js/dom-query"
|
|
1
2
|
import type { Style } from "@zag-js/types"
|
|
2
|
-
import type { GroupMachineContext, MachineContext, Placement, Service,
|
|
3
|
+
import type { GroupMachineContext, MachineContext, Placement, Service, Type } from "./toast.types"
|
|
3
4
|
|
|
4
|
-
export function getToastsByPlacement<T
|
|
5
|
+
export function getToastsByPlacement<T>(toasts: Service<T>[]) {
|
|
5
6
|
const result: Partial<Record<Placement, Service<T>[]>> = {}
|
|
6
7
|
|
|
7
8
|
for (const toast of toasts) {
|
|
@@ -18,17 +19,14 @@ export const defaultTimeouts: Record<Type, number> = {
|
|
|
18
19
|
error: 5000,
|
|
19
20
|
success: 2000,
|
|
20
21
|
loading: Infinity,
|
|
21
|
-
|
|
22
|
+
DEFAULT: 5000,
|
|
22
23
|
}
|
|
23
24
|
|
|
24
25
|
export function getToastDuration(duration: number | undefined, type: MachineContext["type"]) {
|
|
25
|
-
return duration ?? defaultTimeouts[type]
|
|
26
|
+
return duration ?? defaultTimeouts[type] ?? defaultTimeouts.DEFAULT
|
|
26
27
|
}
|
|
27
28
|
|
|
28
|
-
export function getGroupPlacementStyle<T
|
|
29
|
-
ctx: GroupMachineContext<T>,
|
|
30
|
-
placement: Placement,
|
|
31
|
-
): Style {
|
|
29
|
+
export function getGroupPlacementStyle<T>(ctx: GroupMachineContext<T>, placement: Placement): Style {
|
|
32
30
|
const offset = ctx.offsets
|
|
33
31
|
const computedOffset =
|
|
34
32
|
typeof offset === "string" ? { left: offset, right: offset, bottom: offset, top: offset } : offset
|
|
@@ -46,8 +44,9 @@ export function getGroupPlacementStyle<T extends GenericOptions>(
|
|
|
46
44
|
pointerEvents: ctx.count > 0 ? undefined : "none",
|
|
47
45
|
display: "flex",
|
|
48
46
|
flexDirection: "column",
|
|
49
|
-
"--
|
|
50
|
-
|
|
47
|
+
"--gap": `${ctx.gap}px`,
|
|
48
|
+
"--first-height": `${ctx.heights[0]?.height || 0}px`,
|
|
49
|
+
zIndex: MAX_Z_INDEX,
|
|
51
50
|
}
|
|
52
51
|
|
|
53
52
|
let alignItems: Style["alignItems"] = "center"
|
|
@@ -58,23 +57,144 @@ export function getGroupPlacementStyle<T extends GenericOptions>(
|
|
|
58
57
|
|
|
59
58
|
if (computedPlacement.includes("top")) {
|
|
60
59
|
const offset = computedOffset.top
|
|
61
|
-
styles.top = `
|
|
60
|
+
styles.top = `max(env(safe-area-inset-top, 0px), ${offset})`
|
|
62
61
|
}
|
|
63
62
|
|
|
64
63
|
if (computedPlacement.includes("bottom")) {
|
|
65
64
|
const offset = computedOffset.bottom
|
|
66
|
-
styles.bottom = `
|
|
65
|
+
styles.bottom = `max(env(safe-area-inset-bottom, 0px), ${offset})`
|
|
67
66
|
}
|
|
68
67
|
|
|
69
68
|
if (!computedPlacement.includes("left")) {
|
|
70
69
|
const offset = computedOffset.right
|
|
71
|
-
styles.
|
|
70
|
+
styles.insetInlineEnd = `calc(env(safe-area-inset-right, 0px) + ${offset})`
|
|
72
71
|
}
|
|
73
72
|
|
|
74
73
|
if (!computedPlacement.includes("right")) {
|
|
75
74
|
const offset = computedOffset.left
|
|
76
|
-
styles.
|
|
75
|
+
styles.insetInlineStart = `calc(env(safe-area-inset-left, 0px) + ${offset})`
|
|
77
76
|
}
|
|
78
77
|
|
|
79
78
|
return styles
|
|
80
79
|
}
|
|
80
|
+
|
|
81
|
+
export function getPlacementStyle<T>(ctx: MachineContext<T>, visible: boolean): Style {
|
|
82
|
+
const [side] = ctx.placement!.split("-")
|
|
83
|
+
const sibling = !ctx.frontmost
|
|
84
|
+
const overlap = !ctx.stacked
|
|
85
|
+
|
|
86
|
+
const styles: Style = {
|
|
87
|
+
position: "absolute",
|
|
88
|
+
pointerEvents: "auto",
|
|
89
|
+
"--opacity": "0",
|
|
90
|
+
"--remove-delay": `${ctx.removeDelay}ms`,
|
|
91
|
+
"--duration": `${ctx.type === "loading" ? Number.MAX_SAFE_INTEGER : ctx.duration}ms`,
|
|
92
|
+
"--initial-height": `${ctx.height}px`,
|
|
93
|
+
"--offset": `${ctx.offset}px`,
|
|
94
|
+
"--index": ctx.index,
|
|
95
|
+
"--z-index": ctx.zIndex,
|
|
96
|
+
"--lift-amount": "calc(var(--lift) * var(--gap))",
|
|
97
|
+
"--y": "100%",
|
|
98
|
+
"--x": "0",
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
const assign = (overrides: Style) => Object.assign(styles, overrides)
|
|
102
|
+
|
|
103
|
+
if (side === "top") {
|
|
104
|
+
//
|
|
105
|
+
assign({
|
|
106
|
+
top: "0",
|
|
107
|
+
"--sign": "-1",
|
|
108
|
+
"--y": "-100%",
|
|
109
|
+
"--lift": "1",
|
|
110
|
+
})
|
|
111
|
+
//
|
|
112
|
+
} else if (side === "bottom") {
|
|
113
|
+
//
|
|
114
|
+
assign({
|
|
115
|
+
bottom: "0",
|
|
116
|
+
"--sign": "1",
|
|
117
|
+
"--y": "100%",
|
|
118
|
+
"--lift": "-1",
|
|
119
|
+
})
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
if (ctx.mounted) {
|
|
123
|
+
assign({
|
|
124
|
+
"--y": "0",
|
|
125
|
+
"--opacity": "1",
|
|
126
|
+
})
|
|
127
|
+
|
|
128
|
+
if (ctx.stacked) {
|
|
129
|
+
assign({
|
|
130
|
+
"--y": "calc(var(--lift) * var(--offset))",
|
|
131
|
+
"--height": "var(--initial-height)",
|
|
132
|
+
})
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
if (!visible) {
|
|
137
|
+
assign({
|
|
138
|
+
"--opacity": "0",
|
|
139
|
+
pointerEvents: "none",
|
|
140
|
+
})
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
if (sibling && overlap) {
|
|
144
|
+
assign({
|
|
145
|
+
"--base-scale": "var(--index) * 0.05 + 1",
|
|
146
|
+
"--y": "calc(var(--lift-amount) * var(--index))",
|
|
147
|
+
"--scale": "calc(-1 * var(--base-scale))",
|
|
148
|
+
"--height": "var(--first-height)",
|
|
149
|
+
})
|
|
150
|
+
|
|
151
|
+
if (!visible) {
|
|
152
|
+
assign({
|
|
153
|
+
"--y": "calc(var(--sign) * 40%)",
|
|
154
|
+
})
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
if (sibling && ctx.stacked && !visible) {
|
|
159
|
+
assign({
|
|
160
|
+
"--y": "calc(var(--lift) * var(--offset) + var(--lift) * -100%)",
|
|
161
|
+
})
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
if (ctx.frontmost && !visible) {
|
|
165
|
+
assign({
|
|
166
|
+
"--y": "calc(var(--lift) * -100%)",
|
|
167
|
+
})
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
return styles
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
export function getGhostBeforeStyle<T>(ctx: MachineContext<T>, visible: boolean): Style {
|
|
174
|
+
const styles: Style = {
|
|
175
|
+
position: "absolute",
|
|
176
|
+
inset: "0",
|
|
177
|
+
scale: "1 2",
|
|
178
|
+
pointerEvents: visible ? "none" : "auto",
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
const assign = (overrides: Style) => Object.assign(styles, overrides)
|
|
182
|
+
|
|
183
|
+
if (ctx.frontmost && !visible) {
|
|
184
|
+
assign({
|
|
185
|
+
height: "calc(var(--initial-height) + 80%)",
|
|
186
|
+
})
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
return styles
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
export function getGhostAfterStyle<T>(_ctx: MachineContext<T>, _visible: boolean): Style {
|
|
193
|
+
return {
|
|
194
|
+
position: "absolute",
|
|
195
|
+
left: "0",
|
|
196
|
+
height: "calc(var(--gap) + 2px)",
|
|
197
|
+
bottom: "100%",
|
|
198
|
+
width: "100%",
|
|
199
|
+
}
|
|
200
|
+
}
|