Skip to content

Commit

Permalink
auto change side nav current
Browse files Browse the repository at this point in the history
  • Loading branch information
nickbristow committed Dec 11, 2024
1 parent d4e0c51 commit 566c775
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 75 deletions.
86 changes: 86 additions & 0 deletions src/app/components/SideNav/SideNav.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import { useState, useEffect, SetStateAction } from 'react';
import { SideNav as UswdsSideNav } from '@trussworks/react-uswds';
import classNames from 'classnames';

interface Section {
title: string;
id: string;
}

interface SideNavProps {
sections: Section[];
}

export const SideNav = ({ sections }: SideNavProps) => {
const [selectedHash, setSelectedHash] = useState(`#${sections[0].id}`);

useEffect(() => {
let currentSection = '';

const observer = new IntersectionObserver(
(entries) => {
entries.forEach((entry) => {
if (entry.isIntersecting) {
const newSection = `#${entry.target.id}`;
if (currentSection !== newSection) {
currentSection = newSection;
setSelectedHash(newSection);
}
}
});
},
{
rootMargin: '-20% 0px -70% 0px',
threshold: 0,
},
);

sections.forEach(({ id }) => {
const element = document.getElementById(id);
if (element) observer.observe(element);
});

return () => {
sections.forEach(({ id }) => {
const element = document.getElementById(id);
if (element) observer.unobserve(element);
});
};
}, [sections]);

return (
<UswdsSideNav
items={sections.map(({ title, id }) => (
<NavItem
title={title}
id={id}
key={id}
selectedHash={selectedHash}
setSelectedHash={setSelectedHash}
/>
))}
/>
);
};

interface NavItemProps {
title: string;
id: string;
selectedHash: string;
setSelectedHash: (value: SetStateAction<string>) => void;
}
function NavItem({ title, id, selectedHash, setSelectedHash }: NavItemProps) {
const itemHash = `#${id}`;
return (
<a
href={itemHash}
key={id}
className={classNames({
'usa-current': itemHash === selectedHash,
})}
onClick={() => setSelectedHash(itemHash)}
>
{title}
</a>
);
}
85 changes: 10 additions & 75 deletions src/app/products/ecr-viewer/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@ import {
ProcessListHeading,
Accordion,
Link,
SideNav,
} from '@trussworks/react-uswds';
import classNames from 'classnames';
import { SideNav } from '@/app/components/SideNav/SideNav';
import Image from 'next/image';
import { SetStateAction, useState } from 'react';
import './styles.scss';

export default function EcrViewer() {
Expand Down Expand Up @@ -554,78 +552,15 @@ export default function EcrViewer() {
}

function Navigation() {
const [selectedHash, setSelectedHash] = useState('#overview');

return (
<SideNav
items={[
<NavItem
title="Overview"
id="overview"
key="overview"
selectedHash={selectedHash}
setSelectedHash={setSelectedHash}
/>,
<NavItem
title="Product features"
id="product-features"
key="product-features"
selectedHash={selectedHash}
setSelectedHash={setSelectedHash}
/>,
<NavItem
title="How it works"
id="how-it-works"
key="how-it-works"
selectedHash={selectedHash}
setSelectedHash={setSelectedHash}
/>,
<NavItem
title="Getting started"
id="getting-started"
key="getting-started"
selectedHash={selectedHash}
setSelectedHash={setSelectedHash}
/>,
<NavItem
title="Technical resources"
id="technical-resources"
key="technical-resources"
selectedHash={selectedHash}
setSelectedHash={setSelectedHash}
/>,
<NavItem
title="FAQs"
id="faqs"
key="faqs"
selectedHash={selectedHash}
setSelectedHash={setSelectedHash}
/>,
]}
/>
);
}

interface NavItemProps {
title: string;
id: string;
selectedHash: string;
setSelectedHash: (value: SetStateAction<string>) => void;
}
function NavItem({ title, id, selectedHash, setSelectedHash }: NavItemProps) {
const itemHash = `#${id}`;
return (
<a
href={itemHash}
key={id}
className={classNames({
'usa-current': itemHash === selectedHash,
})}
onClick={() => setSelectedHash(itemHash)}
>
{title}
</a>
);
const sections = [
{ title: 'Overview', id: 'overview' },
{ title: 'Product features', id: 'product-features' },
{ title: 'How it works', id: 'how-it-works' },
{ title: 'Getting started', id: 'getting-started' },
{ title: 'Technical resources', id: 'technical-resources' },
{ title: 'FAQs', id: 'faqs' },
];
return <SideNav sections={sections} />;
}

interface GithubNavProps {
Expand Down

0 comments on commit 566c775

Please sign in to comment.