Text Morph
motion-primitives/text-morphFreeComponent
A component that morphs between different text strings with smooth transitions.
Install it
npx shadcn@latest add https://raw.githubusercontent.com/ibelick/motion-primitives/HEAD/public/c/text-morph.jsonSource
1'use client';2import { cn } from '@/lib/utils';3import { AnimatePresence, motion, Transition, Variants } from 'motion/react';4import { useMemo, useId } from 'react';56export type TextMorphProps = {7 children: string;8 as?: React.ElementType;9 className?: string;10 style?: React.CSSProperties;11 variants?: Variants;12 transition?: Transition;13};1415export function TextMorph({16 children,17 as: Component = 'p',18 className,19 style,20 variants,21 transition,22}: TextMorphProps) {23 const uniqueId = useId();2425 const characters = useMemo(() => {26 const charCounts: Record<string, number> = {};2728 return children.split('').map((char) => {29 const lowerChar = char.toLowerCase();30 charCounts[lowerChar] = (charCounts[lowerChar] || 0) + 1;3132 return {33 id: `${uniqueId}-${lowerChar}${charCounts[lowerChar]}`,34 label: char === ' ' ? '\u00A0' : char,35 };36 });37 }, [children, uniqueId]);3839 const defaultVariants: Variants = {40 initial: { opacity: 0 },41 animate: { opacity: 1 },42 exit: { opacity: 0 },43 };4445 const defaultTransition: Transition = {46 type: 'spring',47 stiffness: 280,48 damping: 18,49 mass: 0.3,50 };5152 return (53 <Component className={cn(className)} aria-label={children} style={style}>54 <AnimatePresence mode='popLayout' initial={false}>55 {characters.map((character) => (56 <motion.span57 key={character.id}58 layoutId={character.id}59 className='inline-block'60 aria-hidden='true'61 initial='initial'62 animate='animate'63 exit='exit'64 variants={variants || defaultVariants}65 transition={transition || defaultTransition}66 >67 {character.label}68 </motion.span>69 ))}70 </AnimatePresence>71 </Component>72 );73}
What it pulls in
npm packages
Files it writes
More from motion-primitives
All 33 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| Accordionaccordion | motion-primitives | Components | Free | 1 dep | |
| Animated Backgroundanimated-background | motion-primitives | Components | Free | 1 dep | |
| Animated Groupanimated-group | motion-primitives | Components | Free | 1 dep | |
| Animated Numberanimated-number | motion-primitives | Components | Free | 1 dep | |
| Border Trailborder-trail | motion-primitives | Components | Free | 1 dep | |
| Carouselcarousel | motion-primitives | Components | Free | 1 dep | |
| Cursorcursor | motion-primitives | Components | Free | 1 dep | |
| Dialogdialog | motion-primitives | Components | Free | 1 dep · 2 files |
