@tamagui/toast 1.2.8 → 1.2.10
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/esm/Toast.mjs +587 -0
- package/dist/esm/Toast.mjs.map +7 -0
- package/dist/esm/index.mjs +2 -0
- package/dist/esm/index.mjs.map +7 -0
- package/dist/jsx/Toast.mjs +562 -0
- package/dist/jsx/Toast.mjs.map +7 -0
- package/dist/jsx/index.mjs +2 -0
- package/dist/jsx/index.mjs.map +7 -0
- package/package.json +13 -13
|
@@ -0,0 +1,587 @@
|
|
|
1
|
+
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { useComposedRefs } from "@tamagui/compose-refs";
|
|
3
|
+
import {
|
|
4
|
+
composeEventHandlers,
|
|
5
|
+
isWeb,
|
|
6
|
+
useEvent,
|
|
7
|
+
useIsomorphicLayoutEffect
|
|
8
|
+
} from "@tamagui/core";
|
|
9
|
+
import { Portal } from "@tamagui/portal";
|
|
10
|
+
import { YStack } from "@tamagui/stacks";
|
|
11
|
+
import { useControllableState } from "@tamagui/use-controllable-state";
|
|
12
|
+
import { VisuallyHidden } from "@tamagui/visually-hidden";
|
|
13
|
+
import * as React from "react";
|
|
14
|
+
import * as ReactDOM from "react-dom";
|
|
15
|
+
const PROVIDER_NAME = "ToastProvider";
|
|
16
|
+
const [ToastProviderProvider, useToastProviderContext] = [];
|
|
17
|
+
const ToastProvider = (props) => {
|
|
18
|
+
const {
|
|
19
|
+
__scopeToast,
|
|
20
|
+
label = "Notification",
|
|
21
|
+
duration = 5e3,
|
|
22
|
+
swipeDirection = "right",
|
|
23
|
+
swipeThreshold = 50,
|
|
24
|
+
children
|
|
25
|
+
} = props;
|
|
26
|
+
const [viewport, setViewport] = React.useState(null);
|
|
27
|
+
const [toastCount, setToastCount] = React.useState(0);
|
|
28
|
+
const isFocusedToastEscapeKeyDownRef = React.useRef(false);
|
|
29
|
+
const isClosePausedRef = React.useRef(false);
|
|
30
|
+
return (
|
|
31
|
+
// <Collection.Provider scope={__scopeToast}>
|
|
32
|
+
/* @__PURE__ */ jsx(
|
|
33
|
+
ToastProviderProvider,
|
|
34
|
+
{
|
|
35
|
+
scope: __scopeToast,
|
|
36
|
+
label,
|
|
37
|
+
duration,
|
|
38
|
+
swipeDirection,
|
|
39
|
+
swipeThreshold,
|
|
40
|
+
toastCount,
|
|
41
|
+
viewport,
|
|
42
|
+
onViewportChange: setViewport,
|
|
43
|
+
onToastAdd: React.useCallback(
|
|
44
|
+
() => setToastCount((prevCount) => prevCount + 1),
|
|
45
|
+
[]
|
|
46
|
+
),
|
|
47
|
+
onToastRemove: React.useCallback(
|
|
48
|
+
() => setToastCount((prevCount) => prevCount - 1),
|
|
49
|
+
[]
|
|
50
|
+
),
|
|
51
|
+
isFocusedToastEscapeKeyDownRef,
|
|
52
|
+
isClosePausedRef,
|
|
53
|
+
children
|
|
54
|
+
}
|
|
55
|
+
)
|
|
56
|
+
);
|
|
57
|
+
};
|
|
58
|
+
ToastProvider.propTypes = {
|
|
59
|
+
label(props) {
|
|
60
|
+
if (props.label && typeof props.label === "string" && !props.label.trim()) {
|
|
61
|
+
const error = `Invalid prop \`label\` supplied to \`${PROVIDER_NAME}\`. Expected non-empty \`string\`.`;
|
|
62
|
+
return new Error(error);
|
|
63
|
+
}
|
|
64
|
+
return null;
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
ToastProvider.displayName = PROVIDER_NAME;
|
|
68
|
+
const VIEWPORT_NAME = "ToastViewport";
|
|
69
|
+
const VIEWPORT_DEFAULT_HOTKEY = ["F8"];
|
|
70
|
+
const VIEWPORT_PAUSE = "toast.viewportPause";
|
|
71
|
+
const VIEWPORT_RESUME = "toast.viewportResume";
|
|
72
|
+
const ToastViewport = React.forwardRef(
|
|
73
|
+
(props, forwardedRef) => {
|
|
74
|
+
const {
|
|
75
|
+
__scopeToast,
|
|
76
|
+
hotkey = VIEWPORT_DEFAULT_HOTKEY,
|
|
77
|
+
label = "Notifications ({hotkey})",
|
|
78
|
+
...viewportProps
|
|
79
|
+
} = props;
|
|
80
|
+
const context = useToastProviderContext(VIEWPORT_NAME, __scopeToast);
|
|
81
|
+
const wrapperRef = React.useRef(null);
|
|
82
|
+
const headFocusProxyRef = React.useRef(null);
|
|
83
|
+
const tailFocusProxyRef = React.useRef(null);
|
|
84
|
+
const ref = React.useRef(null);
|
|
85
|
+
const composedRefs = useComposedRefs(forwardedRef, ref, context.onViewportChange);
|
|
86
|
+
const hotkeyLabel = hotkey.join("+").replace(/Key/g, "").replace(/Digit/g, "");
|
|
87
|
+
const hasToasts = context.toastCount > 0;
|
|
88
|
+
React.useEffect(() => {
|
|
89
|
+
const handleKeyDown = (event) => {
|
|
90
|
+
var _a;
|
|
91
|
+
const isHotkeyPressed = hotkey.every(
|
|
92
|
+
(key) => event[key] || event.code === key
|
|
93
|
+
);
|
|
94
|
+
if (isHotkeyPressed)
|
|
95
|
+
(_a = ref.current) == null ? void 0 : _a.focus();
|
|
96
|
+
};
|
|
97
|
+
document.addEventListener("keydown", handleKeyDown);
|
|
98
|
+
return () => document.removeEventListener("keydown", handleKeyDown);
|
|
99
|
+
}, [hotkey]);
|
|
100
|
+
React.useEffect(() => {
|
|
101
|
+
const wrapper = wrapperRef.current;
|
|
102
|
+
const viewport = ref.current;
|
|
103
|
+
if (wrapper && viewport) {
|
|
104
|
+
const handlePause = () => {
|
|
105
|
+
const pauseEvent = new CustomEvent(VIEWPORT_PAUSE);
|
|
106
|
+
context.isClosePausedRef.current = true;
|
|
107
|
+
};
|
|
108
|
+
const handleResume = () => {
|
|
109
|
+
const resumeEvent = new CustomEvent(VIEWPORT_RESUME);
|
|
110
|
+
context.isClosePausedRef.current = false;
|
|
111
|
+
};
|
|
112
|
+
wrapper.addEventListener("focusin", handlePause);
|
|
113
|
+
wrapper.addEventListener("focusout", handleResume);
|
|
114
|
+
wrapper.addEventListener("pointerenter", handlePause);
|
|
115
|
+
wrapper.addEventListener("pointerleave", handleResume);
|
|
116
|
+
window.addEventListener("blur", handlePause);
|
|
117
|
+
window.addEventListener("focus", handleResume);
|
|
118
|
+
return () => {
|
|
119
|
+
wrapper.removeEventListener("focusin", handlePause);
|
|
120
|
+
wrapper.removeEventListener("focusout", handleResume);
|
|
121
|
+
wrapper.removeEventListener("pointerenter", handlePause);
|
|
122
|
+
wrapper.removeEventListener("pointerleave", handleResume);
|
|
123
|
+
window.removeEventListener("blur", handlePause);
|
|
124
|
+
window.removeEventListener("focus", handleResume);
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
}, [context.isClosePausedRef]);
|
|
128
|
+
React.useEffect(
|
|
129
|
+
() => {
|
|
130
|
+
const viewport = ref.current;
|
|
131
|
+
if (viewport) {
|
|
132
|
+
const handleKeyDown = (event) => {
|
|
133
|
+
var _a;
|
|
134
|
+
const isMetaKey = event.altKey || event.ctrlKey || event.metaKey;
|
|
135
|
+
const isTabKey = event.key === "Tab" && !isMetaKey;
|
|
136
|
+
if (isTabKey) {
|
|
137
|
+
const focusedElement = document.activeElement;
|
|
138
|
+
const isTabbingBackwards = event.shiftKey;
|
|
139
|
+
const targetIsViewport = event.target === viewport;
|
|
140
|
+
if (targetIsViewport && isTabbingBackwards) {
|
|
141
|
+
(_a = headFocusProxyRef.current) == null ? void 0 : _a.focus();
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
const tabbingDirection = isTabbingBackwards ? "backwards" : "forwards";
|
|
145
|
+
}
|
|
146
|
+
};
|
|
147
|
+
return () => {
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
},
|
|
151
|
+
[
|
|
152
|
+
// getItems, getSortedTabbableCandidates
|
|
153
|
+
]
|
|
154
|
+
);
|
|
155
|
+
return null;
|
|
156
|
+
}
|
|
157
|
+
);
|
|
158
|
+
ToastViewport.displayName = VIEWPORT_NAME;
|
|
159
|
+
const FOCUS_PROXY_NAME = "ToastFocusProxy";
|
|
160
|
+
const FocusProxy = React.forwardRef(
|
|
161
|
+
(props, forwardedRef) => {
|
|
162
|
+
const { __scopeToast, onFocusFromOutsideViewport, ...proxyProps } = props;
|
|
163
|
+
const context = useToastProviderContext(FOCUS_PROXY_NAME, __scopeToast);
|
|
164
|
+
return /* @__PURE__ */ jsx(
|
|
165
|
+
VisuallyHidden,
|
|
166
|
+
{
|
|
167
|
+
"aria-hidden": true,
|
|
168
|
+
tabIndex: 0,
|
|
169
|
+
...proxyProps,
|
|
170
|
+
ref: forwardedRef,
|
|
171
|
+
position: isWeb ? "fixed" : "absolute",
|
|
172
|
+
onFocus: (event) => {
|
|
173
|
+
var _a;
|
|
174
|
+
const prevFocusedElement = event.relatedTarget;
|
|
175
|
+
const isFocusFromOutsideViewport = !((_a = context.viewport) == null ? void 0 : _a.contains(prevFocusedElement));
|
|
176
|
+
if (isFocusFromOutsideViewport)
|
|
177
|
+
onFocusFromOutsideViewport();
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
);
|
|
181
|
+
}
|
|
182
|
+
);
|
|
183
|
+
FocusProxy.displayName = FOCUS_PROXY_NAME;
|
|
184
|
+
const TOAST_NAME = "Toast";
|
|
185
|
+
const TOAST_SWIPE_START = "toast.swipeStart";
|
|
186
|
+
const TOAST_SWIPE_MOVE = "toast.swipeMove";
|
|
187
|
+
const TOAST_SWIPE_CANCEL = "toast.swipeCancel";
|
|
188
|
+
const TOAST_SWIPE_END = "toast.swipeEnd";
|
|
189
|
+
const Toast = React.forwardRef(
|
|
190
|
+
(props, forwardedRef) => {
|
|
191
|
+
const {
|
|
192
|
+
forceMount,
|
|
193
|
+
open: openProp,
|
|
194
|
+
defaultOpen,
|
|
195
|
+
onOpenChange,
|
|
196
|
+
...toastProps
|
|
197
|
+
} = props;
|
|
198
|
+
const [open, setOpen] = useControllableState({
|
|
199
|
+
prop: openProp,
|
|
200
|
+
defaultProp: true,
|
|
201
|
+
onChange: onOpenChange,
|
|
202
|
+
transition: true
|
|
203
|
+
});
|
|
204
|
+
return (
|
|
205
|
+
// <Presence present={forceMount || open}>
|
|
206
|
+
/* @__PURE__ */ jsx(
|
|
207
|
+
ToastImpl,
|
|
208
|
+
{
|
|
209
|
+
open,
|
|
210
|
+
...toastProps,
|
|
211
|
+
ref: forwardedRef,
|
|
212
|
+
onClose: () => setOpen(false)
|
|
213
|
+
}
|
|
214
|
+
)
|
|
215
|
+
);
|
|
216
|
+
}
|
|
217
|
+
);
|
|
218
|
+
Toast.displayName = TOAST_NAME;
|
|
219
|
+
const [ToastInteractiveProvider, useToastInteractiveContext] = {};
|
|
220
|
+
const ToastImpl = React.forwardRef(
|
|
221
|
+
(props, forwardedRef) => {
|
|
222
|
+
const {
|
|
223
|
+
__scopeToast,
|
|
224
|
+
type = "foreground",
|
|
225
|
+
duration: durationProp,
|
|
226
|
+
open,
|
|
227
|
+
onClose,
|
|
228
|
+
onEscapeKeyDown,
|
|
229
|
+
onSwipeStart,
|
|
230
|
+
onSwipeMove,
|
|
231
|
+
onSwipeCancel,
|
|
232
|
+
onSwipeEnd,
|
|
233
|
+
...toastProps
|
|
234
|
+
} = props;
|
|
235
|
+
const context = useToastProviderContext(TOAST_NAME, __scopeToast);
|
|
236
|
+
const ref = React.useRef(null);
|
|
237
|
+
const composedRefs = useComposedRefs(forwardedRef, ref);
|
|
238
|
+
const pointerStartRef = React.useRef(null);
|
|
239
|
+
const swipeDeltaRef = React.useRef(null);
|
|
240
|
+
const duration = durationProp || context.duration;
|
|
241
|
+
const closeTimerStartTimeRef = React.useRef(0);
|
|
242
|
+
const closeTimerRemainingTimeRef = React.useRef(duration);
|
|
243
|
+
const closeTimerRef = React.useRef(0);
|
|
244
|
+
const { onToastAdd, onToastRemove } = context;
|
|
245
|
+
const handleClose = useEvent(() => {
|
|
246
|
+
onClose();
|
|
247
|
+
});
|
|
248
|
+
const startTimer = React.useCallback(
|
|
249
|
+
(duration2) => {
|
|
250
|
+
if (!duration2 || duration2 === Infinity)
|
|
251
|
+
return;
|
|
252
|
+
window.clearTimeout(closeTimerRef.current);
|
|
253
|
+
closeTimerStartTimeRef.current = (/* @__PURE__ */ new Date()).getTime();
|
|
254
|
+
closeTimerRef.current = window.setTimeout(handleClose, duration2);
|
|
255
|
+
},
|
|
256
|
+
[handleClose]
|
|
257
|
+
);
|
|
258
|
+
React.useEffect(() => {
|
|
259
|
+
const viewport = context.viewport;
|
|
260
|
+
if (viewport) {
|
|
261
|
+
const handleResume = () => {
|
|
262
|
+
startTimer(closeTimerRemainingTimeRef.current);
|
|
263
|
+
};
|
|
264
|
+
const handlePause = () => {
|
|
265
|
+
const elapsedTime = (/* @__PURE__ */ new Date()).getTime() - closeTimerStartTimeRef.current;
|
|
266
|
+
closeTimerRemainingTimeRef.current = closeTimerRemainingTimeRef.current - elapsedTime;
|
|
267
|
+
window.clearTimeout(closeTimerRef.current);
|
|
268
|
+
};
|
|
269
|
+
viewport.addEventListener(VIEWPORT_PAUSE, handlePause);
|
|
270
|
+
viewport.addEventListener(VIEWPORT_RESUME, handleResume);
|
|
271
|
+
return () => {
|
|
272
|
+
viewport.removeEventListener(VIEWPORT_PAUSE, handlePause);
|
|
273
|
+
viewport.removeEventListener(VIEWPORT_RESUME, handleResume);
|
|
274
|
+
};
|
|
275
|
+
}
|
|
276
|
+
}, [context.viewport, duration, startTimer]);
|
|
277
|
+
React.useEffect(() => {
|
|
278
|
+
if (open && !context.isClosePausedRef.current)
|
|
279
|
+
startTimer(duration);
|
|
280
|
+
}, [open, duration, context.isClosePausedRef, startTimer]);
|
|
281
|
+
React.useEffect(() => {
|
|
282
|
+
onToastAdd();
|
|
283
|
+
return () => onToastRemove();
|
|
284
|
+
}, [onToastAdd, onToastRemove]);
|
|
285
|
+
if (!context.viewport)
|
|
286
|
+
return null;
|
|
287
|
+
return /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
288
|
+
/* @__PURE__ */ jsx(
|
|
289
|
+
ToastAnnounce,
|
|
290
|
+
{
|
|
291
|
+
__scopeToast,
|
|
292
|
+
role: "status",
|
|
293
|
+
"aria-live": type === "foreground" ? "assertive" : "polite",
|
|
294
|
+
"aria-atomic": true,
|
|
295
|
+
children: props.children
|
|
296
|
+
}
|
|
297
|
+
),
|
|
298
|
+
/* @__PURE__ */ jsx(
|
|
299
|
+
ToastInteractiveProvider,
|
|
300
|
+
{
|
|
301
|
+
scope: __scopeToast,
|
|
302
|
+
isInteractive: true,
|
|
303
|
+
onClose: handleClose,
|
|
304
|
+
children: ReactDOM.createPortal(
|
|
305
|
+
/* @__PURE__ */ jsx("div", {}),
|
|
306
|
+
// <Collection.ItemSlot scope={__scopeToast}>
|
|
307
|
+
// <DismissableLayer.Root
|
|
308
|
+
// asChild
|
|
309
|
+
// onEscapeKeyDown={composeEventHandlers(onEscapeKeyDown, () => {
|
|
310
|
+
// if (!context.isFocusedToastEscapeKeyDownRef.current) handleClose()
|
|
311
|
+
// context.isFocusedToastEscapeKeyDownRef.current = false
|
|
312
|
+
// })}
|
|
313
|
+
// >
|
|
314
|
+
// <Primitive.li
|
|
315
|
+
// // Ensure toasts are announced as status list or status when focused
|
|
316
|
+
// role="status"
|
|
317
|
+
// aria-live="off"
|
|
318
|
+
// aria-atomic
|
|
319
|
+
// tabIndex={0}
|
|
320
|
+
// data-state={open ? 'open' : 'closed'}
|
|
321
|
+
// data-swipe-direction={context.swipeDirection}
|
|
322
|
+
// {...toastProps}
|
|
323
|
+
// ref={composedRefs}
|
|
324
|
+
// style={{ userSelect: 'none', touchAction: 'none', ...props.style }}
|
|
325
|
+
// onKeyDown={composeEventHandlers(props.onKeyDown, (event) => {
|
|
326
|
+
// if (event.key !== 'Escape') return
|
|
327
|
+
// onEscapeKeyDown?.(event.nativeEvent)
|
|
328
|
+
// if (!event.nativeEvent.defaultPrevented) {
|
|
329
|
+
// context.isFocusedToastEscapeKeyDownRef.current = true
|
|
330
|
+
// handleClose()
|
|
331
|
+
// }
|
|
332
|
+
// })}
|
|
333
|
+
// onPointerDown={composeEventHandlers(props.onPointerDown, (event) => {
|
|
334
|
+
// if (event.button !== 0) return
|
|
335
|
+
// pointerStartRef.current = { x: event.clientX, y: event.clientY }
|
|
336
|
+
// })}
|
|
337
|
+
// onPointerMove={composeEventHandlers(props.onPointerMove, (event) => {
|
|
338
|
+
// if (!pointerStartRef.current) return
|
|
339
|
+
// const x = event.clientX - pointerStartRef.current.x
|
|
340
|
+
// const y = event.clientY - pointerStartRef.current.y
|
|
341
|
+
// const hasSwipeMoveStarted = Boolean(swipeDeltaRef.current)
|
|
342
|
+
// const isHorizontalSwipe = ['left', 'right'].includes(context.swipeDirection)
|
|
343
|
+
// const clamp = ['left', 'up'].includes(context.swipeDirection)
|
|
344
|
+
// ? Math.min
|
|
345
|
+
// : Math.max
|
|
346
|
+
// const clampedX = isHorizontalSwipe ? clamp(0, x) : 0
|
|
347
|
+
// const clampedY = !isHorizontalSwipe ? clamp(0, y) : 0
|
|
348
|
+
// const moveStartBuffer = event.pointerType === 'touch' ? 10 : 2
|
|
349
|
+
// const delta = { x: clampedX, y: clampedY }
|
|
350
|
+
// const eventDetail = { originalEvent: event, delta }
|
|
351
|
+
// if (hasSwipeMoveStarted) {
|
|
352
|
+
// swipeDeltaRef.current = delta
|
|
353
|
+
// handleAndDispatchCustomEvent(TOAST_SWIPE_MOVE, onSwipeMove, eventDetail, {
|
|
354
|
+
// discrete: false,
|
|
355
|
+
// })
|
|
356
|
+
// } else if (isDeltaInDirection(delta, context.swipeDirection, moveStartBuffer)) {
|
|
357
|
+
// swipeDeltaRef.current = delta
|
|
358
|
+
// handleAndDispatchCustomEvent(TOAST_SWIPE_START, onSwipeStart, eventDetail, {
|
|
359
|
+
// discrete: false,
|
|
360
|
+
// })
|
|
361
|
+
// ;(event.target as HTMLElement).setPointerCapture(event.pointerId)
|
|
362
|
+
// } else if (Math.abs(x) > moveStartBuffer || Math.abs(y) > moveStartBuffer) {
|
|
363
|
+
// // User is swiping in wrong direction so we disable swipe gesture
|
|
364
|
+
// // for the current pointer down interaction
|
|
365
|
+
// pointerStartRef.current = null
|
|
366
|
+
// }
|
|
367
|
+
// })}
|
|
368
|
+
// onPointerUp={composeEventHandlers(props.onPointerUp, (event) => {
|
|
369
|
+
// const delta = swipeDeltaRef.current
|
|
370
|
+
// ;(event.target as HTMLElement).releasePointerCapture(event.pointerId)
|
|
371
|
+
// swipeDeltaRef.current = null
|
|
372
|
+
// pointerStartRef.current = null
|
|
373
|
+
// if (delta) {
|
|
374
|
+
// const toast = event.currentTarget
|
|
375
|
+
// const eventDetail = { originalEvent: event, delta }
|
|
376
|
+
// if (isDeltaInDirection(delta, context.swipeDirection, context.swipeThreshold)) {
|
|
377
|
+
// handleAndDispatchCustomEvent(TOAST_SWIPE_END, onSwipeEnd, eventDetail, {
|
|
378
|
+
// discrete: true,
|
|
379
|
+
// })
|
|
380
|
+
// } else {
|
|
381
|
+
// handleAndDispatchCustomEvent(TOAST_SWIPE_CANCEL, onSwipeCancel, eventDetail, {
|
|
382
|
+
// discrete: true,
|
|
383
|
+
// })
|
|
384
|
+
// }
|
|
385
|
+
// // Prevent click event from triggering on items within the toast when
|
|
386
|
+
// // pointer up is part of a swipe gesture
|
|
387
|
+
// toast.addEventListener('click', (event) => event.preventDefault(), {
|
|
388
|
+
// once: true,
|
|
389
|
+
// })
|
|
390
|
+
// }
|
|
391
|
+
// })}
|
|
392
|
+
// />
|
|
393
|
+
// </DismissableLayer.Root>,
|
|
394
|
+
// </Collection.ItemSlot>,
|
|
395
|
+
context.viewport
|
|
396
|
+
)
|
|
397
|
+
}
|
|
398
|
+
)
|
|
399
|
+
] });
|
|
400
|
+
}
|
|
401
|
+
);
|
|
402
|
+
ToastImpl.propTypes = {
|
|
403
|
+
type(props) {
|
|
404
|
+
if (props.type && !["foreground", "background"].includes(props.type)) {
|
|
405
|
+
const error = `Invalid prop \`type\` supplied to \`${TOAST_NAME}\`. Expected \`foreground | background\`.`;
|
|
406
|
+
return new Error(error);
|
|
407
|
+
}
|
|
408
|
+
return null;
|
|
409
|
+
}
|
|
410
|
+
};
|
|
411
|
+
const ToastAnnounce = (props) => {
|
|
412
|
+
const { __scopeToast, children, ...announceProps } = props;
|
|
413
|
+
const context = useToastProviderContext(TOAST_NAME, __scopeToast);
|
|
414
|
+
const [renderAnnounceText, setRenderAnnounceText] = React.useState(false);
|
|
415
|
+
const [isAnnounced, setIsAnnounced] = React.useState(false);
|
|
416
|
+
const [fragment, setFragment] = React.useState();
|
|
417
|
+
const [rootFragmentNode, setRootFragmentNode] = React.useState(null);
|
|
418
|
+
const announceTextContent = React.useMemo(() => {
|
|
419
|
+
return rootFragmentNode ? getAnnounceTextContent(rootFragmentNode) : null;
|
|
420
|
+
}, [rootFragmentNode]);
|
|
421
|
+
useIsomorphicLayoutEffect(() => {
|
|
422
|
+
setFragment(new DocumentFragment());
|
|
423
|
+
}, []);
|
|
424
|
+
useNextFrame(() => setRenderAnnounceText(true));
|
|
425
|
+
React.useEffect(() => {
|
|
426
|
+
const timer = window.setTimeout(() => setIsAnnounced(true), 1e3);
|
|
427
|
+
return () => window.clearTimeout(timer);
|
|
428
|
+
}, []);
|
|
429
|
+
return isAnnounced ? null : /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
430
|
+
fragment && // @ts-ignore
|
|
431
|
+
/* @__PURE__ */ jsxs(Portal, { container: fragment, ref: setRootFragmentNode, children: [
|
|
432
|
+
context.label,
|
|
433
|
+
" ",
|
|
434
|
+
children
|
|
435
|
+
] }),
|
|
436
|
+
/* @__PURE__ */ jsx(Portal, { asChild: true, children: /* @__PURE__ */ jsx(VisuallyHidden, { ...announceProps, children: renderAnnounceText && announceTextContent }) })
|
|
437
|
+
] });
|
|
438
|
+
};
|
|
439
|
+
const TITLE_NAME = "ToastTitle";
|
|
440
|
+
const ToastTitle = React.forwardRef(
|
|
441
|
+
(props, forwardedRef) => {
|
|
442
|
+
const { __scopeToast, ...titleProps } = props;
|
|
443
|
+
return /* @__PURE__ */ jsx(YStack, { ...titleProps, ref: forwardedRef });
|
|
444
|
+
}
|
|
445
|
+
);
|
|
446
|
+
ToastTitle.displayName = TITLE_NAME;
|
|
447
|
+
const DESCRIPTION_NAME = "ToastDescription";
|
|
448
|
+
const ToastDescription = React.forwardRef((props, forwardedRef) => {
|
|
449
|
+
const { __scopeToast, ...descriptionProps } = props;
|
|
450
|
+
return /* @__PURE__ */ jsx(YStack, { ...descriptionProps, ref: forwardedRef });
|
|
451
|
+
});
|
|
452
|
+
ToastDescription.displayName = DESCRIPTION_NAME;
|
|
453
|
+
const ACTION_NAME = "ToastAction";
|
|
454
|
+
const ToastAction = React.forwardRef(
|
|
455
|
+
(props, forwardedRef) => {
|
|
456
|
+
const { altText, ...actionProps } = props;
|
|
457
|
+
const context = useToastInteractiveContext(ACTION_NAME, props.__scopeToast);
|
|
458
|
+
if (!altText)
|
|
459
|
+
return null;
|
|
460
|
+
return context.isInteractive ? /* @__PURE__ */ jsx(ToastClose, { ...actionProps, ref: forwardedRef }) : /* @__PURE__ */ jsx("span", { children: altText });
|
|
461
|
+
}
|
|
462
|
+
);
|
|
463
|
+
ToastAction.propTypes = {
|
|
464
|
+
altText(props) {
|
|
465
|
+
if (!props.altText) {
|
|
466
|
+
return new Error(`Missing prop \`altText\` expected on \`${ACTION_NAME}\``);
|
|
467
|
+
}
|
|
468
|
+
return null;
|
|
469
|
+
}
|
|
470
|
+
};
|
|
471
|
+
ToastAction.displayName = ACTION_NAME;
|
|
472
|
+
const CLOSE_NAME = "ToastClose";
|
|
473
|
+
const ToastClose = React.forwardRef(
|
|
474
|
+
(props, forwardedRef) => {
|
|
475
|
+
const { __scopeToast, ...closeProps } = props;
|
|
476
|
+
const interactiveContext = useToastInteractiveContext(CLOSE_NAME, __scopeToast);
|
|
477
|
+
return interactiveContext.isInteractive ? /* @__PURE__ */ jsx(
|
|
478
|
+
YStack,
|
|
479
|
+
{
|
|
480
|
+
tag: "button",
|
|
481
|
+
...closeProps,
|
|
482
|
+
ref: forwardedRef,
|
|
483
|
+
onPress: composeEventHandlers(props.onPress, interactiveContext.onClose)
|
|
484
|
+
}
|
|
485
|
+
) : null;
|
|
486
|
+
}
|
|
487
|
+
);
|
|
488
|
+
ToastClose.displayName = CLOSE_NAME;
|
|
489
|
+
function handleAndDispatchCustomEvent(name, handler, detail, { discrete }) {
|
|
490
|
+
const currentTarget = detail.originalEvent.currentTarget;
|
|
491
|
+
const event = new CustomEvent(name, { bubbles: true, cancelable: true, detail });
|
|
492
|
+
if (handler)
|
|
493
|
+
currentTarget.addEventListener(name, handler, { once: true });
|
|
494
|
+
if (discrete) {
|
|
495
|
+
} else {
|
|
496
|
+
currentTarget.dispatchEvent(event);
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
const isDeltaInDirection = (delta, direction, threshold = 0) => {
|
|
500
|
+
const deltaX = Math.abs(delta.x);
|
|
501
|
+
const deltaY = Math.abs(delta.y);
|
|
502
|
+
const isDeltaX = deltaX > deltaY;
|
|
503
|
+
if (direction === "left" || direction === "right") {
|
|
504
|
+
return isDeltaX && deltaX > threshold;
|
|
505
|
+
} else {
|
|
506
|
+
return !isDeltaX && deltaY > threshold;
|
|
507
|
+
}
|
|
508
|
+
};
|
|
509
|
+
function useNextFrame(callback = () => {
|
|
510
|
+
}) {
|
|
511
|
+
const fn = useEvent(callback);
|
|
512
|
+
useIsomorphicLayoutEffect(() => {
|
|
513
|
+
let raf1 = 0;
|
|
514
|
+
let raf2 = 0;
|
|
515
|
+
raf1 = window.requestAnimationFrame(
|
|
516
|
+
() => raf2 = window.requestAnimationFrame(fn)
|
|
517
|
+
);
|
|
518
|
+
return () => {
|
|
519
|
+
window.cancelAnimationFrame(raf1);
|
|
520
|
+
window.cancelAnimationFrame(raf2);
|
|
521
|
+
};
|
|
522
|
+
}, [fn]);
|
|
523
|
+
}
|
|
524
|
+
function getAnnounceTextContent(container) {
|
|
525
|
+
const textContent = [];
|
|
526
|
+
const childNodes = Array.from(container.childNodes);
|
|
527
|
+
childNodes.forEach((node) => {
|
|
528
|
+
if (node.nodeType === node.TEXT_NODE && node.textContent)
|
|
529
|
+
textContent.push(node.textContent);
|
|
530
|
+
if (isHTMLElement(node)) {
|
|
531
|
+
const isHidden = node.ariaHidden || node.hidden || node.style.display === "none";
|
|
532
|
+
if (!isHidden)
|
|
533
|
+
textContent.push(...getAnnounceTextContent(node));
|
|
534
|
+
}
|
|
535
|
+
});
|
|
536
|
+
return textContent;
|
|
537
|
+
}
|
|
538
|
+
function isHTMLElement(node) {
|
|
539
|
+
return node.nodeType === node.ELEMENT_NODE;
|
|
540
|
+
}
|
|
541
|
+
function getTabbableCandidates(container) {
|
|
542
|
+
const nodes = [];
|
|
543
|
+
const walker = document.createTreeWalker(container, NodeFilter.SHOW_ELEMENT, {
|
|
544
|
+
acceptNode: (node) => {
|
|
545
|
+
const isHiddenInput = node.tagName === "INPUT" && node.type === "hidden";
|
|
546
|
+
if (node.disabled || node.hidden || isHiddenInput)
|
|
547
|
+
return NodeFilter.FILTER_SKIP;
|
|
548
|
+
return node.tabIndex >= 0 ? NodeFilter.FILTER_ACCEPT : NodeFilter.FILTER_SKIP;
|
|
549
|
+
}
|
|
550
|
+
});
|
|
551
|
+
while (walker.nextNode())
|
|
552
|
+
nodes.push(walker.currentNode);
|
|
553
|
+
return nodes;
|
|
554
|
+
}
|
|
555
|
+
function focusFirst(candidates) {
|
|
556
|
+
const previouslyFocusedElement = document.activeElement;
|
|
557
|
+
return candidates.some((candidate) => {
|
|
558
|
+
if (candidate === previouslyFocusedElement)
|
|
559
|
+
return true;
|
|
560
|
+
candidate.focus();
|
|
561
|
+
return document.activeElement !== previouslyFocusedElement;
|
|
562
|
+
});
|
|
563
|
+
}
|
|
564
|
+
const Provider = ToastProvider;
|
|
565
|
+
const Viewport = ToastViewport;
|
|
566
|
+
const Root = Toast;
|
|
567
|
+
const Title = ToastTitle;
|
|
568
|
+
const Description = ToastDescription;
|
|
569
|
+
const Action = ToastAction;
|
|
570
|
+
const Close = ToastClose;
|
|
571
|
+
export {
|
|
572
|
+
Action,
|
|
573
|
+
Close,
|
|
574
|
+
Description,
|
|
575
|
+
Provider,
|
|
576
|
+
Root,
|
|
577
|
+
Title,
|
|
578
|
+
Toast,
|
|
579
|
+
ToastAction,
|
|
580
|
+
ToastClose,
|
|
581
|
+
ToastDescription,
|
|
582
|
+
ToastProvider,
|
|
583
|
+
ToastTitle,
|
|
584
|
+
ToastViewport,
|
|
585
|
+
Viewport
|
|
586
|
+
};
|
|
587
|
+
//# sourceMappingURL=Toast.mjs.map
|