Skip to content

Commit

Permalink
Mark active app nav element
Browse files Browse the repository at this point in the history
  • Loading branch information
jhildenbiddle committed Apr 15, 2024
1 parent e9d20be commit 876c587
Showing 1 changed file with 42 additions and 11 deletions.
53 changes: 42 additions & 11 deletions src/core/event/index.js
Expand Up @@ -303,6 +303,7 @@ export function Events(Base) {
// Update page title
dom.$.title = currentTitle || this.#title;

this.#markAppNavActiveElm();
this.#markSidebarCurrentPage();
this.#initHeadings();
}
Expand Down Expand Up @@ -380,24 +381,52 @@ export function Events(Base) {
focusEl?.focus(settings);
}

/**
* Marks the active app nav item
*
* @param {string} [href] Matching element HREF value. If unspecified,
* defaults to the current path (without query params)
* @returns Element|undefined
*/
#markAppNavActiveElm() {
const href = this.router.toURL(this.route.path);
const navElm = dom.find('nav.app-nav');

if (!navElm) {
return;
}

const newActive = dom
.findAll(navElm, 'a')
.sort((a, b) => b.href.length - a.href.length)
.find(a => href.includes(decodeURI(a.getAttribute('href'))));
const oldActive = dom.find(navElm, 'li.active');

oldActive?.classList.remove('active');
newActive?.classList.add('active');

return newActive;
}

/**
* Marks the active sidebar item
*
* @param {string} [href] Matching sidebar element HREF value. If
* unspecified, defaults to the current path (with query params)
* @returns Element
* @param {string} [href] Matching element HREF value. If unspecified,
* defaults to the current path (with query params)
* @returns Element|undefined
*/
#markSidebarActiveElm(href) {
href ??= this.router.toURL(this.router.getCurrentPath());

const sidebar = dom.find('.sidebar');
const oldActive = dom.find(sidebar, 'li.active');
const newActive = dom.find(`.sidebar a[href='${href}']`)?.closest('li');

if (!newActive || newActive === oldActive) {
if (!sidebar) {
return;
}

const oldActive = dom.find(sidebar, 'li.active');
const newActive = dom.find(sidebar, `a[href='${href}']`)?.closest('li');

oldActive?.classList.remove('active');
newActive?.classList.add('active');

Expand All @@ -409,19 +438,21 @@ export function Events(Base) {
*
* @param {string} [href] Matching sidebar element HREF value. If
* unspecified, defaults to the current path (without query params)
* @returns Element
* @returns Element|undefined
*/
#markSidebarCurrentPage(href) {
href ??= this.router.toURL(this.route.path);

const path = href?.split('?')[0];
const oldPage = dom.find('.sidebar li[aria-current]');
const newPage = dom.find(`.sidebar a[href='${path}']`)?.closest('li');
const sidebar = dom.find('.sidebar');

if (!newPage || newPage === oldPage) {
if (!sidebar) {
return;
}

const path = href?.split('?')[0];
const oldPage = dom.find(sidebar, 'li[aria-current]');
const newPage = dom.find(sidebar, `a[href='${path}']`)?.closest('li');

oldPage?.removeAttribute('aria-current');
newPage?.setAttribute('aria-current', 'page');

Expand Down

0 comments on commit 876c587

Please sign in to comment.