MCPcopy Create free account
hub / github.com/adobe/react-spectrum / ToastContainer

Function ToastContainer

packages/@react-spectrum/s2/src/Toast.tsx:348–453  ·  view source on GitHub ↗
(props: ToastContainerProps)

Source from the content-addressed store, hash-verified

346 * at the root of the app.
347 */
348export function ToastContainer(props: ToastContainerProps): ReactNode {
349 let {placement = 'bottom'} = props;
350 let queue = getGlobalToastQueue();
351 let align = 'center';
352 [placement, align = 'center'] = placement.split(' ') as any;
353 let stringFormatter = useLocalizedStringFormatter(intlMessages, '@react-spectrum/s2');
354 let regionRef = useRef<HTMLDivElement | null>(null);
355
356 let state = useOverlayTriggerState({});
357 let {isOpen: isExpanded, close, toggle} = state;
358 let ctx = useMemo(
359 () => ({
360 isExpanded,
361 toggleExpanded() {
362 if (!isExpanded && queue.visibleToasts.length <= 1) {
363 return;
364 }
365
366 startViewTransition(() => toggle(), isExpanded ? 'toast-collapse' : 'toast-expand');
367 }
368 }),
369 [isExpanded, toggle, queue]
370 );
371
372 // Set the state to collapsed whenever the queue is emptied.
373 useEffect(() => {
374 return queue.subscribe(() => {
375 if (queue.visibleToasts.length === 0 && isExpanded) {
376 close();
377 }
378 });
379 }, [queue, isExpanded, close]);
380
381 let collapse = () => {
382 regionRef.current?.focus();
383 ctx.toggleExpanded();
384 };
385
386 // Prevent scroll, aria hide outside, and contain focus when expanded, since we take over the whole screen.
387 // Attach event handler to the ref since ToastRegion doesn't pass through onKeyDown.
388 useModalOverlay({}, state, regionRef);
389 useEvent(
390 regionRef,
391 'keydown',
392 isExpanded
393 ? e => {
394 if (e.key === 'Escape') {
395 collapse();
396 }
397 }
398 : undefined
399 );
400
401 let prefersReducedMotion = useMediaQuery('(prefers-reduced-motion)');
402 let reduceMotion = props['PRIVATE_forceReducedMotion'] ?? prefersReducedMotion;
403 useEffect(() => {
404 let oldGlobalReduceMotion = globalReduceMotion;
405 globalReduceMotion = reduceMotion;

Callers

nothing calls this directly

Calls 11

useOverlayTriggerStateFunction · 0.90
useModalOverlayFunction · 0.90
useEventFunction · 0.90
useMediaQueryFunction · 0.90
closeFunction · 0.85
collapseFunction · 0.85
getGlobalToastQueueFunction · 0.70
subscribeMethod · 0.45
clearMethod · 0.45
formatMethod · 0.45

Tested by

no test coverage detected