Can't get access to ref on Transition.Child or Transition
See original GitHub issueI’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"
>
​
</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:
- Created 3 years ago
- Reactions:8
- Comments:9
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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!
Facing a similar issue