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.

Example with popper + portal + transition

See original GitHub issue

Could we get an example of usage with popper and transition in portal?

This example is helpful: https://github.com/tailwindlabs/headlessui/blob/develop/packages/%40headlessui-react/pages/menu/menu-with-transition-and-popper.tsx

However, it doesn’t work when we put Portal into play. Just wrapping with Portal will either remove transitions or the dropdown will disappear after the transition depending on what you wrap inside of your Portal.

I kinda made it work (using their own hook) with code like this:

<Portal>
  <Transition
    show={isOpen}
    enter="transition ease-out duration-100"
    enterFrom="transform opacity-0 scale-95"
    enterTo="transform opacity-100 scale-100"
    leave="transition ease-in duration-750"
    leaveFrom="transform opacity-100 scale-100"
    leaveTo="transform opacity-0 scale-95"
  >
    {(ref) => (
      <div
        ref={setPopperElement}
        {...popperProps}
      >
        <div
          ref={ref}
        >
          {children}
        </div>
      </div>
    )}
  </Transition>
</Portal>

It animates enter, but it fails to animate leave though. Also, this is only a custom dropdown element, it becomes even more complicated and fragile with Listbox or Menu. Similar approach worked for Menu, but for Listbox, even enter animations did not work until I added transform opacity-0 scale-95 to className for Listbox.Options in addition to Transition, which is super unintuitive.

Can we get a proper example? Maybe some errors will get fixed in the process too

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6

github_iconTop GitHub Comments

23reactions
vincasltcommented, Dec 9, 2020

Ok, I figured it out. Here is a working example adapted from Transition+Menu example here:

https://codesandbox.io/s/headlessuireact-transitionmenupopper-example-forked-zl7zg

It is using the dev version of headlessui, because I needed callbacks for beforeEnter and afterLeave. I also used the official react-popper hook, as opposed to what other examples in headlessui use - you’d probably want to use an offical hook anyway.

Main changes are:

  • Wrap transition inside a portal and add a div wrapper with ref (because it must be in dom before transition)
  • In Transition callbacks set the popper element refs in state to the wrapper ref (avoids animation jankiness)
  • Use static prop on listbox and menu items!
  • Position is calculated automatically now by react-popper, so just pass style props to respective elements, do not use relative or absolute positioning on dropdown elements anymore.

The example might be simplified, but it’s a good working base for implementation. @RobinMalfait could be a good thing to add to docs

5reactions
ycs77commented, Mar 15, 2022

@vincaslt Thanks for the example.

Your example inspired me to create a package Headless UI Float, that can be easy to position floating Headless UI elements using Floating UI, support Transition & Portal.

The example below is a fork from your example, but replace Popper.js with my created package: https://codesandbox.io/s/headlessui-react-transition-menu-floating-ui-example-forked-sv28um?file=/src/App.js

Read more comments on GitHub >

github_iconTop Results From Across the Web

headlessui/react Transition+Menu+Popper Example (forked)
@headlessui/react Transition+Menu+Popper Example (forked) - CodeSandbox. You need to enable JavaScript to run this app.
Read more >
React Popper Example - StackBlitz
Starter project for React apps that exports to the create-react-app CLI.
Read more >
React Popper component - Material UI - MUI
The open/close state of the popper can be animated with a render prop child and a transition component. This component should respect the...
Read more >
react-popper usePopper TypeScript Examples
react-popper#usePopper TypeScript Examples ; usePopper(referenceElement, popperElement, { placement: placements[placement], modifiers, strategy, }); if ; Array.
Read more >
React Portals - Popper
createPortal (or any pre React 16 alternative) to move the popper component ... import { usePopper } from 'react-popper'; const Example = ()...
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