|
| 1 | +import React, { useRef, useEffect } from 'react'; |
| 2 | + |
| 3 | +interface Props { |
| 4 | + borderColor?: string; |
| 5 | + borderWidth?: string | number; |
| 6 | + trailLength?: number; // Adjust the length of the trail |
| 7 | + duration?: number; |
| 8 | + children?: React.ReactNode; |
| 9 | +} |
| 10 | + |
| 11 | +const TrailBorderBox: React.FC<Props> = ({ |
| 12 | + borderWidth = 2, |
| 13 | + trailLength = 20, // Default trail length |
| 14 | + duration = 2, |
| 15 | + children, |
| 16 | +}) => { |
| 17 | + const boxRef = useRef<HTMLDivElement>(null); |
| 18 | + |
| 19 | + useEffect(() => { |
| 20 | + if (boxRef.current) { |
| 21 | + const box = boxRef.current; |
| 22 | + const width = box.offsetWidth; |
| 23 | + const height = box.offsetHeight; |
| 24 | + |
| 25 | + const animateTrail = () => { |
| 26 | + let currentPosition = 0; |
| 27 | + let isHorizontal = true; // Start with horizontal movement |
| 28 | + |
| 29 | + const animateSegment = () => { |
| 30 | + const segmentLength = isHorizontal ? width : height; |
| 31 | + const endPosition = currentPosition + segmentLength; |
| 32 | + |
| 33 | + const trail = document.createElement('div'); |
| 34 | + trail.style.position = 'absolute'; |
| 35 | + trail.style.zIndex = '1'; // Ensure trail is above content but below border |
| 36 | + |
| 37 | + if (isHorizontal) { |
| 38 | + trail.style.top = '0px'; |
| 39 | + trail.style.left = `${currentPosition}px`; |
| 40 | + trail.style.width = `${trailLength}px`; |
| 41 | + trail.style.height = `${borderWidth}px`; |
| 42 | + } else { |
| 43 | + trail.style.left = `${borderWidth}px`; |
| 44 | + trail.style.top = `${currentPosition}px`; |
| 45 | + trail.style.width = `${borderWidth}px`; |
| 46 | + trail.style.height = `${trailLength}px`; |
| 47 | + } |
| 48 | + |
| 49 | + box.appendChild(trail); |
| 50 | + |
| 51 | + const animation = trail.animate( |
| 52 | + isHorizontal |
| 53 | + ? [{ left: `${currentPosition}px` }, { left: `${endPosition}px` }] |
| 54 | + : [{ top: `${currentPosition}px` }, { top: `${endPosition}px` }], |
| 55 | + { |
| 56 | + duration: |
| 57 | + duration * 1000 * (segmentLength / (width * 2 + height * 2)), // Adjust duration based on segment length and total perimeter |
| 58 | + easing: 'linear', |
| 59 | + fill: 'forwards', |
| 60 | + }, |
| 61 | + ); |
| 62 | + |
| 63 | + animation.onfinish = () => { |
| 64 | + trail.remove(); // Remove trail segment after animation |
| 65 | + currentPosition = endPosition; |
| 66 | + if (currentPosition >= (isHorizontal ? width : height)) { |
| 67 | + currentPosition = 0; |
| 68 | + isHorizontal = !isHorizontal; // Switch direction |
| 69 | + } |
| 70 | + animateSegment(); // Continue animation |
| 71 | + }; |
| 72 | + }; |
| 73 | + |
| 74 | + animateSegment(); // Start the first segment |
| 75 | + }; |
| 76 | + |
| 77 | + animateTrail(); // Start the animation loop |
| 78 | + } |
| 79 | + }, [borderWidth, duration, trailLength]); // Re-run effect if props change |
| 80 | + |
| 81 | + return ( |
| 82 | + <div |
| 83 | + ref={boxRef} |
| 84 | + style={{ |
| 85 | + border: `${borderWidth}px solid 'black'`, |
| 86 | + borderRadius: '8px', |
| 87 | + position: 'relative', // Needed for absolute positioning of trails |
| 88 | + overflow: 'hidden', // Hide trails that go outside the box during animation |
| 89 | + display: 'flex', |
| 90 | + alignItems: 'center', |
| 91 | + justifyContent: 'center', |
| 92 | + }} |
| 93 | + > |
| 94 | + {children} |
| 95 | + </div> |
| 96 | + ); |
| 97 | +}; |
| 98 | + |
| 99 | +export default TrailBorderBox; |
0 commit comments