Skip to content

Use the correct ownerDocument #4634

Open

Description

@ritz078

🙋 Feature Request

The codebase has a lot of references of document. The problem of using document is that it causes a lot of issues when the the component is dynamically added to an iframe. This technique is different from loading a separate URL in an iframe. See https://dev.to/graftini/rendering-in-an-iframe-in-a-react-app-2boa for more details about this approach.

This technique is generally useful for libraries because:

  • You can keep your styling isolated
  • You can keep your events listeners isolated and
  • You can accept any JS as a library option which would not have been possible if you loaded a separate HTML.

🤔 Expected Behavior

react-aria should accept document from outside.

😯 Current Behavior

The codebase doesn't accept a custom ownerDocument

💁 Possible Solution

There are three ways to accomplish this:

  • The first option is to implement it at the hooks level or by passing the document to a DocumentProvider, similar to how the I18nProvider works.
  • The second option is to pass the ownerDocument in every hook that uses document/window, making the API easier to use without any breaking changes. Additionally, if a custom window is desired, the ownerDocument.defaultView can be used.
usePopover({
  // If not passed the default document is used.
  ownerDocument?: Document
})
  • The third option is to just deduce the ownerDocument using the ref used in the hooks. This means that nothing changes in the public API and correct document is always used. This is the simplest solution.

🔦 Context

We have a library with its own user interface (UI) that can be placed within any webpage. To ensure isolation of events and styling, we render the React component inside an iframe. This approach reduces the likelihood of bugs caused by conflicting customer code. Since certain options in the software development kit (SDK) cannot be serialized and passed into an iframe, we cannot load a separate HTML page to handle those options. Consequently, dynamically rendering the React component is the only viable solution for utilizing these options.

💻 Examples

Unanswered discussion: #2237 similar to this.
Open issue: #3350

I have opened this issue because the issue is not limited to FocusScope

Popover suffers similar issue where it calculates the popover positioning related to main window.

Kapture.2023-06-05.at.20.59.19.mp4

PR

I will be happy to open a PR and avoid maintaining fork. The API can be gradually implemented and it doesn't have to be one big PR.

Activity

  1. changed the title [-]Accept document from outside[/-] [+]Accept ownerDocument from outside[/+] on Jun 5, 2023
  2. reidbarber commented on Jun 5, 2023

    @reidbarber
    Member

    Adding an option to every hook seems excessive. I think the DocumentProvider solution could be non-breaking if it is opt-in, and the default behavior is how it works now. I think whether or not this is something we want would need some discussion by the team first, however.

  3. ritz078 commented on Jun 5, 2023

    @ritz078
    ContributorAuthor

    Thank you for the quick response. That definitely makes sense.

    I think whether or not this is something we want would need some discussion by the team first, however.

    Sure. I will wait for your response. We have already done half of our design system and then ran into this issue which makes this super important for us. Also, no other libraries like radix (there is partial support in few components), ariakit etc. support this.

    If there's a positive response, I will be happy to work on a PR.

  4. diegohaz commented on Jun 14, 2023

    @diegohaz

    Also, no other libraries like radix (there is partial support in few components), ariakit etc. support this.

    Ariakit does support this: https://codesandbox.io/s/ariakit-popover-inside-iframe-wvqrq6

  5. ritz078 commented on Jun 14, 2023

    @ritz078
    ContributorAuthor

    @diegohaz Thanks for correcting me.

  6. ritz078 commented on Jun 14, 2023

    @ritz078
    ContributorAuthor

    @reidbarber There is a third approach that changes nothing for the user -- We can get the document from the ref passed to hooks using ref.ownerDocument. This is the approach being used by ariakit. I didn't suggest this earlier because I am not sure if all the hooks using document have access to a ref or not but thinking more about it, I think they do so this becomes the easiest change.

  7. changed the title [-]Accept ownerDocument from outside[/-] [+]Use the correct ownerDocument[/+] on Jun 21, 2023
  8. ritz078 commented on Jun 22, 2023

    @ritz078
    ContributorAuthor

    I am working on a PR using the Provider approach because other approaches won't work. I am aware that it hasn't been decided that the PR will be accepted or not but I have to work on it anyways because of our requirement. I feel it will be better if I opened a draft PR anyways.

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions