1class AIChatButton extends HTMLElement { 2 #root = null; 3 #dialog = null; 4 5 6 constructor() { 7 super(); 8 this.#root = this.attachShadow({mode: 'open'}); 9 this.#root.innerHTML = ` 10 <button class="toggle start"> 11 <svg viewBox="0 0 24 24"><path d="M12,3C17.5,3 22,6.58 22,11C22,15.42 17.5,19 12,19C10.76,19 9.57,18.82 8.47,18.5C5.55,21 2,21 2,21C4.33,18.67 4.7,17.1 4.75,16.5C3.05,15.07 2,13.13 2,11C2,6.58 6.5,3 12,3Z" /></svg> 12 </button> 13 <dialog> 14 <div> 15 <header> 16 <button class="fs" title="Fullscreen"> 17 <svg viewBox="0 0 24 24"><path d="M12 5.5L10 8H14L12 5.5M18 10V14L20.5 12L18 10M6 10L3.5 12L6 14V10M14 16H10L12 18.5L14 16M21 3H3C1.9 3 1 3.9 1 5V19C1 20.1 1.9 21 3 21H21C22.1 21 23 20.1 23 19V5C23 3.9 22.1 3 21 3M21 19H3V5H21V19Z" /></svg> 18 </button> 19 <h1>AI Chat</h1> 20 <button class="toggle" title="Close"> 21 <svg viewBox="0 0 24 24"><path d="M13.46,12L19,17.54V19H17.54L12,13.46L6.46,19H5V17.54L10.54,12L5,6.46V5H6.46L12,10.54L17.54,5H19V6.46L13.46,12Z" /></svg> 22 </button> 23 </header> 24 <main> 25 <slot></slot> 26 </main> 27 </div> 28 </dialog> 29 `; 30 31 this.#root.appendChild(this.getStyle()); 32 this.#dialog = this.#root.querySelector('dialog'); 33 34 const toggleButtons = this.#root.querySelectorAll('button.toggle'); 35 toggleButtons.forEach(function (button) { 36 button.addEventListener('click', this.toggleDialog.bind(this)) 37 }.bind(this)); 38 39 this.#dialog.querySelector('button.fs').addEventListener('click', function() { 40 this.#dialog.classList.toggle('fullscreen'); 41 }.bind(this)); 42 } 43 44 /** 45 * Called when the DOM has been connected 46 * 47 * We initialize the attribute based states here 48 */ 49 connectedCallback() { 50 this.#root.querySelector('button.start').title = this.getAttribute('label') || 'AI Chat'; 51 this.#dialog.querySelector('header h1').textContent = this.getAttribute('label') || 'AI Chat'; 52 } 53 54 /** 55 * Define the web component's internal styles 56 * 57 * @returns {HTMLStyleElement} 58 */ 59 getStyle() { 60 const style = document.createElement('style'); 61 style.textContent = ` 62 :host { 63 --color-chat-icon: #4881bf; 64 } 65 button { 66 background: none; 67 border: none; 68 cursor: pointer; 69 } 70 :host > button svg { 71 fill: var(--color-chat-icon); 72 } 73 svg { 74 width: 2em; 75 height: 2em; 76 77 } 78 dialog { 79 width: 500px; 80 max-width: 90vw; 81 height: 800px; 82 max-height: 90vh; 83 84 position: fixed; 85 top: 1em; 86 right: 1em; 87 left: auto; 88 89 padding: 0.25em; 90 91 box-shadow: 0 4px 5px rgb(0 0 0 / 30%); 92 border-radius: 8px; 93 border: 1px solid #fff; 94 } 95 dialog.fullscreen { 96 width: 100%; 97 height: 100%; 98 left: 1em; 99 right: 1em; 100 101 } 102 dialog > div { 103 display: flex; 104 flex-direction: column; 105 height: 100%; 106 } 107 dialog header { 108 display: flex; 109 justify-content: space-between; 110 align-items: flex-start; 111 } 112 dialog main { 113 overflow: auto; 114 flex-grow: 1; 115 } 116 `; 117 return style; 118 } 119 120 toggleDialog() { 121 if (this.#dialog.open) { 122 this.#dialog.close(); 123 } else { 124 this.#dialog.show(); 125 } 126 } 127} 128 129window.customElements.define('aichat-button', AIChatButton); 130