From 5919dff9a939959d5b2c209e6bfe052fbe2e5711 Mon Sep 17 00:00:00 2001 From: admin Date: Wed, 30 Sep 2026 02:07:04 +0800 Subject: [PATCH] fix: lock mobile admin sidebar viewport and restore scrolling --- web/public/theme/venix/css/admin-home.css | 34 +- .../views/venix/admin/static_footer.tpl | 100 +++-- .../views/venix/admin/static_header.tpl | 6 +- web/resources/views/venix/layout_assets.tpl | 2 +- web/tests/AdminOverviewContractTest.php | 14 +- web/tests/manual/admin_sidebar_fixture.py | 353 ++++++++++++++++++ 6 files changed, 442 insertions(+), 67 deletions(-) create mode 100644 web/tests/manual/admin_sidebar_fixture.py diff --git a/web/public/theme/venix/css/admin-home.css b/web/public/theme/venix/css/admin-home.css index 8a1c617f..ca0ae531 100644 --- a/web/public/theme/venix/css/admin-home.css +++ b/web/public/theme/venix/css/admin-home.css @@ -98,6 +98,12 @@ display: none; } +html.admin-sidebar-scroll-locked { + overflow: hidden; + overscroll-behavior: none; + scroll-behavior: auto !important; +} + .admin-profile { display: flex; align-items: center; @@ -2114,10 +2120,13 @@ body.billing-detail-open { .admin-template .app-sidebar-menu { position: fixed; + min-height: 100vh; + min-height: 100dvh; width: 280px; max-width: calc(100vw - 54px); transform: translateX(-100%); transition: transform 0.22s ease; + overscroll-behavior-y: contain; } .admin-template .content-page { @@ -2130,6 +2139,11 @@ body.billing-detail-open { } .admin-template-body.admin-sidebar-open { + position: fixed; + top: var(--admin-sidebar-scroll-top, 0px); + left: 0; + right: 0; + width: 100%; overflow: hidden; } @@ -2146,26 +2160,12 @@ body.billing-detail-open { position: fixed; inset: 0; height: auto; + min-height: 100vh; + min-height: 100dvh; z-index: 1000; border: 0; background: rgba(0, 8, 26, 0.48); - } - - .admin-template-body.admin-sidebar-navigating .admin-sidebar-backdrop { - background: #7e838e; - cursor: progress; - transition: none !important; - } - - .admin-template-body.admin-sidebar-navigating .app-sidebar-menu { - pointer-events: none; - transition: none !important; - } - - .admin-template-body.admin-sidebar-navigating .topbar-custom, - .admin-template-body.admin-sidebar-navigating .content-page { - visibility: hidden !important; - transition: none !important; + touch-action: none; } .admin-summary-grid, diff --git a/web/resources/views/venix/admin/static_footer.tpl b/web/resources/views/venix/admin/static_footer.tpl index 3b0df988..4476c071 100644 --- a/web/resources/views/venix/admin/static_footer.tpl +++ b/web/resources/views/venix/admin/static_footer.tpl @@ -26,10 +26,46 @@ var sidebarToggle = document.querySelector('.button-toggle-menu'); var sidebarBackdrop = document.querySelector('.admin-sidebar-backdrop'); var sidebarLinks = document.querySelectorAll('.app-sidebar-menu a'); - var sidebarNavigationPending = false; + var mobileSidebar = window.matchMedia('(max-width: 991.98px)'); + var savedPageScroll = null; + var scrollHistoryKey = '__venixAdminSidebarScroll'; + + function rememberPageScroll(scroll) { + var state = window.history.state; + if (state !== null && (typeof state !== 'object' || Array.isArray(state))) return; + if (!scroll && (!state || !state[scrollHistoryKey])) return; + var nextState = Object.assign({}, state); + if (scroll) nextState[scrollHistoryKey] = scroll; + else delete nextState[scrollHistoryKey]; + try { + window.history.replaceState(nextState, ''); + } catch (error) { + // History may be unavailable; menu controls must still work. + } + } + + function openSidebar() { + if (!mobileSidebar.matches || savedPageScroll !== null) return; + + savedPageScroll = { x: window.scrollX, y: window.scrollY }; + // Fixed-body locking reports scrollY=0; retain the original history position. + rememberPageScroll(savedPageScroll); + document.body.style.setProperty('--admin-sidebar-scroll-top', -savedPageScroll.y + 'px'); + document.documentElement.classList.add('admin-sidebar-scroll-locked'); + document.body.classList.add('admin-sidebar-open'); + if (sidebarToggle) sidebarToggle.setAttribute('aria-expanded', 'true'); + } function closeSidebar() { document.body.classList.remove('admin-sidebar-open'); + document.body.style.removeProperty('--admin-sidebar-scroll-top'); + if (savedPageScroll !== null) { + window.scrollTo(savedPageScroll.x, savedPageScroll.y); + savedPageScroll = null; + rememberPageScroll(null); + } + document.documentElement.classList.remove('admin-sidebar-scroll-locked'); + if (sidebarToggle) sidebarToggle.setAttribute('aria-expanded', 'false'); } function closeSidebarImmediately() { @@ -42,40 +78,23 @@ }); } - function resetSidebarNavigation() { - sidebarNavigationPending = false; - document.body.classList.remove('admin-sidebar-navigating'); - document.body.removeAttribute('aria-busy'); - } - - function startSidebarNavigation(target) { - if (sidebarNavigationPending) return; - - sidebarNavigationPending = true; - document.body.classList.add('admin-sidebar-navigating'); - document.body.setAttribute('aria-busy', 'true'); - window.requestAnimationFrame(function () { - window.requestAnimationFrame(function () { - window.location.assign(target.href); - }); - }); - } - if (sidebarToggle) { sidebarToggle.addEventListener('click', function () { - document.body.classList.toggle('admin-sidebar-open'); + if (document.body.classList.contains('admin-sidebar-open')) { + closeSidebar(); + } else { + openSidebar(); + } }); } if (sidebarBackdrop) { - sidebarBackdrop.addEventListener('click', function () { - if (!sidebarNavigationPending) closeSidebar(); - }); + sidebarBackdrop.addEventListener('click', closeSidebar); } sidebarLinks.forEach(function (link) { link.addEventListener('click', function (event) { - if (window.innerWidth > 991 + if (!mobileSidebar.matches || event.defaultPrevented || event.button !== 0 || event.metaKey @@ -87,11 +106,6 @@ return; } - if (sidebarNavigationPending) { - event.preventDefault(); - return; - } - var target = new URL(link.href, window.location.href); var current = new URL(window.location.href); if (target.protocol !== 'http:' && target.protocol !== 'https:') { @@ -104,19 +118,35 @@ event.preventDefault(); } closeSidebar(); - return; } - - event.preventDefault(); - startSidebarNavigation(target); }); }); + mobileSidebar.addEventListener('change', function (event) { + if (!event.matches) closeSidebarImmediately(); + }); + + document.addEventListener('keydown', function (event) { + if (event.key === 'Escape' && savedPageScroll !== null) { + closeSidebar(); + if (sidebarToggle) sidebarToggle.focus({ preventScroll: true }); + } + }); + window.addEventListener('pageshow', function (event) { if (event.persisted) { - resetSidebarNavigation(); closeSidebarImmediately(); + return; } + var navigation = window.performance.getEntriesByType('navigation')[0]; + var scroll = window.history.state && window.history.state[scrollHistoryKey]; + if (!navigation || (navigation.type !== 'back_forward' && navigation.type !== 'reload') + || !scroll || !Number.isFinite(scroll.x) || !Number.isFinite(scroll.y)) return; + // Run after the browser's native history restoration, which saved the locked position. + window.requestAnimationFrame(function () { + window.scrollTo({ left: scroll.x, top: scroll.y, behavior: 'instant' }); + rememberPageScroll(null); + }); }); })(); diff --git a/web/resources/views/venix/admin/static_header.tpl b/web/resources/views/venix/admin/static_header.tpl index 240e73a5..e8c354da 100644 --- a/web/resources/views/venix/admin/static_header.tpl +++ b/web/resources/views/venix/admin/static_header.tpl @@ -54,7 +54,7 @@ var query = params.toString(); var nextUrl = window.location.pathname + (query ? '?' + query : '') + window.location.hash; - window.history.replaceState(null, '', nextUrl); + window.history.replaceState(window.history.state, '', nextUrl); } function bind(input, options) { @@ -101,7 +101,7 @@
-
@@ -117,7 +117,7 @@
-
+