How to use the Dialog component with headlessui's Transition component in an uncontrolled way? #1939
-
I'm trying to wrap the radixUI dialog primitive and having a very hard time triggering the Transition element on mount and unmount in an uncontrolled way (ie., that doesn't require an external useState. I know this controlled version works (simplified for this question)... import { Transition } from '@headlessui/react';
import * as DialogPrimitive from '@radix-ui/react-dialog';
function Controlled () {
const [open, setOpen] = React.useState(false);
return (
<Dialog.Root open={open} onOpenChange={setOpen}>
<Dialog.Trigger>Button Trigger</Dialog.Trigger>
<Dialog.Portal forceMount>
<Transition.Root show={open}>
<Transition.Child as={Fragment}>
<DialogPrimitive.Overlay forceMount />
</Transition.Child>
<Transition.Child as={Fragment}>
<Dialog.Content> Content </Dialog.Content>
</Transition.Child>
</Transition.Child>
</Dialog.Portal>
</Dialog.Root>
) But I can't seem to get an uncontrolled version to work.
// Props removed in `Dialog.Root`
// Removed `forceMount` in Dialog.Portal
// appear={true} added in `Transition.Root`
function Uncontrolled () {
return (
<Dialog.Root>
<Dialog.Trigger>Button Trigger</Dialog.Trigger>
<Dialog.Portal>
<Transition.Root show={true} appear={true}>
<Transition.Child as={Fragment}>
<DialogPrimitive.Overlay forceMount />
</Transition.Child>
<Transition.Child as={Fragment}>
<Dialog.Content> Content </Dialog.Content>
</Transition.Child>
</Transition.Child>
</Dialog.Portal>
</Dialog.Root>
)
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
Sure you can get data attributes into a javascript variable, but I don't know if it's the best solution 🤔 Why not use CSS animations? |
Beta Was this translation helpful? Give feedback.
-
Yes, by using the controlled API, as you have discovered. That is what it is for. It enables use cases where you need access to the state outside (like JS animations for example). |
Beta Was this translation helpful? Give feedback.
Yes, by using the controlled API, as you have discovered. That is what it is for. It enables use cases where you need access to the state outside (like JS animations for example).