Advertisement
fakesamgregory

Master React in Webflow - Libraries (GSAP)

Nov 17th, 2024 (edited)
10
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* --------------    
  2.     * -- npm scripts -- *
  3.     npm install gsap @gsap/react
  4.  
  5. /* ----------- */
  6. // index.js
  7. import React from "react";
  8. import ReactDOM from "react-dom";
  9.  
  10. // opted do do this here (you still need to import them in the files you use, just don't need to register the plugin).
  11. import gsap from 'gsap';
  12. import { useGSAP } from '@react-gsap';
  13. gsap.registerPlugins(useGSAP);
  14.  
  15. import App from "./App";
  16. const root = ReactDOM.createRoot(document.getElementsByClassName("react")[0]);
  17. root.render(<App />);
  18.  
  19. /* ----------- */
  20. // App.js
  21. import React from 'react';
  22. import PokemonList from './PokemonList';
  23.  
  24. export default function App() {
  25.     return (
  26.         <PokemonList />
  27.     );
  28. };
  29.  
  30. /* ----------- */
  31. // PokemonList.js
  32. /*
  33.  * In this instance we are totally faking calling the Pokemon API
  34.  * Just to show a delay/dependency in useGSAP
  35.  */
  36. import React, { useState, useRef, useEffect } from "react";
  37. import gsap from "gsap";
  38. import { useGSAP } from "@gsap/react";
  39.  
  40. export default function PokemonList() {
  41.   const [pokemon, setPokemon] = useState([]);
  42.   const listRef = useRef(null);
  43.  
  44.   useEffect(() => {
  45.     let timeout = null;
  46.  
  47.     const getPokemon = () => {
  48.       // after 1 second, set pokemon
  49.       timeout = setTimeout(() => {
  50.         setPokemon(["bulbasaur", "ivysaur", "venusaur"]);
  51.       }, 1000);
  52.     };
  53.  
  54.     getPokemon();
  55.  
  56.     return () => {
  57.       if (timeout) {
  58.         timeout.clearTimeout();
  59.       }
  60.     };
  61.   }, [setPokemon]);
  62.  
  63.   useGSAP(
  64.     () => {
  65.       if (listRef.current && pokemon.length) {
  66.         const listElement = gsap.utils.selector(listRef.current);
  67.         const listItems = listElement("li");
  68.  
  69.         gsap.from(listItems, {
  70.           opacity: 0,
  71.           stagger: 0.05,
  72.         });
  73.       }
  74.     },
  75.     { scope: listRef, dependencies: [pokemon] }
  76.   );
  77.  
  78.   return (
  79.     <ul ref={listRef}>
  80.       {pokemon.map((item) => (
  81.         <li>{item}</li>
  82.       ))}
  83.     </ul>
  84.   );
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement