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.

Can't get access to ref on Transition.Child or Transition

See original GitHub issue

I’m trying to make a modal utility in React, and I wаnt to add code that allows clicking outside of the modal.

<Transition
              show={visible}
              className={clsx("fixed z-10 inset-0 overflow-y-auto")}
            >
              <div className="flex items-end justify-center msin-h-screen pt-4 px-4 pb-20 text-center sm:block sm:p-0">
                <Transition.Child
                  enter="ease-out duration-300"
                  enterFrom="opacity-0"
                  enterTo="opacity-100"
                  leave="ease-in duration-200"
                  leaveFrom="opacity-100"
                  leaveTo="opacity-0"
                  className="fixed inset-0 transition-opacity"
                >
                  <div aria-hidden="true">
                    <div className="absolute inset-0 bg-gray-500 opacity-75"></div>
                  </div>
                </Transition.Child>

                {/* This element is to trick the browser into centering the modal contents. */}
                <span
                  className="hidden sm:inline-block sm:align-middle sm:h-screen"
                  aria-hidden="true"
                >
                  &#8203;
                </span>
                <Transition.Child
                  enter="ease-out duration-300"
                  enterFrom="opacity-0 translate-y-4 sm:translate-y-0 sm:scale-95"
                  enterTo="opacity-100 translate-y-0 sm:scale-100"
                  leave="ease-in duration-200"
                  leaveFrom="opacity-100 translate-y-0 sm:scale-100"
                  leaveTo="opacity-0 translate-y-4 sm:translate-y-0 sm:scale-95"
                  className="inline-block align-bottom bg-white rounded-lg text-left overflow-hidden shadow-xl transform transition-all sm:my-8 sm:align-middle sm:max-w-lg sm:w-full"
                  role="dialog"
                  aria-modal="true"
                  aria-labelledby="modal-headline"
                >
...

What I wanted to do was attach a ref to my child so that I can detect whether or not the user has clicked outside the modal. This is however not possible, and I think it should be, as the <Transition.Child> creates a div for me. I’d like to be able to referece that div:

function MyComponent() {
   const ref = useRef(null);
   
   useEffect(() => {
    function handleClickOutside(event) {
      if (ref.current && !ref.current.contains(event.target)) {
          // user has clicked outside of modal...
      }
    }

    document.addEventListener("mousedown", handleClickOutside);
    return () => {
      document.removeEventListener("mousedown", handleClickOutside);
    };
  }, [ref]);
   
   return <Transition><Transition.Child ref={ref}>...</Transition.Child></Transition>
}

This doesn’t work, because Transition.Child and Transition components don’t forward refs to the div.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:8
  • Comments:9

github_iconTop GitHub Comments

10reactions
RobinMalfaitcommented, Mar 26, 2021

Possible solutions:

  1. Use the Dialog component to have this functionality, alongside additional relevant functionality for modals, for free
  2. Use the ref that gets exposed by Transition and prevent it from rendering its own div, like so:
<Transition show={open} as={React.Fragment}>
      {(ref) => (
        <div ref={ref}></div>
      )}
</Transition>

Transition and Transition.Child both expose their ref like that. See e.g. the UI Docs for further reference.

Going to comment on this real quick, because in the latest version (in develop branch) this is not the case anymore, because we now have the same API for every component.

That said, I’ll make sure that every component will be able to expose a ref to the underlying DOM node if it makes sense!

6reactions
moiz-frostcommented, Mar 10, 2021

Facing a similar issue

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to access the ref of child element - reactjs - Stack Overflow
With this compound components solution you need to asure that there is only one child of the CollapseTransition element.
Read more >
$refs not available when using Transition - Vue Forum
Hi, In my application I use a Transition effect to load my pages(page component) with an ... v-on:enter to get access to the...
Read more >
Overlays | React-Bootstrap
Overlay is the fundamental component for positioning and controlling tooltip visibility. It's a wrapper around Popper.js, that adds support for transitions, ...
Read more >
Activity | Android Developers
On this page; Developer Guides; Fragments; Activity Lifecycle; Configuration Changes; Starting Activities and Getting Results; Saving Persistent State ...
Read more >
Animation transitions - Unity - Manual
Transitions define not only how long the blend between states should take, ... Has Exit Time, Exit Time is a special transition that...
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