Skip to content
This repository has been archived by the owner on Jan 8, 2025. It is now read-only.

Commit

Permalink
Fix issue in determining if an entry is selected
Browse files Browse the repository at this point in the history
Recursively descend through child entries instead of using URL path
segments, which don't necessarily map on to the recursive `entries`
structure.
  • Loading branch information
ptgott committed Aug 7, 2024
1 parent b1afbef commit fea0a93
Showing 1 changed file with 20 additions and 8 deletions.
28 changes: 20 additions & 8 deletions layouts/DocsPage/Navigation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,13 @@ const DocsNavigationItems = ({
<>
{!!entries.length &&
entries.map((entry) => {
// Determine whether to highlight the entry in the navigation sidebar.
// We highlight an entry if:
// - It is the currently selected entry.
// - One of its entries is either the currently selected entry or the
// parent of the currently selected entry.
const selected = entry.slug === docPath;
const active =
selected ||
entry.entries?.some((entry) => {
return (
docPath.startsWith(entry.slug) ||
docPath.startsWith(dirname(entry.slug))
);
});
const active = hasChildEntry(entry, docPath);

return (
<li key={entry.slug}>
Expand Down Expand Up @@ -90,6 +88,20 @@ const DocsNavigationItems = ({
);
};

// hasChildEntry recursively descends through entry to determine if it or one of
// its children has the provided slug.
function hasChildEntry(entry: NavigationCategory, slug: string) {
if (entry.slug === slug) {
return true;
}
if (!entry.entries) {
return false;
}
return entry.entries.some((e) => {
return hasChildEntry(e, slug);
});
}

interface DocNavigationCategoryProps extends NavigationCategory {
id: number;
opened: boolean;
Expand Down

0 comments on commit fea0a93

Please sign in to comment.