Add auto-active links to sidebar

This commit is contained in:
Tobias Kunze 2025-03-20 10:51:56 +01:00
parent 6e6f2d0993
commit 1d4162582a
2 changed files with 24 additions and 1 deletions

View file

@ -1,4 +1,4 @@
{% load i18n %}
{% load i18n static %}
<div id="sidebar">
<div id="sidebar">
<div class="sidebar-wrapper active">
@ -132,3 +132,4 @@
</div>
</div>
</div>
<script src="{% static 'js/sidebar.js' %}" defer></script>

View file

@ -0,0 +1,22 @@
/**
* This script marks the current path as active in the sidebar.
*/
document.addEventListener('DOMContentLoaded', () => {
const currentPath = window.location.pathname;
const sidebarLinks = document.querySelectorAll('.sidebar-link');
sidebarLinks.forEach(link => {
// Skip links that are inside buttons (like logout)
if (link.tagName === 'BUTTON') return;
if (link.getAttribute('href') === currentPath) {
const parentItem = link.closest('.sidebar-item');
if (parentItem) {
parentItem.classList.add('active');
} else {
link.classList.add('active');
}
}
})
})