Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: table content active link not showing properly #674

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 52 additions & 7 deletions www/src/components/navigation/tableOfContents.astro
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,58 @@ headings = [{ depth: 2, slug: "overview", text: title }, ...headings].filter(
).at(-1);
// this occurs when the id = "toc-heading"
if (!tocItem) return;
juliusmarminge marked this conversation as resolved.
Show resolved Hide resolved
const func = entry.isIntersecting ? "add" : "remove";
tocItem.classList[func](
"font-medium",
"text-t3-purple-700",
"dark:text-t3-purple-100",
);
tocItem.parentElement.classList[func]("border-t3-purple-300/100");

if (entry.isIntersecting) {
// get current all active elements
const currentActiveElements = Array.from(
document.querySelectorAll(
`[data-current-active-tab-content-item="true"]`,
),
);

// get current all parent active elements
const currentActiveParentElements = Array.from(
document.querySelectorAll(
`[data-current-active-parent-tab-content-item="true"]`,
),
);

// if has active elements
if (currentActiveElements.length) {
// loop through all elements and remove class and attr
for (const el of currentActiveElements) {
el.classList.remove(
"font-medium",
"text-t3-purple-700",
"dark:text-t3-purple-100",
);
el.removeAttribute("data-current-active-tab-content-item");
}
}
// if has active parent elements
if (currentActiveParentElements.length) {
// loop through all parent elements and remove class and attr
for (const parentEl of currentActiveParentElements) {
parentEl.classList.remove("border-t3-purple-300/100");
parentEl.removeAttribute(
"data-current-active-parent-tab-content-item",
);
}
}

tocItem.classList.add(
"font-medium",
"text-t3-purple-700",
"dark:text-t3-purple-100",
);
tocItem.parentElement.classList.add("border-t3-purple-300/100");
// set data attr to active element also will helpful when need to remove active state
tocItem.setAttribute("data-current-active-tab-content-item", true);
tocItem.parentElement.setAttribute(
"data-current-active-parent-tab-content-item",
true,
);
}
});
},
{
Expand Down