diff --git a/components/core/text-morph.tsx b/components/core/text-morph.tsx index f8ea5f7f..e5d2a950 100644 --- a/components/core/text-morph.tsx +++ b/components/core/text-morph.tsx @@ -1,7 +1,7 @@ -'use client'; -import { cn } from '@/lib/utils'; -import { AnimatePresence, motion, Transition, Variants } from 'motion/react'; -import { useMemo, useId } from 'react'; +"use client"; +import { cn } from "@/lib/utils"; +import { AnimatePresence, motion, Transition, Variants } from "motion/react"; +import { useMemo, useId } from "react"; export type TextMorphProps = { children: string; @@ -14,7 +14,7 @@ export type TextMorphProps = { export function TextMorph({ children, - as: Component = 'p', + as: Component = "p", className, style, variants, @@ -23,15 +23,34 @@ export function TextMorph({ const uniqueId = useId(); const characters = useMemo(() => { - const charCounts: Record = {}; + const chars = children.split(""); - return children.split('').map((char) => { + const totalCharCounts = chars.reduce( + (acc, char) => { + const lower = char.toLowerCase(); + acc[lower] = (acc[lower] || 0) + 1; + return acc; + }, + {} as Record + ); + + const leftCharCounts: Record = {}; + const rightCharCounts = { ...totalCharCounts }; + + return chars.map((char) => { const lowerChar = char.toLowerCase(); - charCounts[lowerChar] = (charCounts[lowerChar] || 0) + 1; + + const leftCount = (leftCharCounts[lowerChar] = + (leftCharCounts[lowerChar] || 0) + 1); + + const rightCount = rightCharCounts[lowerChar]; + if (rightCharCounts[lowerChar] !== undefined) { + rightCharCounts[lowerChar]--; + } return { - id: `${uniqueId}-${lowerChar}${charCounts[lowerChar]}`, - label: char === ' ' ? '\u00A0' : char, + id: `${uniqueId}-${lowerChar}-${leftCount}-${rightCount}`, + label: char === " " ? "\u00A0" : char, }; }); }, [children, uniqueId]); @@ -43,7 +62,7 @@ export function TextMorph({ }; const defaultTransition: Transition = { - type: 'spring', + type: "spring", stiffness: 280, damping: 18, mass: 0.3, @@ -51,16 +70,16 @@ export function TextMorph({ return ( - + {characters.map((character) => (