Spaces:
Running
Running
| class UserMenu extends HTMLElement { | |
| connectedCallback() { | |
| this.attachShadow({ mode: 'open' }); | |
| this.shadowRoot.innerHTML = ` | |
| <style> | |
| :host { | |
| display: block; | |
| } | |
| .user-menu { | |
| position: relative; | |
| } | |
| .user-button { | |
| background: none; | |
| border: none; | |
| cursor: pointer; | |
| display: flex; | |
| align-items: center; | |
| padding: 0.5rem; | |
| border-radius: 9999px; | |
| transition: background-color 0.3s ease; | |
| } | |
| .user-button:hover { | |
| background-color: #f3f4f6; | |
| } | |
| .avatar { | |
| width: 32px; | |
| height: 32px; | |
| border-radius: 50%; | |
| background-color: #3b82f6; | |
| color: white; | |
| display: flex; | |
| align-items: center; | |
| justify-content: center; | |
| font-weight: 500; | |
| font-size: 0.875rem; | |
| } | |
| .dropdown { | |
| position: absolute; | |
| top: 100%; | |
| right: 0; | |
| background: white; | |
| border-radius: 0.5rem; | |
| box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -2px rgba(0, 0, 0, 0.05); | |
| width: 200px; | |
| z-index: 1000; | |
| display: none; | |
| } | |
| .dropdown.open { | |
| display: block; | |
| } | |
| .dropdown-item { | |
| display: block; | |
| padding: 0.75rem 1rem; | |
| text-decoration: none; | |
| color: #374151; | |
| font-weight: 500; | |
| transition: background-color 0.3s ease; | |
| } | |
| .dropdown-item:hover { | |
| background-color: #f3f4f6; | |
| } | |
| .divider { | |
| height: 1px; | |
| background-color: #e5e7eb; | |
| margin: 0.25rem 0; | |
| } | |
| </style> | |
| <div class="user-menu"> | |
| <button class="user-button" id="userButton"> | |
| <div class="avatar">U</div> | |
| </button> | |
| <div class="dropdown" id="dropdown"> | |
| <a href="/profile.html" class="dropdown-item">Profile</a> | |
| <a href="/settings.html" class="dropdown-item">Settings</a> | |
| <div class="divider"></div> | |
| <a href="/logout.html" class="dropdown-item">Log Out</a> | |
| </div> | |
| </div> | |
| `; | |
| // Add event listeners after content is rendered | |
| setTimeout(() => { | |
| this.setupEventListeners(); | |
| }, 0); | |
| } | |
| setupEventListeners() { | |
| const userButton = this.shadowRoot.getElementById('userButton'); | |
| const dropdown = this.shadowRoot.getElementById('dropdown'); | |
| userButton.addEventListener('click', () => { | |
| dropdown.classList.toggle('open'); | |
| }); | |
| // Close dropdown when clicking outside | |
| document.addEventListener('click', (event) => { | |
| if (!this.contains(event.target)) { | |
| dropdown.classList.remove('open'); | |
| } | |
| }); | |
| } | |
| } | |
| customElements.define('user-menu', UserMenu); |