livebook/assets/js/hooks/focus_on_update.js
Jonatan Kłosko b3b79afed4
Refactor JS hooks (#1055)
* Restructure hook files

* Simplify app.js

* Refactor hooks

* Implement password toggle with JS commands
2022-03-16 11:33:53 +01:00

29 lines
546 B
JavaScript

import { isEditableElement } from "../lib/utils";
/**
* A hook used to focus an element whenever it receives LV update.
*/
const FocusOnUpdate = {
mounted() {
this.focus();
},
updated() {
if (this.el !== document.activeElement) {
this.focus();
}
},
focus() {
if (isEditableElement(document.activeElement)) {
return;
}
this.el.focus();
this.el.selectionStart = this.el.selectionEnd = this.el.value.length;
this.el.scrollLeft = this.el.scrollWidth;
},
};
export default FocusOnUpdate;