From e055da422402d358880a074d20839dd685bbb7a1 Mon Sep 17 00:00:00 2001 From: tarunsinghofficial Date: Fri, 28 Feb 2025 21:12:24 +0530 Subject: [PATCH 1/7] Improve navbar overflow handling and simplify link width measurement --- src/components/navigation/Header.svelte | 148 ++++++++++++++++++------ 1 file changed, 113 insertions(+), 35 deletions(-) diff --git a/src/components/navigation/Header.svelte b/src/components/navigation/Header.svelte index 5e18b6c0..70dfac05 100644 --- a/src/components/navigation/Header.svelte +++ b/src/components/navigation/Header.svelte @@ -5,10 +5,15 @@ PUBLIC_NAV_BAR_LINKS } from '$env/static/public'; + import { onMount } from 'svelte'; import ThemeSwitcher from '$lib/ThemeSwitch/ThemeSwitcher.svelte'; import MobileMenu from './MobileMenu.svelte'; let isMobileMenuOpen = $state(false); + let shouldShowMobile = $state(false); + let navContainer; + let linksContainer; + let linksWidthCache = 0; let headerLinks = $state(null); @@ -19,14 +24,81 @@ function toggleNavbar() { isMobileMenuOpen = !isMobileMenuOpen; } + + function checkOverflow() { + if (!navContainer) return; + + const navWidth = navContainer.clientWidth; + const logoWidth = navContainer.querySelector('.logo-container')?.clientWidth || 0; + const themeWidth = 48; + const availableWidth = navWidth - logoWidth - themeWidth - 32; + + let linksWidth = 0; + if (linksContainer) { + linksWidth = linksContainer.scrollWidth; + linksWidthCache = linksWidth; + } else { + linksWidth = linksWidthCache; + } + + const newShouldShowMobileMenu = linksWidth > availableWidth; + + if (shouldShowMobile !== newShouldShowMobileMenu) { + shouldShowMobile = newShouldShowMobileMenu; + } + } + + function measureLinksWidth() { + if (headerLinks && !linksContainer) { + const tempDiv = document.createElement('div'); + tempDiv.style.position = 'absolute'; + tempDiv.style.visibility = 'hidden'; + tempDiv.style.display = 'flex'; + tempDiv.style.gap = '1rem'; + + if (Object.keys(headerLinks).length > 0) { + Object.keys(headerLinks).forEach((key) => { + const linkDiv = document.createElement('div'); + linkDiv.style.padding = '0.25rem 0.5rem'; + linkDiv.style.flexShrink = '0'; + linkDiv.textContent = key; + tempDiv.appendChild(linkDiv); + }); + } else { + const linkDiv = document.createElement('div'); + linkDiv.style.width = '600px'; + tempDiv.appendChild(linkDiv); + } + + document.body.appendChild(tempDiv); + linksWidthCache = tempDiv.scrollWidth; + document.body.removeChild(tempDiv); + } + } + + onMount(() => { + measureLinksWidth(); + checkOverflow(); + + const headerResizeObserver = new ResizeObserver(() => { + checkOverflow(); + }); + + headerResizeObserver.observe(navContainer); + + return () => { + headerResizeObserver.disconnect(); + }; + });
-
-
-
+
+
+
{PUBLIC_OBA_REGION_NAME} @@ -34,42 +106,48 @@ {PUBLIC_OBA_REGION_NAME}
- -
- -
- - From f0593e44ade5a8bf8c68df414ca1d6a33c36be8a Mon Sep 17 00:00:00 2001 From: tarunsinghofficial Date: Fri, 28 Feb 2025 21:26:54 +0530 Subject: [PATCH 2/7] linting --- src/components/navigation/Header.svelte | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/components/navigation/Header.svelte b/src/components/navigation/Header.svelte index 70dfac05..05a97ad3 100644 --- a/src/components/navigation/Header.svelte +++ b/src/components/navigation/Header.svelte @@ -93,12 +93,12 @@
-
-
-
+
+
+
{PUBLIC_OBA_REGION_NAME} @@ -110,11 +110,11 @@ {#if !shouldShowMobile}
-
+
{#if headerLinks && Object.keys(headerLinks).length > 0} {#each Object.entries(headerLinks) as [key, value]} - @@ -129,7 +129,7 @@ {#if shouldShowMobile}