class CustomSidebar extends HTMLElement { connectedCallback() { this.attachShadow({ mode: 'open' }); this.shadowRoot.innerHTML = `
`; // Add event listeners after rendering setTimeout(() => { this.setupEventListeners(); if (typeof feather !== 'undefined') { feather.replace(); } }); } setupEventListeners() { const sidebar = this.shadowRoot.querySelector('.sidebar'); const overlay = this.shadowRoot.getElementById('sidebarOverlay'); const navItems = this.shadowRoot.querySelectorAll('.nav-item'); // Close sidebar when clicking overlay if (overlay) { overlay.addEventListener('click', () => { this.toggleMobile(); }); } // Close sidebar when clicking a nav item on mobile navItems.forEach(item => { item.addEventListener('click', () => { if (window.innerWidth < 768) { this.toggleMobile(); } }); }); // Update active nav item based on current page this.updateActiveNav(); } toggleMobile() { const sidebar = this.shadowRoot.querySelector('.sidebar'); const overlay = this.shadowRoot.getElementById('sidebarOverlay'); sidebar.classList.toggle('open'); overlay.classList.toggle('show'); } updateActiveNav() { const currentPath = window.location.pathname.split('/').pop() || 'index.html'; const navItems = this.shadowRoot.querySelectorAll('.nav-item'); navItems.forEach(item => { const href = item.getAttribute('href'); if (href === currentPath) { item.classList.add('active'); } else { item.classList.remove('active'); } }); } } customElements.define('custom-sidebar', CustomSidebar);