|
| 1 | +import type React from 'react'; |
| 2 | +import { cn } from '@/lib/utils'; |
| 3 | + |
| 4 | +type StarBorderProps<T extends React.ElementType> = |
| 5 | + React.ComponentPropsWithoutRef<T> & { |
| 6 | + as?: T; |
| 7 | + className?: string; |
| 8 | + children?: React.ReactNode; |
| 9 | + color?: string; |
| 10 | + speed?: React.CSSProperties['animationDuration']; |
| 11 | + thickness?: number; |
| 12 | + isAnimating?: boolean; |
| 13 | + }; |
| 14 | + |
| 15 | +const StarBorder = <T extends React.ElementType = 'div'>({ |
| 16 | + as, |
| 17 | + className = '', |
| 18 | + color = 'white', |
| 19 | + speed = '6s', |
| 20 | + thickness = 1, |
| 21 | + isAnimating = true, |
| 22 | + children, |
| 23 | + ...rest |
| 24 | +}: StarBorderProps<T>) => { |
| 25 | + const Component = as || 'div'; |
| 26 | + |
| 27 | + return ( |
| 28 | + <Component |
| 29 | + className={cn('relative inline-block overflow-hidden', className)} |
| 30 | + {...(rest as React.ComponentPropsWithoutRef<T>)} |
| 31 | + style={{ |
| 32 | + padding: `${thickness}px`, |
| 33 | + ...(rest as React.CSSProperties & { style?: React.CSSProperties }) |
| 34 | + .style, |
| 35 | + }} |
| 36 | + > |
| 37 | + <div |
| 38 | + className={cn( |
| 39 | + 'absolute w-[300%] h-[50%] bottom-[-11px] right-[-250%] rounded-full animate-star-movement-bottom z-0 transition-opacity duration-500', |
| 40 | + !isAnimating && 'opacity-0', |
| 41 | + )} |
| 42 | + style={{ |
| 43 | + background: `radial-gradient(circle, ${color}, transparent 10%)`, |
| 44 | + animationDuration: speed, |
| 45 | + animationPlayState: isAnimating ? 'running' : 'paused', |
| 46 | + opacity: isAnimating ? 0.7 : 0, |
| 47 | + }} |
| 48 | + /> |
| 49 | + <div |
| 50 | + className={cn( |
| 51 | + 'absolute w-[300%] h-[50%] top-[-10px] left-[-250%] rounded-full animate-star-movement-top z-0 transition-opacity duration-500', |
| 52 | + !isAnimating && 'opacity-0', |
| 53 | + )} |
| 54 | + style={{ |
| 55 | + background: `radial-gradient(circle, ${color}, transparent 10%)`, |
| 56 | + animationDuration: speed, |
| 57 | + animationPlayState: isAnimating ? 'running' : 'paused', |
| 58 | + opacity: isAnimating ? 0.7 : 0, |
| 59 | + }} |
| 60 | + /> |
| 61 | + <div className="relative z-[1]">{children}</div> |
| 62 | + </Component> |
| 63 | + ); |
| 64 | +}; |
| 65 | + |
| 66 | +export default StarBorder; |
0 commit comments