Skip to content

Commit

Permalink
chore(input): add improved tracking while focused
Browse files Browse the repository at this point in the history
  • Loading branch information
guilhermerodz committed Feb 21, 2024
1 parent 2217693 commit 7afe406
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,24 @@ export const OTPInput = React.forwardRef<HTMLInputElement, OTPInputProps>(
}
}, [maxLength, onComplete, value])

// Run improved selection tracking while focused
React.useEffect(() => {
if (!isFocused) {
return
}

const interval = setInterval(() => {
if (inputRef.current && document.activeElement === inputRef.current) {
setMirrorSelectionStart(inputRef.current.selectionStart)
setMirrorSelectionEnd(inputRef.current.selectionEnd)
}
}, 5_0)

return () => {
clearInterval(interval)
}
}, [isFocused, mirrorSelectionStart, mirrorSelectionEnd])

/** Event handlers */
function _selectListener() {
if (!inputRef.current) {
Expand Down

0 comments on commit 7afe406

Please sign in to comment.