From bd8ab827c236bd91f4421fcbab40579f85eeaf4e Mon Sep 17 00:00:00 2001 From: Dillion Verma Date: Tue, 24 Dec 2024 06:04:56 -0500 Subject: [PATCH] fix: word pullup refactor (#458) --- content/docs/components/word-pull-up.mdx | 15 +++--- .../default/example/word-pull-up-demo.tsx | 7 +-- registry/default/magicui/word-pull-up.tsx | 46 +++++++++++-------- 3 files changed, 38 insertions(+), 30 deletions(-) diff --git a/content/docs/components/word-pull-up.mdx b/content/docs/components/word-pull-up.mdx index 12a981dcb..3edba36b3 100644 --- a/content/docs/components/word-pull-up.mdx +++ b/content/docs/components/word-pull-up.mdx @@ -42,9 +42,12 @@ npx shadcn@latest add "https://magicui.design/r/word-pull-up" ## Props -| Prop | Type | Description | Default | -| ------------------ | --------------- | -------------------------------------------------- | ------- | -| className | string | The class name to be applied to the component | | -| words | string | An string of word to pull up | "" | -| framerProps | HTMLMotionProps | An object containing framer-motion animation props | {} | -| wrapperFramerProps | HTMLMotionProps | An object containing framer-motion animation props | {} | +| Prop | Type | Description | Default | +| ------------- | ----------------- | --------------------------------------------- | ------- | +| className | string | The class name to be applied to the component | | +| children | string | The text content to be animated | "" | +| as | React.ElementType | The component type to render as | "h1" | +| delayMultiple | number | The delay multiplier for staggered animation | | +| variants | Variants | Variants for the parent motion component | | +| wordVariants | Variants | Variants for the word motion components | | +| startOnView | boolean | Whether to start the animation on view | false | diff --git a/registry/default/example/word-pull-up-demo.tsx b/registry/default/example/word-pull-up-demo.tsx index 6e9c2a885..dd28461f5 100644 --- a/registry/default/example/word-pull-up-demo.tsx +++ b/registry/default/example/word-pull-up-demo.tsx @@ -1,10 +1,5 @@ import WordPullUp from "@/registry/default/magicui/word-pull-up"; export default function WordPullUpDemo() { - return ( - - ); + return Word Pull Up; } diff --git a/registry/default/magicui/word-pull-up.tsx b/registry/default/magicui/word-pull-up.tsx index ed53da904..7705675cc 100644 --- a/registry/default/magicui/word-pull-up.tsx +++ b/registry/default/magicui/word-pull-up.tsx @@ -1,20 +1,24 @@ "use client"; -import { motion, Variants } from "framer-motion"; - import { cn } from "@/lib/utils"; +import { motion, MotionProps, useInView, Variants } from "motion/react"; +import { useRef } from "react"; -interface WordPullUpProps { - words: string; - delayMultiple?: number; - wrapperFramerProps?: Variants; - framerProps?: Variants; +interface WordPullUpProps extends MotionProps { + children: string; className?: string; + as?: React.ElementType; + delayMultiple?: number; + variants?: Variants; + wordVariants?: Variants; + startOnView?: boolean; } export default function WordPullUp({ - words, - wrapperFramerProps = { + children, + className, + as: Component = "h1", + variants = { hidden: { opacity: 0 }, show: { opacity: 1, @@ -23,31 +27,37 @@ export default function WordPullUp({ }, }, }, - framerProps = { + wordVariants = { hidden: { y: 20, opacity: 0 }, show: { y: 0, opacity: 1 }, }, - className, + startOnView = false, }: WordPullUpProps) { + const MotionComponent = motion.create(Component); + const ref = useRef(null); + const inView = useInView(ref, { once: true, margin: "-30% 0px -30% 0px" }); + const shouldAnimate = startOnView ? inView : true; + return ( - - {words.split(" ").map((word, i) => ( + {children.split(" ").map((word, i) => ( {word === "" ?   : word} ))} - + ); }