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

Function SpectrumToast

packages/@react-spectrum/s2/src/Toast.tsx:511–637  ·  view source on GitHub ↗
(props: SpectrumToastProps)

Source from the content-addressed store, hash-verified

509
510// Exported locally for stories.
511export function SpectrumToast(props: SpectrumToastProps): ReactNode {
512 let {toast, placement = 'bottom', align = 'center'} = props;
513 let variant = toast.content.variant || 'info';
514 let Icon = ICONS[variant];
515 let state = useContext(ToastStateContext)!;
516 let visibleToasts = state.visibleToasts;
517 let index = visibleToasts.indexOf(toast);
518 let isMain = index <= 0;
519 let ctx = useContext(ToastContainerContext);
520 let isExpanded = ctx?.isExpanded || false;
521 let toastRef = useRef<HTMLDivElement | null>(null);
522 let stringFormatter = useLocalizedStringFormatter(intlMessages, '@react-spectrum/s2');
523
524 // When not expanded, use a presentational div for the toasts behind the top one.
525 // The content is invisible, all we show is the background color.
526 if (!isMain && ctx && !ctx.isExpanded) {
527 return (
528 <div
529 role="presentation"
530 style={{
531 position: 'absolute',
532 [placement === 'top' ? 'bottom' : 'top']: 0,
533 left: 0,
534 width: '100%',
535 translate: `0 0 ${(-12 * index) / 16}rem`,
536 // Only 3 toasts are visible in the stack at once, but all toasts are in the DOM.
537 // This allows view transitions to smoothly animate them from where they would be
538 // in the collapsed stack to their final position in the expanded list.
539 opacity: index >= 3 ? 0 : 1,
540 zIndex: visibleToasts.length - index - 1,
541 // When reduced motion is enabled, use append index to view-transition-name
542 // so that adding/removing a toast cross fades instead of transitioning the position.
543 // This works because the toasts are seen as separate elements instead of the same one when their index changes.
544 viewTransitionName: toast.key + (props.reduceMotion ? '-' + index : ''),
545 viewTransitionClass: [toastCss.toast, toastCss['background-toast']]
546 .map(c => CSS.escape(c))
547 .join(' ')
548 }}
549 className={
550 toastCss.toast + toastStyle({variant: toast.content.variant || 'info', index, isExpanded})
551 }
552 ref={fixSafariTransform}
553 />
554 );
555 }
556
557 return (
558 <Toast
559 ref={toastRef}
560 toast={toast}
561 style={{
562 zIndex: visibleToasts.length - index - 1,
563 viewTransitionName: toast.key,
564 viewTransitionClass: [
565 toastCss.toast,
566 !isMain ? toastCss['background-toast'] : '',
567 toastCss[placement],
568 toastCss[align]

Callers

nothing calls this directly

Calls 6

filterDOMPropsFunction · 0.90
focusMethod · 0.80
filterMethod · 0.65
closeMethod · 0.65
formatMethod · 0.45

Tested by

no test coverage detected