@@ -81,7 +81,7 @@ export const Dialog: FunctionComponent<OpenDialogOptions> = ({
8181 showArrow = true ,
8282} ) => {
8383 const arrowRef = useRef < HTMLDivElement > ( null ) ;
84- const dialogRef = useRef < HTMLDialogElement > ( null ) ;
84+ const dialogRef = useRef < HTMLElement > ( null ) ;
8585
8686 const anchor = position . type === "POPOVER" ? position . anchor : null ;
8787 const placement =
@@ -173,23 +173,56 @@ export const Dialog: FunctionComponent<OpenDialogOptions> = ({
173173 // eslint-disable-next-line react-hooks/exhaustive-deps -- anchor only exists in popover
174174 } , [ position . type , close , ( position as any ) . anchor , dismiss , containerId ] ) ;
175175
176- function setDiagRef ( node : HTMLDialogElement | null ) {
176+ function setDiagRef ( node : HTMLElement | null ) {
177177 refs . setFloating ( node ) ;
178178 dialogRef . current = node ;
179179 }
180180
181181 useEffect ( ( ) => {
182182 if ( ! dialogRef . current ) return ;
183- if ( isOpen && ! dialogRef . current . hasAttribute ( "open" ) ) {
184- dialogRef . current [ position . type === "MODAL" ? "showModal" : "show" ] ( ) ;
183+
184+ const isPopoverOpen = ( ) => {
185+ try {
186+ return dialogRef . current ?. matches ( ":popover-open" ) ?? false ;
187+ } catch {
188+ return false ;
189+ }
190+ } ;
191+
192+ const isModalOpen =
193+ dialogRef . current instanceof HTMLDialogElement &&
194+ dialogRef . current . hasAttribute ( "open" ) ;
195+
196+ if (
197+ isOpen &&
198+ ( ( position . type === "MODAL" && ! isModalOpen ) ||
199+ ( position . type !== "MODAL" && ! isPopoverOpen ( ) ) )
200+ ) {
201+ if (
202+ position . type === "MODAL" &&
203+ dialogRef . current instanceof HTMLDialogElement
204+ ) {
205+ dialogRef . current . showModal ( ) ;
206+ } else {
207+ dialogRef . current . showPopover ( ) ;
208+ }
185209 }
186- if ( ! isOpen && dialogRef . current . hasAttribute ( "open" ) ) {
187- dialogRef . current . close ( ) ;
210+ if ( ! isOpen ) {
211+ if (
212+ position . type === "MODAL" &&
213+ dialogRef . current instanceof HTMLDialogElement &&
214+ dialogRef . current . hasAttribute ( "open" )
215+ ) {
216+ dialogRef . current . close ( ) ;
217+ } else if ( position . type !== "MODAL" && isPopoverOpen ( ) ) {
218+ dialogRef . current . hidePopover ( ) ;
219+ }
188220 }
189221 } , [ dialogRef , isOpen , position . type ] ) ;
190222
191223 const classes = [
192224 "dialog" ,
225+ isOpen ? "open" : "" ,
193226 position . type === "MODAL"
194227 ? "modal"
195228 : position . type === "POPOVER"
@@ -201,21 +234,40 @@ export const Dialog: FunctionComponent<OpenDialogOptions> = ({
201234 return (
202235 < >
203236 < style dangerouslySetInnerHTML = { { __html : styles } } />
204- < dialog
205- ref = { setDiagRef }
206- class = { classes }
207- style = { anchor ? floatingStyles : unanchoredPosition }
208- >
209- { children && < Fragment > { children } </ Fragment > }
210-
211- { anchor && showArrow && (
212- < DialogArrow
213- arrowData = { middlewareData ?. arrow }
214- arrowRef = { arrowRef }
215- placement = { actualPlacement }
216- />
217- ) }
218- </ dialog >
237+ { position . type === "MODAL" ? (
238+ < dialog
239+ ref = { setDiagRef }
240+ class = { classes }
241+ style = { anchor ? floatingStyles : unanchoredPosition }
242+ >
243+ { children && < Fragment > { children } </ Fragment > }
244+
245+ { anchor && showArrow && (
246+ < DialogArrow
247+ arrowData = { middlewareData ?. arrow }
248+ arrowRef = { arrowRef }
249+ placement = { actualPlacement }
250+ />
251+ ) }
252+ </ dialog >
253+ ) : (
254+ < div
255+ ref = { setDiagRef }
256+ class = { classes }
257+ style = { anchor ? floatingStyles : unanchoredPosition }
258+ popover = "manual"
259+ >
260+ { children && < Fragment > { children } </ Fragment > }
261+
262+ { anchor && showArrow && (
263+ < DialogArrow
264+ arrowData = { middlewareData ?. arrow }
265+ arrowRef = { arrowRef }
266+ placement = { actualPlacement }
267+ />
268+ ) }
269+ </ div >
270+ ) }
219271 </ >
220272 ) ;
221273} ;
0 commit comments