File tree Expand file tree Collapse file tree 3 files changed +51
-1
lines changed
packages/@headlessui-react Expand file tree Collapse file tree 3 files changed +51
-1
lines changed Original file line number Diff line number Diff line change @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
10
10
### Fixed
11
11
12
12
- Don't call ` <Dialog> ` 's ` onClose ` twice on mobile devices ([ #2690 ] ( https://github.com/tailwindlabs/headlessui/pull/2690 ) )
13
+ - Lazily resolve default containers in ` <Dialog> ` ([ #2697 ] ( https://github.com/tailwindlabs/headlessui/pull/2697 ) )
13
14
14
15
## [ 1.7.17] - 2023-08-17
15
16
Original file line number Diff line number Diff line change @@ -1525,6 +1525,43 @@ describe('Mouse interactions', () => {
1525
1525
} )
1526
1526
)
1527
1527
1528
+ // NOTE: This test doesn't actually fail in JSDOM when it's supposed to
1529
+ // We're keeping it around for documentation purposes
1530
+ it (
1531
+ 'should not close the Dialog if it starts open and we click inside the Dialog when it has only a panel' ,
1532
+ suppressConsoleLogs ( async ( ) => {
1533
+ function Example ( ) {
1534
+ let [ isOpen , setIsOpen ] = useState ( true )
1535
+ return (
1536
+ < >
1537
+ < button id = "trigger" onClick = { ( ) => setIsOpen ( ( v ) => ! v ) } >
1538
+ Trigger
1539
+ </ button >
1540
+ < Dialog open = { isOpen } onClose = { ( ) => setIsOpen ( false ) } >
1541
+ < Dialog . Panel >
1542
+ < p id = "inside" > My content</ p >
1543
+ < button > close</ button >
1544
+ </ Dialog . Panel >
1545
+ </ Dialog >
1546
+ </ >
1547
+ )
1548
+ }
1549
+
1550
+ render ( < Example /> )
1551
+
1552
+ // Open the dialog
1553
+ await click ( document . getElementById ( 'trigger' ) )
1554
+
1555
+ assertDialog ( { state : DialogState . Visible } )
1556
+
1557
+ // Click the p tag inside the dialog
1558
+ await click ( document . getElementById ( 'inside' ) )
1559
+
1560
+ // It should not have closed
1561
+ assertDialog ( { state : DialogState . Visible } )
1562
+ } )
1563
+ )
1564
+
1528
1565
it (
1529
1566
'should close the Dialog if we click outside the Dialog.Panel' ,
1530
1567
suppressConsoleLogs ( async ( ) => {
Original file line number Diff line number Diff line change @@ -14,6 +14,7 @@ import React, {
14
14
ContextType ,
15
15
ElementType ,
16
16
MouseEvent as ReactMouseEvent ,
17
+ RefObject ,
17
18
MutableRefObject ,
18
19
Ref ,
19
20
} from 'react'
@@ -210,13 +211,24 @@ function DialogFn<TTag extends ElementType = typeof DEFAULT_DIALOG_TAG>(
210
211
let hasNestedDialogs = nestedDialogCount > 1 // 1 is the current dialog
211
212
let hasParentDialog = useContext ( DialogContext ) !== null
212
213
let [ portals , PortalWrapper ] = useNestedPortals ( )
214
+
215
+ // We use this because reading these values during iniital render(s)
216
+ // can result in `null` rather then the actual elements
217
+ // This doesn't happen when using certain components like a
218
+ // `<Dialog.Title>` because they cause the parent to re-render
219
+ let defaultContainer : RefObject < HTMLElement > = {
220
+ get current ( ) {
221
+ return state . panelRef . current ?? internalDialogRef . current
222
+ } ,
223
+ }
224
+
213
225
let {
214
226
resolveContainers : resolveRootContainers ,
215
227
mainTreeNodeRef,
216
228
MainTreeNode,
217
229
} = useRootContainers ( {
218
230
portals,
219
- defaultContainers : [ state . panelRef . current ?? internalDialogRef . current ] ,
231
+ defaultContainers : [ defaultContainer ] ,
220
232
} )
221
233
222
234
// If there are multiple dialogs, then you can be the root, the leaf or one
You can’t perform that action at this time.
0 commit comments