Last active
July 23, 2024 17:20
-
-
Save pongo/f390b61e4ebc5d67fe183d301692f19a to your computer and use it in GitHub Desktop.
Vue directive textarea autoheight
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
// <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