class BottomNav extends HTMLElement {
connectedCallback() {
this.attachShadow({ mode: 'open' });
this.shadowRoot.innerHTML = `
`;
// Add click event listeners
this.shadowRoot.querySelectorAll('.nav-item').forEach(item => {
item.addEventListener('click', (e) => {
e.preventDefault();
const targetId = item.getAttribute('href').substring(1);
// Update active state
this.shadowRoot.querySelectorAll('.nav-item').forEach(navItem => {
navItem.classList.remove('active');
});
item.classList.add('active');
// Navigate to page
navigateToPage(targetId);
});
});
}
}
customElements.define('bottom-nav', BottomNav);
function navigateToPage(pageId) {
// Hide all pages
document.querySelectorAll('.page-content').forEach(page => {
page.classList.remove('active');
});
// Show target page
const targetPage = document.getElementById(pageId);
if (targetPage) {
targetPage.classList.add('active');
}
// Update URL hash
window.location.hash = pageId;
// Scroll to top
window.scrollTo(0, 0);
}
// Handle browser back/forward buttons
window.addEventListener('popstate', function() {
const hash = window.location.hash.substring(1);
if (hash) {
navigateToPage(hash);
// Update bottom nav active state
document.querySelectorAll('bottom-nav .nav-item').forEach(item => {
item.classList.remove('active');
if (item.getAttribute('href') === '#' + hash) {
item.classList.add('active');
}
});
}
});
// Set initial page based on URL hash
window.addEventListener('load', function() {
const hash = window.location.hash.substring(1) || 'dashboard';
navigateToPage(hash);
// Update bottom nav active state
document.querySelectorAll('bottom-nav .nav-item').forEach(item => {
item.classList.remove('active');
if (item.getAttribute('href') === '#' + hash) {
item.classList.add('active');
}
});
});