Repository navigation
Commit 511e6d6
fix(focus-scope): trap focus inside a shadow root (#2780)
* fix(focus-scope): trap focus inside a shadow root
FocusScope attaches its focusin/focusout listeners to `document`. When
the scope is teleported into a shadow root, those events are retargeted
to the host, so `container.contains(target)` never matches and focus is
not trapped — Tab escapes to the background.
Resolve the scope's root node via `getRootNode()` (the shadow root when
teleported, otherwise the document) and attach the listeners there.
Related: #1667
* fix(focus-scope): add document focusin fallback for shadow-root escapes
Listening only on the scope's root node misses focus escaping to a
light-DOM element outside the shadow tree: that focusin fires on the
owner document, not the shadow root, so the trap never recalls it.
Keep the root listeners for intra-shadow moves and, when the root is a
shadow root, also listen for focusin on the owner document. That handler
resolves the real target via composedPath() and checks containment with
a new containsComposed() helper that walks up through shadow hosts,
since the event target is retargeted to the host at the document level.
Rewrite the shadow-root test to assert behaviour (focus escaping to a
light-DOM element is recalled into the scope) instead of spying on
addEventListener, and clean up the host/listener leaks it left behind.
Resolve the root via an explicit ternary and fall back to
container.ownerDocument for iframe consistency.
* test(focus-scope): keep shadow-root suite clear of the #2886 test region
Both this PR and #2888 appended a describe block to the end of
FocusScope.test.ts and extended adjacent import lines, so the two
changes collided on merge. Move the shadow-root suite ahead of the
#2550 block (keeping issue numbers ascending) and scope its DOM
cleanup to the test body, so it no longer touches the same lines.
---------
Co-authored-by: Qudsia Mehmood <qudsia.mehmood@open-xchange.com>1 parent 2fbeade commit 511e6d6
3 files changed
Lines changed: 99 additions & 4 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
149 | 149 | | |
150 | 150 | | |
151 | 151 | | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
152 | 210 | | |
153 | 211 | | |
154 | 212 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
51 | 51 | | |
52 | 52 | | |
53 | 53 | | |
| 54 | + | |
54 | 55 | | |
55 | 56 | | |
56 | 57 | | |
| |||
92 | 93 | | |
93 | 94 | | |
94 | 95 | | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
95 | 104 | | |
96 | 105 | | |
97 | 106 | | |
| |||
164 | 173 | | |
165 | 174 | | |
166 | 175 | | |
167 | | - | |
168 | | - | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
169 | 193 | | |
170 | 194 | | |
171 | 195 | | |
172 | 196 | | |
173 | 197 | | |
174 | | - | |
175 | | - | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
176 | 202 | | |
177 | 203 | | |
178 | 204 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
9 | 20 | | |
10 | 21 | | |
11 | 22 | | |
| |||
0 commit comments