Repository navigation
Description
🙋 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 theI18nProviderworks. - The second option is to pass the
ownerDocumentin every hook that uses document/window, making the API easier to use without any breaking changes. Additionally, if a custom window is desired, theownerDocument.defaultViewcan be used.
usePopover({
// If not passed the default document is used.
ownerDocument?: Document
})- The third option is to just deduce the
ownerDocumentusing 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
[-]Accept document from outside[/-][+]Accept ownerDocument from outside[/+]reidbarber commentedon Jun 5, 2023
Adding an option to every hook seems excessive. I think the
DocumentProvidersolution 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.ritz078 commentedon Jun 5, 2023
Thank you for the quick response. That definitely makes sense.
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),
ariakitetc. support this.If there's a positive response, I will be happy to work on a PR.
diegohaz commentedon Jun 14, 2023
Ariakit does support this: https://codesandbox.io/s/ariakit-popover-inside-iframe-wvqrq6
ritz078 commentedon Jun 14, 2023
@diegohaz Thanks for correcting me.
ritz078 commentedon Jun 14, 2023
@reidbarber There is a third approach that changes nothing for the user -- We can get the
documentfrom the ref passed to hooks usingref.ownerDocument. This is the approach being used by ariakit. I didn't suggest this earlier because I am not sure if all the hooks usingdocumenthave access to a ref or not but thinking more about it, I think they do so this becomes the easiest change.[-]Accept ownerDocument from outside[/-][+]Use the correct ownerDocument[/+]ritz078 commentedon Jun 22, 2023
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.