import { cn } from '@/lib/utils'; import { type MotionProps, motion } from 'motion/react'; import { type CSSProperties, type ElementType, type JSX, memo, useMemo, useRef } from 'react'; type MotionComponentType = React.FC< MotionProps & React.HTMLAttributes & { children?: React.ReactNode } >; export type TextShimmerProps = { children: string; as?: ElementType; className?: string; duration?: number; spread?: number; }; /* eslint-disable react-hooks/refs -- Ref-based cache for motion.create component identity */ const ShimmerComponent = ({ children, as: Component = 'p', className, duration = 2, spread = 2, }: TextShimmerProps) => { const motionRef = useRef(null); const prevComponentRef = useRef(Component); if (!motionRef.current || prevComponentRef.current !== Component) { motionRef.current = motion.create( Component as keyof JSX.IntrinsicElements, ) as unknown as MotionComponentType; prevComponentRef.current = Component; } const MotionComponent = motionRef.current; const dynamicSpread = useMemo(() => (children?.length ?? 0) * spread, [children, spread]); return ( {children} ); }; /* eslint-enable react-hooks/refs */ export const Shimmer = memo(ShimmerComponent);