Skip to content

Instantly share code, notes, and snippets.

@pongo
Last active July 23, 2024 17:20
Show Gist options
  • Save pongo/f390b61e4ebc5d67fe183d301692f19a to your computer and use it in GitHub Desktop.
Save pongo/f390b61e4ebc5d67fe183d301692f19a to your computer and use it in GitHub Desktop.
Vue directive textarea autoheight
// <textarea rows="3" v-textarea-autoheight></textarea>
const textareaAutoheight = {
autoHeight(el) {
el.style.height = "auto"; // Revert height
const style = window.getComputedStyle(el);
const borderTop = parseFloat(style.getPropertyValue("border-top-width"));
const borderBottom = parseFloat(style.getPropertyValue("border-bottom-width"));
el.style.height = el.scrollHeight + borderTop + borderBottom + "px";
},
mounted(el, binding) {
el.style.overflowY = "hidden";
el.style.resize = "none";
el.style.boxSizing = "border-box";
el.__autoHeightHandler__ = () => binding.dir.autoHeight(el);
el.addEventListener('input', el.__autoHeightHandler__);
},
beforeUnmount(el) {
el.removeEventListener('input', () => el.__autoHeightHandler__);
},
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment