|
| 1 | +import React from 'react'; |
| 2 | +import styled from 'styled-components'; |
| 3 | +import { ButtonProps, Button } from 'react-aria-components'; |
| 4 | +import { IconProps } from '../../../icons'; |
| 5 | +import { getSemanticValue } from '../../../essentials/experimental'; |
| 6 | + |
| 7 | +export interface IconButtonProps extends ButtonProps { |
| 8 | + isActive?: boolean; |
| 9 | + variant?: 'standard' | 'tonal'; |
| 10 | + Icon: React.FC<IconProps>; |
| 11 | + onPress: () => void; |
| 12 | +} |
| 13 | + |
| 14 | +const StandardIconContainer = styled(Button)<Omit<IconButtonProps, 'Icon'>>` |
| 15 | + height: 2.5rem; |
| 16 | + width: 2.5rem; |
| 17 | + border-radius: 100%; |
| 18 | + background-color: transparent; |
| 19 | + border-color: transparent; |
| 20 | +
|
| 21 | + /* we create a before pseudo element to mess with the opacity (see the hovered state) */ |
| 22 | + &::before { |
| 23 | + position: absolute; |
| 24 | + content: ''; |
| 25 | + border-radius: inherit; |
| 26 | + opacity: 0; |
| 27 | + height: inherit; |
| 28 | + width: inherit; |
| 29 | + } |
| 30 | +
|
| 31 | + /* we want to change the opacity here but not affect the icon, so we have to use the before pseudo element */ |
| 32 | + &[data-hovered]::before { |
| 33 | + opacity: 0.16; |
| 34 | + background-color: ${getSemanticValue('on-surface')}; |
| 35 | + } |
| 36 | +
|
| 37 | + display: flex; |
| 38 | + align-items: center; |
| 39 | + justify-content: center; |
| 40 | +
|
| 41 | + &:not([data-disabled]) { |
| 42 | + color: ${props => (props.isActive ? getSemanticValue('interactive') : getSemanticValue('on-surface'))}; |
| 43 | + } |
| 44 | +
|
| 45 | + &[data-disabled] { |
| 46 | + opacity: 0.38; |
| 47 | + } |
| 48 | +`; |
| 49 | + |
| 50 | +const TonalIconContainer = styled(Button)<Omit<IconButtonProps, 'Icon'>>` |
| 51 | + height: 3.5rem; |
| 52 | + width: 3.5rem; |
| 53 | + border-radius: 100%; |
| 54 | + border-color: transparent; |
| 55 | + background: none; |
| 56 | +
|
| 57 | + /* we create a before pseudo element to mess with the opacity (see the hovered state) */ |
| 58 | + &::before { |
| 59 | + position: absolute; |
| 60 | + content: ''; |
| 61 | + border-radius: inherit; |
| 62 | + height: inherit; |
| 63 | + width: inherit; |
| 64 | + background-color: ${props => |
| 65 | + props.isActive && !props.isDisabled |
| 66 | + ? getSemanticValue('interactive-container') |
| 67 | + : getSemanticValue('surface')}; |
| 68 | + z-index: -1; |
| 69 | + } |
| 70 | +
|
| 71 | + /* we want to change the opacity here but not affect the icon, so we have to use the before pseudo element */ |
| 72 | + &[data-hovered]::before { |
| 73 | + background-color: color-mix( |
| 74 | + in hsl, |
| 75 | + ${getSemanticValue('on-surface')} 100%, |
| 76 | + ${props => (props.isActive ? getSemanticValue('interactive-container') : getSemanticValue('on-surface'))} |
| 77 | + 100% |
| 78 | + ); |
| 79 | + opacity: 0.16; |
| 80 | + } |
| 81 | +
|
| 82 | + display: flex; |
| 83 | + align-items: center; |
| 84 | + justify-content: center; |
| 85 | +
|
| 86 | + &:not([data-disabled]) { |
| 87 | + color: ${props => |
| 88 | + props.isActive ? getSemanticValue('on-interactive-container') : getSemanticValue('on-surface')}; |
| 89 | + } |
| 90 | +
|
| 91 | + &[data-disabled] { |
| 92 | + opacity: 0.38; |
| 93 | + } |
| 94 | +`; |
| 95 | + |
| 96 | +export const IconButton = ({ |
| 97 | + isDisabled = false, |
| 98 | + isActive = false, |
| 99 | + Icon, |
| 100 | + variant = 'standard', |
| 101 | + onPress |
| 102 | +}: IconButtonProps) => |
| 103 | + variant === 'standard' ? ( |
| 104 | + <StandardIconContainer |
| 105 | + data-testid="standard-icon-container" |
| 106 | + onPress={onPress} |
| 107 | + isDisabled={isDisabled} |
| 108 | + isActive={isActive} |
| 109 | + > |
| 110 | + <Icon data-testid="iconbutton-icon" /> |
| 111 | + </StandardIconContainer> |
| 112 | + ) : ( |
| 113 | + <TonalIconContainer |
| 114 | + data-testid="tonal-icon-container" |
| 115 | + onPress={onPress} |
| 116 | + isDisabled={isDisabled} |
| 117 | + isActive={isActive} |
| 118 | + > |
| 119 | + <Icon data-testid="iconbutton-icon" /> |
| 120 | + </TonalIconContainer> |
| 121 | + ); |
0 commit comments