Skip to content

Commit 4d16205

Browse files
author
Carms Ng
committed
[ADD] Smooth Scroll behavior backin by pausing the observer entry effects
1 parent 5791077 commit 4d16205

File tree

3 files changed

+32
-9
lines changed

3 files changed

+32
-9
lines changed

src/components/hero.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import LogoGarden from '../components/logo-garden';
77
import SiteBorderStyles from '../styles/SiteBorderStyles';
88
import StackedAvatar from "./stacked-avatar";
99

10-
export default function Hero({ sectionRefs, setTeamIndex }) {
10+
export default function Hero({ sectionRefs, setTeamIndex, pausedRef }) {
1111
return (
1212
<HeroStyles>
1313
<SiteBorderStyles>
@@ -25,7 +25,11 @@ export default function Hero({ sectionRefs, setTeamIndex }) {
2525
<p className="text-1xl md:text-2xl mt-2">
2626
<Trans>We empower freelancer community to excel in long term projects.</Trans>
2727
</p>
28-
<StackedAvatar sectionRefs={sectionRefs} setTeamIndex={setTeamIndex} />
28+
<StackedAvatar
29+
sectionRefs={sectionRefs}
30+
setTeamIndex={setTeamIndex}
31+
pausedRef={pausedRef}
32+
/>
2933

3034
</div>
3135

src/components/stacked-avatar.js

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as React from "react";
22
import { graphql, useStaticQuery } from 'gatsby';
33
import { GatsbyImage } from "gatsby-plugin-image";
44

5-
export default function StackedAvatar({ sectionRefs, setTeamIndex }) {
5+
export default function StackedAvatar({ sectionRefs, setTeamIndex, pausedRef }) {
66
// query all team pics
77
const data = useStaticQuery(graphql`{
88
allFile(filter: {absolutePath: {regex: "/portraits/"}}, sort: {fields: base}) {
@@ -22,13 +22,29 @@ export default function StackedAvatar({ sectionRefs, setTeamIndex }) {
2222
}
2323
}`);
2424

25+
const scrollToTeam = (top) => {
26+
window.scrollTo({ top, behavior: 'smooth' })
27+
}
28+
29+
const unpauseObserver = () => {
30+
pausedRef.current = false
31+
}
32+
33+
const moveToTeam = async(top) => {
34+
scrollToTeam(top);
35+
unpauseObserver();
36+
}
37+
2538
const handleClick = (ev) => {
39+
// Pause Observer
40+
pausedRef.current = true
41+
// Update Team Index
2642
const updatedTeamIndex = parseInt(ev.currentTarget.dataset.team)
2743
setTeamIndex(updatedTeamIndex)
28-
// Navigate to the team section
44+
// Find Team Section Top
2945
const top = sectionRefs.current[2].offsetTop
30-
// Remove smooth scroll behavior to bypass the intersection observer
31-
window.scrollTo({ top })
46+
// Navigate to the Team Section
47+
moveToTeam(top)
3248
}
3349

3450
const pics = data.allFile.edges;

src/pages/index.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import 'aos/dist/aos.css';
1515

1616
export default function IndexPage() {
1717
// Set who in the team is being featured
18+
const pausedRef = useRef(false);
1819
const [teamIndex, setTeamIndex ] = useState(0);
1920

2021
// You can access the elements with itemsRef.current[n]
@@ -34,7 +35,7 @@ export default function IndexPage() {
3435

3536
// Set up observer
3637
const observer = new IntersectionObserver(([entry]) => {
37-
if (entry.isIntersecting) {
38+
if (entry.isIntersecting && !pausedRef.current) {
3839
const refIndex = parseInt(entry.target.dataset.step)
3940
const top = sectionRefs.current[refIndex].offsetTop
4041
window.scrollTo({ top, behavior: 'smooth' })
@@ -57,13 +58,15 @@ export default function IndexPage() {
5758
}
5859
})
5960
}
60-
}, [sectionRefs])
61+
}, [sectionRefs, pausedRef])
6162

6263
const {t} = useTranslation();
6364
return (
6465
<Layout>
6566
<Seo title={t('Home')} />
66-
<div ref={addToRefs} data-step="0"><Hero sectionRefs={sectionRefs} setTeamIndex={setTeamIndex} /></div>
67+
<div ref={addToRefs} data-step="0">
68+
<Hero sectionRefs={sectionRefs} setTeamIndex={setTeamIndex} pausedRef={pausedRef} />
69+
</div>
6770
<div ref={addToRefs} data-step="1"><Service /></div>
6871
<div ref={addToRefs} data-step="2"><Team teamIndex={teamIndex} setTeamIndex={setTeamIndex} /></div>
6972
<div ref={addToRefs} data-step="3"><Steps /></div>

0 commit comments

Comments
 (0)