Skip to content

<FocusScope contain/> doesn't work when rendered into an iframe #3350

Closed

Description

@chungwu

🐛 Bug Report

When rendering something containing <FocusScope contain/> into an iframe, FocusScope has trouble detecting the newly-focused element and goes into an infinite focusing loop.

Here's a repro: https://codesandbox.io/s/react-aria-focusscope-in-iframe-0e6fkj?file=/src/App.tsx

You can focus on one input in the iframe, and as long as you focus on another input, FocusScope will keep switching focus between the two.

The issue seems to be that in FocusScope.tsx, onBlur, it is checking if the newly-focused element is within the focus scope, and if not, returns the focus to the previously-focused node. Unfortunately it is assuming that document.activeElement is the newly-focused element instead of using e.relatedTarget, and document refers to the parent frame and not the child frame, so it incorrectly tries to restore focus to the previously-focused input.

🤔 Expected Behavior

<FocusScope/> continues working in an iframe.

😯 Current Behavior

<FocusScope/> cannot detect focused element across iframes.

💁 Possible Solution

It's possible here to look for activeElement in all the iframes until you find one, instead of directly relying on document.activeElement. But then, I'm not sure if this is even a use case that react-aria is trying to allow 😅 I see many direct references to document in the react-aria code, so they'll all probably break in some way when used across iframes.

🔦 Context

In Plasmic, we're allowing users to register React components that we then use to render in another iframe. This happens when those registered components use <FocusScope contain/>.

💻 Code Sample

https://codesandbox.io/s/react-aria-focusscope-in-iframe-0e6fkj?file=/src/App.tsx

🌍 Your Environment

Software Version(s)
@react-aria/focus 3.6.1

🧢 Your Company/Team

Plasmic

Activity

  1. LFDanLu commented on Jul 28, 2022

    @LFDanLu
    Member

    Thanks for the sandbox, definitely not ideal. I forget exactly why I made this change, perhaps we could use e.relatedTarget instead of document.activeElement

    Perhaps we can use the ownerDocument containing the FocusScope instead

  2. added
    bug
    on Jul 28, 2022
  3. moved this from To Do to To Groom in RSP Component Milestoneson Aug 3, 2022
  4. moved this from To Groom to To Do in RSP Component Milestoneson Aug 3, 2022
  5. moved this from To Do to To Do2 in RSP Component Milestoneson Aug 4, 2022
  6. slye-stripe commented on Sep 13, 2023

    @slye-stripe
    Contributor

    I have a potential solution for this and would like to submit a RFC @LFDanLu

  7. LFDanLu commented on Sep 13, 2023

    @LFDanLu
    Member

    Sounds good, thanks! I'll bring it to the teams attention.

  8. snowystinger commented on Dec 23, 2023

    @snowystinger
    Member

    This appears to have been fixed, I can't reproduce with latest https://codesandbox.io/p/sandbox/react-aria-focusscope-in-iframe-forked-ds3vwm
    Thanks @slye-stripe, I'm guessing one of your PRs fixed it!

  9. moved this from 📋 Waiting for Sprint to ✅ Done in RSP Component Milestoneson Dec 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions