Skip to content

Commit

Permalink
Refactor sidebar initialization in DoubleSidebar and SingleSidebar co…
Browse files Browse the repository at this point in the history
…mponents
  • Loading branch information
Adammatthiesen committed Dec 22, 2024
1 parent b4202fc commit 58edf6d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 36 deletions.
19 changes: 1 addition & 18 deletions packages/studiocms_dashboard/src/components/DoubleSidebar.astro
Original file line number Diff line number Diff line change
Expand Up @@ -72,24 +72,7 @@ const t = useTranslations(lang, '@studiocms/dashboard:sidebar');
<script>
import { DoubleSidebarHelper } from "@studiocms/ui/components";
import { DropdownHelper } from "@studiocms/ui/components";

function removeTrailingSlash(url: string) {
return url.replace(/\/$/, '');
}

function sidebarLinks() {
const sidebar = document.querySelector<HTMLElement>('.sidebar')!;
const sidebarLinks = sidebar.querySelectorAll<HTMLAnchorElement>('a');

for (const link of sidebarLinks) {
link.classList.remove('active');
if (removeTrailingSlash(window.location.pathname) === removeTrailingSlash(new URL(link.href).pathname)) {
link.classList.add('active');
}
}
}

// sidebarLinks();
import { sidebarLinks } from "./component-scripts/sidebarLinks";

document.addEventListener('astro:page-load', () => {
if (document.getElementById('sui-sidebars')) {
Expand Down
19 changes: 1 addition & 18 deletions packages/studiocms_dashboard/src/components/SingleSidebar.astro
Original file line number Diff line number Diff line change
Expand Up @@ -62,24 +62,7 @@ const t = useTranslations(lang, '@studiocms/dashboard:sidebar');
<script>
import { SingleSidebarHelper } from "@studiocms/ui/components";
import { DropdownHelper } from "@studiocms/ui/components";

function removeTrailingSlash(url: string) {
return url.replace(/\/$/, '');
}

function sidebarLinks() {
const sidebar = document.querySelector<HTMLElement>('.sidebar')!;
const sidebarLinks = sidebar.querySelectorAll<HTMLAnchorElement>('a');

for (const link of sidebarLinks) {
link.classList.remove('active');
if (removeTrailingSlash(window.location.pathname) === removeTrailingSlash(new URL(link.href).pathname)) {
link.classList.add('active');
}
}
}

// sidebarLinks();
import { sidebarLinks } from "./component-scripts/sidebarLinks";

document.addEventListener('astro:page-load', () => {
if (document.getElementById('sui-sidebar')) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
function removeTrailingSlash(url: string) {
return url.replace(/\/$/, '');
}

export function sidebarLinks() {
const sidebar = document.querySelector<HTMLElement>('.sidebar');
if (!sidebar) {
return;
}
const sidebarLinks = sidebar.querySelectorAll<HTMLAnchorElement>('a');

for (const link of sidebarLinks) {
link.classList.remove('active');
if (
removeTrailingSlash(window.location.pathname) ===
removeTrailingSlash(new URL(link.href).pathname)
) {
link.classList.add('active');
}
}
}

0 comments on commit 58edf6d

Please sign in to comment.