Skip to content

Commit 71ac41e

Browse files
authored
refactor: optimize toggleSidebar trigger conditions (#2557)
1 parent 3d53203 commit 71ac41e

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

src/core/event/index.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -343,8 +343,7 @@ export function Events(Base) {
343343
onNavigate(source) {
344344
const { auto2top, topMargin } = this.config;
345345
const { path, query } = this.route;
346-
347-
this.#markSidebarActiveElm();
346+
const activeSidebarElm = this.#markSidebarActiveElm();
348347

349348
// Note: Scroll position set by browser on forward/back (i.e. "history")
350349
if (source !== 'history') {
@@ -371,13 +370,20 @@ export function Events(Base) {
371370
}
372371
}
373372

373+
const isNavigate = source === 'navigate';
374+
const hasId = 'id' in query;
375+
const noSubSidebar = !activeSidebarElm?.querySelector('.app-sub-sidebar');
376+
374377
// Clicked anchor link
375-
if (path === '/' || (query.id && source === 'navigate')) {
376-
isMobile() && this.#toggleSidebar(false);
378+
const shouldCloseSidebar =
379+
path === '/' || (isNavigate && (hasId || noSubSidebar));
380+
381+
if (shouldCloseSidebar && isMobile()) {
382+
this.#toggleSidebar(false);
377383
}
378384

379385
// Clicked anchor link or page load with anchor ID
380-
if (query.id || source === 'navigate') {
386+
if (hasId || isNavigate) {
381387
this.#focusContent();
382388
}
383389
}

0 commit comments

Comments
 (0)