-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathautosize-textarea.js
48 lines (39 loc) · 1.47 KB
/
autosize-textarea.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
class AutosizeTextarea extends HTMLElement {
static register(tagName = 'autosize-textarea') {
if ("customElements" in window) {
customElements.define(tagName, this);
}
}
handleTextareaChange(e) {
const target = e.target;
const computed = window.getComputedStyle(target);
const borderTop = computed.borderTopWidth;
const borderBottom = computed.borderBottomWidth;
const borderHeight = parseFloat(borderTop) + parseFloat(borderBottom);
// Because the scrollheight won't decrease after its grown, we reset the height to 0 on every change
target.style.height = '0px';
const scrollHeight = target.scrollHeight;
const totalNewHeight = borderHeight + scrollHeight;
target.style.height = `${totalNewHeight}px`;
}
connectedCallback() {
const textareaEl = this.querySelector('textarea');
if (document.readyState === "loading") {
return document.addEventListener("readystatechange", this.connectedCallback.bind(this), { once: true })
}
if (textareaEl) {
textareaEl.style.resize = 'none';
textareaEl.addEventListener('input', this.handleTextareaChange);
textareaEl.setAttribute('rows', '1')
} else {
console.warn('No textarea element found in autosize-textarea');
}
}
disconnectedCallback() {
const textareaEl = this.querySelector('textarea');
if (textareaEl) {
textareaEl.removeEventListener('input', this.handleTextareaChange);
}
}
}
AutosizeTextarea.register();