From 04c1ca729214962ca35e93fa0412dd93251727ee Mon Sep 17 00:00:00 2001 From: monilprajapati Date: Thu, 1 Feb 2024 21:24:00 +0530 Subject: [PATCH 1/3] Added animation in feature section --- components/useElementOnScreen.jsx | 30 ++++++++++++++++++ pages/index.js | 52 ++++++++++++++++++++++++------- tailwind.config.js | 27 ++++++++++++++++ 3 files changed, 97 insertions(+), 12 deletions(-) create mode 100644 components/useElementOnScreen.jsx diff --git a/components/useElementOnScreen.jsx b/components/useElementOnScreen.jsx new file mode 100644 index 00000000000..1b3d64441bb --- /dev/null +++ b/components/useElementOnScreen.jsx @@ -0,0 +1,30 @@ +import { useRef, useEffect, useState } from "react"; + +const useElementOnScreen = (options) => { + const containerRef = useRef(null); + const [isVisible, setIsVisible] = useState(false); + + const callbackFunction = (entries) => { + const [entry] = entries; + setIsVisible(entry.isIntersecting); + }; + + useEffect(() => { + const observer = new IntersectionObserver(callbackFunction, options); + const currentRef = containerRef.current; + + if (currentRef) { + observer.observe(currentRef); + } + + return () => { + if (currentRef) { + observer.unobserve(currentRef); + } + }; + }, [options]); + + return [containerRef, isVisible]; +}; + +export default useElementOnScreen; diff --git a/pages/index.js b/pages/index.js index f5401ec0319..7a4c16e80e9 100644 --- a/pages/index.js +++ b/pages/index.js @@ -21,6 +21,7 @@ import ThemedImage from "@components/ThemedImage"; import { serverEnv } from "@config/schemas/serverSchema"; import { PROJECT_NAME } from "@constants/index"; import Button from "@components/Button"; +import useElementOnScreen from "@components/useElementOnScreen"; export async function getStaticProps() { const pageConfig = config.isr.homepage; @@ -160,6 +161,14 @@ export default function Home({ }, ]; + const featureRefs = featuresDetails.map(() => { + return useElementOnScreen({ + root: null, + rootMargin: "0px", + threshold: 0.4, + }); + }); + return ( <> @@ -275,16 +284,26 @@ export default function Home({
{featuresDetails.map((feature, featureIdx) => (

{feature.name} @@ -299,12 +318,21 @@ export default function Home({ )}

Date: Wed, 3 Apr 2024 15:26:04 +0530 Subject: [PATCH 2/3] Animation feature: Made some required changes --- {components => hooks}/useElementOnScreen.jsx | 0 pages/index.js | 48 ++++++++++---------- 2 files changed, 23 insertions(+), 25 deletions(-) rename {components => hooks}/useElementOnScreen.jsx (100%) diff --git a/components/useElementOnScreen.jsx b/hooks/useElementOnScreen.jsx similarity index 100% rename from components/useElementOnScreen.jsx rename to hooks/useElementOnScreen.jsx diff --git a/pages/index.js b/pages/index.js index 7a4c16e80e9..3a2224bcb60 100644 --- a/pages/index.js +++ b/pages/index.js @@ -21,7 +21,7 @@ import ThemedImage from "@components/ThemedImage"; import { serverEnv } from "@config/schemas/serverSchema"; import { PROJECT_NAME } from "@constants/index"; import Button from "@components/Button"; -import useElementOnScreen from "@components/useElementOnScreen"; +import useElementOnScreen from "../hooks/useElementOnScreen.jsx"; export async function getStaticProps() { const pageConfig = config.isr.homepage; @@ -161,13 +161,11 @@ export default function Home({ }, ]; - const featureRefs = featuresDetails.map(() => { - return useElementOnScreen({ - root: null, - rootMargin: "0px", - threshold: 0.4, - }); - }); + const featureRefs = featuresDetails.map(() => useElementOnScreen({ + root: null, + rootMargin: "0px", + threshold: 0.4, + })); return ( <> @@ -289,7 +287,7 @@ export default function Home({ className="flex flex-col-reverse overflow-x-hidden lg:grid lg:grid-cols-12 lg:items-center lg:gap-x-8" >

{feature.name} @@ -318,21 +316,21 @@ export default function Home({ )}

Date: Sun, 14 Apr 2024 00:50:48 +0530 Subject: [PATCH 3/3] Changed the ternaries with classNames() method --- pages/index.js | 50 +++++++++++++++++++++----------------------------- 1 file changed, 21 insertions(+), 29 deletions(-) diff --git a/pages/index.js b/pages/index.js index 3a2224bcb60..379396d2beb 100644 --- a/pages/index.js +++ b/pages/index.js @@ -287,21 +287,17 @@ export default function Home({ className="flex flex-col-reverse overflow-x-hidden lg:grid lg:grid-cols-12 lg:items-center lg:gap-x-8" >

{feature.name} @@ -316,21 +312,17 @@ export default function Home({ )}