-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIconButton.tsx
More file actions
65 lines (56 loc) · 1.97 KB
/
IconButton.tsx
File metadata and controls
65 lines (56 loc) · 1.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import React, { useState, useRef, useCallback } from 'react';
import Tooltip from './Tooltip';
interface IconButtonProps extends React.ButtonHTMLAttributes<HTMLButtonElement> {
children: React.ReactNode;
tooltip?: string;
variant?: 'primary' | 'ghost' | 'destructive';
size?: 'xs' | 'sm' | 'md';
tooltipPosition?: 'top' | 'bottom';
}
const IconButton: React.FC<IconButtonProps> = ({ children, tooltip, className, variant = 'primary', size = 'md', tooltipPosition = 'top', ...props }) => {
const [isHovered, setIsHovered] = useState(false);
const wrapperRef = useRef<HTMLSpanElement>(null);
const { ['aria-label']: ariaLabel, ...buttonProps } = props;
const computedAriaLabel = ariaLabel ?? tooltip;
const handleMouseEnter = useCallback(() => {
if (tooltip) setIsHovered(true);
}, [tooltip]);
const handleMouseLeave = useCallback(() => {
setIsHovered(false);
}, []);
const baseClasses = "flex items-center justify-center rounded-sm focus:outline-none transition-colors duration-100";
const variantClasses = {
primary: 'text-text-secondary hover:text-text-main hover:bg-border-color/30',
ghost: 'text-text-secondary hover:text-text-main',
destructive: 'text-destructive-text bg-transparent hover:bg-destructive-bg/50'
};
const sizeClasses = {
xs: 'w-6 h-6',
sm: 'w-7 h-7',
md: 'w-8 h-8'
};
return (
<>
<span
ref={wrapperRef}
className="inline-flex"
onMouseEnter={handleMouseEnter}
onMouseLeave={handleMouseLeave}
onFocus={handleMouseEnter}
onBlur={handleMouseLeave}
>
<button
className={`${baseClasses} ${variantClasses[variant]} ${sizeClasses[size]} ${className}`}
aria-label={computedAriaLabel}
{...buttonProps}
>
{children}
</button>
</span>
{isHovered && (
<Tooltip targetRef={wrapperRef} content={tooltip} position={tooltipPosition} />
)}
</>
);
};
export default IconButton;