11"use client" ;
22
3- import type { MotionProps } from "motion/react" ;
4- import type { CSSProperties , ElementType , JSX } from "react" ;
3+ import type { CSSProperties } from "react" ;
54
65import { cn } from "@/lib/utils" ;
76import { motion } from "motion/react" ;
87import { memo , useMemo } from "react" ;
98
10- type MotionHTMLProps = MotionProps & Record < string , unknown > ;
11-
12- // Cache motion components at module level to avoid creating during render
13- const motionComponentCache = new Map <
14- keyof JSX . IntrinsicElements ,
15- React . ComponentType < MotionHTMLProps >
16- > ( ) ;
17-
18- const getMotionComponent = ( element : keyof JSX . IntrinsicElements ) => {
19- let component = motionComponentCache . get ( element ) ;
20- if ( ! component ) {
21- component = motion . create ( element ) ;
22- motionComponentCache . set ( element , component ) ;
23- }
24- return component ;
25- } ;
9+ // Pre-create the motion.p component at module level to avoid creating during render
10+ const MotionP = motion . create ( "p" ) ;
2611
2712export interface TextShimmerProps {
2813 children : string ;
29- as ?: ElementType ;
3014 className ?: string ;
3115 duration ?: number ;
3216 spread ?: number ;
3317}
3418
3519const ShimmerComponent = ( {
3620 children,
37- as : Component = "p" ,
3821 className,
3922 duration = 2 ,
4023 spread = 2 ,
4124} : TextShimmerProps ) => {
42- const MotionComponent = getMotionComponent (
43- Component as keyof JSX . IntrinsicElements
44- ) ;
45-
4625 const dynamicSpread = useMemo (
4726 ( ) => ( children ?. length ?? 0 ) * spread ,
4827 [ children , spread ]
4928 ) ;
5029
5130 return (
52- < MotionComponent
31+ < MotionP
5332 animate = { { backgroundPosition : "0% center" } }
5433 className = { cn (
5534 "relative inline-block bg-[length:250%_100%,auto] bg-clip-text text-transparent" ,
@@ -71,7 +50,7 @@ const ShimmerComponent = ({
7150 } }
7251 >
7352 { children }
74- </ MotionComponent >
53+ </ MotionP >
7554 ) ;
7655} ;
7756
0 commit comments