|
| 1 | +import React from 'react'; |
| 2 | +import { twMerge } from 'tailwind-merge'; |
| 3 | + |
| 4 | +interface ButtonProps { |
| 5 | + children: React.ReactNode; |
| 6 | + variant?: 'primary' | 'secondary' | 'danger'; |
| 7 | + style: 'fill' | 'outline' | 'link'; |
| 8 | + type?: 'button' | 'submit' | 'reset'; |
| 9 | + size: 'small' | 'medium' | 'large'; |
| 10 | + disabled?: boolean; |
| 11 | + as?: React.ElementType; |
| 12 | + className?: string; |
| 13 | + onClick?: () => void; |
| 14 | + href?: string; |
| 15 | + target?: string; |
| 16 | + rel?: string; |
| 17 | +} |
| 18 | + |
| 19 | +const Styles = { |
| 20 | + 'primary:fill': |
| 21 | + 'bg-indigo-600 text-white shadow-sm hover:bg-indigo-500 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600', |
| 22 | + 'primary:outline': |
| 23 | + 'bg-white ring-1 ring-inset ring-indigo-600 text-indigo-600 hover:bg-indigo-600 hover:text-white shadow-sm', |
| 24 | + 'primary:link': 'text-indigo-600 hover:text-indigo-500', |
| 25 | + 'secondary:fill': |
| 26 | + 'bg-white ring-1 ring-inset ring-gray-300 hover:bg-gray-50 text-gray-900 shadow-sm', |
| 27 | + 'secondary:outline': 'bg-white hover:bg-gray-50 text-gray-900 shadow-sm', |
| 28 | + 'secondary:link': 'text-gray-900 hover:text-gray-500', |
| 29 | + 'danger:fill': 'bg-red-600 hover:bg-red-500 text-white', |
| 30 | + 'danger:outline': |
| 31 | + 'bg-white ring-1 ring-inset ring-red-600 hover:bg-red-600 text-red-600 hover:text-white shadow-sm', |
| 32 | + 'danger:link': 'text-red-600 hover:text-red-500', |
| 33 | +}; |
| 34 | + |
| 35 | +const Button: React.FC<ButtonProps> = ({ |
| 36 | + children, |
| 37 | + variant = 'primary', |
| 38 | + style = 'fill', |
| 39 | + type = 'button', |
| 40 | + disabled = false, |
| 41 | + as = 'button', |
| 42 | + className = '', |
| 43 | + onClick, |
| 44 | + href, |
| 45 | + target, |
| 46 | + rel, |
| 47 | + size = 'medium', |
| 48 | +}) => { |
| 49 | + const getSizeStyles = () => { |
| 50 | + switch (size) { |
| 51 | + case 'small': |
| 52 | + return 'rounded px-2 py-1 text-xs'; |
| 53 | + case 'medium': |
| 54 | + return 'rounded-md px-3 py-2 text-sm'; |
| 55 | + case 'large': |
| 56 | + return 'rounded-md px-4 py-3 text-sm'; |
| 57 | + default: |
| 58 | + return ''; |
| 59 | + } |
| 60 | + }; |
| 61 | + |
| 62 | + const handleClick = (event: React.MouseEvent<HTMLButtonElement>) => { |
| 63 | + if (disabled) { |
| 64 | + event.preventDefault(); |
| 65 | + return; |
| 66 | + } |
| 67 | + |
| 68 | + if (onClick) { |
| 69 | + onClick(); |
| 70 | + } |
| 71 | + }; |
| 72 | + |
| 73 | + const renderButton = () => { |
| 74 | + const commonStyles = 'font-semibold focus:outline-none'; |
| 75 | + const sizeStyles = getSizeStyles(); |
| 76 | + |
| 77 | + const disabledStyles = disabled ? 'opacity-50 cursor-not-allowed' : ''; |
| 78 | + |
| 79 | + return React.createElement( |
| 80 | + as, |
| 81 | + { |
| 82 | + className: twMerge( |
| 83 | + commonStyles, |
| 84 | + Styles[`${variant}:${style}`], |
| 85 | + sizeStyles, |
| 86 | + disabledStyles, |
| 87 | + className |
| 88 | + ), |
| 89 | + type, |
| 90 | + disabled: disabled, |
| 91 | + onClick: handleClick, |
| 92 | + href, |
| 93 | + target, |
| 94 | + rel, |
| 95 | + }, |
| 96 | + children |
| 97 | + ); |
| 98 | + }; |
| 99 | + |
| 100 | + return renderButton(); |
| 101 | +}; |
| 102 | + |
| 103 | +export default Button; |
0 commit comments