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
Next Next commit
useDidElementMove: handle HTMLElement
This change should be temporary, and it will allow us to use the
`useDidElementMove` with ref objects and direct `HTMLElement`s.
  • Loading branch information
RobinMalfait committed Aug 1, 2024
commit 7fd628f2909f0e7eaaca0ecc4a100b418784b968
22 changes: 17 additions & 5 deletions packages/@headlessui-react/src/hooks/use-did-element-move.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,33 @@
import { useRef, type MutableRefObject } from 'react'
import { useIsoMorphicEffect } from './use-iso-morphic-effect'

export function useDidElementMove(enabled: boolean, element: MutableRefObject<HTMLElement | null>) {
export function useDidElementMove(
enabled: boolean,
ref: MutableRefObject<HTMLElement | null> | HTMLElement | null
) {
let elementPosition = useRef({ left: 0, top: 0 })

useIsoMorphicEffect(() => {
let el = element.current
let el = ref === null ? null : ref instanceof HTMLElement ? ref : ref.current
if (!el) return

let DOMRect = el.getBoundingClientRect()
if (DOMRect) elementPosition.current = DOMRect
}, [enabled])

if (element.current == null) return false
let element =
typeof window === 'undefined'
? null
: ref === null
? null
: ref instanceof HTMLElement
? ref
: ref.current
if (element == null) return false
if (!enabled) return false
if (element.current === document.activeElement) return false
if (element === document.activeElement) return false

let buttonRect = element.current.getBoundingClientRect()
let buttonRect = element.getBoundingClientRect()

let didElementMove =
buttonRect.top !== elementPosition.current.top ||
Expand Down