Skip to content

Commit ce12406

Browse files
authored
fix transition enter bug (#1519)
We had an issue where an open Dialog got hidden by css didn't properly unmount because the Transition never "finished". We fixed this by checking if the node was hidden by using `getBoundingClientRect`. Today I learned that just *reading* those values (aka call `node.getBoundingClientRect()`) it for whatever reason completely stops the transition. This causes the enter transitions to completely stop working. Instead, we move this code so that we only check the existence of the Node when we try to transition out because this means that the Node is definitely there, just have to check its bounding rect.
1 parent d3ed3f5 commit ce12406

File tree

2 files changed

+11
-12
lines changed

2 files changed

+11
-12
lines changed

packages/@headlessui-react/src/components/transitions/transition.tsx

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,8 @@ let TransitionRoot = forwardRefWithAs(function Transition<
339339
>(props: TransitionChildProps<TTag> & { show?: boolean; appear?: boolean }, ref: Ref<HTMLElement>) {
340340
// @ts-expect-error
341341
let { show, appear = false, unmount, ...theirProps } = props as typeof props
342-
let transitionRef = useSyncRefs(ref)
342+
let internalTransitionRef = useRef<HTMLElement | null>(null)
343+
let transitionRef = useSyncRefs(internalTransitionRef, ref)
343344

344345
// The TransitionChild will also call this hook, and we have to make sure that we are ready.
345346
useServerHandoffComplete()
@@ -390,6 +391,15 @@ let TransitionRoot = forwardRefWithAs(function Transition<
390391
setState(TreeStates.Visible)
391392
} else if (!hasChildren(nestingBag)) {
392393
setState(TreeStates.Hidden)
394+
} else {
395+
let node = internalTransitionRef.current
396+
if (!node) return
397+
let rect = node.getBoundingClientRect()
398+
399+
if (rect.x === 0 && rect.y === 0 && rect.width === 0 && rect.height === 0) {
400+
// The node is completely hidden, let's hide it
401+
setState(TreeStates.Hidden)
402+
}
393403
}
394404
}, [show, nestingBag])
395405

packages/@headlessui-react/src/hooks/use-transition.ts

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -72,17 +72,6 @@ export function useTransition({
7272
if (latestDirection.current === 'idle') return // We don't need to transition
7373
if (!mounted.current) return
7474

75-
dd.add(() => {
76-
if (!node) return
77-
78-
let rect = node.getBoundingClientRect()
79-
80-
if (rect.x === 0 && rect.y === 0 && rect.width === 0 && rect.height === 0) {
81-
// The node is completely hidden
82-
onStop.current(latestDirection.current)
83-
}
84-
})
85-
8675
dd.dispose()
8776

8877
beforeEvent()

0 commit comments

Comments
 (0)