From df41a238793f1cb842f2d79944e25aa84dbd4464 Mon Sep 17 00:00:00 2001 From: dangthang Date: Wed, 20 Nov 2024 09:43:28 +0700 Subject: [PATCH 01/19] feat: add motion animations to various components and implement useMotion hook for scroll reveal functionality --- @/components/button.tsx | 2 +- app/hooks/use-motion.tsx | 48 +++++++++++++++++++ app/sections/atoms/Description.tsx | 1 + app/sections/atoms/Heading.tsx | 1 + app/sections/atoms/Section.tsx | 8 ++-- app/sections/atoms/SubHeading.tsx | 1 + app/sections/atoms/button.tsx | 1 + app/sections/before-and-after/slider.tsx | 2 +- app/sections/blog-post/blog-post.tsx | 1 + app/sections/blogs/index.tsx | 5 +- app/sections/contact-form/contact-form.tsx | 18 ++++--- app/sections/countdown/timer.tsx | 1 + .../feature-product/list-products.tsx | 4 +- app/sections/hightlight/item.tsx | 1 + app/sections/video-banner/index.tsx | 5 +- app/weaverse/schema.server.ts | 6 +++ 16 files changed, 89 insertions(+), 16 deletions(-) create mode 100644 app/hooks/use-motion.tsx diff --git a/@/components/button.tsx b/@/components/button.tsx index 3cc9ca4..499a855 100644 --- a/@/components/button.tsx +++ b/@/components/button.tsx @@ -14,7 +14,7 @@ const buttonVariants = cva( secondary: 'border border-bar bg-transparent hover:bg-primary hover:text-secondary', outline: - 'bg-transparent border border-bar text-secondary-foreground font-heading text-xl', + 'bg-transparent border border-bar text-secondary-foreground !font-heading', link: 'text-primary underline-offset-4 hover:underline', decor: 'relative', custom: '', diff --git a/app/hooks/use-motion.tsx b/app/hooks/use-motion.tsx new file mode 100644 index 0000000..aa204e6 --- /dev/null +++ b/app/hooks/use-motion.tsx @@ -0,0 +1,48 @@ +import { useThemeSettings } from "@weaverse/hydrogen"; +import { animate, inView, useAnimate } from "framer-motion"; +import { ForwardedRef, useEffect } from "react"; + +type MotionType = "fade-up" | "fade-in" | "zoom-in" | "slide-in"; + +const animations: Record = { + "fade-up": { opacity: [0, 1], y: [20, 0] }, + "fade-in": { opacity: [0, 1] }, + "zoom-in": { opacity: [0, 1], scale: [0.8, 1], y: [20, 0] }, + "slide-in": { opacity: [0, 1], x: [20, 0] }, +}; + +export function useMotion(ref?: ForwardedRef) { + let { enableViewTransition } = useThemeSettings(); + const [scope] = useAnimate(); + useEffect(() => { + if (!scope.current || !ref) return; + Object.assign(ref, { current: scope.current }); + }, [scope, ref]); + useEffect(() => { + if (!enableViewTransition) { + return; + } + scope.current?.classList.add("animated-scope"); + scope.current + .querySelectorAll("[data-motion]") + .forEach((el: HTMLElement, index: number) => { + inView( + el, + (info) => { + const dataDelay = Number.parseInt(el.dataset.delay as string); + const motionType = (el.dataset.motion || "fade-up") as MotionType; + const motionSettings = animations[motionType]; + const delay = dataDelay || index * 0.15; + animate(info.target, motionSettings, { + duration: 0.5, + delay, + }); + }, + { + amount: 0.3, + } + ); + }); + }, []); + return [scope] as const; +} diff --git a/app/sections/atoms/Description.tsx b/app/sections/atoms/Description.tsx index 5bcf34f..4849967 100644 --- a/app/sections/atoms/Description.tsx +++ b/app/sections/atoms/Description.tsx @@ -66,6 +66,7 @@ let Description = forwardRef< ((props, ref) => { + const [scope] = useMotion(ref); let { as: Component = "section", width, @@ -112,12 +114,12 @@ export let Section = forwardRef((props, ref) => { return ( {!isBgForContent && } @@ -125,7 +127,7 @@ export let Section = forwardRef((props, ref) => { className={cn( variants({ gap, width, verticalPadding, overflow }), hasBackground && isBgForContent && "has-background px-4 sm:px-8", - containerClassName, + containerClassName )} > {isBgForContent && } diff --git a/app/sections/atoms/SubHeading.tsx b/app/sections/atoms/SubHeading.tsx index 83baecc..80ae5ac 100644 --- a/app/sections/atoms/SubHeading.tsx +++ b/app/sections/atoms/SubHeading.tsx @@ -56,6 +56,7 @@ let SubHeading = forwardRef< diff --git a/app/sections/atoms/button.tsx b/app/sections/atoms/button.tsx index 382d711..d4cc2ce 100644 --- a/app/sections/atoms/button.tsx +++ b/app/sections/atoms/button.tsx @@ -36,6 +36,7 @@ const WeaverseButton = forwardRef(