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

(#997) Highlight Active page #1011

Merged
merged 1 commit into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
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
7 changes: 2 additions & 5 deletions src/components/sidebar/SidebarListItem.astro
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
// Scripts and types
import type { ContentTree } from '@scripts/types';
import { isActivePage } from '@scripts/is-active-page';

interface Props {
content: ContentTree;
Expand All @@ -9,12 +10,8 @@ interface Props {

const { content, paddingClass } = Astro.props;
const activeSlug = Astro.url.pathname;

const isActivePage = () => {
return content.slug === activeSlug || content.slug.slice(0, -1) === activeSlug;
};
---

<li class={`nav-item nav-item-blue`}>
<a class={`nav-link ${paddingClass} ${isActivePage() ? 'active active-page' : ''}`} href={content.slug}>{content.data.title}</a>
<a class={`nav-link ${paddingClass} ${isActivePage(content.slug, activeSlug) ? 'active active-page' : ''}`} href={content.slug}>{content.data.title}</a>
</li>
9 changes: 3 additions & 6 deletions src/components/sidebar/SidebarSelf.astro
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
// Scripts and types
import type { ContentTree } from '@scripts/types';
import { isActivePage } from '@scripts/is-active-page';
import { slugId } from '@scripts/helpers';

// Components
Expand Down Expand Up @@ -75,7 +76,7 @@ const isActiveChild = (doc: ContentTree) => {
}

// Check if the current document's slug matches the activeSlug
if (currentDoc.slug === activeSlug || currentDoc.slug.slice(0, -1) === activeSlug) {
if (isActivePage(currentDoc.slug, activeSlug)) {
return true;
}

Expand All @@ -90,16 +91,12 @@ const isActiveChild = (doc: ContentTree) => {
// If the activeSlug is not found in any document, return false
return false;
};

const isActivePage = (doc: ContentTree) => {
return doc.slug === activeSlug || doc.slug.slice(0, -1) === activeSlug;
};
---

{content.filter(doc => doc.data.showInSidebar === true).map(doc => (
doc.children && doc.children.length > 0 ? (
<li class={`nav-item nav-item-blue ${isActiveChild(doc) ? 'active' : ''}`}>
<div class={`nav-link nav-link-collapse d-flex ${isActiveChild(doc) ? 'active' : ''} ${isActivePage(doc) ? 'active-page' : ''}`}>
<div class={`nav-link nav-link-collapse d-flex ${isActiveChild(doc) ? 'active' : ''} ${isActivePage(doc.slug, activeSlug) ? 'active-page' : ''}`}>
<button class={`btn btn-collapse rounded-0 border-0 ${listPaddingClass(doc.data.depth)} ${isActiveChild(doc) ? '' : 'collapsed'}`} type="button" data-bs-toggle="collapse" aria-expanded={`${isActiveChild(doc) ? 'true' : 'false'}`} aria-controls={slugId(`id-${doc.slug}`)} aria-label="collapse or expand navigation" data-bs-target={slugId(`#id-${doc.slug}`)}></button>
<a href={doc.slug}>{doc.data.title}</a>
</div>
Expand Down
5 changes: 5 additions & 0 deletions src/scripts/is-active-page.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import type { ContentTree } from '@scripts/types';

export const isActivePage = (passedSlug: string | ContentTree[], activeSlug: string) => {
return passedSlug === activeSlug || `${passedSlug}/` === activeSlug || passedSlug.slice(0, -1) === activeSlug;
};