|
| 1 | +import React from 'react'; |
| 2 | +import styled, { DefaultTheme } from 'styled-components'; |
| 3 | +import { transition } from '../../helpers/transition'; |
| 4 | + |
| 5 | +export enum IconButtonVariant { |
| 6 | + Simple, |
| 7 | + Outline, |
| 8 | + Fill, |
| 9 | +} |
| 10 | + |
| 11 | +type ColorProp = keyof DefaultTheme['colors'] | 'inherit'; |
| 12 | + |
| 13 | +interface BaseProps { |
| 14 | + className?: string; |
| 15 | + variant?: IconButtonVariant; |
| 16 | + color?: ColorProp; |
| 17 | + size?: string; |
| 18 | + title: string; |
| 19 | +} |
| 20 | + |
| 21 | +export type IconButtonProps = BaseProps & |
| 22 | + React.ButtonHTMLAttributes<HTMLButtonElement>; |
| 23 | + |
| 24 | +export const IconButton = React.forwardRef< |
| 25 | + HTMLButtonElement, |
| 26 | + React.PropsWithChildren<IconButtonProps> |
| 27 | +>(({ variant, children, color, ...props }, ref) => { |
| 28 | + const Comp = ComponentMap.get(variant!) ?? SimpleIconButton; |
| 29 | + |
| 30 | + return ( |
| 31 | + <Comp ref={ref} color={color!} {...props}> |
| 32 | + {children} |
| 33 | + </Comp> |
| 34 | + ); |
| 35 | +}); |
| 36 | + |
| 37 | +IconButton.displayName = 'IconButton'; |
| 38 | + |
| 39 | +const defaultProps = { |
| 40 | + variant: IconButtonVariant.Simple, |
| 41 | + color: 'inherit', |
| 42 | + size: '1em', |
| 43 | +} as IconButtonProps; |
| 44 | + |
| 45 | +IconButton.defaultProps = defaultProps; |
| 46 | + |
| 47 | +export type IconButtonLinkProps = BaseProps & |
| 48 | + React.AnchorHTMLAttributes<HTMLAnchorElement> & { |
| 49 | + href: string; |
| 50 | + }; |
| 51 | + |
| 52 | +export const IconButtonLink = React.forwardRef< |
| 53 | + HTMLAnchorElement, |
| 54 | + React.PropsWithChildren<IconButtonLinkProps> |
| 55 | +>(({ variant, children, color, ...props }, ref) => { |
| 56 | + const Comp = ComponentMap.get(variant!) ?? SimpleIconButton; |
| 57 | + |
| 58 | + return ( |
| 59 | + <Comp ref={ref} color={color!} as='a' {...props}> |
| 60 | + {children} |
| 61 | + </Comp> |
| 62 | + ); |
| 63 | +}); |
| 64 | + |
| 65 | +IconButtonLink.displayName = 'IconButtonLink'; |
| 66 | + |
| 67 | +IconButtonLink.defaultProps = defaultProps as IconButtonLinkProps; |
| 68 | + |
| 69 | +interface BaseProps { |
| 70 | + size?: string; |
| 71 | +} |
| 72 | + |
| 73 | +const IconButtonBase = styled.button<BaseProps>` |
| 74 | + --button-padding: 0.4em; |
| 75 | + cursor: pointer; |
| 76 | + display: inline-grid; |
| 77 | + place-items: center; |
| 78 | + transition: ${() => transition('background-color', 'color', 'box-shadow')}; |
| 79 | + color: ${p => p.theme.colors.text}; |
| 80 | + font-size: ${p => p.size ?? '1em'}; |
| 81 | + border: none; |
| 82 | +
|
| 83 | + padding: var(--button-padding); |
| 84 | + width: calc(1em + var(--button-padding) * 2); |
| 85 | + height: calc(1em + var(--button-padding) * 2); |
| 86 | +
|
| 87 | + &[disabled] { |
| 88 | + opacity: 0.5; |
| 89 | + cursor: not-allowed; |
| 90 | + } |
| 91 | +`; |
| 92 | + |
| 93 | +interface ButtonStyleProps { |
| 94 | + color: ColorProp; |
| 95 | +} |
| 96 | + |
| 97 | +const SimpleIconButton = styled(IconButtonBase)<ButtonStyleProps>` |
| 98 | + color: ${p => (p.color === 'inherit' ? 'inherit' : p.theme.colors[p.color])}; |
| 99 | + background-color: transparent; |
| 100 | + border-radius: ${p => p.theme.radius}; |
| 101 | +
|
| 102 | + &:not([disabled]) { |
| 103 | + &:hover, |
| 104 | + &:focus { |
| 105 | + background-color: ${p => p.theme.colors.bg1}; |
| 106 | + } |
| 107 | +
|
| 108 | + :active { |
| 109 | + background-color: ${p => p.theme.colors.bg2}; |
| 110 | + } |
| 111 | + } |
| 112 | +`; |
| 113 | + |
| 114 | +const OutlineIconButton = styled(IconButtonBase)<ButtonStyleProps>` |
| 115 | + color: ${p => (p.color === 'inherit' ? 'inherit' : p.theme.colors[p.color])}; |
| 116 | + background-color: ${p => p.theme.colors.bg}; |
| 117 | + border-radius: 50%; |
| 118 | +
|
| 119 | + &:not([disabled]) { |
| 120 | + &:hover, |
| 121 | + &:focus { |
| 122 | + color: ${p => p.theme.colors.main}; |
| 123 | + box-shadow: 0px 0px 0px 1.5px ${p => p.theme.colors.main}, |
| 124 | + ${p => p.theme.boxShadowSoft}; |
| 125 | + } |
| 126 | + } |
| 127 | +
|
| 128 | + &&:active { |
| 129 | + background-color: ${p => p.theme.colors.main}; |
| 130 | + color: white; |
| 131 | + } |
| 132 | +`; |
| 133 | + |
| 134 | +const FillIconButton = styled(IconButtonBase)<ButtonStyleProps>` |
| 135 | + color: ${p => (p.color === 'inherit' ? 'inherit' : p.theme.colors[p.color])}; |
| 136 | + background-color: unset; |
| 137 | + border-radius: 50%; |
| 138 | + &:hover, |
| 139 | + &:focus { |
| 140 | + color: white; |
| 141 | + background-color: ${p => p.theme.colors.main}; |
| 142 | + box-shadow: ${p => p.theme.boxShadowSoft}; |
| 143 | + } |
| 144 | +`; |
| 145 | + |
| 146 | +const ComponentMap = new Map([ |
| 147 | + [IconButtonVariant.Simple, SimpleIconButton], |
| 148 | + [IconButtonVariant.Outline, OutlineIconButton], |
| 149 | + [IconButtonVariant.Fill, FillIconButton], |
| 150 | +]); |
0 commit comments