Commit fe9ec17
authored
Improve focus management in shadow roots (#3794)
This PR improves the focus management when Headless UI components are
used inside of a shadow DOM.
We already tried to not use `document` directly, but instead rely on
`someKnownElement.ownerDocument` to get the correct document context.
This is unfortunately not enough, because if you have a focused element
_inside_ the shadow DOM, then `ownerDocument.activeElement` will return
the wrapping shadow DOM node and not the actual focused element inside
of the shadow DOM.
This PR improves that behavior by using `getRootNode()` instead and then
we read information such as the `activeElement` from there.
It's going to be easier if you look at this reproduction. The element
with the black border is a shadow DOM element. The `toggle` button is a
Popover button.
Expected behavior:
1. You can tab through the elements in order from outside the shadow DOM
into the shadow DOM and out again.
1. You can shift+tab in reverse order as well.
1. When the Popover is open, pressing `Escape` should close the Popover.
1. When tabbing outside of the open popover, the popover should close.
Let's see these things in action:
---
1. Pressing escape on an open Popover should close it
**before:**
https://github.com/user-attachments/assets/99ff5768-f6ac-490f-a607-391bbdd21d65
The reason it doesn't is because we check the `activeElement`, and we
only close the popover if it is the active element. But if you look at
devtools, the button is not the active element so therefore it doesn't
work.
**after:**
https://github.com/user-attachments/assets/22511835-98d3-452d-baac-447fa4e5a5e7
---
2. Tabbing out of an open popover with <kbd>Tab</kbd> should close the
Popover and focus the next element.
**before:**
https://github.com/user-attachments/assets/ab2c5849-f524-436b-aa7c-9e65e5debc67
Here it does all the wrong things, it doesn't go into the PopoverPanel,
it doesn't go to the next button, but it goes to the next button
_outside_ of the shadow dom.
**after:**
https://github.com/user-attachments/assets/c5f653f1-338d-41c4-a166-735e6c1d1435
---
## Test plan
1. Existing code still passes (we don't have shadow DOM tests)
2. Tested the behavior from the reproduction and it all works as
expected now:
https://github.com/user-attachments/assets/a90206e6-ae5e-4748-a4e4-c65dfa69d8f6
Fixes: #29511 parent 8759a5c commit fe9ec17
File tree
18 files changed
+107
-84
lines changed- packages/@headlessui-react
- src
- components
- combobox
- dialog
- disclosure
- focus-trap
- listbox
- popover
- portal
- radio-group
- tabs
- hooks
- test-utils
- utils
18 files changed
+107
-84
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
10 | | - | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
11 | 13 | | |
12 | 14 | | |
13 | 15 | | |
| |||
Lines changed: 3 additions & 5 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
67 | 67 | | |
68 | 68 | | |
69 | 69 | | |
| 70 | + | |
70 | 71 | | |
71 | 72 | | |
72 | 73 | | |
| |||
543 | 544 | | |
544 | 545 | | |
545 | 546 | | |
546 | | - | |
547 | | - | |
548 | 547 | | |
549 | 548 | | |
550 | 549 | | |
551 | 550 | | |
552 | 551 | | |
553 | 552 | | |
554 | 553 | | |
555 | | - | |
556 | 554 | | |
557 | 555 | | |
558 | 556 | | |
| |||
628 | 626 | | |
629 | 627 | | |
630 | 628 | | |
631 | | - | |
| 629 | + | |
632 | 630 | | |
633 | 631 | | |
634 | 632 | | |
| |||
642 | 640 | | |
643 | 641 | | |
644 | 642 | | |
645 | | - | |
| 643 | + | |
646 | 644 | | |
647 | 645 | | |
648 | 646 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
160 | 160 | | |
161 | 161 | | |
162 | 162 | | |
163 | | - | |
| 163 | + | |
164 | 164 | | |
165 | 165 | | |
166 | 166 | | |
| |||
Lines changed: 1 addition & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
206 | 206 | | |
207 | 207 | | |
208 | 208 | | |
209 | | - | |
| 209 | + | |
210 | 210 | | |
211 | 211 | | |
212 | 212 | | |
| |||
Lines changed: 3 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
25 | 25 | | |
26 | 26 | | |
27 | 27 | | |
| 28 | + | |
28 | 29 | | |
29 | 30 | | |
30 | 31 | | |
| |||
108 | 109 | | |
109 | 110 | | |
110 | 111 | | |
111 | | - | |
| 112 | + | |
112 | 113 | | |
113 | 114 | | |
114 | 115 | | |
| |||
288 | 289 | | |
289 | 290 | | |
290 | 291 | | |
291 | | - | |
| 292 | + | |
292 | 293 | | |
293 | 294 | | |
294 | 295 | | |
| |||
Lines changed: 2 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
71 | 71 | | |
72 | 72 | | |
73 | 73 | | |
74 | | - | |
| 74 | + | |
75 | 75 | | |
76 | 76 | | |
77 | 77 | | |
| |||
666 | 666 | | |
667 | 667 | | |
668 | 668 | | |
669 | | - | |
| 669 | + | |
670 | 670 | | |
671 | 671 | | |
672 | 672 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
60 | 60 | | |
61 | 61 | | |
62 | 62 | | |
| 63 | + | |
63 | 64 | | |
64 | 65 | | |
65 | 66 | | |
| |||
453 | 454 | | |
454 | 455 | | |
455 | 456 | | |
456 | | - | |
| 457 | + | |
457 | 458 | | |
458 | 459 | | |
459 | | - | |
| 460 | + | |
460 | 461 | | |
461 | 462 | | |
462 | 463 | | |
| |||
Lines changed: 5 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
| 7 | + | |
7 | 8 | | |
8 | 9 | | |
9 | 10 | | |
| |||
142 | 143 | | |
143 | 144 | | |
144 | 145 | | |
| 146 | + | |
| 147 | + | |
145 | 148 | | |
146 | 149 | | |
147 | 150 | | |
148 | | - | |
| 151 | + | |
149 | 152 | | |
150 | 153 | | |
151 | 154 | | |
| |||
157 | 160 | | |
158 | 161 | | |
159 | 162 | | |
160 | | - | |
| 163 | + | |
161 | 164 | | |
162 | 165 | | |
163 | 166 | | |
| |||
Lines changed: 24 additions & 26 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
28 | 28 | | |
29 | 29 | | |
30 | 30 | | |
31 | | - | |
| 31 | + | |
32 | 32 | | |
33 | 33 | | |
34 | 34 | | |
| |||
70 | 70 | | |
71 | 71 | | |
72 | 72 | | |
73 | | - | |
| 73 | + | |
74 | 74 | | |
75 | 75 | | |
76 | 76 | | |
| |||
156 | 156 | | |
157 | 157 | | |
158 | 158 | | |
159 | | - | |
| 159 | + | |
160 | 160 | | |
161 | 161 | | |
162 | 162 | | |
| |||
173 | 173 | | |
174 | 174 | | |
175 | 175 | | |
| 176 | + | |
| 177 | + | |
176 | 178 | | |
177 | 179 | | |
178 | | - | |
179 | | - | |
180 | | - | |
| 180 | + | |
181 | 181 | | |
182 | 182 | | |
183 | 183 | | |
| |||
204 | 204 | | |
205 | 205 | | |
206 | 206 | | |
207 | | - | |
| 207 | + | |
208 | 208 | | |
209 | 209 | | |
210 | 210 | | |
| |||
392 | 392 | | |
393 | 393 | | |
394 | 394 | | |
395 | | - | |
396 | 395 | | |
397 | 396 | | |
398 | 397 | | |
| |||
426 | 425 | | |
427 | 426 | | |
428 | 427 | | |
429 | | - | |
430 | | - | |
431 | | - | |
432 | | - | |
| 428 | + | |
| 429 | + | |
433 | 430 | | |
434 | 431 | | |
435 | 432 | | |
| |||
535 | 532 | | |
536 | 533 | | |
537 | 534 | | |
538 | | - | |
| 535 | + | |
| 536 | + | |
| 537 | + | |
539 | 538 | | |
540 | 539 | | |
541 | 540 | | |
| |||
749 | 748 | | |
750 | 749 | | |
751 | 750 | | |
752 | | - | |
| 751 | + | |
753 | 752 | | |
754 | 753 | | |
755 | 754 | | |
| |||
777 | 776 | | |
778 | 777 | | |
779 | 778 | | |
780 | | - | |
781 | | - | |
782 | | - | |
783 | | - | |
| 779 | + | |
| 780 | + | |
784 | 781 | | |
785 | 782 | | |
786 | 783 | | |
| |||
807 | 804 | | |
808 | 805 | | |
809 | 806 | | |
810 | | - | |
| 807 | + | |
811 | 808 | | |
812 | 809 | | |
813 | 810 | | |
| |||
889 | 886 | | |
890 | 887 | | |
891 | 888 | | |
892 | | - | |
| 889 | + | |
| 890 | + | |
893 | 891 | | |
894 | 892 | | |
895 | 893 | | |
| |||
1015 | 1013 | | |
1016 | 1014 | | |
1017 | 1015 | | |
1018 | | - | |
1019 | | - | |
1020 | | - | |
| 1016 | + | |
| 1017 | + | |
| 1018 | + | |
1021 | 1019 | | |
1022 | | - | |
| 1020 | + | |
1023 | 1021 | | |
1024 | 1022 | | |
1025 | 1023 | | |
1026 | 1024 | | |
1027 | | - | |
1028 | | - | |
| 1025 | + | |
| 1026 | + | |
1029 | 1027 | | |
1030 | 1028 | | |
1031 | 1029 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
89 | 89 | | |
90 | 90 | | |
91 | 91 | | |
92 | | - | |
| 92 | + | |
93 | 93 | | |
94 | 94 | | |
95 | 95 | | |
| |||
0 commit comments