From eb546e3e0392f886d6482f186bad8ce6640998a5 Mon Sep 17 00:00:00 2001 From: Luffy <52o@qq52o.cn> Date: Thu, 12 Jun 2025 16:03:01 +0800 Subject: [PATCH] refactor: optimize toggleSidebar trigger conditions --- src/core/event/index.js | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/core/event/index.js b/src/core/event/index.js index fe4076f05..c524c051d 100644 --- a/src/core/event/index.js +++ b/src/core/event/index.js @@ -343,8 +343,7 @@ export function Events(Base) { onNavigate(source) { const { auto2top, topMargin } = this.config; const { path, query } = this.route; - - this.#markSidebarActiveElm(); + const activeSidebarElm = this.#markSidebarActiveElm(); // Note: Scroll position set by browser on forward/back (i.e. "history") if (source !== 'history') { @@ -371,13 +370,20 @@ export function Events(Base) { } } + const isNavigate = source === 'navigate'; + const hasId = 'id' in query; + const noSubSidebar = !activeSidebarElm?.querySelector('.app-sub-sidebar'); + // Clicked anchor link - if (path === '/' || (query.id && source === 'navigate')) { - isMobile() && this.#toggleSidebar(false); + const shouldCloseSidebar = + path === '/' || (isNavigate && (hasId || noSubSidebar)); + + if (shouldCloseSidebar && isMobile()) { + this.#toggleSidebar(false); } // Clicked anchor link or page load with anchor ID - if (query.id || source === 'navigate') { + if (hasId || isNavigate) { this.#focusContent(); } }