47 lines
1.4 KiB
TypeScript
47 lines
1.4 KiB
TypeScript
import { forwardRef, ButtonHTMLAttributes } from 'react';
|
|
import { cn } from '@/lib/utils';
|
|
import { LoadingSpinner } from './LoadingSpinner';
|
|
|
|
interface ButtonProps extends ButtonHTMLAttributes<HTMLButtonElement> {
|
|
variant?: 'primary' | 'secondary' | 'danger' | 'ghost';
|
|
size?: 'sm' | 'md' | 'lg';
|
|
isLoading?: boolean;
|
|
}
|
|
|
|
export const Button = forwardRef<HTMLButtonElement, ButtonProps>(
|
|
(
|
|
{ className, variant = 'primary', size = 'md', isLoading, children, disabled, ...props },
|
|
ref
|
|
) => {
|
|
const baseClasses =
|
|
'inline-flex items-center justify-center font-medium rounded-md focus:outline-none focus:ring-2 focus:ring-offset-2 disabled:opacity-50 disabled:cursor-not-allowed';
|
|
|
|
const variants = {
|
|
primary: 'bg-blue-600 text-white hover:bg-blue-700 focus:ring-blue-500',
|
|
secondary: 'bg-gray-600 text-white hover:bg-gray-700 focus:ring-gray-500',
|
|
danger: 'bg-red-600 text-white hover:bg-red-700 focus:ring-red-500',
|
|
ghost: 'text-gray-300 hover:text-white hover:bg-gray-700 focus:ring-gray-500'
|
|
};
|
|
|
|
const sizes = {
|
|
sm: 'px-3 py-1 text-sm',
|
|
md: 'px-4 py-2 text-sm',
|
|
lg: 'px-6 py-3 text-base'
|
|
};
|
|
|
|
return (
|
|
<button
|
|
className={cn(baseClasses, variants[variant], sizes[size], className)}
|
|
disabled={disabled || isLoading}
|
|
ref={ref}
|
|
{...props}
|
|
>
|
|
{isLoading && <LoadingSpinner size="sm" className="mr-2" />}
|
|
{children}
|
|
</button>
|
|
);
|
|
}
|
|
);
|
|
|
|
Button.displayName = 'Button';
|