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
simplify useOutsideClick
Only accept `HTMLElement | null` instead of `MutableRefObject<HTMLElement | null>`
  • Loading branch information
RobinMalfait committed Aug 1, 2024
commit 8d26d4c17390f2f72e66ff5a1aafc0eb6e566a82
9 changes: 4 additions & 5 deletions packages/@headlessui-react/src/hooks/use-outside-click.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { useCallback, useRef, type MutableRefObject } from 'react'
import { useCallback, useRef } from 'react'
import { FocusableMode, isFocusableElement } from '../utils/focus-management'
import { isMobile } from '../utils/platform'
import { useDocumentEvent } from './use-document-event'
import { useIsTopLayer } from './use-is-top-layer'
import { useLatestValue } from './use-latest-value'
import { useWindowEvent } from './use-window-event'

type Container = MutableRefObject<HTMLElement | null> | HTMLElement | null
type Container = HTMLElement | null
type ContainerCollection = Container[] | Set<Container>
type ContainerInput = Container | ContainerCollection

Expand Down Expand Up @@ -69,15 +69,14 @@ export function useOutsideClick(
// Ignore if the target exists in one of the containers
for (let container of _containers) {
if (container === null) continue
let domNode = container instanceof HTMLElement ? container : container.current
if (domNode?.contains(target)) {
if (container.contains(target)) {
return
}

// If the click crossed a shadow boundary, we need to check if the
// container is inside the tree by using `composedPath` to "pierce" the
// shadow boundary
if (event.composed && event.composedPath().includes(domNode as EventTarget)) {
if (event.composed && event.composedPath().includes(container as EventTarget)) {
return
}
}
Expand Down