@@ -18,6 +18,7 @@ import {forwardRefType} from '@react-types/shared';
1818import { QueuedToast , ToastQueue , ToastState , useToastQueue } from 'react-stately' ;
1919import React , { createContext , ForwardedRef , forwardRef , HTMLAttributes , JSX , ReactElement , useContext } from 'react' ;
2020import { TextContext } from './Text' ;
21+ import { useIsSSR } from '@react-aria/ssr' ;
2122import { useObjectRef } from '@react-aria/utils' ;
2223
2324const ToastStateContext = createContext < ToastState < any > | null > ( null ) ;
@@ -41,13 +42,19 @@ export interface ToastRegionProps<T> extends AriaToastRegionProps, StyleRenderPr
4142 /** The queue of toasts to display. */
4243 queue : ToastQueue < T > ,
4344 /** A function to render each toast. */
44- children : ( renderProps : { toast : QueuedToast < T > } ) => ReactElement
45+ children : ( renderProps : { toast : QueuedToast < T > } ) => ReactElement ,
46+ /**
47+ * The container element in which the toast region portal will be placed.
48+ * @default document.body
49+ */
50+ portalContainer ?: Element
4551}
4652
4753/**
4854 * A ToastRegion displays one or more toast notifications.
4955 */
5056export const ToastRegion = /*#__PURE__*/ ( forwardRef as forwardRefType ) ( function ToastRegion < T > ( props : ToastRegionProps < T > , ref : ForwardedRef < HTMLDivElement > ) : JSX . Element | null {
57+ let isSSR = useIsSSR ( ) ;
5158 let state = useToastQueue ( props . queue ) ;
5259 let objectRef = useObjectRef ( ref ) ;
5360 let { regionProps} = useToastRegion ( props , state , objectRef ) ;
@@ -64,6 +71,8 @@ export const ToastRegion = /*#__PURE__*/ (forwardRef as forwardRefType)(function
6471 }
6572 } ) ;
6673
74+ let { portalContainer = isSSR ? null : document . body } = props ;
75+
6776 let region = (
6877 < ToastStateContext . Provider value = { state } >
6978 < div
@@ -77,8 +86,8 @@ export const ToastRegion = /*#__PURE__*/ (forwardRef as forwardRefType)(function
7786 </ ToastStateContext . Provider >
7887 ) ;
7988
80- return state . visibleToasts . length > 0 && typeof document !== 'undefined'
81- ? createPortal ( region , document . body )
89+ return state . visibleToasts . length > 0 && portalContainer
90+ ? createPortal ( region , portalContainer )
8291 : null ;
8392} ) ;
8493
0 commit comments