Skip to content

Commit 6a2d794

Browse files
committed
added functionality for a 'scroll buffer' that prevents the hash from being checked or changed for about 1 second after a navlink is clicked. This prevents the scroll from stuttering or restarting.
1 parent 427c2d1 commit 6a2d794

File tree

2 files changed

+49
-7
lines changed

2 files changed

+49
-7
lines changed

src/components/HVContent.tsx

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
import { MutableRefObject, useContext, useEffect, useState } from "react";
1+
import {
2+
MutableRefObject,
3+
SetStateAction,
4+
useContext,
5+
useEffect,
6+
useState,
7+
} from "react";
28
import { animated, useTransition } from "react-spring";
39
import "./styles/HVContent.css";
410
import Deerfall from "./projects/Deerfall";
@@ -22,6 +28,7 @@ const HVContent = ({ project, isIntro, allParams }: Props) => {
2228
const navigate = useNavigate();
2329
// - - - - CONTEXT - - - -
2430
const { scrollRefs, hueRotation } = useContext(AppContext);
31+
const [scrollIsBuffering, setScrollIsBuffering] = useState(false);
2532
const observerOptions: IntersectionObserverInit = {
2633
threshold: 0.5,
2734
root: null,
@@ -107,26 +114,46 @@ const HVContent = ({ project, isIntro, allParams }: Props) => {
107114
}, [allParams]);
108115

109116
useEffect(() => {
110-
if (mediaScrollObserver?.isIntersecting) {
117+
if (mediaScrollObserver?.isIntersecting && !scrollIsBuffering) {
111118
navigate(`${location.pathname}#media`);
112119
}
113120
}, [mediaScrollObserver?.isIntersecting]);
114121

115122
useEffect(() => {
116-
if (techScrollObserver?.isIntersecting)
123+
if (techScrollObserver?.isIntersecting && !scrollIsBuffering)
117124
navigate(`${location.pathname}#tech`);
118125
}, [techScrollObserver?.isIntersecting]);
119126

120127
useEffect(() => {
121-
if (blogScrollObserver?.isIntersecting)
128+
if (blogScrollObserver?.isIntersecting && !scrollIsBuffering)
122129
navigate(`${location.pathname}#blog`);
123130
}, [blogScrollObserver?.isIntersecting]);
124131

125132
useEffect(() => {
126-
if (aboutScrollObserver?.isIntersecting)
133+
if (aboutScrollObserver?.isIntersecting && !scrollIsBuffering)
127134
navigate(`${location.pathname}#about`);
128135
}, [aboutScrollObserver?.isIntersecting]);
129136

137+
useEffect(() => {
138+
if (location.hash === "#media") {
139+
scrollToElement(scrollRefs.media);
140+
} else if (location.hash === "#tech") {
141+
scrollToElement(scrollRefs.tech);
142+
} else if (location.hash === "#about") {
143+
scrollToElement(scrollRefs.about);
144+
} else if (location.hash === "#blog") {
145+
scrollToElement(scrollRefs.blog);
146+
}
147+
console.log(location.hash);
148+
}, [location.hash]);
149+
150+
useEffect(() => {
151+
if (scrollIsBuffering) {
152+
setTimeout(() => setScrollIsBuffering(false), 800);
153+
}
154+
console.log(scrollIsBuffering);
155+
}, [scrollIsBuffering]);
156+
130157
return (
131158
<main className='HVContent'>
132159
{isIntro ? (
@@ -138,6 +165,7 @@ const HVContent = ({ project, isIntro, allParams }: Props) => {
138165
scrollRefs={scrollRefs}
139166
hueRotation={hueRotation}
140167
scrollToElement={scrollToElement}
168+
setScrollIsBuffering={setScrollIsBuffering}
141169
/>
142170
)}
143171

src/components/HVSideNav.tsx

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@ import blogIcon from "../img/navIcons/blog.png";
66
import { animated, useSpring } from "react-spring";
77
import { useEffect, useState } from "react";
88
import { ScrollRefs, HueRotation } from "../models/Models";
9+
import { useLocation, useNavigate } from "react-router-dom";
910

1011
interface Props {
1112
scrollRefs: ScrollRefs;
1213
hueRotation: HueRotation;
1314
isPortfolio: boolean;
1415
allParams: string[];
1516
scrollToElement: (ref: React.MutableRefObject<any>) => void;
17+
setScrollIsBuffering: React.Dispatch<React.SetStateAction<boolean>>;
1618
}
1719

1820
const HVSideNav = ({
@@ -21,25 +23,37 @@ const HVSideNav = ({
2123
scrollRefs,
2224
hueRotation,
2325
scrollToElement,
26+
setScrollIsBuffering,
2427
}: Props) => {
2528
const [hlNav, setHLNav] = useState("media");
29+
const navigate = useNavigate();
30+
const location = useLocation();
2631
const navSizeChange = useSpring({
2732
from: { height: "150px" },
2833
to: { height: "110px" },
2934
config: { mass: 1, tension: 200, friction: 15 },
3035
reverse: isPortfolio,
3136
});
3237

38+
// const handleNavClick = (
39+
// ref: React.MutableRefObject<any>,
40+
// navName: string
41+
// ) => {
42+
// scrollToElement(ref);
43+
// setHLNav(navName);
44+
// };
45+
3346
const handleNavClick = (
3447
ref: React.MutableRefObject<any>,
3548
navName: string
3649
) => {
37-
scrollToElement(ref);
50+
navigate(`${location.pathname}#${navName}`);
51+
setScrollIsBuffering(true);
3852
setHLNav(navName);
3953
};
4054

4155
useEffect(() => {
42-
scrollRefs.media.current?.scrollIntoView({ behavior: "smooth" });
56+
navigate(`${location.pathname}#media`);
4357
setHLNav("media");
4458
}, [allParams[1]]);
4559

0 commit comments

Comments
 (0)