@zag-js/toast 2.0.0-next.3 → 2.0.0-next.4
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 +10 -17
- package/dist/index.d.ts +10 -17
- package/dist/index.js +18 -45
- package/dist/index.mjs +5 -11
- package/dist/toast-group.connect.d.mts +4 -8
- package/dist/toast-group.connect.d.ts +4 -8
- package/dist/toast-group.connect.js +82 -103
- package/dist/toast-group.connect.mjs +77 -67
- package/dist/toast-group.machine.d.mts +2 -8
- package/dist/toast-group.machine.d.ts +2 -8
- package/dist/toast-group.machine.js +295 -290
- package/dist/toast-group.machine.mjs +290 -254
- package/dist/toast.anatomy.d.mts +2 -6
- package/dist/toast.anatomy.d.ts +2 -6
- package/dist/toast.anatomy.js +8 -40
- package/dist/toast.anatomy.mjs +4 -14
- package/dist/toast.connect.d.mts +4 -8
- package/dist/toast.connect.d.ts +4 -8
- package/dist/toast.connect.js +176 -160
- package/dist/toast.connect.mjs +171 -124
- package/dist/toast.dom.d.mts +21 -14
- package/dist/toast.dom.d.ts +21 -14
- package/dist/toast.dom.js +50 -48
- package/dist/toast.dom.mjs +35 -17
- package/dist/toast.machine.d.mts +2 -8
- package/dist/toast.machine.d.ts +2 -8
- package/dist/toast.machine.js +404 -288
- package/dist/toast.machine.mjs +400 -253
- package/dist/toast.store.d.mts +2 -8
- package/dist/toast.store.d.ts +2 -8
- package/dist/toast.store.js +345 -263
- package/dist/toast.store.mjs +341 -238
- package/dist/toast.types.d.mts +460 -427
- package/dist/toast.types.d.ts +460 -427
- package/dist/toast.types.js +4 -18
- package/dist/toast.types.mjs +1 -0
- package/dist/toast.utils.d.mts +10 -13
- package/dist/toast.utils.d.ts +10 -13
- package/dist/toast.utils.js +185 -198
- package/dist/toast.utils.mjs +175 -168
- package/dist/utils/swipe.d.mts +16 -0
- package/dist/utils/swipe.d.ts +16 -0
- package/dist/utils/swipe.js +100 -0
- package/dist/utils/swipe.mjs +93 -0
- package/package.json +8 -8
package/dist/toast.machine.mjs
CHANGED
|
@@ -1,257 +1,404 @@
|
|
|
1
|
-
|
|
1
|
+
import { getContentEl, getDescriptionEl, getRootEl, getTitleEl, observeContent } from "./toast.dom.mjs";
|
|
2
|
+
import { getToastDuration, setHeight } from "./toast.utils.mjs";
|
|
3
|
+
import { createSwipeTracker, getDefaultSwipeDirections } from "./utils/swipe.mjs";
|
|
4
|
+
import { AnimationFrame, AnimationFrameTimeout, addDomEvent, contains, raf } from "@zag-js/dom-query";
|
|
5
|
+
import { callAll, ensureProps, warn } from "@zag-js/utils";
|
|
2
6
|
import { createGuards, createMachine } from "@zag-js/core";
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
7
|
+
// src/toast.machine.ts
|
|
8
|
+
const { not, and } = createGuards();
|
|
9
|
+
const machine = createMachine({
|
|
10
|
+
props({ props }) {
|
|
11
|
+
ensureProps(props, [
|
|
12
|
+
"id",
|
|
13
|
+
"type",
|
|
14
|
+
"parent",
|
|
15
|
+
"removeDelay"
|
|
16
|
+
], "toast");
|
|
17
|
+
return {
|
|
18
|
+
closable: true,
|
|
19
|
+
...props,
|
|
20
|
+
duration: getToastDuration(props.duration, props.type)
|
|
21
|
+
};
|
|
22
|
+
},
|
|
23
|
+
initialState({ prop }) {
|
|
24
|
+
if (prop("message") === "DISMISS") return "dismissing";
|
|
25
|
+
return prop("type") === "loading" || prop("duration") === 1e400 || prop("paused") ? "visible:persist" : "visible";
|
|
26
|
+
},
|
|
27
|
+
context({ prop, bindable }) {
|
|
28
|
+
return {
|
|
29
|
+
remainingTime: bindable(() => ({ defaultValue: getToastDuration(prop("duration"), prop("type")) })),
|
|
30
|
+
createdAt: bindable(() => ({ defaultValue: Date.now() })),
|
|
31
|
+
mounted: bindable(() => ({ defaultValue: false })),
|
|
32
|
+
initialHeight: bindable(() => ({ defaultValue: 0 })),
|
|
33
|
+
rendered: bindable(() => ({ defaultValue: {
|
|
34
|
+
title: true,
|
|
35
|
+
description: true
|
|
36
|
+
} })),
|
|
37
|
+
swipeOffset: bindable(() => ({ defaultValue: {
|
|
38
|
+
x: 0,
|
|
39
|
+
y: 0
|
|
40
|
+
} })),
|
|
41
|
+
swipeDirection: bindable(() => ({ defaultValue: null }))
|
|
42
|
+
};
|
|
43
|
+
},
|
|
44
|
+
refs() {
|
|
45
|
+
return {
|
|
46
|
+
closeTimerStartTime: Date.now(),
|
|
47
|
+
lastCloseStartTimerStartTime: 0,
|
|
48
|
+
swipeStart: null,
|
|
49
|
+
mountFrame: AnimationFrame.create()
|
|
50
|
+
};
|
|
51
|
+
},
|
|
52
|
+
computed: {
|
|
53
|
+
zIndex: ({ prop }) => {
|
|
54
|
+
const toasts = prop("parent").context.get("toasts");
|
|
55
|
+
const index = toasts.findIndex((toast) => toast.id === prop("id"));
|
|
56
|
+
return toasts.length - index;
|
|
57
|
+
},
|
|
58
|
+
height: ({ prop }) => {
|
|
59
|
+
return prop("parent").context.get("heights").find((height) => height.id === prop("id"))?.height ?? 0;
|
|
60
|
+
},
|
|
61
|
+
heightIndex: ({ prop }) => {
|
|
62
|
+
return prop("parent").context.get("heights").findIndex((height) => height.id === prop("id"));
|
|
63
|
+
},
|
|
64
|
+
frontmost: ({ prop }) => prop("index") === 0,
|
|
65
|
+
heightBefore: ({ prop }) => {
|
|
66
|
+
const heights = prop("parent").context.get("heights");
|
|
67
|
+
const heightIndex = heights.findIndex((height) => height.id === prop("id"));
|
|
68
|
+
return heights.reduce((prev, curr, reducerIndex) => {
|
|
69
|
+
if (reducerIndex >= heightIndex) return prev;
|
|
70
|
+
return prev + curr.height;
|
|
71
|
+
}, 0);
|
|
72
|
+
},
|
|
73
|
+
shouldPersist: ({ prop }) => prop("type") === "loading" || prop("duration") === 1e400,
|
|
74
|
+
swipeDirections: ({ prop }) => prop("swipeDirections") ?? getDefaultSwipeDirections(prop("parent").computed("placement"), prop("dir"))
|
|
75
|
+
},
|
|
76
|
+
watch({ track, prop, send }) {
|
|
77
|
+
track([() => prop("message")], () => {
|
|
78
|
+
const message = prop("message");
|
|
79
|
+
if (message) send({
|
|
80
|
+
type: message,
|
|
81
|
+
src: "programmatic"
|
|
82
|
+
});
|
|
83
|
+
});
|
|
84
|
+
track([() => prop("paused")], () => {
|
|
85
|
+
send({ type: prop("paused") ? "PAUSE" : "RESUME" });
|
|
86
|
+
});
|
|
87
|
+
track([() => prop("type"), () => prop("duration")], () => {
|
|
88
|
+
send({ type: "UPDATE" });
|
|
89
|
+
});
|
|
90
|
+
},
|
|
91
|
+
on: {
|
|
92
|
+
UPDATE: [{
|
|
93
|
+
guard: and("isVisible", "shouldPause"),
|
|
94
|
+
target: "visible:persist",
|
|
95
|
+
actions: ["resetCloseTimer"]
|
|
96
|
+
}, {
|
|
97
|
+
guard: "isVisible",
|
|
98
|
+
target: "visible:updating",
|
|
99
|
+
actions: ["resetCloseTimer"]
|
|
100
|
+
}],
|
|
101
|
+
MEASURE: { actions: ["measureHeight"] }
|
|
102
|
+
},
|
|
103
|
+
entry: [
|
|
104
|
+
"setMounted",
|
|
105
|
+
"measureHeight",
|
|
106
|
+
"invokeOnVisible"
|
|
107
|
+
],
|
|
108
|
+
exit: ["cancelMountFrame"],
|
|
109
|
+
effects: ["trackContent"],
|
|
110
|
+
states: {
|
|
111
|
+
"visible:updating": {
|
|
112
|
+
tags: ["visible", "updating"],
|
|
113
|
+
effects: ["waitForNextTick"],
|
|
114
|
+
on: {
|
|
115
|
+
DISMISS: { target: "dismissing" },
|
|
116
|
+
PAUSE: { target: "visible:persist" },
|
|
117
|
+
SHOW: { target: "visible" }
|
|
118
|
+
}
|
|
119
|
+
},
|
|
120
|
+
"visible:persist": {
|
|
121
|
+
tags: ["visible", "paused"],
|
|
122
|
+
on: {
|
|
123
|
+
RESUME: {
|
|
124
|
+
guard: not("shouldPause"),
|
|
125
|
+
target: "visible",
|
|
126
|
+
actions: ["setCloseTimer"]
|
|
127
|
+
},
|
|
128
|
+
DISMISS: { target: "dismissing" },
|
|
129
|
+
"SWIPE.START": {
|
|
130
|
+
guard: "canSwipe",
|
|
131
|
+
target: "visible:swiping",
|
|
132
|
+
actions: ["setSwipeStart"]
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
},
|
|
136
|
+
visible: {
|
|
137
|
+
tags: ["visible"],
|
|
138
|
+
effects: ["waitForDuration"],
|
|
139
|
+
on: {
|
|
140
|
+
DISMISS: { target: "dismissing" },
|
|
141
|
+
PAUSE: {
|
|
142
|
+
target: "visible:persist",
|
|
143
|
+
actions: ["syncRemainingTime"]
|
|
144
|
+
},
|
|
145
|
+
"SWIPE.START": {
|
|
146
|
+
guard: "canSwipe",
|
|
147
|
+
target: "visible:swiping",
|
|
148
|
+
actions: ["syncRemainingTime", "setSwipeStart"]
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
},
|
|
152
|
+
"visible:swiping": {
|
|
153
|
+
tags: ["visible", "paused"],
|
|
154
|
+
entry: ["pauseGroup"],
|
|
155
|
+
exit: ["resumeGroup"],
|
|
156
|
+
effects: ["trackSwipe"],
|
|
157
|
+
on: {
|
|
158
|
+
"SWIPE.MOVE": { actions: ["setSwipeOffset"] },
|
|
159
|
+
"SWIPE.END": [
|
|
160
|
+
{
|
|
161
|
+
guard: "isSwipeDismiss",
|
|
162
|
+
target: "dismissing",
|
|
163
|
+
actions: ["setSwipeDirection"]
|
|
164
|
+
},
|
|
165
|
+
{
|
|
166
|
+
guard: "shouldPause",
|
|
167
|
+
target: "visible:persist",
|
|
168
|
+
actions: ["resetSwipe"]
|
|
169
|
+
},
|
|
170
|
+
{
|
|
171
|
+
target: "visible",
|
|
172
|
+
actions: ["resetSwipe", "setCloseTimer"]
|
|
173
|
+
}
|
|
174
|
+
],
|
|
175
|
+
"SWIPE.CANCEL": [{
|
|
176
|
+
guard: "shouldPause",
|
|
177
|
+
target: "visible:persist",
|
|
178
|
+
actions: ["resetSwipe"]
|
|
179
|
+
}, {
|
|
180
|
+
target: "visible",
|
|
181
|
+
actions: ["resetSwipe", "setCloseTimer"]
|
|
182
|
+
}],
|
|
183
|
+
DISMISS: {
|
|
184
|
+
target: "dismissing",
|
|
185
|
+
actions: ["resetSwipe"]
|
|
186
|
+
},
|
|
187
|
+
UPDATE: { actions: ["resetCloseTimer"] }
|
|
188
|
+
}
|
|
189
|
+
},
|
|
190
|
+
dismissing: {
|
|
191
|
+
entry: ["notifyParentOfDismiss", "invokeOnDismiss"],
|
|
192
|
+
effects: ["waitForRemoveDelay"],
|
|
193
|
+
on: { REMOVE: {
|
|
194
|
+
target: "unmounted",
|
|
195
|
+
actions: ["notifyParentToRemove"]
|
|
196
|
+
} }
|
|
197
|
+
},
|
|
198
|
+
unmounted: { entry: ["invokeOnUnmount"] }
|
|
199
|
+
},
|
|
200
|
+
implementations: {
|
|
201
|
+
effects: {
|
|
202
|
+
waitForRemoveDelay({ prop, send }) {
|
|
203
|
+
const timeout = AnimationFrameTimeout.create();
|
|
204
|
+
timeout.start(prop("removeDelay"), () => send({
|
|
205
|
+
type: "REMOVE",
|
|
206
|
+
src: "timer"
|
|
207
|
+
}));
|
|
208
|
+
return timeout.cancel;
|
|
209
|
+
},
|
|
210
|
+
waitForDuration({ send, context, computed }) {
|
|
211
|
+
if (computed("shouldPersist")) return;
|
|
212
|
+
const timeout = AnimationFrameTimeout.create();
|
|
213
|
+
timeout.start(context.get("remainingTime"), () => send({
|
|
214
|
+
type: "DISMISS",
|
|
215
|
+
src: "timer"
|
|
216
|
+
}));
|
|
217
|
+
return timeout.cancel;
|
|
218
|
+
},
|
|
219
|
+
waitForNextTick({ send }) {
|
|
220
|
+
return raf(() => send({
|
|
221
|
+
type: "SHOW",
|
|
222
|
+
src: "timer"
|
|
223
|
+
}));
|
|
224
|
+
},
|
|
225
|
+
trackContent({ scope, prop, context, state }) {
|
|
226
|
+
return raf(() => {
|
|
227
|
+
const rootEl = getRootEl(scope);
|
|
228
|
+
if (!rootEl) return;
|
|
229
|
+
const syncHeight = () => {
|
|
230
|
+
if (!state.hasTag("visible") || !rootEl.isConnected) return;
|
|
231
|
+
const height = measureLayoutHeight(rootEl);
|
|
232
|
+
setHeight(prop("parent"), {
|
|
233
|
+
id: prop("id"),
|
|
234
|
+
height
|
|
235
|
+
});
|
|
236
|
+
};
|
|
237
|
+
const syncRenderedElements = () => {
|
|
238
|
+
context.set("rendered", {
|
|
239
|
+
title: !!getTitleEl(scope),
|
|
240
|
+
description: !!getDescriptionEl(scope)
|
|
241
|
+
});
|
|
242
|
+
};
|
|
243
|
+
syncRenderedElements();
|
|
244
|
+
const contentEl = getContentEl(scope);
|
|
245
|
+
if (!contentEl) {
|
|
246
|
+
warn("[zag-js/toast] Render the content part (`api.getContentProps()`) so the toast height follows its content.");
|
|
247
|
+
return;
|
|
248
|
+
}
|
|
249
|
+
return observeContent(contentEl, {
|
|
250
|
+
onChange: syncHeight,
|
|
251
|
+
onMutate: syncRenderedElements
|
|
252
|
+
});
|
|
253
|
+
});
|
|
254
|
+
},
|
|
255
|
+
trackSwipe({ scope, refs, send, computed }) {
|
|
256
|
+
const rootEl = getRootEl(scope);
|
|
257
|
+
const start = refs.get("swipeStart");
|
|
258
|
+
if (!rootEl || !start) return;
|
|
259
|
+
const doc = scope.getDoc();
|
|
260
|
+
const tracker = createSwipeTracker(computed("swipeDirections"), start.point);
|
|
261
|
+
try {
|
|
262
|
+
rootEl.setPointerCapture(start.pointerId);
|
|
263
|
+
} catch {}
|
|
264
|
+
const isActivePointer = (event) => event.pointerId === start.pointerId;
|
|
265
|
+
const onPointerMove = (event) => {
|
|
266
|
+
if (!isActivePointer(event)) return;
|
|
267
|
+
event.preventDefault();
|
|
268
|
+
const { offset, direction } = tracker.move({
|
|
269
|
+
x: event.clientX,
|
|
270
|
+
y: event.clientY
|
|
271
|
+
});
|
|
272
|
+
send({
|
|
273
|
+
type: "SWIPE.MOVE",
|
|
274
|
+
offset,
|
|
275
|
+
direction
|
|
276
|
+
});
|
|
277
|
+
};
|
|
278
|
+
const onPointerUp = (event) => {
|
|
279
|
+
if (!isActivePointer(event)) return;
|
|
280
|
+
send({
|
|
281
|
+
type: "SWIPE.END",
|
|
282
|
+
direction: tracker.end()
|
|
283
|
+
});
|
|
284
|
+
};
|
|
285
|
+
const onPointerCancel = (event) => {
|
|
286
|
+
if (!isActivePointer(event)) return;
|
|
287
|
+
send({ type: "SWIPE.CANCEL" });
|
|
288
|
+
};
|
|
289
|
+
const onVisibilityChange = () => {
|
|
290
|
+
if (doc.visibilityState === "hidden") send({ type: "SWIPE.CANCEL" });
|
|
291
|
+
};
|
|
292
|
+
const onTouchMove = (event) => event.preventDefault();
|
|
293
|
+
return callAll(addDomEvent(doc, "pointermove", onPointerMove), addDomEvent(doc, "pointerup", onPointerUp), addDomEvent(doc, "pointercancel", onPointerCancel), addDomEvent(doc, "visibilitychange", onVisibilityChange), addDomEvent(rootEl, "touchmove", onTouchMove, { passive: false }));
|
|
294
|
+
}
|
|
295
|
+
},
|
|
296
|
+
guards: {
|
|
297
|
+
isVisible: ({ state }) => state.hasTag("visible"),
|
|
298
|
+
shouldPause: ({ computed, prop }) => computed("shouldPersist") || !!prop("paused"),
|
|
299
|
+
canSwipe: ({ computed }) => computed("swipeDirections").length > 0,
|
|
300
|
+
isSwipeDismiss: ({ event }) => event.direction != null
|
|
301
|
+
},
|
|
302
|
+
actions: {
|
|
303
|
+
setSwipeStart({ refs, event }) {
|
|
304
|
+
refs.set("swipeStart", {
|
|
305
|
+
point: event.point,
|
|
306
|
+
pointerId: event.pointerId
|
|
307
|
+
});
|
|
308
|
+
},
|
|
309
|
+
setSwipeOffset({ context, event }) {
|
|
310
|
+
context.set("swipeOffset", event.offset);
|
|
311
|
+
context.set("swipeDirection", event.direction);
|
|
312
|
+
},
|
|
313
|
+
setSwipeDirection({ context, event }) {
|
|
314
|
+
context.set("swipeDirection", event.direction);
|
|
315
|
+
},
|
|
316
|
+
resetSwipe({ context, refs }) {
|
|
317
|
+
context.set("swipeOffset", {
|
|
318
|
+
x: 0,
|
|
319
|
+
y: 0
|
|
320
|
+
});
|
|
321
|
+
context.set("swipeDirection", null);
|
|
322
|
+
refs.set("swipeStart", null);
|
|
323
|
+
},
|
|
324
|
+
pauseGroup({ prop }) {
|
|
325
|
+
prop("parent").send({ type: "TOAST.PAUSE" });
|
|
326
|
+
},
|
|
327
|
+
resumeGroup({ prop }) {
|
|
328
|
+
prop("parent").send({ type: "TOAST.SWIPE_END" });
|
|
329
|
+
},
|
|
330
|
+
setMounted({ context, refs }) {
|
|
331
|
+
refs.get("mountFrame").request(() => {
|
|
332
|
+
context.set("mounted", true);
|
|
333
|
+
});
|
|
334
|
+
},
|
|
335
|
+
cancelMountFrame({ refs }) {
|
|
336
|
+
refs.get("mountFrame").cancel();
|
|
337
|
+
},
|
|
338
|
+
measureHeight({ scope, prop, context }) {
|
|
339
|
+
queueMicrotask(() => {
|
|
340
|
+
const rootEl = getRootEl(scope);
|
|
341
|
+
if (!rootEl) return;
|
|
342
|
+
const height = measureLayoutHeight(rootEl);
|
|
343
|
+
context.set("initialHeight", height);
|
|
344
|
+
const item = {
|
|
345
|
+
id: prop("id"),
|
|
346
|
+
height
|
|
347
|
+
};
|
|
348
|
+
setHeight(prop("parent"), item);
|
|
349
|
+
});
|
|
350
|
+
},
|
|
351
|
+
setCloseTimer({ refs }) {
|
|
352
|
+
refs.set("closeTimerStartTime", Date.now());
|
|
353
|
+
},
|
|
354
|
+
resetCloseTimer({ context, refs, prop }) {
|
|
355
|
+
refs.set("closeTimerStartTime", Date.now());
|
|
356
|
+
context.set("remainingTime", getToastDuration(prop("duration"), prop("type")));
|
|
357
|
+
},
|
|
358
|
+
syncRemainingTime({ context, refs }) {
|
|
359
|
+
context.set("remainingTime", (prev) => {
|
|
360
|
+
const closeTimerStartTime = refs.get("closeTimerStartTime");
|
|
361
|
+
const elapsedTime = Date.now() - closeTimerStartTime;
|
|
362
|
+
refs.set("lastCloseStartTimerStartTime", Date.now());
|
|
363
|
+
return prev - elapsedTime;
|
|
364
|
+
});
|
|
365
|
+
},
|
|
366
|
+
notifyParentToRemove({ prop }) {
|
|
367
|
+
prop("parent").send({
|
|
368
|
+
type: "TOAST.REMOVE",
|
|
369
|
+
id: prop("id")
|
|
370
|
+
});
|
|
371
|
+
},
|
|
372
|
+
notifyParentOfDismiss({ prop, scope }) {
|
|
373
|
+
const activeEl = scope.getActiveElement();
|
|
374
|
+
if (!contains(getRootEl(scope), activeEl)) return;
|
|
375
|
+
const focusVisible = !!activeEl?.matches(":focus-visible");
|
|
376
|
+
prop("parent").send({
|
|
377
|
+
type: "TOAST.DISMISS",
|
|
378
|
+
focusVisible
|
|
379
|
+
});
|
|
380
|
+
},
|
|
381
|
+
invokeOnDismiss({ prop, event }) {
|
|
382
|
+
prop("parent").prop("store").dismiss(prop("id"));
|
|
383
|
+
prop("onStatusChange")?.({
|
|
384
|
+
status: "dismissing",
|
|
385
|
+
src: event.src
|
|
386
|
+
});
|
|
387
|
+
},
|
|
388
|
+
invokeOnUnmount({ prop }) {
|
|
389
|
+
prop("onStatusChange")?.({ status: "unmounted" });
|
|
390
|
+
},
|
|
391
|
+
invokeOnVisible({ prop }) {
|
|
392
|
+
prop("onStatusChange")?.({ status: "visible" });
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
}
|
|
236
396
|
});
|
|
237
397
|
function measureLayoutHeight(el) {
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
}
|
|
244
|
-
function setHeight(parent, item) {
|
|
245
|
-
const { id, height } = item;
|
|
246
|
-
parent.context.set("heights", (prev) => {
|
|
247
|
-
const alreadyExists = prev.find((i) => i.id === id);
|
|
248
|
-
if (!alreadyExists) {
|
|
249
|
-
return [{ id, height }, ...prev];
|
|
250
|
-
} else {
|
|
251
|
-
return prev.map((i) => i.id === id ? { ...i, height } : i);
|
|
252
|
-
}
|
|
253
|
-
});
|
|
398
|
+
const prevHeight = el.style.height;
|
|
399
|
+
el.style.height = "auto";
|
|
400
|
+
const height = el.offsetHeight;
|
|
401
|
+
el.style.height = prevHeight;
|
|
402
|
+
return height;
|
|
254
403
|
}
|
|
255
|
-
export {
|
|
256
|
-
machine
|
|
257
|
-
};
|
|
404
|
+
export { machine };
|