-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path@radix-ui_react-toast.js
More file actions
675 lines (674 loc) · 26.4 KB
/
Copy path@radix-ui_react-toast.js
File metadata and controls
675 lines (674 loc) · 26.4 KB
1
2
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
"use client";
import {
createCollection
} from "./chunk-FZBXXBZ6.js";
import {
Branch,
Portal,
Root,
VisuallyHidden
} from "./chunk-X2HY5QSC.js";
import {
Presence,
Primitive,
composeEventHandlers,
createContextScope,
dispatchDiscreteCustomEvent,
useCallbackRef,
useControllableState,
useLayoutEffect2
} from "./chunk-X73GERWR.js";
import {
useComposedRefs
} from "./chunk-TDQPM5J2.js";
import {
require_jsx_runtime
} from "./chunk-QIFPAX7L.js";
import {
require_react_dom
} from "./chunk-T2SWDQEL.js";
import {
require_react
} from "./chunk-QCHXOAYK.js";
import {
__toESM
} from "./chunk-WOOG5QLI.js";
// node_modules/@radix-ui/react-toast/dist/index.mjs
var React = __toESM(require_react(), 1);
var ReactDOM = __toESM(require_react_dom(), 1);
var import_jsx_runtime = __toESM(require_jsx_runtime(), 1);
var PROVIDER_NAME = "ToastProvider";
var [Collection, useCollection, createCollectionScope] = createCollection("Toast");
var [createToastContext, createToastScope] = createContextScope("Toast", [createCollectionScope]);
var [ToastProviderProvider, useToastProviderContext] = createToastContext(PROVIDER_NAME);
var ToastProvider = (props) => {
const {
__scopeToast,
label = "Notification",
duration = 5e3,
swipeDirection = "right",
swipeThreshold = 50,
children
} = props;
const [viewport, setViewport] = React.useState(null);
const [toastCount, setToastCount] = React.useState(0);
const isFocusedToastEscapeKeyDownRef = React.useRef(false);
const isClosePausedRef = React.useRef(false);
if (!label.trim()) {
console.error(
`Invalid prop \`label\` supplied to \`${PROVIDER_NAME}\`. Expected non-empty \`string\`.`
);
}
return (0, import_jsx_runtime.jsx)(Collection.Provider, { scope: __scopeToast, children: (0, import_jsx_runtime.jsx)(
ToastProviderProvider,
{
scope: __scopeToast,
label,
duration,
swipeDirection,
swipeThreshold,
toastCount,
viewport,
onViewportChange: setViewport,
onToastAdd: React.useCallback(() => setToastCount((prevCount) => prevCount + 1), []),
onToastRemove: React.useCallback(() => setToastCount((prevCount) => prevCount - 1), []),
isFocusedToastEscapeKeyDownRef,
isClosePausedRef,
children
}
) });
};
ToastProvider.displayName = PROVIDER_NAME;
var VIEWPORT_NAME = "ToastViewport";
var VIEWPORT_DEFAULT_HOTKEY = ["F8"];
var VIEWPORT_PAUSE = "toast.viewportPause";
var VIEWPORT_RESUME = "toast.viewportResume";
var ToastViewport = React.forwardRef(
(props, forwardedRef) => {
const {
__scopeToast,
hotkey = VIEWPORT_DEFAULT_HOTKEY,
label = "Notifications ({hotkey})",
...viewportProps
} = props;
const context = useToastProviderContext(VIEWPORT_NAME, __scopeToast);
const getItems = useCollection(__scopeToast);
const wrapperRef = React.useRef(null);
const headFocusProxyRef = React.useRef(null);
const tailFocusProxyRef = React.useRef(null);
const ref = React.useRef(null);
const composedRefs = useComposedRefs(forwardedRef, ref, context.onViewportChange);
const hotkeyLabel = hotkey.join("+").replace(/Key/g, "").replace(/Digit/g, "");
const hasToasts = context.toastCount > 0;
React.useEffect(() => {
const handleKeyDown = (event) => {
var _a;
const isHotkeyPressed = hotkey.length !== 0 && hotkey.every((key) => event[key] || event.code === key);
if (isHotkeyPressed) (_a = ref.current) == null ? void 0 : _a.focus();
};
document.addEventListener("keydown", handleKeyDown);
return () => document.removeEventListener("keydown", handleKeyDown);
}, [hotkey]);
React.useEffect(() => {
const wrapper = wrapperRef.current;
const viewport = ref.current;
if (hasToasts && wrapper && viewport) {
const handlePause = () => {
if (!context.isClosePausedRef.current) {
const pauseEvent = new CustomEvent(VIEWPORT_PAUSE);
viewport.dispatchEvent(pauseEvent);
context.isClosePausedRef.current = true;
}
};
const handleResume = () => {
if (context.isClosePausedRef.current) {
const resumeEvent = new CustomEvent(VIEWPORT_RESUME);
viewport.dispatchEvent(resumeEvent);
context.isClosePausedRef.current = false;
}
};
const handleFocusOutResume = (event) => {
const isFocusMovingOutside = !wrapper.contains(event.relatedTarget);
if (isFocusMovingOutside) handleResume();
};
const handlePointerLeaveResume = () => {
const isFocusInside = wrapper.contains(document.activeElement);
if (!isFocusInside) handleResume();
};
wrapper.addEventListener("focusin", handlePause);
wrapper.addEventListener("focusout", handleFocusOutResume);
wrapper.addEventListener("pointermove", handlePause);
wrapper.addEventListener("pointerleave", handlePointerLeaveResume);
window.addEventListener("blur", handlePause);
window.addEventListener("focus", handleResume);
return () => {
wrapper.removeEventListener("focusin", handlePause);
wrapper.removeEventListener("focusout", handleFocusOutResume);
wrapper.removeEventListener("pointermove", handlePause);
wrapper.removeEventListener("pointerleave", handlePointerLeaveResume);
window.removeEventListener("blur", handlePause);
window.removeEventListener("focus", handleResume);
};
}
}, [hasToasts, context.isClosePausedRef]);
const getSortedTabbableCandidates = React.useCallback(
({ tabbingDirection }) => {
const toastItems = getItems();
const tabbableCandidates = toastItems.map((toastItem) => {
const toastNode = toastItem.ref.current;
const toastTabbableCandidates = [toastNode, ...getTabbableCandidates(toastNode)];
return tabbingDirection === "forwards" ? toastTabbableCandidates : toastTabbableCandidates.reverse();
});
return (tabbingDirection === "forwards" ? tabbableCandidates.reverse() : tabbableCandidates).flat();
},
[getItems]
);
React.useEffect(() => {
const viewport = ref.current;
if (viewport) {
const handleKeyDown = (event) => {
var _a, _b, _c;
const isMetaKey = event.altKey || event.ctrlKey || event.metaKey;
const isTabKey = event.key === "Tab" && !isMetaKey;
if (isTabKey) {
const focusedElement = document.activeElement;
const isTabbingBackwards = event.shiftKey;
const targetIsViewport = event.target === viewport;
if (targetIsViewport && isTabbingBackwards) {
(_a = headFocusProxyRef.current) == null ? void 0 : _a.focus();
return;
}
const tabbingDirection = isTabbingBackwards ? "backwards" : "forwards";
const sortedCandidates = getSortedTabbableCandidates({ tabbingDirection });
const index = sortedCandidates.findIndex((candidate) => candidate === focusedElement);
if (focusFirst(sortedCandidates.slice(index + 1))) {
event.preventDefault();
} else {
isTabbingBackwards ? (_b = headFocusProxyRef.current) == null ? void 0 : _b.focus() : (_c = tailFocusProxyRef.current) == null ? void 0 : _c.focus();
}
}
};
viewport.addEventListener("keydown", handleKeyDown);
return () => viewport.removeEventListener("keydown", handleKeyDown);
}
}, [getItems, getSortedTabbableCandidates]);
return (0, import_jsx_runtime.jsxs)(
Branch,
{
ref: wrapperRef,
role: "region",
"aria-label": label.replace("{hotkey}", hotkeyLabel),
tabIndex: -1,
style: { pointerEvents: hasToasts ? void 0 : "none" },
children: [
hasToasts && (0, import_jsx_runtime.jsx)(
FocusProxy,
{
ref: headFocusProxyRef,
onFocusFromOutsideViewport: () => {
const tabbableCandidates = getSortedTabbableCandidates({
tabbingDirection: "forwards"
});
focusFirst(tabbableCandidates);
}
}
),
(0, import_jsx_runtime.jsx)(Collection.Slot, { scope: __scopeToast, children: (0, import_jsx_runtime.jsx)(Primitive.ol, { tabIndex: -1, ...viewportProps, ref: composedRefs }) }),
hasToasts && (0, import_jsx_runtime.jsx)(
FocusProxy,
{
ref: tailFocusProxyRef,
onFocusFromOutsideViewport: () => {
const tabbableCandidates = getSortedTabbableCandidates({
tabbingDirection: "backwards"
});
focusFirst(tabbableCandidates);
}
}
)
]
}
);
}
);
ToastViewport.displayName = VIEWPORT_NAME;
var FOCUS_PROXY_NAME = "ToastFocusProxy";
var FocusProxy = React.forwardRef(
(props, forwardedRef) => {
const { __scopeToast, onFocusFromOutsideViewport, ...proxyProps } = props;
const context = useToastProviderContext(FOCUS_PROXY_NAME, __scopeToast);
return (0, import_jsx_runtime.jsx)(
VisuallyHidden,
{
"aria-hidden": true,
tabIndex: 0,
...proxyProps,
ref: forwardedRef,
style: { position: "fixed" },
onFocus: (event) => {
var _a;
const prevFocusedElement = event.relatedTarget;
const isFocusFromOutsideViewport = !((_a = context.viewport) == null ? void 0 : _a.contains(prevFocusedElement));
if (isFocusFromOutsideViewport) onFocusFromOutsideViewport();
}
}
);
}
);
FocusProxy.displayName = FOCUS_PROXY_NAME;
var TOAST_NAME = "Toast";
var TOAST_SWIPE_START = "toast.swipeStart";
var TOAST_SWIPE_MOVE = "toast.swipeMove";
var TOAST_SWIPE_CANCEL = "toast.swipeCancel";
var TOAST_SWIPE_END = "toast.swipeEnd";
var Toast = React.forwardRef(
(props, forwardedRef) => {
const { forceMount, open: openProp, defaultOpen, onOpenChange, ...toastProps } = props;
const [open = true, setOpen] = useControllableState({
prop: openProp,
defaultProp: defaultOpen,
onChange: onOpenChange
});
return (0, import_jsx_runtime.jsx)(Presence, { present: forceMount || open, children: (0, import_jsx_runtime.jsx)(
ToastImpl,
{
open,
...toastProps,
ref: forwardedRef,
onClose: () => setOpen(false),
onPause: useCallbackRef(props.onPause),
onResume: useCallbackRef(props.onResume),
onSwipeStart: composeEventHandlers(props.onSwipeStart, (event) => {
event.currentTarget.setAttribute("data-swipe", "start");
}),
onSwipeMove: composeEventHandlers(props.onSwipeMove, (event) => {
const { x, y } = event.detail.delta;
event.currentTarget.setAttribute("data-swipe", "move");
event.currentTarget.style.setProperty("--radix-toast-swipe-move-x", `${x}px`);
event.currentTarget.style.setProperty("--radix-toast-swipe-move-y", `${y}px`);
}),
onSwipeCancel: composeEventHandlers(props.onSwipeCancel, (event) => {
event.currentTarget.setAttribute("data-swipe", "cancel");
event.currentTarget.style.removeProperty("--radix-toast-swipe-move-x");
event.currentTarget.style.removeProperty("--radix-toast-swipe-move-y");
event.currentTarget.style.removeProperty("--radix-toast-swipe-end-x");
event.currentTarget.style.removeProperty("--radix-toast-swipe-end-y");
}),
onSwipeEnd: composeEventHandlers(props.onSwipeEnd, (event) => {
const { x, y } = event.detail.delta;
event.currentTarget.setAttribute("data-swipe", "end");
event.currentTarget.style.removeProperty("--radix-toast-swipe-move-x");
event.currentTarget.style.removeProperty("--radix-toast-swipe-move-y");
event.currentTarget.style.setProperty("--radix-toast-swipe-end-x", `${x}px`);
event.currentTarget.style.setProperty("--radix-toast-swipe-end-y", `${y}px`);
setOpen(false);
})
}
) });
}
);
Toast.displayName = TOAST_NAME;
var [ToastInteractiveProvider, useToastInteractiveContext] = createToastContext(TOAST_NAME, {
onClose() {
}
});
var ToastImpl = React.forwardRef(
(props, forwardedRef) => {
const {
__scopeToast,
type = "foreground",
duration: durationProp,
open,
onClose,
onEscapeKeyDown,
onPause,
onResume,
onSwipeStart,
onSwipeMove,
onSwipeCancel,
onSwipeEnd,
...toastProps
} = props;
const context = useToastProviderContext(TOAST_NAME, __scopeToast);
const [node, setNode] = React.useState(null);
const composedRefs = useComposedRefs(forwardedRef, (node2) => setNode(node2));
const pointerStartRef = React.useRef(null);
const swipeDeltaRef = React.useRef(null);
const duration = durationProp || context.duration;
const closeTimerStartTimeRef = React.useRef(0);
const closeTimerRemainingTimeRef = React.useRef(duration);
const closeTimerRef = React.useRef(0);
const { onToastAdd, onToastRemove } = context;
const handleClose = useCallbackRef(() => {
var _a;
const isFocusInToast = node == null ? void 0 : node.contains(document.activeElement);
if (isFocusInToast) (_a = context.viewport) == null ? void 0 : _a.focus();
onClose();
});
const startTimer = React.useCallback(
(duration2) => {
if (!duration2 || duration2 === Infinity) return;
window.clearTimeout(closeTimerRef.current);
closeTimerStartTimeRef.current = (/* @__PURE__ */ new Date()).getTime();
closeTimerRef.current = window.setTimeout(handleClose, duration2);
},
[handleClose]
);
React.useEffect(() => {
const viewport = context.viewport;
if (viewport) {
const handleResume = () => {
startTimer(closeTimerRemainingTimeRef.current);
onResume == null ? void 0 : onResume();
};
const handlePause = () => {
const elapsedTime = (/* @__PURE__ */ new Date()).getTime() - closeTimerStartTimeRef.current;
closeTimerRemainingTimeRef.current = closeTimerRemainingTimeRef.current - elapsedTime;
window.clearTimeout(closeTimerRef.current);
onPause == null ? void 0 : onPause();
};
viewport.addEventListener(VIEWPORT_PAUSE, handlePause);
viewport.addEventListener(VIEWPORT_RESUME, handleResume);
return () => {
viewport.removeEventListener(VIEWPORT_PAUSE, handlePause);
viewport.removeEventListener(VIEWPORT_RESUME, handleResume);
};
}
}, [context.viewport, duration, onPause, onResume, startTimer]);
React.useEffect(() => {
if (open && !context.isClosePausedRef.current) startTimer(duration);
}, [open, duration, context.isClosePausedRef, startTimer]);
React.useEffect(() => {
onToastAdd();
return () => onToastRemove();
}, [onToastAdd, onToastRemove]);
const announceTextContent = React.useMemo(() => {
return node ? getAnnounceTextContent(node) : null;
}, [node]);
if (!context.viewport) return null;
return (0, import_jsx_runtime.jsxs)(import_jsx_runtime.Fragment, { children: [
announceTextContent && (0, import_jsx_runtime.jsx)(
ToastAnnounce,
{
__scopeToast,
role: "status",
"aria-live": type === "foreground" ? "assertive" : "polite",
"aria-atomic": true,
children: announceTextContent
}
),
(0, import_jsx_runtime.jsx)(ToastInteractiveProvider, { scope: __scopeToast, onClose: handleClose, children: ReactDOM.createPortal(
(0, import_jsx_runtime.jsx)(Collection.ItemSlot, { scope: __scopeToast, children: (0, import_jsx_runtime.jsx)(
Root,
{
asChild: true,
onEscapeKeyDown: composeEventHandlers(onEscapeKeyDown, () => {
if (!context.isFocusedToastEscapeKeyDownRef.current) handleClose();
context.isFocusedToastEscapeKeyDownRef.current = false;
}),
children: (0, import_jsx_runtime.jsx)(
Primitive.li,
{
role: "status",
"aria-live": "off",
"aria-atomic": true,
tabIndex: 0,
"data-state": open ? "open" : "closed",
"data-swipe-direction": context.swipeDirection,
...toastProps,
ref: composedRefs,
style: { userSelect: "none", touchAction: "none", ...props.style },
onKeyDown: composeEventHandlers(props.onKeyDown, (event) => {
if (event.key !== "Escape") return;
onEscapeKeyDown == null ? void 0 : onEscapeKeyDown(event.nativeEvent);
if (!event.nativeEvent.defaultPrevented) {
context.isFocusedToastEscapeKeyDownRef.current = true;
handleClose();
}
}),
onPointerDown: composeEventHandlers(props.onPointerDown, (event) => {
if (event.button !== 0) return;
pointerStartRef.current = { x: event.clientX, y: event.clientY };
}),
onPointerMove: composeEventHandlers(props.onPointerMove, (event) => {
if (!pointerStartRef.current) return;
const x = event.clientX - pointerStartRef.current.x;
const y = event.clientY - pointerStartRef.current.y;
const hasSwipeMoveStarted = Boolean(swipeDeltaRef.current);
const isHorizontalSwipe = ["left", "right"].includes(context.swipeDirection);
const clamp = ["left", "up"].includes(context.swipeDirection) ? Math.min : Math.max;
const clampedX = isHorizontalSwipe ? clamp(0, x) : 0;
const clampedY = !isHorizontalSwipe ? clamp(0, y) : 0;
const moveStartBuffer = event.pointerType === "touch" ? 10 : 2;
const delta = { x: clampedX, y: clampedY };
const eventDetail = { originalEvent: event, delta };
if (hasSwipeMoveStarted) {
swipeDeltaRef.current = delta;
handleAndDispatchCustomEvent(TOAST_SWIPE_MOVE, onSwipeMove, eventDetail, {
discrete: false
});
} else if (isDeltaInDirection(delta, context.swipeDirection, moveStartBuffer)) {
swipeDeltaRef.current = delta;
handleAndDispatchCustomEvent(TOAST_SWIPE_START, onSwipeStart, eventDetail, {
discrete: false
});
event.target.setPointerCapture(event.pointerId);
} else if (Math.abs(x) > moveStartBuffer || Math.abs(y) > moveStartBuffer) {
pointerStartRef.current = null;
}
}),
onPointerUp: composeEventHandlers(props.onPointerUp, (event) => {
const delta = swipeDeltaRef.current;
const target = event.target;
if (target.hasPointerCapture(event.pointerId)) {
target.releasePointerCapture(event.pointerId);
}
swipeDeltaRef.current = null;
pointerStartRef.current = null;
if (delta) {
const toast = event.currentTarget;
const eventDetail = { originalEvent: event, delta };
if (isDeltaInDirection(delta, context.swipeDirection, context.swipeThreshold)) {
handleAndDispatchCustomEvent(TOAST_SWIPE_END, onSwipeEnd, eventDetail, {
discrete: true
});
} else {
handleAndDispatchCustomEvent(
TOAST_SWIPE_CANCEL,
onSwipeCancel,
eventDetail,
{
discrete: true
}
);
}
toast.addEventListener("click", (event2) => event2.preventDefault(), {
once: true
});
}
})
}
)
}
) }),
context.viewport
) })
] });
}
);
var ToastAnnounce = (props) => {
const { __scopeToast, children, ...announceProps } = props;
const context = useToastProviderContext(TOAST_NAME, __scopeToast);
const [renderAnnounceText, setRenderAnnounceText] = React.useState(false);
const [isAnnounced, setIsAnnounced] = React.useState(false);
useNextFrame(() => setRenderAnnounceText(true));
React.useEffect(() => {
const timer = window.setTimeout(() => setIsAnnounced(true), 1e3);
return () => window.clearTimeout(timer);
}, []);
return isAnnounced ? null : (0, import_jsx_runtime.jsx)(Portal, { asChild: true, children: (0, import_jsx_runtime.jsx)(VisuallyHidden, { ...announceProps, children: renderAnnounceText && (0, import_jsx_runtime.jsxs)(import_jsx_runtime.Fragment, { children: [
context.label,
" ",
children
] }) }) });
};
var TITLE_NAME = "ToastTitle";
var ToastTitle = React.forwardRef(
(props, forwardedRef) => {
const { __scopeToast, ...titleProps } = props;
return (0, import_jsx_runtime.jsx)(Primitive.div, { ...titleProps, ref: forwardedRef });
}
);
ToastTitle.displayName = TITLE_NAME;
var DESCRIPTION_NAME = "ToastDescription";
var ToastDescription = React.forwardRef(
(props, forwardedRef) => {
const { __scopeToast, ...descriptionProps } = props;
return (0, import_jsx_runtime.jsx)(Primitive.div, { ...descriptionProps, ref: forwardedRef });
}
);
ToastDescription.displayName = DESCRIPTION_NAME;
var ACTION_NAME = "ToastAction";
var ToastAction = React.forwardRef(
(props, forwardedRef) => {
const { altText, ...actionProps } = props;
if (!altText.trim()) {
console.error(
`Invalid prop \`altText\` supplied to \`${ACTION_NAME}\`. Expected non-empty \`string\`.`
);
return null;
}
return (0, import_jsx_runtime.jsx)(ToastAnnounceExclude, { altText, asChild: true, children: (0, import_jsx_runtime.jsx)(ToastClose, { ...actionProps, ref: forwardedRef }) });
}
);
ToastAction.displayName = ACTION_NAME;
var CLOSE_NAME = "ToastClose";
var ToastClose = React.forwardRef(
(props, forwardedRef) => {
const { __scopeToast, ...closeProps } = props;
const interactiveContext = useToastInteractiveContext(CLOSE_NAME, __scopeToast);
return (0, import_jsx_runtime.jsx)(ToastAnnounceExclude, { asChild: true, children: (0, import_jsx_runtime.jsx)(
Primitive.button,
{
type: "button",
...closeProps,
ref: forwardedRef,
onClick: composeEventHandlers(props.onClick, interactiveContext.onClose)
}
) });
}
);
ToastClose.displayName = CLOSE_NAME;
var ToastAnnounceExclude = React.forwardRef((props, forwardedRef) => {
const { __scopeToast, altText, ...announceExcludeProps } = props;
return (0, import_jsx_runtime.jsx)(
Primitive.div,
{
"data-radix-toast-announce-exclude": "",
"data-radix-toast-announce-alt": altText || void 0,
...announceExcludeProps,
ref: forwardedRef
}
);
});
function getAnnounceTextContent(container) {
const textContent = [];
const childNodes = Array.from(container.childNodes);
childNodes.forEach((node) => {
if (node.nodeType === node.TEXT_NODE && node.textContent) textContent.push(node.textContent);
if (isHTMLElement(node)) {
const isHidden = node.ariaHidden || node.hidden || node.style.display === "none";
const isExcluded = node.dataset.radixToastAnnounceExclude === "";
if (!isHidden) {
if (isExcluded) {
const altText = node.dataset.radixToastAnnounceAlt;
if (altText) textContent.push(altText);
} else {
textContent.push(...getAnnounceTextContent(node));
}
}
}
});
return textContent;
}
function handleAndDispatchCustomEvent(name, handler, detail, { discrete }) {
const currentTarget = detail.originalEvent.currentTarget;
const event = new CustomEvent(name, { bubbles: true, cancelable: true, detail });
if (handler) currentTarget.addEventListener(name, handler, { once: true });
if (discrete) {
dispatchDiscreteCustomEvent(currentTarget, event);
} else {
currentTarget.dispatchEvent(event);
}
}
var isDeltaInDirection = (delta, direction, threshold = 0) => {
const deltaX = Math.abs(delta.x);
const deltaY = Math.abs(delta.y);
const isDeltaX = deltaX > deltaY;
if (direction === "left" || direction === "right") {
return isDeltaX && deltaX > threshold;
} else {
return !isDeltaX && deltaY > threshold;
}
};
function useNextFrame(callback = () => {
}) {
const fn = useCallbackRef(callback);
useLayoutEffect2(() => {
let raf1 = 0;
let raf2 = 0;
raf1 = window.requestAnimationFrame(() => raf2 = window.requestAnimationFrame(fn));
return () => {
window.cancelAnimationFrame(raf1);
window.cancelAnimationFrame(raf2);
};
}, [fn]);
}
function isHTMLElement(node) {
return node.nodeType === node.ELEMENT_NODE;
}
function getTabbableCandidates(container) {
const nodes = [];
const walker = document.createTreeWalker(container, NodeFilter.SHOW_ELEMENT, {
acceptNode: (node) => {
const isHiddenInput = node.tagName === "INPUT" && node.type === "hidden";
if (node.disabled || node.hidden || isHiddenInput) return NodeFilter.FILTER_SKIP;
return node.tabIndex >= 0 ? NodeFilter.FILTER_ACCEPT : NodeFilter.FILTER_SKIP;
}
});
while (walker.nextNode()) nodes.push(walker.currentNode);
return nodes;
}
function focusFirst(candidates) {
const previouslyFocusedElement = document.activeElement;
return candidates.some((candidate) => {
if (candidate === previouslyFocusedElement) return true;
candidate.focus();
return document.activeElement !== previouslyFocusedElement;
});
}
var Provider = ToastProvider;
var Viewport = ToastViewport;
var Root2 = Toast;
var Title = ToastTitle;
var Description = ToastDescription;
var Action = ToastAction;
var Close = ToastClose;
export {
Action,
Close,
Description,
Provider,
Root2 as Root,
Title,
Toast,
ToastAction,
ToastClose,
ToastDescription,
ToastProvider,
ToastTitle,
ToastViewport,
Viewport,
createToastScope
};
//# sourceMappingURL=@radix-ui_react-toast.js.map