class CustomNavigation extends HTMLElement {
connectedCallback() {
this.attachShadow({ mode: 'open' });
this.shadowRoot.innerHTML = `
`;
// Add mobile menu functionality
const mobileMenuBtn = this.shadowRoot.getElementById('mobileMenuBtn');
const mobileMenu = this.shadowRoot.getElementById('mobileMenu');
if (mobileMenuBtn && mobileMenu) {
mobileMenuBtn.addEventListener('click', () => {
mobileMenu.classList.toggle('hidden');
});
// Close mobile menu when clicking outside
document.addEventListener('click', (event) => {
if (!this.contains(event.target) && !mobileMenu.classList.contains('hidden')) {
mobileMenu.classList.add('hidden');
}
});
}
// Update active link based on current page
const updateActiveLink = () => {
const currentPage = window.location.pathname.split('/').pop() || 'index.html';
const links = this.shadowRoot.querySelectorAll('.nav-link');
links.forEach(link => {
const href = link.getAttribute('href');
if (href === currentPage || (currentPage === '' && href === 'index.html')) {
link.classList.add('active');
} else {
link.classList.remove('active');
}
});
};
// Update on load and when navigation occurs
setTimeout(updateActiveLink, 100);
// Listen for navigation events (for single page apps or hash changes)
window.addEventListener('popstate', updateActiveLink);
// Close mobile menu when a link is clicked
const mobileLinks = this.shadowRoot.querySelectorAll('#mobileMenu .nav-link');
mobileLinks.forEach(link => {
link.addEventListener('click', () => {
if (mobileMenu) {
mobileMenu.classList.add('hidden');
}
});
});
}
}
customElements.define('custom-navigation', CustomNavigation);