Autofocus keeps firing inside shadow dom
See original GitHub issueA problem occurs when a focus lock component with autoFocus is rendered inside a shadow root. Component successfully focuses first interactive element, but then repeatedly resets focus to it, when a user tries to focus anything else.
Here is the code of the demo reproducing the issue:
import "./styles.css";
import { StrictMode } from "react";
import ReactDOM from "react-dom";
import ReactFocusLock from "react-focus-lock";
export default function App() {
return (
<ReactFocusLock autoFocus={true}>
<div className="App">
<h1>Hello CodeSandbox</h1>
<input></input>
<input></input>
<h2>Start editing to see some magic happen!</h2>
</div>
</ReactFocusLock>
);
}
const template = document.createElement("template");
template.innerHTML = `
<div>
<p part="title">React attached below</p>
<div id="root"></div>
</div>
`;
export class WebComp extends HTMLElement {
constructor() {
super();
// attach to the Shadow DOM
const root = this.attachShadow({ mode: "closed" });
root.appendChild(template.content.cloneNode(true));
ReactDOM.render(
<StrictMode>
<App />
</StrictMode>,
root
);
}
}
window.customElements.define("web-comp", WebComp);
Issue Analytics
- State:
- Created 2 years ago
- Reactions:4
- Comments:7 (4 by maintainers)
Top Results From Across the Web
Managing focus in the shadow DOM | Read the Tea Leaves
The first issue is irksome but not impossible to solve: you just have to listen for dialog open/close events and keep track of...
Read more >Shadow DOM v1 - Self-Contained Web Components
Handling focus #. If you recall from shadow DOM's event model, events that are fired inside shadow DOM are adjusted to look like...
Read more >Focus inside Shadow DOM - Medium
In this demo, the first two input elements are part of a shadow root, but the last one is not — it's a...
Read more >Shadow DOM - W3C
Shadow DOM specification is being upstreamed to DOM Standard [ WHATWG-DOM ], HTML Standard [ HTML ], CSS Scoping Module Level 1 ...
Read more >Focus an element within a ShadowDOM - Stack Overflow
The problem arises when the window load event is triggered. Focus returns to the main body (verified by issuing document.activeElement in the ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

Here is a test that reliably fails on the current version. If you remove the use of FocusLock component, the test passes.
I had to update the version of jsdom this project is using, because currently installed version of the library does not support shadow DOM yet
The test provided by @Jaodi has been added as a base specification for shadow-dom and
react-focus-lockis currently passing it with the updatedfocus-lockfunctionality provided by @ShermaysterNecessary updates were released as a part of v
2.8.1I cannot verify the correctness of this implementation as I don’t have a real usecase. Please try how the new version matching your expectations.