Created
January 25, 2021 20:59
-
-
Save sibelius/8b01b5677d6b8e4c329400e3049e03a0 to your computer and use it in GitHub Desktop.
listen to caret position of an input
This file contains hidden or 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
const useCaretPosition = () => { | |
const [caret, setCaret] = useState({ | |
selectionStart: 0, | |
selectionEnd: 0, | |
}); | |
const onEvent = useCallback((e) => { | |
const input = e.target; | |
setCaret({ | |
selectionStart: input.selectionStart, | |
selectionEnd: input.selectionEnd | |
}); | |
}, []); | |
const events = { | |
onClick: onEvent, | |
onKeyUp: onEvent, | |
onFocus: onEvent, | |
} | |
return [caret, events]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment