|
| 1 | +import React, {MouseEventHandler, ReactNode} from 'react' |
| 2 | +import {usePopperTooltip} from 'react-popper-tooltip' |
| 3 | + |
| 4 | +const classNames = `border p-1 transition-colors duration-100 rounded` |
| 5 | +const disabledClassNames = `bg-gray-100 border-gray-200 text-gray-200 cursor-not-allowed pointer-events-none` |
| 6 | +const toneClassNames = { |
| 7 | + danger: `border-gray-200 text-gray-400 hover:text-red-500 hover:border-red-500 hover:bg-red-100`, |
| 8 | + success: `border-gray-200 text-gray-400 hover:text-green-500 hover:border-green-500 hover:bg-green-100`, |
| 9 | +} |
| 10 | +const selectedClassNames = { |
| 11 | + danger: `text-red-500 border-red-500 bg-red-100`, |
| 12 | + success: `text-green-500 border-green-500 bg-green-100`, |
| 13 | +} |
| 14 | +const toneTooltipClassNames = { |
| 15 | + danger: `text-red-700`, |
| 16 | + success: `text-green-700`, |
| 17 | +} |
| 18 | + |
| 19 | +export default function ButtonIcon({ |
| 20 | + title, |
| 21 | + onClick, |
| 22 | + icon, |
| 23 | + href = ``, |
| 24 | + disabled = false, |
| 25 | + selected = false, |
| 26 | + tone = 'success', |
| 27 | + tabIndex = -1, |
| 28 | +}: { |
| 29 | + title: string |
| 30 | + icon: ReactNode |
| 31 | + href?: string |
| 32 | + onClick?: MouseEventHandler | undefined |
| 33 | + disabled?: boolean |
| 34 | + selected?: boolean |
| 35 | + tone?: 'danger' | 'success' |
| 36 | + tabIndex?: number |
| 37 | +}) { |
| 38 | + const className = [ |
| 39 | + disabled ? disabledClassNames : toneClassNames[tone], |
| 40 | + selected ? selectedClassNames[tone] : ``, |
| 41 | + classNames, |
| 42 | + ] |
| 43 | + .filter(Boolean) |
| 44 | + .join(' ') |
| 45 | + |
| 46 | + const {getArrowProps, getTooltipProps, setTooltipRef, setTriggerRef, visible} = usePopperTooltip() |
| 47 | + |
| 48 | + if (href) { |
| 49 | + return ( |
| 50 | + <a |
| 51 | + href={href} |
| 52 | + className={className} |
| 53 | + tabIndex={tabIndex} |
| 54 | + title={title} |
| 55 | + target="_blank" |
| 56 | + rel="noopener noreferrer" |
| 57 | + > |
| 58 | + <> |
| 59 | + {icon ? React.createElement(icon, {className: `w-5 h-auto`}) : null} |
| 60 | + <span className="sr-only">{title}</span> |
| 61 | + </> |
| 62 | + </a> |
| 63 | + ) |
| 64 | + } |
| 65 | + |
| 66 | + return ( |
| 67 | + <> |
| 68 | + <button |
| 69 | + ref={setTriggerRef} |
| 70 | + type="button" |
| 71 | + className={className} |
| 72 | + onClick={onClick} |
| 73 | + disabled={disabled} |
| 74 | + tabIndex={tabIndex} |
| 75 | + // title={title} |
| 76 | + > |
| 77 | + {icon ? React.createElement(icon, {className: `w-5 h-auto`}) : null} |
| 78 | + <span className="sr-only">{title}</span> |
| 79 | + </button> |
| 80 | + {visible && ( |
| 81 | + <div |
| 82 | + ref={setTooltipRef} |
| 83 | + {...getTooltipProps({ |
| 84 | + className: `w-32 text-center z-50 bg-white p-2 rounded shadow text-xs font-medium ${toneTooltipClassNames[tone]}`, |
| 85 | + })} |
| 86 | + > |
| 87 | + <div |
| 88 | + {...getArrowProps({ |
| 89 | + className: `absolute bottom-full bg-white h-2 w-4 pointer-events-none`, |
| 90 | + style: {clipPath: `polygon(50% 0%, 0% 100%, 100% 100%)`}, |
| 91 | + })} |
| 92 | + /> |
| 93 | + {title} |
| 94 | + </div> |
| 95 | + )} |
| 96 | + </> |
| 97 | + ) |
| 98 | +} |
0 commit comments