@@ -6,15 +6,11 @@ import {
6
6
assertActiveElement ,
7
7
assertDialog ,
8
8
assertDialogDescription ,
9
- assertDialogOverlay ,
10
9
assertDialogTitle ,
11
10
assertPopoverPanel ,
12
11
DialogState ,
13
12
getByText ,
14
13
getDialog ,
15
- getDialogBackdrop ,
16
- getDialogOverlay ,
17
- getDialogOverlays ,
18
14
getDialogs ,
19
15
getPopoverButton ,
20
16
PopoverState ,
@@ -46,9 +42,7 @@ function TabSentinel(props: PropsOf<'button'>) {
46
42
47
43
describe ( 'Safe guards' , ( ) => {
48
44
it . each ( [
49
- [ 'Dialog.Overlay' , Dialog . Overlay ] ,
50
45
[ 'Dialog.Title' , Dialog . Title ] ,
51
- [ 'Dialog.Backdrop' , Dialog . Backdrop ] ,
52
46
[ 'Dialog.Panel' , Dialog . Panel ] ,
53
47
] ) (
54
48
'should error when we are using a <%s /> without a parent <Dialog />' ,
@@ -66,7 +60,6 @@ describe('Safe guards', () => {
66
60
render (
67
61
< Dialog autoFocus = { false } open = { false } onClose = { console . log } >
68
62
< button > Trigger</ button >
69
- < Dialog . Overlay />
70
63
< Dialog . Title />
71
64
< p > Contents</ p >
72
65
< Dialog . Description />
@@ -553,148 +546,6 @@ describe('Rendering', () => {
553
546
)
554
547
} )
555
548
556
- describe ( 'Dialog.Overlay' , ( ) => {
557
- it (
558
- 'should be possible to render Dialog.Overlay using a render prop' ,
559
- suppressConsoleLogs ( async ( ) => {
560
- let overlay = jest . fn ( ) . mockReturnValue ( null )
561
- function Example ( ) {
562
- let [ isOpen , setIsOpen ] = useState ( false )
563
- return (
564
- < >
565
- < button id = "trigger" onClick = { ( ) => setIsOpen ( ( v ) => ! v ) } >
566
- Trigger
567
- </ button >
568
- < Dialog autoFocus = { false } open = { isOpen } onClose = { setIsOpen } >
569
- < Dialog . Overlay > { overlay } </ Dialog . Overlay >
570
- < TabSentinel />
571
- </ Dialog >
572
- </ >
573
- )
574
- }
575
-
576
- render ( < Example /> )
577
-
578
- assertDialogOverlay ( {
579
- state : DialogState . InvisibleUnmounted ,
580
- attributes : { id : 'headlessui-dialog-overlay-2' } ,
581
- } )
582
-
583
- await click ( document . getElementById ( 'trigger' ) )
584
-
585
- assertDialogOverlay ( {
586
- state : DialogState . Visible ,
587
- attributes : { id : 'headlessui-dialog-overlay-2' } ,
588
- } )
589
- expect ( overlay ) . toHaveBeenCalledWith ( { open : true } )
590
- } )
591
- )
592
- } )
593
-
594
- describe ( 'Dialog.Backdrop' , ( ) => {
595
- it (
596
- 'should throw an error if a Dialog.Backdrop is used without a Dialog.Panel' ,
597
- suppressConsoleLogs ( async ( ) => {
598
- function Example ( ) {
599
- let [ isOpen , setIsOpen ] = useState ( false )
600
- return (
601
- < >
602
- < button id = "trigger" onClick = { ( ) => setIsOpen ( ( v ) => ! v ) } >
603
- Trigger
604
- </ button >
605
- < Dialog autoFocus = { false } open = { isOpen } onClose = { setIsOpen } >
606
- < Dialog . Backdrop />
607
- < TabSentinel />
608
- </ Dialog >
609
- </ >
610
- )
611
- }
612
-
613
- render ( < Example /> )
614
-
615
- try {
616
- await click ( document . getElementById ( 'trigger' ) )
617
-
618
- expect ( true ) . toBe ( false )
619
- } catch ( e : unknown ) {
620
- expect ( ( e as Error ) . message ) . toBe (
621
- 'A <Dialog.Backdrop /> component is being used, but a <Dialog.Panel /> component is missing.'
622
- )
623
- }
624
- } )
625
- )
626
-
627
- it (
628
- 'should not throw an error if a Dialog.Backdrop is used with a Dialog.Panel' ,
629
- suppressConsoleLogs ( async ( ) => {
630
- function Example ( ) {
631
- let [ isOpen , setIsOpen ] = useState ( false )
632
- return (
633
- < >
634
- < button id = "trigger" onClick = { ( ) => setIsOpen ( ( v ) => ! v ) } >
635
- Trigger
636
- </ button >
637
- < Dialog autoFocus = { false } open = { isOpen } onClose = { setIsOpen } >
638
- < Dialog . Backdrop />
639
- < Dialog . Panel >
640
- < TabSentinel />
641
- </ Dialog . Panel >
642
- </ Dialog >
643
- </ >
644
- )
645
- }
646
-
647
- render ( < Example /> )
648
-
649
- await click ( document . getElementById ( 'trigger' ) )
650
- } )
651
- )
652
-
653
- it (
654
- 'should portal the Dialog.Backdrop' ,
655
- suppressConsoleLogs ( async ( ) => {
656
- function Example ( ) {
657
- let [ isOpen , setIsOpen ] = useState ( false )
658
- return (
659
- < >
660
- < button id = "trigger" onClick = { ( ) => setIsOpen ( ( v ) => ! v ) } >
661
- Trigger
662
- </ button >
663
- < Dialog autoFocus = { false } open = { isOpen } onClose = { setIsOpen } >
664
- < Dialog . Backdrop />
665
- < Dialog . Panel >
666
- < TabSentinel />
667
- </ Dialog . Panel >
668
- </ Dialog >
669
- </ >
670
- )
671
- }
672
-
673
- render ( < Example /> )
674
-
675
- await click ( document . getElementById ( 'trigger' ) )
676
-
677
- let dialog = getDialog ( )
678
- let backdrop = getDialogBackdrop ( )
679
-
680
- expect ( dialog ) . not . toBe ( null )
681
- dialog = dialog as HTMLElement
682
-
683
- expect ( backdrop ) . not . toBe ( null )
684
- backdrop = backdrop as HTMLElement
685
-
686
- // It should not be nested
687
- let position = dialog . compareDocumentPosition ( backdrop )
688
- expect ( position & Node . DOCUMENT_POSITION_CONTAINED_BY ) . not . toBe (
689
- Node . DOCUMENT_POSITION_CONTAINED_BY
690
- )
691
-
692
- // It should be a sibling
693
- expect ( position & Node . DOCUMENT_POSITION_FOLLOWING ) . toBe ( Node . DOCUMENT_POSITION_FOLLOWING )
694
- } )
695
- )
696
- } )
697
-
698
549
describe ( 'Dialog.Title' , ( ) => {
699
550
it (
700
551
'should be possible to render Dialog.Title using a render prop' ,
@@ -1134,76 +985,6 @@ describe('Keyboard interactions', () => {
1134
985
} )
1135
986
1136
987
describe ( 'Mouse interactions' , ( ) => {
1137
- it (
1138
- 'should be possible to close a Dialog using a click on the Dialog.Overlay' ,
1139
- suppressConsoleLogs ( async ( ) => {
1140
- function Example ( ) {
1141
- let [ isOpen , setIsOpen ] = useState ( false )
1142
- return (
1143
- < >
1144
- < button id = "trigger" onClick = { ( ) => setIsOpen ( ( v ) => ! v ) } >
1145
- Trigger
1146
- </ button >
1147
- < Dialog autoFocus = { false } open = { isOpen } onClose = { setIsOpen } >
1148
- < Dialog . Overlay />
1149
- Contents
1150
- < TabSentinel />
1151
- </ Dialog >
1152
- </ >
1153
- )
1154
- }
1155
- render ( < Example /> )
1156
-
1157
- // Open dialog
1158
- await click ( document . getElementById ( 'trigger' ) )
1159
-
1160
- // Verify it is open
1161
- assertDialog ( { state : DialogState . Visible } )
1162
-
1163
- // Click to close
1164
- await click ( getDialogOverlay ( ) )
1165
-
1166
- // Verify it is closed
1167
- assertDialog ( { state : DialogState . InvisibleUnmounted } )
1168
- } )
1169
- )
1170
-
1171
- it (
1172
- 'should not close the Dialog when clicking on contents of the Dialog.Overlay' ,
1173
- suppressConsoleLogs ( async ( ) => {
1174
- function Example ( ) {
1175
- let [ isOpen , setIsOpen ] = useState ( false )
1176
- return (
1177
- < >
1178
- < button id = "trigger" onClick = { ( ) => setIsOpen ( ( v ) => ! v ) } >
1179
- Trigger
1180
- </ button >
1181
- < Dialog autoFocus = { false } open = { isOpen } onClose = { setIsOpen } >
1182
- < Dialog . Overlay >
1183
- < button > hi</ button >
1184
- </ Dialog . Overlay >
1185
- Contents
1186
- < TabSentinel />
1187
- </ Dialog >
1188
- </ >
1189
- )
1190
- }
1191
- render ( < Example /> )
1192
-
1193
- // Open dialog
1194
- await click ( document . getElementById ( 'trigger' ) )
1195
-
1196
- // Verify it is open
1197
- assertDialog ( { state : DialogState . Visible } )
1198
-
1199
- // Click on an element inside the overlay
1200
- await click ( getByText ( 'hi' ) )
1201
-
1202
- // Verify it is still open
1203
- assertDialog ( { state : DialogState . Visible } )
1204
- } )
1205
- )
1206
-
1207
988
it (
1208
989
'should be possible to close the dialog, and re-focus the button when we click outside on the body element' ,
1209
990
suppressConsoleLogs ( async ( ) => {
@@ -1273,44 +1054,6 @@ describe('Mouse interactions', () => {
1273
1054
} )
1274
1055
)
1275
1056
1276
- it (
1277
- 'should stop propagating click events when clicking on the Dialog.Overlay' ,
1278
- suppressConsoleLogs ( async ( ) => {
1279
- let wrapperFn = jest . fn ( )
1280
- function Example ( ) {
1281
- let [ isOpen , setIsOpen ] = useState ( true )
1282
- return (
1283
- < div onClick = { wrapperFn } >
1284
- < Dialog autoFocus = { false } open = { isOpen } onClose = { setIsOpen } >
1285
- Contents
1286
- < Dialog . Overlay />
1287
- < TabSentinel />
1288
- </ Dialog >
1289
- </ div >
1290
- )
1291
- }
1292
-
1293
- render ( < Example /> )
1294
-
1295
- await nextFrame ( )
1296
-
1297
- // Verify it is open
1298
- assertDialog ( { state : DialogState . Visible } )
1299
-
1300
- // Verify that the wrapper function has not been called yet
1301
- expect ( wrapperFn ) . toHaveBeenCalledTimes ( 0 )
1302
-
1303
- // Click the Dialog.Overlay to close the Dialog
1304
- await click ( getDialogOverlay ( ) )
1305
-
1306
- // Verify it is closed
1307
- assertDialog ( { state : DialogState . InvisibleUnmounted } )
1308
-
1309
- // Verify that the wrapper function has not been called yet
1310
- expect ( wrapperFn ) . toHaveBeenCalledTimes ( 0 )
1311
- } )
1312
- )
1313
-
1314
1057
it (
1315
1058
'should be possible to submit a form inside a Dialog' ,
1316
1059
suppressConsoleLogs ( async ( ) => {
@@ -1667,7 +1410,6 @@ describe('Mouse interactions', () => {
1667
1410
Trigger
1668
1411
</ button >
1669
1412
< Dialog autoFocus = { false } open = { isOpen } onClose = { setIsOpen } >
1670
- < Dialog . Backdrop />
1671
1413
< Dialog . Panel >
1672
1414
< TabSentinel />
1673
1415
</ Dialog . Panel >
@@ -1700,7 +1442,6 @@ describe('Mouse interactions', () => {
1700
1442
Trigger
1701
1443
</ button >
1702
1444
< Dialog autoFocus = { false } open = { isOpen } onClose = { setIsOpen } >
1703
- < Dialog . Backdrop />
1704
1445
< Dialog . Panel >
1705
1446
< button id = "inside" > Inside</ button >
1706
1447
< TabSentinel />
@@ -1733,7 +1474,6 @@ describe('Mouse interactions', () => {
1733
1474
Trigger
1734
1475
</ button >
1735
1476
< Dialog autoFocus = { false } open = { isOpen } onClose = { setIsOpen } >
1736
- < Dialog . Backdrop />
1737
1477
< Dialog . Panel >
1738
1478
< button id = "inside" > Inside</ button >
1739
1479
< TabSentinel />
@@ -1767,7 +1507,6 @@ describe('Mouse interactions', () => {
1767
1507
</ button >
1768
1508
< div id = "i-am-outside" > this thing</ div >
1769
1509
< Dialog autoFocus = { false } open = { isOpen } onClose = { setIsOpen } >
1770
- < Dialog . Backdrop />
1771
1510
< Dialog . Panel >
1772
1511
< button id = "inside" > Inside</ button >
1773
1512
< TabSentinel />
@@ -1816,8 +1555,6 @@ describe('Nesting', () => {
1816
1555
1817
1556
return (
1818
1557
< Dialog autoFocus = { false } open = { open } onClose = { onClose } >
1819
- < Dialog . Overlay />
1820
-
1821
1558
< div >
1822
1559
< p > Level: { level } </ p >
1823
1560
< button onClick = { ( ) => setShowChild ( true ) } > Open { level + 1 } a</ button >
@@ -1850,12 +1587,11 @@ describe('Nesting', () => {
1850
1587
}
1851
1588
1852
1589
it . each `
1853
- strategy | when | action
1854
- ${ 'with `Escape`' } | ${ 'mounted' } | ${ ( ) => press ( Keys . Escape ) }
1855
- ${ 'with `Outside Click`' } | ${ 'mounted' } | ${ ( ) => click ( document . body ) }
1856
- ${ 'with `Click on Dialog.Overlay`' } | ${ 'mounted' } | ${ ( ) => click ( getDialogOverlays ( ) . pop ( ) ! ) }
1857
- ${ 'with `Escape`' } | ${ 'always' } | ${ ( ) => press ( Keys . Escape ) }
1858
- ${ 'with `Outside Click`' } | ${ 'always' } | ${ ( ) => click ( document . body ) }
1590
+ strategy | when | action
1591
+ ${ 'with `Escape`' } | ${ 'mounted' } | ${ ( ) => press ( Keys . Escape ) }
1592
+ ${ 'with `Outside Click`' } | ${ 'mounted' } | ${ ( ) => click ( document . body ) }
1593
+ ${ 'with `Escape`' } | ${ 'always' } | ${ ( ) => press ( Keys . Escape ) }
1594
+ ${ 'with `Outside Click`' } | ${ 'always' } | ${ ( ) => click ( document . body ) }
1859
1595
` (
1860
1596
'should be possible to open nested Dialog components (visible when $when) and close them $strategy' ,
1861
1597
async ( { when, action } ) => {
0 commit comments