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