Url:
https://stackblitz-starters-ougpwp.stackblitz.io
Code: React Starter (forked) - StackBlitz
I'm trying to use the observer methods' onDown/onUp function and passing in a function that handles scrollToView method to each section. It works fine when I add the function to a button, it just snaps well to the desired section but when I add the function to the onDown property it gets very buggy and does not transition well.
I logged out values in the function when called by the onDown property and I see a continuous render of the log results.
Ref:
import { useState, useRef, useLayoutEffect } from 'react';
import React = require('react');
import gsap from 'gsap';
import ScrollTrigger from 'gsap/ScrollTrigger';
import './style.css';
import Footer from './Footer';
gsap.registerPlugin(ScrollTrigger);
export default function App() {
const tl = useRef();
const scope = useRef(null);
const [showFooter, setShowFooter] = useState(false);
const handleClickScroll = () => {
const element = document.querySelector('.panel-2');
if (element) {
// ? Will scroll smoothly to the top of the next section
element.scrollIntoView({ behavior: 'smooth' });
console.log('worked');
}
};
useLayoutEffect(() => {
const ctx = gsap.context((self) => {}, scope);
ScrollTrigger.observe({
target: '.container',
onDown: () => handleClickScroll(),
});
return () => ctx.revert();
}, []);
return (
<div className="container">
{/* {showFooter && <Footer setShowFooter={setShowFooter} />} */}
<section className="panel panel-1">
<button onClick={handleClickScroll}>Scrollerrrrrrrrrr</button>
<h1>GSAP and CSS Scroll Snapping</h1>
</section>
<section className="panel panel-2">
<p>Section 2</p>
</section>
<section className="panel panel-3">
<p>Section 3</p>
<p>next slide should reveal footer modal</p>
</section>
</div>
);
}