Skip to content
Merged
Changes from 1 commit
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
7fd628f
`useDidElementMove`: handle `HTMLElement`
RobinMalfait Jul 30, 2024
ffdc6c4
`useResolveButtonType`: handle `HTMLElement`
RobinMalfait Jul 30, 2024
7dedb66
`useRefocusableInput`: handle `HTMLElement`
RobinMalfait Jul 30, 2024
b194e65
`useTransition`: handle `HTMLElement`
RobinMalfait Jul 30, 2024
6f90b1e
ensure `containers` are a dependency of `useEffect`
RobinMalfait Jul 30, 2024
a7bbcee
`Menu`: track `button` and `items` elements in state
RobinMalfait Jul 30, 2024
6298af9
`Combobox`: track `input`, `button` and `options` elements in state
RobinMalfait Aug 1, 2024
ef6afd5
`Disclosure`: track `button` and `panel` elements in state
RobinMalfait Aug 1, 2024
5bd4d58
`Listbox`: track `button` and `options` elements in state
RobinMalfait Aug 1, 2024
d01b25b
`Popover`: track `button` and `panel` elements in state
RobinMalfait Aug 1, 2024
2d14b2c
`Transition`: track the `container` element in state
RobinMalfait Aug 1, 2024
632b2d0
remove incorrect leftover `style=""` attribute
RobinMalfait Jul 30, 2024
477f259
simplify `useDidElementMove`, only accept `HTMLElement | null`
RobinMalfait Aug 1, 2024
6a90d34
pass `HTMLElement | null` directly to `useResolveButtonType`
RobinMalfait Aug 1, 2024
d56dfcf
simplify `useResolveButtonType`, only handle `HTMLElement | null`
RobinMalfait Aug 1, 2024
7128c24
simplify `useRefocusableInput`
RobinMalfait Aug 1, 2024
4d71797
simplify `useElementSize`
RobinMalfait Aug 1, 2024
8d26d4c
simplify `useOutsideClick`
RobinMalfait Aug 1, 2024
7e61373
do not rely on `HTMLButtonElement` being available
RobinMalfait Aug 1, 2024
697468d
update changelog
RobinMalfait Aug 2, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
useTransition: handle HTMLElement
Accept `HTMLElement| null` instead of `MutableRefObject<HTMLElement |
null>` in the `useTransition` hook.
  • Loading branch information
RobinMalfait committed Aug 1, 2024
commit b194e65e654a67aa2e43f62e61b313c45e05c011
144 changes: 69 additions & 75 deletions packages/@headlessui-react/src/hooks/use-transition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export function transitionDataAttributes(data: TransitionData) {

export function useTransition(
enabled: boolean,
elementRef: MutableRefObject<HTMLElement | null>,
element: HTMLElement | null,
show: boolean,
events?: {
start?(show: boolean): void
Expand All @@ -68,98 +68,92 @@ export function useTransition(

let d = useDisposables()

useIsoMorphicEffect(
function retry() {
if (!enabled) return
useIsoMorphicEffect(() => {
if (!enabled) return

if (show) {
setVisible(true)
}

if (!element) {
if (show) {
setVisible(true)
addFlag(TransitionState.Enter | TransitionState.Closed)
}
return
}

let node = elementRef.current
if (!node) {
// Retry if the DOM node isn't available yet
if (show) {
addFlag(TransitionState.Enter | TransitionState.Closed)
return d.nextFrame(() => retry())
events?.start?.(show)

return transition(element, {
inFlight,
prepare() {
if (cancelledRef.current) {
// Cancelled a cancellation, we're back to the original state.
cancelledRef.current = false
} else {
// If we were already in-flight, then we want to cancel the current
// transition.
cancelledRef.current = inFlight.current
}
return
}

events?.start?.(show)
inFlight.current = true

return transition(node, {
inFlight,
prepare() {
if (cancelledRef.current) {
// Cancelled a cancellation, we're back to the original state.
cancelledRef.current = false
} else {
// If we were already in-flight, then we want to cancel the current
// transition.
cancelledRef.current = inFlight.current
}

inFlight.current = true

if (cancelledRef.current) return
if (cancelledRef.current) return

if (show) {
addFlag(TransitionState.Enter | TransitionState.Closed)
removeFlag(TransitionState.Leave)
} else {
addFlag(TransitionState.Leave)
removeFlag(TransitionState.Enter)
}
},
run() {
if (cancelledRef.current) {
// If we cancelled a transition, then the `show` state is going to
// be inverted already, but that doesn't mean we have to go to that
// new state.
//
// What we actually want is to revert to the "idle" state (the
// stable state where an `Enter` transitions to, and a `Leave`
// transitions from.)
//
// Because of this, it might look like we are swapping the flags in
// the following branches, but that's not the case.
if (show) {
addFlag(TransitionState.Enter | TransitionState.Closed)
removeFlag(TransitionState.Leave)
} else {
removeFlag(TransitionState.Enter | TransitionState.Closed)
addFlag(TransitionState.Leave)
removeFlag(TransitionState.Enter)
} else {
removeFlag(TransitionState.Leave)
addFlag(TransitionState.Enter | TransitionState.Closed)
}
},
run() {
if (cancelledRef.current) {
// If we cancelled a transition, then the `show` state is going to
// be inverted already, but that doesn't mean we have to go to that
// new state.
//
// What we actually want is to revert to the "idle" state (the
// stable state where an `Enter` transitions to, and a `Leave`
// transitions from.)
//
// Because of this, it might look like we are swapping the flags in
// the following branches, but that's not the case.
if (show) {
removeFlag(TransitionState.Enter | TransitionState.Closed)
addFlag(TransitionState.Leave)
} else {
removeFlag(TransitionState.Leave)
addFlag(TransitionState.Enter | TransitionState.Closed)
}
} else {
if (show) {
removeFlag(TransitionState.Closed)
} else {
if (show) {
removeFlag(TransitionState.Closed)
} else {
addFlag(TransitionState.Closed)
}
addFlag(TransitionState.Closed)
}
},
done() {
if (cancelledRef.current) {
if (typeof node.getAnimations === 'function' && node.getAnimations().length > 0) {
return
}
}
},
done() {
if (cancelledRef.current) {
if (typeof element.getAnimations === 'function' && element.getAnimations().length > 0) {
return
}
}

inFlight.current = false
inFlight.current = false

removeFlag(TransitionState.Enter | TransitionState.Leave | TransitionState.Closed)
removeFlag(TransitionState.Enter | TransitionState.Leave | TransitionState.Closed)

if (!show) {
setVisible(false)
}
if (!show) {
setVisible(false)
}

events?.end?.(show)
},
})
},
[enabled, show, elementRef, d]
)
events?.end?.(show)
},
})
}, [enabled, show, element, d])

if (!enabled) {
return [
Expand Down