/** * Sticky header behaviour (theme-wide; do not duplicate sticky CSS/scroll JS in header.php). * * @package Trafium_AI */ (function () { 'use strict'; var STICKY_CLASS = 'trf-is-sticky'; var NAV_TARGET_CLASS = 'trf-header-sticky-target'; var MAX_FULL_BAR_RATIO = 0.22; var MAX_FULL_BAR_PX = 112; function adminBarOffset() { var bar = document.getElementById('wpadminbar'); if (!bar) { return 0; } return bar.offsetHeight || 0; } function normalizeMode(raw) { if (!raw) { return ''; } return String(raw) .trim() .toLowerCase() .replace(/_/g, '-'); } function clearSticky(el) { if (!el) { return; } el.classList.remove(STICKY_CLASS); if (el.style && el.style.position === 'sticky') { el.style.position = ''; } if (el.style && el.style.top) { el.style.top = ''; } } function applySticky(el) { if (!el) { return; } el.classList.add(STICKY_CLASS); } function findNavRow(header) { var explicit = header.querySelector( '.trf-header-sticky-target, .trf-header__nav-row, [class*="__nav-row"]' ); if (explicit) { return explicit; } var nav = header.querySelector('nav[aria-label*="Primary" i], nav[aria-label*="primary" i]'); if (nav) { var row = nav.closest('[class*="__nav-row"], [class*="__nav-inner"], [class*="__bar"]'); return row || nav.parentElement || nav; } var children = header.children; for (var i = children.length - 1; i >= 0; i--) { var child = children[i]; if (child && child.querySelector && child.querySelector('nav')) { return child; } } return null; } function countBands(header) { var count = 0; var children = header.children; for (var i = 0; i < children.length; i++) { var node = children[i]; if (!node || node.nodeType !== 1) { continue; } if (node.tagName === 'SCRIPT' || node.tagName === 'STYLE') { continue; } count++; } return count; } function inferMode(header) { var bands = countBands(header); var height = header.offsetHeight || 0; if (bands >= 2 || findNavRow(header) !== header && findNavRow(header) !== null && bands >= 2) { return 'nav-only'; } if (height > MAX_FULL_BAR_PX || height > window.innerHeight * MAX_FULL_BAR_RATIO) { return findNavRow(header) ? 'nav-only' : 'none'; } if (height <= 96 && bands <= 1) { return 'full-bar'; } return 'none'; } function resolveMode(header) { var declared = normalizeMode(header.getAttribute('data-trf-sticky')); if (declared === 'none' || declared === 'off' || declared === 'false') { return 'none'; } if (declared === 'nav-only' || declared === 'nav') { return 'nav-only'; } if (declared === 'full-bar' || declared === 'full') { return 'full-bar'; } return inferMode(header); } function initHeaderSticky() { var header = document.querySelector( 'header.trf-site-header, header#site-header, body > header[role="banner"]' ); if (!header) { return; } var mode = resolveMode(header); if (mode === 'none') { clearSticky(header); return; } document.documentElement.style.setProperty( '--trf-admin-bar-offset', adminBarOffset() + 'px' ); var navRow = findNavRow(header); clearSticky(header); if (mode === 'nav-only') { if (navRow) { applySticky(navRow); } return; } var height = header.offsetHeight || 0; if ( height > MAX_FULL_BAR_PX || height > window.innerHeight * MAX_FULL_BAR_RATIO ) { if (navRow) { applySticky(navRow); } return; } applySticky(header); } function onReady() { initHeaderSticky(); window.addEventListener('resize', initHeaderSticky, { passive: true }); } if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', onReady); } else { onReady(); } })();