From 21f4241ad746bbad7819365f8a5af2cdad5e148c Mon Sep 17 00:00:00 2001 From: TheCoderGeekSquadLeader Date: Sun, 22 Nov 2026 16:15:20 +0000 Subject: [PATCH 1/2] fix(ui) : keep mobile navigation menu sticky while scrolling Signed-off-by: TheCoderGeekSquadLeader --- apps/site/components/NavBar.tsx | 4 +- apps/site/tests/e2e/general-behavior.spec.ts | 43 +++++++++++++++++++ .../src/Containers/NavBar/index.module.css | 31 +++++++++++-- .../src/Containers/NavBar/index.tsx | 37 +++++++++++++++- 4 files changed, 109 insertions(+), 6 deletions(-) diff --git a/apps/site/components/NavBar.tsx b/apps/site/components/NavBar.tsx index 6473bf42d6ab7..da01fb94797e8 100644 --- a/apps/site/components/NavBar.tsx +++ b/apps/site/components/NavBar.tsx @@ -46,7 +46,7 @@ const NavBar: FC = () => { replace(pathname!, { locale: locale.code }); return ( -
+ <> {t('components.common.skipToContent')} @@ -97,7 +97,7 @@ const NavBar: FC = () => { -
+ ); }; diff --git a/apps/site/tests/e2e/general-behavior.spec.ts b/apps/site/tests/e2e/general-behavior.spec.ts index fc0ca9107daa9..f3a0643b99354 100644 --- a/apps/site/tests/e2e/general-behavior.spec.ts +++ b/apps/site/tests/e2e/general-behavior.spec.ts @@ -164,12 +164,55 @@ test.describe('Node.js Website', () => { const navLinks = page.locator(locators.navLinksLocator); // Toggle menu open and verify it's visible + const contentBefore = await page.locator('main').boundingBox(); await mobileToggle.click(); await expect(navLinks.first()).toBeVisible(); + const contentAfter = await page.locator('main').boundingBox(); + expect(contentAfter?.y).toBe(contentBefore?.y); + const menuBounds = await page + .locator(locators.navLinksLocator) + .boundingBox(); + const availableHeight = await page.evaluate( + () => + window.innerHeight - + parseFloat( + getComputedStyle(document.documentElement).getPropertyValue( + '--header-height' + ) + ) + ); + expect(menuBounds?.height).toBeLessThan(availableHeight); // Toggle menu closed and verify it's hidden await mobileToggle.click(); await expect(navLinks.first()).not.toBeVisible(); }); + + test('should keep the mobile menu accessible after scrolling', async ({ + page, + }) => { + await page.setViewportSize({ width: 375, height: 667 }); + await page.goto('/en/download'); + + await page.evaluate(() => window.scrollTo(0, document.body.scrollHeight)); + await expect + .poll(() => page.evaluate(() => window.scrollY)) + .toBeGreaterThan(0); + + const mobileToggle = page.getByRole('button', { + name: locators.mobileMenuToggleName, + }); + await expect(mobileToggle).toBeInViewport(); + + await mobileToggle.click(); + + await expect(page.locator(locators.navLinksLocator)).toBeVisible(); + + await mobileToggle.click(); + await expect(page.locator(locators.navLinksLocator)).not.toBeVisible(); + await expect + .poll(() => page.evaluate(() => window.scrollY)) + .toBeGreaterThan(0); + }); }); }); diff --git a/packages/ui-components/src/Containers/NavBar/index.module.css b/packages/ui-components/src/Containers/NavBar/index.module.css index e445929b15c7e..dfd3826b7e769 100644 --- a/packages/ui-components/src/Containers/NavBar/index.module.css +++ b/packages/ui-components/src/Containers/NavBar/index.module.css @@ -1,8 +1,13 @@ @reference "../../styles/index.css"; .container { - @apply border-neutral-200 + @apply sticky + top-0 + z-50 + border-neutral-200 bg-white + xl:static + xl:z-auto xl:h-16 xl:border-b xl:px-8 @@ -12,6 +17,7 @@ .innerContainer { @apply max-w-10xl 3xl:max-w-12xl + relative xl:mx-auto xl:flex xl:h-16 @@ -62,13 +68,32 @@ } .main { - @apply flex-1 + @apply fixed + top-[var(--header-height)] + right-0 + left-0 + z-50 + max-h-[calc(100dvh-var(--header-height))] + flex-1 flex-col gap-4 + overflow-y-auto + overscroll-contain + bg-white + xl:relative + xl:top-auto + xl:right-auto + xl:bottom-auto + xl:left-auto xl:flex + xl:max-h-none xl:flex-row xl:items-center - xl:justify-between; + xl:justify-between + xl:overflow-visible + xl:bg-transparent + dark:bg-neutral-950 + xl:dark:bg-transparent; } .navItems { diff --git a/packages/ui-components/src/Containers/NavBar/index.tsx b/packages/ui-components/src/Containers/NavBar/index.tsx index b2eac53f34ffd..4ae2af3c451a2 100644 --- a/packages/ui-components/src/Containers/NavBar/index.tsx +++ b/packages/ui-components/src/Containers/NavBar/index.tsx @@ -2,7 +2,7 @@ import Hamburger from '@heroicons/react/24/solid/Bars3Icon'; import XMark from '@heroicons/react/24/solid/XMarkIcon'; import * as Label from '@radix-ui/react-label'; import classNames from 'classnames'; -import { useState } from 'react'; +import { useEffect, useState } from 'react'; import NavItem from '#ui/Containers/NavBar/NavItem'; @@ -58,6 +58,41 @@ const NavBar: FC> = ({ const activeLink = getActiveNavLink(navItems ?? [], pathname); + useEffect(() => { + if (!isMenuOpen || window.matchMedia('(min-width: 1280px)').matches) { + return; + } + + const scrollPosition = window.scrollY; + const scrollbarWidth = + window.innerWidth - document.documentElement.clientWidth; + const { body } = document; + const previousStyles = { + position: body.style.position, + top: body.style.top, + width: body.style.width, + overflow: body.style.overflow, + paddingRight: body.style.paddingRight, + }; + + body.style.position = 'fixed'; + body.style.top = `-${scrollPosition}px`; + body.style.width = '100%'; + body.style.overflow = 'hidden'; + if (scrollbarWidth > 0) { + body.style.paddingRight = `${scrollbarWidth}px`; + } + + return () => { + body.style.position = previousStyles.position; + body.style.top = previousStyles.top; + body.style.width = previousStyles.width; + body.style.overflow = previousStyles.overflow; + body.style.paddingRight = previousStyles.paddingRight; + window.scrollTo(0, scrollPosition); + }; + }, [isMenuOpen]); + return (