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
useRefocusableInput: handle HTMLElement
This change should be temporary, and it will allow us to use the
`useRefocusableInput` hook with ref objects and direct `HTMLElement`s.
  • Loading branch information
RobinMalfait committed Aug 1, 2024
commit 7dedb661d21e0bf1acfcea7aeb40af4bb43991a9
10 changes: 7 additions & 3 deletions packages/@headlessui-react/src/hooks/use-refocusable-input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,19 @@ import { useEventListener } from './use-event-listener'
* This hook will also keep the cursor position into account to make sure the
* cursor is placed at the correct position as-if we didn't loose focus at all.
*/
export function useRefocusableInput(ref: MutableRefObject<HTMLInputElement | null>) {
export function useRefocusableInput(
ref: MutableRefObject<HTMLInputElement | null> | HTMLInputElement | null
) {
// Track the cursor position and the value of the input
let info = useRef({
value: '',
selectionStart: null as number | null,
selectionEnd: null as number | null,
})

useEventListener(ref.current, 'blur', (event) => {
let element = ref === null ? null : ref instanceof HTMLInputElement ? ref : ref.current

useEventListener(element, 'blur', (event) => {
let target = event.target
if (!(target instanceof HTMLInputElement)) return

Expand All @@ -28,7 +32,7 @@ export function useRefocusableInput(ref: MutableRefObject<HTMLInputElement | nul
})

return useEvent(() => {
let input = ref.current
let input = ref === null ? null : ref instanceof HTMLInputElement ? ref : ref.current

// If the input is already focused, we don't need to do anything
if (document.activeElement === input) return
Expand Down