question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`SSRProvider` creates issues with conditionally-rendered `OverlayContainer`s

See original GitHub issue

🐛 Bug Report

When wrapping an app with SSRProvider, conditionally-rendered overlays don’t display correctly on the first display. If you show one based on state.isOpen from a trigger, the rendering is odd the first go. In my app’s case, the tooltip that should be rendered stays offscreen until mousing away, then the overlay flashes on and off in a second. Hovering over the same element again, the overlay displays correctly.

I created a sandbox that shows a similar problem. In the sandbox, the overlay shows, but the offset is being ignored.

These issues only show up when the app is wrapped in an SSRProvider. If the provider is removed, the overlays display as expected, but then the app complains about SSR and IDs.

I can side-step the issue by conditionally rendering the content inside the OverlayContainers, but this results in hundreds of portals sitting at the bottom of body.

🤔 Expected Behavior

I expect overlays to display correctly even when the app is wrapped in an SSRProvider.

😯 Current Behavior

Overlays exhibit odd behavior, sometimes not showing at all, or having odd positionProps.style values when wrapped with SSRProvider.

🔦 Context

I’m building a Remix app that would seemingly require SSRProvider and I have lots of menus and tooltips that need to be in OverlayContainers because of overflowing/scrolling parents.

💻 Code Sample

https://codesandbox.io/s/react-spectrum-template-forked-9io4uj?file=/src/index.js

🌍 Your Environment

Software Version(s)
react-aria 3.19.0
react-stately 3.17.0
Chrome 103
macOS 12.5.1

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:5
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

4reactions
rk-lunarcommented, Sep 9, 2022

Update: 2022-09-09. After playing around today a bit more, I was able to get an OverlayContainer to conditionally render when triggered off of useMenuTrigger by moving the OverlayContainer inside the Popover component and then conditionally rendering the Popover. Works as expected.

Tried the same with my Tooltip, but no matter what I do, the Tooltip (using OverlayContainer) won’t position itself when conditionally rendered and the OverlayContainer is inside the conditionally rendered component. If I conditionally render inside the OverlayContainer, things look fine, but there are a hundred OverlayContainers in the body. Also, if I remove the SSRProvider, everything works as expected, but React Aria yells at me about IDs not matching.

I’ve made it work by manually handling the ref in a callback, and then calling updatePostion which is returned from the useOverlayPosition hook.

    const popoverRefCallback = useCallback(
      (ref: HTMLDivElement) => {
        popoverRef.current = ref;
        updatePosition();
      },
      [popoverRef, updatePosition]
    );
3reactions
rk-lunarcommented, Sep 8, 2022

We have the same problem, and i found this issue which might be related to the problem.

https://github.com/adobe/react-spectrum/issues/3293

Read more comments on GitHub >

github_iconTop Results From Across the Web

OverlayContainer createPortal(contents, document.body) ...
Perhaps you could conditionally render your Overlay only when your Modal is open?
Read more >
useOverlayTrigger – React Aria - React Spectrum Libraries
Props for the overlay container element. State is managed by the. useOverlayTriggerState. hook in @react-stately/overlays . The state object should be ...
Read more >
document is not defined in Next.js while working with React ...
Use it to conditionally render your OverlayContainer . import { useRef } from "react"; import { useOverlay, useModal, OverlayContainer } ...
Read more >
Conditional Rendering
In React, you can create distinct components that encapsulate behavior you need. Then, you can render only some of them, depending on the...
Read more >
Conditional React Hooks
So if we conditionally render a Hook, for instance, ... Hook in order to hide an overlay when the user clicks outside of...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found