const collapsedText = document.createElement('template'); collapsedText.innerHTML = `
`; class CollapsedText extends HTMLElement { constructor() { super(); this.attachShadow({ mode: 'open' }).append(collapsedText.content.cloneNode(true)); this.isCollapsed = true; this.toggleCollapsedText = this.toggleCollapsedText.bind(this); this.wrapper = this.shadowRoot.querySelector('.collapsed-text'); this.toggleButton = this.shadowRoot.querySelector('.toggle-button'); } toggleCollapsedText() { this.toggleButton.textContent = this.isCollapsed ? 'Show less' : 'Show more'; if (this.isCollapsed) { this.setAttribute('open', ''); this.wrapper.removeEventListener('click', this.toggleCollapsedText); } else { this.removeAttribute('open'); this.wrapper.addEventListener('click', this.toggleCollapsedText); } this.isCollapsed = !this.isCollapsed; } connectedCallback() { this.wrapper.addEventListener('click', this.toggleCollapsedText); this.toggleButton.addEventListener('click', this.toggleCollapsedText); } disconnectedCallback() { this.wrapper.removeEventListener('click', this.toggleCollapsedText); this.toggleButton.removeEventListener('click', this.toggleCollapsedText); } } window.customElements.define('collapsed-text', CollapsedText);