users.tsx95 lines · main
| 1 | 'use client'; |
| 2 | |
| 3 | import { motion, useAnimation } from 'motion/react'; |
| 4 | import type { HTMLAttributes } from 'react'; |
| 5 | import { forwardRef, useCallback, useImperativeHandle, useRef } from 'react'; |
| 6 | |
| 7 | import { cn } from '@/lib/utils'; |
| 8 | |
| 9 | export interface UsersIconHandle { |
| 10 | startAnimation: () => void; |
| 11 | stopAnimation: () => void; |
| 12 | } |
| 13 | |
| 14 | interface UsersIconProps extends HTMLAttributes<HTMLDivElement> { |
| 15 | size?: number; |
| 16 | } |
| 17 | |
| 18 | const UsersIcon = forwardRef<UsersIconHandle, UsersIconProps>( |
| 19 | ({ onMouseEnter, onMouseLeave, className, size = 28, ...props }, ref) => { |
| 20 | const controls = useAnimation(); |
| 21 | const isControlledRef = useRef(false); |
| 22 | |
| 23 | useImperativeHandle(ref, () => { |
| 24 | isControlledRef.current = true; |
| 25 | return { |
| 26 | startAnimation: () => controls.start('animate'), |
| 27 | stopAnimation: () => controls.start('normal'), |
| 28 | }; |
| 29 | }); |
| 30 | |
| 31 | const handleMouseEnter = useCallback( |
| 32 | (e: React.MouseEvent<HTMLDivElement>) => { |
| 33 | if (isControlledRef.current) onMouseEnter?.(e); |
| 34 | else controls.start('animate'); |
| 35 | }, |
| 36 | [controls, onMouseEnter], |
| 37 | ); |
| 38 | |
| 39 | const handleMouseLeave = useCallback( |
| 40 | (e: React.MouseEvent<HTMLDivElement>) => { |
| 41 | if (isControlledRef.current) onMouseLeave?.(e); |
| 42 | else controls.start('normal'); |
| 43 | }, |
| 44 | [controls, onMouseLeave], |
| 45 | ); |
| 46 | |
| 47 | return ( |
| 48 | <div |
| 49 | className={cn(className)} |
| 50 | onMouseEnter={handleMouseEnter} |
| 51 | onMouseLeave={handleMouseLeave} |
| 52 | {...props} |
| 53 | > |
| 54 | <svg |
| 55 | fill="none" |
| 56 | height={size} |
| 57 | stroke="currentColor" |
| 58 | strokeLinecap="round" |
| 59 | strokeLinejoin="round" |
| 60 | strokeWidth="2" |
| 61 | viewBox="0 0 24 24" |
| 62 | width={size} |
| 63 | xmlns="http://www.w3.org/2000/svg" |
| 64 | > |
| 65 | {/* Primary user (left). Stays put on hover. */} |
| 66 | <path d="M16 21v-2a4 4 0 0 0-4-4H5a4 4 0 0 0-4 4v2" /> |
| 67 | <circle cx="8.5" cy="7" r="4" /> |
| 68 | {/* Secondary user (right). Pops in on hover. */} |
| 69 | <motion.path |
| 70 | animate={controls} |
| 71 | d="M22 21v-2a4 4 0 0 0-3-3.87" |
| 72 | transition={{ type: 'spring', stiffness: 250, damping: 25 }} |
| 73 | variants={{ |
| 74 | normal: { translateX: 0, opacity: 1 }, |
| 75 | animate: { translateX: -1, opacity: 1 }, |
| 76 | }} |
| 77 | /> |
| 78 | <motion.path |
| 79 | animate={controls} |
| 80 | d="M16 3.13a4 4 0 0 1 0 7.75" |
| 81 | transition={{ type: 'spring', stiffness: 250, damping: 25 }} |
| 82 | variants={{ |
| 83 | normal: { translateX: 0, opacity: 1 }, |
| 84 | animate: { translateX: -1, opacity: 1 }, |
| 85 | }} |
| 86 | /> |
| 87 | </svg> |
| 88 | </div> |
| 89 | ); |
| 90 | }, |
| 91 | ); |
| 92 | |
| 93 | UsersIcon.displayName = 'UsersIcon'; |
| 94 | |
| 95 | export { UsersIcon }; |