Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[All] Ensure all document handlers are attached to ownerDocument #1721

Open
benoitgrelard opened this issue Oct 14, 2022 · 10 comments · May be fixed by #3357
Open

[All] Ensure all document handlers are attached to ownerDocument #1721

benoitgrelard opened this issue Oct 14, 2022 · 10 comments · May be fixed by #3357
Labels

Comments

@benoitgrelard
Copy link
Contributor

Feature request

Overview

Currently, wherever we attach document handlers, we do so at the global document level.
Following up from #1677 where it was patched only for DismissableLayer we need to find a solution to do this throughout the entire library.

Additional context

Here's another recent discussion which demonstrates similar issues with tooltips rendered in iframes: #1715

@hugotiger
Copy link

hugotiger commented Oct 26, 2022

We're facing some issues similar to this when rendering within an iframe. Our current problem is that the Dialog.Portal is rendering within the root document instead of in the nearest document (we're running it in same-origin on localhost).

Shouldn't the Portal's default behaviour be to attach it to the nearest body node, in this case the iframe's? I'm not entirely sure what should be the behaviour here since I only have limited experience with iframes 😅

@ritz078
Copy link

ritz078 commented Dec 19, 2022

We are stuck because of this so I opened a PR. It includes some of the important components

@ritz078
Copy link

ritz078 commented Mar 16, 2023

Is there any update on this issue ?

@joaodematte
Copy link

any updates?

@LordZardeck
Copy link

Chiming in on this, we are having a similar issue. We are running react on our main document, but then portaling into an iframe some other react components, including some radix primitives. Everything works except the focus guard is being put onto the main document, not the iframe, and I'm assuming event listeners as well. This is a pretty major issue for us as it means we can't use radix at all for iframe portaled components, and that severely breaks our UI consistency.

@william-will-angi
Copy link

Has anyone found a patch to make this work (even it's just a workaround)?

@gregorybolkenstijn
Copy link

We've run into this as well in our project. A solution or workaround would be greatly appreciated.

@2DTW
Copy link

2DTW commented Nov 14, 2024

Hitting the same issue. Has anyone found a solution or a workaround?

@hugotiger
Copy link

hugotiger commented Nov 27, 2024

Here's the solution we went with:

import * as DialogPrimitive from '@radix-ui/react-dialog'
import { useState } from 'react'

import { useSafeLayoutEffect } from '../../../hooks/use-safe-layout-effect'

type DialogPortalProps = {
  children: React.ReactNode
}
/**
 * This component wraps Radix's Portal implementation to handle SSR issues
 * as well as portalling to the wrong body element when rendering in an iframe.
 */
export function DialogPortal(props: DialogPortalProps) {
  const { children } = props
  const [tempNode, setTempNode] = useState<HTMLElement | null>(null)
  const [portalNode, setPortalNode] = useState<HTMLElement | null>(null)

  useSafeLayoutEffect(() => {
    if (!tempNode) return

    const portalNode = tempNode.ownerDocument.body

    setPortalNode(portalNode)
  }, [tempNode])

  return portalNode ? (
    <DialogPrimitive.Portal container={portalNode}>
      {children}
    </DialogPrimitive.Portal>
  ) : (
    <span
      ref={(el) => {
        if (el) setTempNode(el)
      }}
    />
  )
}

Very long since I wrote the code so maybe it could be simplified a little. But hopefully it's helpful to someone!

@seleckis
Copy link

seleckis commented Dec 14, 2024

@hugotiger Yout solution just sets container, this can be done separately. But current issue is about document event handlers inside iframes.

Still waiting for fix

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
9 participants