'use client'; import { useState, useCallback, cloneElement, ReactElement, HTMLAttributes } from 'react'; export interface RgbShiftProps extends HTMLAttributes { /** Child element to apply RGB shift to */ children: ReactElement; /** Shift intensity */ intensity?: 'subtle' | 'moderate' | 'intense' | 'extreme'; /** Shift direction */ direction?: 'horizontal' | 'vertical' | 'diagonal' | 'random'; /** Animation style */ animation?: 'none' | 'pulse' | 'glitch' | 'shimmer'; /** Transition speed in seconds */ transitionSpeed?: number; /** Trigger on click instead of hover */ clickToTrigger?: boolean; /** Custom shift amount in pixels */ customShift?: number; } export function RgbShift({ children, intensity = 'moderate', direction = 'horizontal', animation = 'none', transitionSpeed = 0.3, clickToTrigger = false, customShift, className = '', style, ...props }: RgbShiftProps) { const [isActive, setIsActive] = useState(false); const handleClick = useCallback(() => { if (clickToTrigger) { setIsActive(prev => !prev); } }, [clickToTrigger]); const getShiftAmount = () => { if (customShift) return customShift; switch (intensity) { case 'subtle': return 2; case 'moderate': return 4; case 'intense': return 8; case 'extreme': return 12; default: return 4; } }; const shift = getShiftAmount(); const getTransform = (channel: 'red' | 'green' | 'blue') => { if (animation === 'glitch') { return `translate(${Math.random() * shift - shift/2}px, ${Math.random() * shift - shift/2}px)`; } if (isActive || !clickToTrigger) { switch (channel) { case 'red': return direction === 'vertical' ? `translate(0, -${shift}px)` : direction === 'diagonal' ? `translate(-${shift}px, -${shift/2}px)` : `translate(-${shift}px, 0)`; case 'green': return 'translate(0, 0)'; case 'blue': return direction === 'vertical' ? `translate(0, ${shift}px)` : direction === 'diagonal' ? `translate(${shift}px, ${shift/2}px)` : `translate(${shift}px, 0)`; } } return 'translate(0, 0)'; }; return (
{/* Red channel */}
{cloneElement(children, { style: { ...children.props.style, color: 'transparent', textShadow: '2px 0 0.3px rgba(255, 0, 0, 0.8)', }, })}
{/* Green channel */}
{cloneElement(children, { style: { ...children.props.style, color: 'transparent', textShadow: '-1px 0 0.3px rgba(0, 255, 0, 0.8)', }, })}
{/* Blue channel */}
{cloneElement(children, { style: { ...children.props.style, color: 'transparent', textShadow: '-1px 0 0.3px rgba(0, 0, 255, 0.8)', }, })}
{/* Original content */}
{children}
); } RgbShift.displayName = 'RgbShift'; export default RgbShift;