-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix cursor position when re-focusing the
ComboboxInput
component (#…
…3065) * add `useRefocusableInput` hook * use the new `useRefocusableInput` hook * update changelog * infer types of the `ref`
- Loading branch information
1 parent
d03fbb1
commit 4f89588
Showing
3 changed files
with
68 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
packages/@headlessui-react/src/hooks/use-refocusable-input.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { useRef, type MutableRefObject } from 'react' | ||
import { useEvent } from './use-event' | ||
import { useEventListener } from './use-event-listener' | ||
|
||
/** | ||
* The `useRefocusableInput` hook exposes a function to re-focus the input element. | ||
* | ||
* 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>) { | ||
// 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 target = event.target | ||
if (!(target instanceof HTMLInputElement)) return | ||
|
||
info.current = { | ||
value: target.value, | ||
selectionStart: target.selectionStart, | ||
selectionEnd: target.selectionEnd, | ||
} | ||
}) | ||
|
||
return useEvent(() => { | ||
let input = ref.current | ||
if (!(input instanceof HTMLInputElement)) return | ||
if (!input.isConnected) return | ||
|
||
// Focus the input | ||
input.focus({ preventScroll: true }) | ||
|
||
// Try to restore the cursor position | ||
// | ||
// If the value changed since we recorded the cursor position, then we can't | ||
// restore the cursor position and we'll just leave it at the end. | ||
if (input.value !== info.current.value) { | ||
input.setSelectionRange(input.value.length, input.value.length) | ||
} | ||
|
||
// If the value is the same, we can restore to the previous cursor position. | ||
else { | ||
let { selectionStart, selectionEnd } = info.current | ||
if (selectionStart !== null && selectionEnd !== null) { | ||
input.setSelectionRange(selectionStart, selectionEnd) | ||
} | ||
} | ||
|
||
// Reset the cursor position | ||
info.current = { value: '', selectionStart: null, selectionEnd: null } | ||
}) | ||
} |