Header menu via header.js

This commit is contained in:
karim hassan
2025-08-24 23:47:39 +00:00
parent f1103d67a0
commit eabd0aa50f
25 changed files with 869 additions and 185 deletions

View File

@@ -0,0 +1,42 @@
// Shared header navigation for all pages
(function(){
const links = [
{ href: '/', label: 'Accueil' },
{ href: '/events', label: 'Événements' },
{ href: '/admin', label: 'Admin' },
{ href: '/scoreboard', label: 'Classement' },
];
const path = location.pathname.replace(/\/index\.html$/, '');
const isActive = (href) => {
if (href === '/') return path === '/' || path === '';
return path.startsWith(href);
};
const nav = document.createElement('nav');
nav.className = 'site-nav';
nav.innerHTML = links.map(l =>
`<a href="${l.href}" class="${isActive(l.href) ? 'active' : ''}">${l.label}</a>`
).join('');
let header = document.querySelector('header');
if (!header) {
header = document.createElement('header');
document.body.prepend(header);
}
// Basic style if not present (non-intrusive)
header.style.position = header.style.position || 'sticky';
header.style.top = header.style.top || '0';
header.style.zIndex = header.style.zIndex || '4';
// Title (keep existing title if present)
let h1 = header.querySelector('h1');
if (!h1) {
h1 = document.createElement('h1');
h1.textContent = 'Super Sunday';
header.prepend(h1);
}
// Replace or add nav
const oldNav = header.querySelector('nav');
if (oldNav) oldNav.replaceWith(nav); else header.appendChild(nav);
})();