File tree Expand file tree Collapse file tree 1 file changed +8
-17
lines changed
Expand file tree Collapse file tree 1 file changed +8
-17
lines changed Original file line number Diff line number Diff line change @@ -35,23 +35,14 @@ import Article from "../layouts/templates/Article.astro";
3535 <script >
3636 function register() {
3737 const localeElements = document.querySelectorAll("[data-translation]");
38-
39- for (const element of localeElements) {
40- if (!(element instanceof HTMLElement)) continue;
41- const { translation } = element.dataset;
42- if (!translation) continue;
43-
44- if (window.location.pathname.startsWith(`/${translation}/`)) {
45- element.classList.add("active");
46- break;
47- }
48- }
49-
50- if (!Array.from(localeElements).some((el) => el instanceof HTMLElement && el.classList.contains("active"))) {
51- const defaultElement = document.querySelector(`[data-translation="de"]`);
52- if (defaultElement instanceof HTMLElement) {
53- defaultElement.classList.add("active");
54- }
38+ const pathname = window.location.pathname;
39+ const activeElement =
40+ Array.from(localeElements).find(
41+ (el) => el instanceof HTMLElement && pathname.startsWith(`/${el.dataset.translation}/`),
42+ ) || document.querySelector('[data-translation="de"]');
43+
44+ if (activeElement instanceof HTMLElement) {
45+ activeElement.classList.add("active");
5546 }
5647 }
5748
You can’t perform that action at this time.
0 commit comments