Mark subpaths as active in the sidebar
All checks were successful
Tests / test (push) Successful in 24s
All checks were successful
Tests / test (push) Successful in 24s
This commit is contained in:
parent
b074a3413a
commit
d093d422ce
1 changed files with 22 additions and 13 deletions
|
@ -2,21 +2,30 @@
|
||||||
* This script marks the current path as active in the sidebar.
|
* This script marks the current path as active in the sidebar.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
const markActive = (link) => {
|
||||||
|
const parentItem = link.closest('.sidebar-item');
|
||||||
|
if (parentItem) {
|
||||||
|
parentItem.classList.add('active');
|
||||||
|
} else {
|
||||||
|
link.classList.add('active');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const checkLink = (fuzzy) => {
|
||||||
|
}
|
||||||
|
|
||||||
document.addEventListener('DOMContentLoaded', () => {
|
document.addEventListener('DOMContentLoaded', () => {
|
||||||
const currentPath = window.location.pathname;
|
const currentPath = window.location.pathname;
|
||||||
const sidebarLinks = document.querySelectorAll('.sidebar-link');
|
const sidebarLinks = [...document.querySelectorAll('a.sidebar-link')]
|
||||||
|
|
||||||
sidebarLinks.forEach(link => {
|
const exactMatches = sidebarLinks.filter(link => link.getAttribute('href') === currentPath)
|
||||||
// Skip links that are inside buttons (like logout)
|
if (exactMatches.length > 0) {
|
||||||
if (link.tagName === 'BUTTON') return;
|
markActive(exactMatches[0])
|
||||||
|
} else {
|
||||||
if (link.getAttribute('href') === currentPath) {
|
fuzzyMatches = sidebarLinks.filter(link => currentPath.startsWith(link.getAttribute('href')))
|
||||||
const parentItem = link.closest('.sidebar-item');
|
if (fuzzyMatches.length > 0) {
|
||||||
if (parentItem) {
|
const longestMatch = fuzzyMatches.sort((a, b) => b.href.length - a.href.length)[0]
|
||||||
parentItem.classList.add('active');
|
markActive(longestMatch)
|
||||||
} else {
|
|
||||||
link.classList.add('active');
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
})
|
}
|
||||||
})
|
})
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue