| 1 | import { ChevronRightIcon } from "@heroicons/react/24/outline"; |
| 2 | import { ComponentType, useState } from "react"; |
| 3 | |
| 4 | interface ToggleWithIconProps { |
| 5 | icon?: ComponentType<React.SVGProps<SVGSVGElement>>; |
| 6 | isToggleable?: boolean; |
| 7 | open?: boolean; |
| 8 | onClick?: () => void; |
| 9 | className?: string; |
| 10 | isClickable?: boolean; |
| 11 | } |
| 12 | |
| 13 | export function ToggleWithIcon({ |
| 14 | icon: Icon, |
| 15 | isToggleable = false, |
| 16 | open = false, |
| 17 | onClick, |
| 18 | className = "", |
| 19 | isClickable = false, |
| 20 | }: ToggleWithIconProps) { |
| 21 | const [isHovered, setIsHovered] = useState(false); |
| 22 | const showChevron = isToggleable && (isHovered || open); |
| 23 | |
| 24 | function handleClick() { |
| 25 | if ((isToggleable || isClickable) && onClick) { |
| 26 | onClick(); |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | function renderIcon() { |
| 31 | if (!Icon && !isToggleable) { |
| 32 | return null; |
| 33 | } |
| 34 | |
| 35 | if (showChevron) { |
| 36 | return ( |
| 37 | <ChevronRightIcon |
| 38 | className={`text-description h-4 w-4 transition-transform duration-200 ease-in-out ${ |
| 39 | open ? "rotate-90" : "rotate-0" |
| 40 | }`} |
| 41 | /> |
| 42 | ); |
| 43 | } |
| 44 | |
| 45 | return Icon ? <Icon className="text-description h-4 w-4" /> : null; |
| 46 | } |
| 47 | |
| 48 | return ( |
| 49 | <div |
| 50 | className={`flex h-4 w-4 flex-shrink-0 flex-col items-center justify-center ${className}`} |
| 51 | onClick={isToggleable || isClickable ? handleClick : undefined} |
| 52 | onMouseEnter={isToggleable ? () => setIsHovered(true) : undefined} |
| 53 | onMouseLeave={isToggleable ? () => setIsHovered(false) : undefined} |
| 54 | > |
| 55 | {renderIcon()} |
| 56 | </div> |
| 57 | ); |
| 58 | } |