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 useRefocusableInput
  • Loading branch information
RobinMalfait committed Aug 1, 2024
commit 7128c24dece28e32000ac25a0721127fc39ef80a
12 changes: 3 additions & 9 deletions packages/@headlessui-react/src/hooks/use-refocusable-input.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useRef, type MutableRefObject } from 'react'
import { useRef } from 'react'
import { useEvent } from './use-event'
import { useEventListener } from './use-event-listener'

Expand All @@ -8,19 +8,15 @@ 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> | HTMLInputElement | null
) {
export function useRefocusableInput(input: 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,
})

let element = ref === null ? null : ref instanceof HTMLInputElement ? ref : ref.current

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

Expand All @@ -32,8 +28,6 @@ export function useRefocusableInput(
})

return useEvent(() => {
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