Skip to content

Instantly share code, notes, and snippets.

@chrisryana
Created June 18, 2021 06:32
Show Gist options
  • Save chrisryana/51c75f9c40bace48b20ce8baca66a151 to your computer and use it in GitHub Desktop.
Save chrisryana/51c75f9c40bace48b20ce8baca66a151 to your computer and use it in GitHub Desktop.
Автофокус на contentEditable ноду
// Ставит курсор в конец contentEditable ноды
const setCaretToEnd = () => {
const selection = window.getSelection();
const range = document.createRange();
const walletNameLength = inputRef.current.innerText.length;
range.setStart(window.getSelection().focusNode, walletNameLength);
range.collapse(true);
selection.removeAllRanges();
selection.addRange(range);
};
useEffect(() => {
if (editable && inputRef) {
inputRef.current.focus();
}
}, [editable]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment