@@ -18,6 +18,7 @@ import {forwardRefType} from '@react-types/shared';
18
18
import { QueuedToast , ToastQueue , ToastState , useToastQueue } from 'react-stately' ;
19
19
import React , { createContext , ForwardedRef , forwardRef , HTMLAttributes , JSX , ReactElement , useContext } from 'react' ;
20
20
import { TextContext } from './Text' ;
21
+ import { useIsSSR } from '@react-aria/ssr' ;
21
22
import { useObjectRef } from '@react-aria/utils' ;
22
23
23
24
const ToastStateContext = createContext < ToastState < any > | null > ( null ) ;
@@ -41,13 +42,19 @@ export interface ToastRegionProps<T> extends AriaToastRegionProps, StyleRenderPr
41
42
/** The queue of toasts to display. */
42
43
queue : ToastQueue < T > ,
43
44
/** 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
45
51
}
46
52
47
53
/**
48
54
* A ToastRegion displays one or more toast notifications.
49
55
*/
50
56
export const ToastRegion = /*#__PURE__*/ ( forwardRef as forwardRefType ) ( function ToastRegion < T > ( props : ToastRegionProps < T > , ref : ForwardedRef < HTMLDivElement > ) : JSX . Element | null {
57
+ let isSSR = useIsSSR ( ) ;
51
58
let state = useToastQueue ( props . queue ) ;
52
59
let objectRef = useObjectRef ( ref ) ;
53
60
let { regionProps} = useToastRegion ( props , state , objectRef ) ;
@@ -64,6 +71,8 @@ export const ToastRegion = /*#__PURE__*/ (forwardRef as forwardRefType)(function
64
71
}
65
72
} ) ;
66
73
74
+ let { portalContainer = isSSR ? null : document . body } = props ;
75
+
67
76
let region = (
68
77
< ToastStateContext . Provider value = { state } >
69
78
< div
@@ -77,8 +86,8 @@ export const ToastRegion = /*#__PURE__*/ (forwardRef as forwardRefType)(function
77
86
</ ToastStateContext . Provider >
78
87
) ;
79
88
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 )
82
91
: null ;
83
92
} ) ;
84
93
0 commit comments