Skip to content

Commit

Permalink
fix: pagemenu conditional section scrolling issues
Browse files Browse the repository at this point in the history
  • Loading branch information
marcellmueller committed Feb 6, 2025
1 parent 785e12c commit 0905c2e
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 64 deletions.
33 changes: 17 additions & 16 deletions frontend/src/components/layout/PageMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ type PageSection = {
sectionIndex: number;
link: string;
display: string;
visible: boolean;
};

type PageMenuProps = {
Expand All @@ -25,20 +24,21 @@ const PageMenu: React.FC<PageMenuProps> = ({
aria-label="Page section navigation"
className="navbar"
>
{pageSections
.filter((s) => s.visible)
.map((section) => (
{pageSections.map((section) => {
const { display, link, sectionIndex } = section;
return (
<a
className={`nav-link ${activeSection === section.sectionIndex ? 'active' : ''}`}
className={`nav-link ${activeSection === sectionIndex ? 'active' : ''}`}
data-active-section={
activeSection === section.sectionIndex ? 'true' : 'false'
activeSection === sectionIndex ? 'true' : 'false'
}
key={section.sectionIndex}
href={section.link}
key={link}
href={link}
>
{section.display}
{display}
</a>
))}
);
})}
</nav>
);
}
Expand All @@ -65,13 +65,14 @@ const PageMenu: React.FC<PageMenuProps> = ({
<option value="" disabled>
Table of Contents
</option>
{pageSections
.filter((s) => s.visible)
.map((section) => (
<option key={section.sectionIndex} value={section.sectionIndex}>
{section.display}
{pageSections.map((section) => {
const { display, sectionIndex } = section;
return (
<option key={display} value={sectionIndex}>
{display}
</option>
))}
);
})}
</select>
</div>
);
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/components/rec-resource/Closures.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const Closures = forwardRef<HTMLElement, ClosuresProps>(
({ comment, siteName }, ref) => {
if (!comment || !siteName) return null;
return (
<section id="site-description" ref={ref}>
<section id="closures" ref={ref}>
<h2 className="section-heading">Closures</h2>
<div className="advisory-container">
<span className="icon-container fw-bold">
Expand Down
104 changes: 57 additions & 47 deletions frontend/src/components/rec-resource/RecResourcePage.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useEffect, useRef, useState } from 'react';
import { useEffect, useMemo, useRef, useState } from 'react';
import { useParams } from 'react-router-dom';
import useScrollSpy from 'react-use-scrollspy';
import BreadCrumbs from '@/components/layout/BreadCrumbs';
Expand Down Expand Up @@ -67,7 +67,8 @@ const RecResourcePage = () => {
setNotFound(true);
});
}
}, [id]);
// eslint-disable-next-line
}, []);

const {
recreation_activity,
Expand All @@ -91,14 +92,58 @@ const RecResourcePage = () => {
const thingsToDoRef = useRef<HTMLElement>(null!);
const contactRef = useRef<HTMLElement>(null!);

const sectionRefs: React.RefObject<HTMLElement>[] = [
closuresRef,
siteDescriptionRef,
mapLocationRef,
campingRef,
thingsToDoRef,
contactRef,
];
const isThingsToDo = recreation_activity && recreation_activity.length > 0;
const isPhotoGallery = photosExample.length > 0;
const isClosures = statusComment && formattedName && statusCode === 2;

const sectionRefs: React.RefObject<HTMLElement>[] = useMemo(
() =>
[
isClosures ? closuresRef : null,
description ? siteDescriptionRef : null,
mapLocationRef,
campingRef,
isThingsToDo ? thingsToDoRef : null,
contactRef,
].filter((ref) => ref !== null),
[description, isClosures, isThingsToDo],
);

const pageSections = useMemo(
() =>
[
isClosures && {
link: '#closures',
display: 'Closures',
},
description && {
link: '#site-description',
display: 'Site Description',
},
{
link: '#maps-and-location',
display: 'Maps and Location',
},
{
link: '#camping',
display: 'Camping',
},
isThingsToDo && {
link: '#things-to-do',
display: 'Things to Do',
},
{
link: '#contact',
display: 'Contact',
},
]
.filter((section) => !!section)
.map((section, i) => ({
...section,
sectionIndex: i,
})),
[description, isClosures, isThingsToDo],
);

const activeSection = useScrollSpy({
sectionElementRefs: sectionRefs,
Expand All @@ -116,10 +161,6 @@ const RecResourcePage = () => {
);
}

const isActivities = recreation_activity && recreation_activity.length > 0;
const isPhotoGallery = photosExample.length > 0;
const isClosures = statusComment && formattedName && statusCode === '02';

return (
<div className="rec-resource-container">
<div className={`bg-container ${isPhotoGallery ? 'with-gallery' : ''}`}>
Expand Down Expand Up @@ -160,38 +201,7 @@ const RecResourcePage = () => {
<div className="row no-gutters">
<div className="page-menu--desktop">
<PageMenu
pageSections={[
{
sectionIndex: 0,
link: '#site-description',
display: 'Site Description',
visible: !!description,
},
{
sectionIndex: 1,
link: '#maps-and-location',
display: 'Maps and Location',
visible: true,
},
{
sectionIndex: 2,
link: '#camping',
display: 'Camping',
visible: true,
},
{
sectionIndex: 3,
link: '#things-to-do',
display: 'Things to Do',
visible: true,
},
{
sectionIndex: 4,
link: '#contact',
display: 'Contact',
visible: true,
},
]}
pageSections={pageSections}
activeSection={activeSection ?? 0}
menuStyle="nav"
/>
Expand All @@ -215,7 +225,7 @@ const RecResourcePage = () => {

<Camping ref={campingRef} />

{isActivities && (
{isThingsToDo && (
<ThingsToDo
activities={recreation_activity}
ref={thingsToDoRef}
Expand Down

0 comments on commit 0905c2e

Please sign in to comment.