focus-trap-react: Doesn't seem to work in other documents
I appreciate the effort you have put into making the focus trap library work in iframes and child windows, but unfortunately it seems like maybe the React version is not prepared for this?
I’m using the FocusTrap like <FocusTrap active={element !== null} focusTrapOptions={{document}}>, where document is the document of the child window (derived from element.ownerDocument), but the focus trap still isn’t trapping focus. When the same component is used in the main document, it works just fine.
Do you know what could be the cause of the issue?
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Comments: 25 (15 by maintainers)
Commits related to this issue
- [#539] Fix incorrect reference to document Code was accessing `document` global instead of first checking for the `focusTrapOptions.document` setting. — committed to focus-trap/focus-trap-react by stefcameron 3 years ago
- [#539] Fix incorrect reference to document Code was accessing `document` global instead of first checking for the `focusTrapOptions.document` setting. — committed to focus-trap/focus-trap-react by stefcameron 3 years ago
- [#539] Fix incorrect reference to document (#540) Code was accessing `document` global instead of first checking for the `focusTrapOptions.document` setting. — committed to focus-trap/focus-trap-react by stefcameron 3 years ago
- [#539] Delay trap creation until it should be active Fixes #539 — committed to focus-trap/focus-trap-react by stefcameron 3 years ago
- [#539] Delay trap creation until it should be active (#546) Fixes #539 — committed to focus-trap/focus-trap-react by stefcameron 3 years ago
That’s true. Then rendering with
<FocusTrap>could cause the div to change, causing the ref to change, and I guess it could be a different document at that point, though that seems unlikely. Still, it wouldn’t be totally “clean” to assume it’s the same document.I’ll give this some thought. It’s a significant change in terms of when the trap gets created. I’ll have to check focus-trap to see if creating the trap triggers any events for which you might expect a callback in one of the options, in which case, you wouldn’t get the callback at the same time anymore.
If that turns out to be a problem, then another option might be to promote the
documentoption to a prop (instead of nesting it infocusTrapOptionswhere there’s no “notification” if it changes) and then re-create the trap if that prop changes. Then we could maintain existing behavior and only have changes affect changing thedocument.So something like
componentDidMount(), only callthis.setupFocusTrap()ifthis.props.activeis true, and then incomponentDidUpdate(), check if(!prevProps.active && this.props.active)and callthis.setupFocusTrap()(if it doesn’t already exist).