const smTextarea = document.createElement('template') smTextarea.innerHTML = ` `; customElements.define('sm-textarea', class extends HTMLElement { constructor() { super() this.attachShadow({ mode: 'open' }).append(smTextarea.content.cloneNode(true)) this.textarea = this.shadowRoot.querySelector('textarea') this.textareaBox = this.shadowRoot.querySelector('.textarea') this.placeholder = this.shadowRoot.querySelector('.placeholder') this.reflectedAttributes = ['disabled', 'required', 'readonly', 'rows', 'minlength', 'maxlength'] this.reset = this.reset.bind(this) this.focusIn = this.focusIn.bind(this) this.fireEvent = this.fireEvent.bind(this) this.checkInput = this.checkInput.bind(this) } static get observedAttributes() { return ['disabled', 'value', 'placeholder', 'required', 'readonly', 'rows', 'minlength', 'maxlength'] } get value() { return this.textarea.value } set value(val) { this.setAttribute('value', val) this.fireEvent() } get disabled() { return this.hasAttribute('disabled') } set disabled(val) { if (val) { this.setAttribute('disabled', '') } else { this.removeAttribute('disabled') } } get isValid() { return this.textarea.checkValidity() } reset() { this.setAttribute('value', '') } focusIn() { this.textarea.focus() } fireEvent() { let event = new Event('input', { bubbles: true, cancelable: true, composed: true }); this.dispatchEvent(event); } checkInput() { if (!this.hasAttribute('placeholder') || this.getAttribute('placeholder') === '') return; if (this.textarea.value !== '') { this.placeholder.classList.add('hide') } else { this.placeholder.classList.remove('hide') } } connectedCallback() { this.textarea.addEventListener('input', e => { this.textareaBox.dataset.value = this.textarea.value this.checkInput() }) } attributeChangedCallback(name, oldValue, newValue) { if (this.reflectedAttributes.includes(name)) { if (this.hasAttribute(name)) { this.textarea.setAttribute(name, this.getAttribute(name) ? this.getAttribute(name) : '') } else { this.textContent.removeAttribute(name) } } else if (name === 'placeholder') { this.placeholder.textContent = this.getAttribute('placeholder') } else if (name === 'value') { this.textarea.value = newValue; this.textareaBox.dataset.value = newValue this.checkInput() } } })