Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix components not properly closing when using the transition prop #3448

Merged
merged 5 commits into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
10 changes: 8 additions & 2 deletions packages/@headlessui-react/src/components/combobox/combobox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1672,14 +1672,20 @@ function OptionsFn<TTag extends ElementType = typeof DEFAULT_OPTIONS_TAG>(
}

let [floatingRef, style] = useFloatingPanel(anchor)
let [localOptionsElement, setLocalOptionsElement] = useState<HTMLElement | null>(null)
RobinMalfait marked this conversation as resolved.
Show resolved Hide resolved
let getFloatingPanelProps = useFloatingPanelProps()
let optionsRef = useSyncRefs(ref, anchor ? floatingRef : null, actions.setOptionsElement)
let optionsRef = useSyncRefs(
ref,
anchor ? floatingRef : null,
actions.setOptionsElement,
setLocalOptionsElement
)
let ownerDocument = useOwnerDocument(data.optionsElement)

let usesOpenClosedState = useOpenClosed()
let [visible, transitionData] = useTransition(
transition,
data.optionsElement,
localOptionsElement,
usesOpenClosedState !== null
? (usesOpenClosedState & State.Open) === State.Open
: data.comboboxState === ComboboxState.Open
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import React, {
useMemo,
useReducer,
useRef,
useState,
type ContextType,
type Dispatch,
type ElementType,
Expand Down Expand Up @@ -450,12 +451,14 @@ function PanelFn<TTag extends ElementType = typeof DEFAULT_PANEL_TAG>(
let [state, dispatch] = useDisclosureContext('Disclosure.Panel')
let { close } = useDisclosureAPIContext('Disclosure.Panel')
let mergeRefs = useMergeRefsFn()
let [localPanelElement, setLocalPanelElement] = useState<HTMLElement | null>(null)

let panelRef = useSyncRefs(
ref,
useEvent((element) => {
startTransition(() => dispatch({ type: ActionTypes.SetPanelElement, element }))
})
}),
setLocalPanelElement
)

useEffect(() => {
Expand All @@ -468,7 +471,7 @@ function PanelFn<TTag extends ElementType = typeof DEFAULT_PANEL_TAG>(
let usesOpenClosedState = useOpenClosed()
let [visible, transitionData] = useTransition(
transition,
state.panelElement,
localPanelElement,
usesOpenClosedState !== null
? (usesOpenClosedState & State.Open) === State.Open
: state.disclosureState === DisclosureStates.Open
Expand Down
11 changes: 9 additions & 2 deletions packages/@headlessui-react/src/components/listbox/listbox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import React, {
useMemo,
useReducer,
useRef,
useState,
type CSSProperties,
type ElementType,
type MutableRefObject,
Expand Down Expand Up @@ -931,6 +932,7 @@ function OptionsFn<TTag extends ElementType = typeof DEFAULT_OPTIONS_TAG>(
...theirProps
} = props
let anchor = useResolvedAnchor(rawAnchor)
let [localOptionsElement, setLocalOptionsElement] = useState<HTMLElement | null>(null)

// Always enable `portal` functionality, when `anchor` is enabled
if (anchor) {
Expand All @@ -945,7 +947,7 @@ function OptionsFn<TTag extends ElementType = typeof DEFAULT_OPTIONS_TAG>(
let usesOpenClosedState = useOpenClosed()
let [visible, transitionData] = useTransition(
transition,
data.optionsElement,
localOptionsElement,
usesOpenClosedState !== null
? (usesOpenClosedState & State.Open) === State.Open
: data.listboxState === ListboxStates.Open
Expand Down Expand Up @@ -1023,7 +1025,12 @@ function OptionsFn<TTag extends ElementType = typeof DEFAULT_OPTIONS_TAG>(

let [floatingRef, style] = useFloatingPanel(anchorOptions)
let getFloatingPanelProps = useFloatingPanelProps()
let optionsRef = useSyncRefs(ref, anchor ? floatingRef : null, actions.setOptionsElement)
let optionsRef = useSyncRefs(
ref,
anchor ? floatingRef : null,
actions.setOptionsElement,
setLocalOptionsElement
)

let searchDisposables = useDisposables()

Expand Down
8 changes: 6 additions & 2 deletions packages/@headlessui-react/src/components/menu/menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import React, {
useMemo,
useReducer,
useRef,
useState,
type CSSProperties,
type Dispatch,
type ElementType,
Expand Down Expand Up @@ -620,10 +621,13 @@ function ItemsFn<TTag extends ElementType = typeof DEFAULT_ITEMS_TAG>(
let [state, dispatch] = useMenuContext('Menu.Items')
let [floatingRef, style] = useFloatingPanel(anchor)
let getFloatingPanelProps = useFloatingPanelProps()
let [localItemsElement, setLocalItemsElement] = useState<HTMLElement | null>(null)

let itemsRef = useSyncRefs(
ref,
anchor ? floatingRef : null,
useEvent((element) => dispatch({ type: ActionTypes.SetItemsElement, element }))
useEvent((element) => dispatch({ type: ActionTypes.SetItemsElement, element })),
setLocalItemsElement
)
let ownerDocument = useOwnerDocument(state.itemsElement)

Expand All @@ -635,7 +639,7 @@ function ItemsFn<TTag extends ElementType = typeof DEFAULT_ITEMS_TAG>(
let usesOpenClosedState = useOpenClosed()
let [visible, transitionData] = useTransition(
transition,
state.itemsElement,
localItemsElement,
usesOpenClosedState !== null
? (usesOpenClosedState & State.Open) === State.Open
: state.menuState === MenuStates.Open
Expand Down
17 changes: 11 additions & 6 deletions packages/@headlessui-react/src/components/popover/popover.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -763,13 +763,13 @@ function BackdropFn<TTag extends ElementType = typeof DEFAULT_BACKDROP_TAG>(
...theirProps
} = props
let [{ popoverState }, dispatch] = usePopoverContext('Popover.Backdrop')
let [backdropElement, setBackdropElement] = useState<HTMLElement | null>(null)
let backdropRef = useSyncRefs(ref, setBackdropElement)
let [localBackdropElement, setLocalBackdropElement] = useState<HTMLElement | null>(null)
let backdropRef = useSyncRefs(ref, setLocalBackdropElement)

let usesOpenClosedState = useOpenClosed()
let [visible, transitionData] = useTransition(
transition,
backdropElement,
localBackdropElement,
usesOpenClosedState !== null
? (usesOpenClosedState & State.Open) === State.Open
: popoverState === PopoverStates.Open
Expand Down Expand Up @@ -865,11 +865,13 @@ function PanelFn<TTag extends ElementType = typeof DEFAULT_PANEL_TAG>(
portal = true
}

let [localPanelElement, setLocalPanelElement] = useState<HTMLElement | null>(null)
let panelRef = useSyncRefs(
internalPanelRef,
ref,
anchor ? floatingRef : null,
useEvent((panel) => dispatch({ type: ActionTypes.SetPanel, panel }))
useEvent((panel) => dispatch({ type: ActionTypes.SetPanel, panel })),
setLocalPanelElement
)
let ownerDocument = useOwnerDocument(internalPanelRef)
let mergeRefs = useMergeRefsFn()
Expand All @@ -884,7 +886,7 @@ function PanelFn<TTag extends ElementType = typeof DEFAULT_PANEL_TAG>(
let usesOpenClosedState = useOpenClosed()
let [visible, transitionData] = useTransition(
transition,
state.panel,
localPanelElement,
usesOpenClosedState !== null
? (usesOpenClosedState & State.Open) === State.Open
: state.popoverState === PopoverStates.Open
Expand Down Expand Up @@ -1028,7 +1030,10 @@ function PanelFn<TTag extends ElementType = typeof DEFAULT_PANEL_TAG>(

// Ignore sentinel buttons and items inside the panel
for (let element of combined.slice()) {
if (element.dataset.headlessuiFocusGuard === 'true' || state.panel?.contains(element)) {
if (
element.dataset.headlessuiFocusGuard === 'true' ||
localPanelElement?.contains(element)
) {
let idx = combined.indexOf(element)
if (idx !== -1) combined.splice(idx, 1)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -319,12 +319,12 @@ function TransitionChildFn<TTag extends ElementType = typeof DEFAULT_TRANSITION_

...theirProps
} = props as typeof props
let [containerElement, setContainerElement] = useState<HTMLElement | null>(null)
let [localContainerElement, setLocalContainerElement] = useState<HTMLElement | null>(null)
let container = useRef<HTMLElement | null>(null)
let requiresRef = shouldForwardRef(props)

let transitionRef = useSyncRefs(
...(requiresRef ? [container, ref, setContainerElement] : ref === null ? [] : [ref])
...(requiresRef ? [container, ref, setLocalContainerElement] : ref === null ? [] : [ref])
)
let strategy = theirProps.unmount ?? true ? RenderStrategy.Unmount : RenderStrategy.Hidden

Expand Down Expand Up @@ -438,7 +438,7 @@ function TransitionChildFn<TTag extends ElementType = typeof DEFAULT_TRANSITION_
// a leave transition on the `<Transition>` is done, but there is still a
// child `<TransitionChild>` busy, then `visible` would be `false`, while
// `state` would still be `TreeStates.Visible`.
let [, transitionData] = useTransition(enabled, containerElement, show, { start, end })
let [, transitionData] = useTransition(enabled, localContainerElement, show, { start, end })

let ourProps = compact({
ref: transitionRef,
Expand Down