|
| 1 | +import { Box, Input, Text } from '@devup-ui/react' |
| 2 | + |
| 3 | +type RadioProps = Omit<React.InputHTMLAttributes<HTMLInputElement>, 'type'> & { |
| 4 | + checked?: boolean |
| 5 | + classNames?: { |
| 6 | + label?: string |
| 7 | + } |
| 8 | + color?: string |
| 9 | + hoverColor?: string |
| 10 | + styles?: { |
| 11 | + label?: React.CSSProperties |
| 12 | + } |
| 13 | + colors?: { |
| 14 | + primary?: string |
| 15 | + border?: string |
| 16 | + text?: string |
| 17 | + bg?: string |
| 18 | + hoverBg?: string |
| 19 | + hoverBorder?: string |
| 20 | + hoverColor?: string |
| 21 | + checkedBg?: string |
| 22 | + checkedBorder?: string |
| 23 | + checkedColor?: string |
| 24 | + disabledBg?: string |
| 25 | + disabledColor?: string |
| 26 | + } |
| 27 | + variant?: 'default' | 'button' |
| 28 | +} & ( |
| 29 | + | { |
| 30 | + variant?: 'default' |
| 31 | + firstButton?: undefined |
| 32 | + lastButton?: undefined |
| 33 | + } |
| 34 | + | { |
| 35 | + variant: 'button' |
| 36 | + firstButton?: boolean |
| 37 | + lastButton?: boolean |
| 38 | + } |
| 39 | + ) |
| 40 | + |
| 41 | +export function Radio({ |
| 42 | + className, |
| 43 | + disabled, |
| 44 | + children, |
| 45 | + variant = 'default', |
| 46 | + checked, |
| 47 | + classNames, |
| 48 | + styles, |
| 49 | + style, |
| 50 | + firstButton, |
| 51 | + lastButton, |
| 52 | + colors, |
| 53 | + ...props |
| 54 | +}: RadioProps) { |
| 55 | + const isButton = variant === 'button' |
| 56 | + return ( |
| 57 | + <Box |
| 58 | + alignItems={isButton ? undefined : 'center'} |
| 59 | + aria-disabled={disabled} |
| 60 | + as="label" |
| 61 | + cursor={isButton ? undefined : 'pointer'} |
| 62 | + display={isButton ? undefined : 'inline-flex'} |
| 63 | + gap={isButton ? undefined : 2} |
| 64 | + selectors={{ |
| 65 | + '&[aria-disabled=true]': { |
| 66 | + cursor: 'default', |
| 67 | + }, |
| 68 | + }} |
| 69 | + > |
| 70 | + {isButton ? ( |
| 71 | + <Input |
| 72 | + checked={checked} |
| 73 | + className={className} |
| 74 | + data-radio-input |
| 75 | + disabled={disabled} |
| 76 | + display="none" |
| 77 | + opacity={0} |
| 78 | + styleOrder={1} |
| 79 | + type="radio" |
| 80 | + {...props} |
| 81 | + /> |
| 82 | + ) : ( |
| 83 | + <Input |
| 84 | + _focus={{ |
| 85 | + outline: '1px sold var(--border, var(--primary))', |
| 86 | + }} |
| 87 | + appearance="none" |
| 88 | + bg="light-dark(#fff, #2E2E2E)" |
| 89 | + border="1px solid" |
| 90 | + borderColor="$border" |
| 91 | + borderRadius="100%" |
| 92 | + checked={checked} |
| 93 | + className={className} |
| 94 | + data-radio-input |
| 95 | + disabled={disabled} |
| 96 | + height="18px" |
| 97 | + m={0} |
| 98 | + selectors={{ |
| 99 | + // checked |
| 100 | + '&:checked:not(:disabled)': { |
| 101 | + bg: 'var(--checkedBg, var(--primary, light-dark(#fff, #2E2E2E)))', |
| 102 | + border: '3px solid', |
| 103 | + borderColor: 'var(--checkedBg, light-dark(#fff, #2E2E2E))', |
| 104 | + boxShadow: '0 0 0 1px var(--checkedBorder, var(--primary))', |
| 105 | + }, |
| 106 | + // hover |
| 107 | + '&:hover:not(:disabled,:checked)': { |
| 108 | + border: '1px solid var(--hoverBorder, var(--primary))', |
| 109 | + bg: 'var(--hoverBg, light-dark(color-mix(in srgb, var(--primary) 10%, white 90%), color-mix(in srgb, var(--primary) 10%, black 90%)))', |
| 110 | + }, |
| 111 | + // disabled |
| 112 | + '&:is(:disabled, [aria-disabled=true])': { |
| 113 | + bgColor: 'var(--disabledBg, light-dark(#F0F0F3, #47474A))', |
| 114 | + }, |
| 115 | + }} |
| 116 | + styleOrder={1} |
| 117 | + styleVars={{ |
| 118 | + primary: colors?.primary, |
| 119 | + border: colors?.border, |
| 120 | + text: colors?.text, |
| 121 | + bg: colors?.bg, |
| 122 | + hoverBg: colors?.hoverBg, |
| 123 | + hoverBorder: colors?.hoverBorder, |
| 124 | + hoverColor: colors?.hoverColor, |
| 125 | + checkedBg: colors?.checkedBg, |
| 126 | + checkedBorder: colors?.checkedBorder, |
| 127 | + checkedColor: colors?.checkedColor, |
| 128 | + disabledBg: colors?.disabledBg, |
| 129 | + disabledColor: colors?.disabledColor, |
| 130 | + }} |
| 131 | + transition=".25s" |
| 132 | + type="radio" |
| 133 | + width="18px" |
| 134 | + {...props} |
| 135 | + /> |
| 136 | + )} |
| 137 | + {variant === 'button' ? ( |
| 138 | + <Box |
| 139 | + _disabled={{ |
| 140 | + cursor: 'not-allowed', |
| 141 | + }} |
| 142 | + aria-disabled={disabled} |
| 143 | + bg="var(--bg, light-dark(#fff, #2E2E2E))" |
| 144 | + border="1px solid" |
| 145 | + borderColor="$border" |
| 146 | + borderRadius={ |
| 147 | + firstButton ? '6px 0 0 6px' : lastButton ? '0 6px 6px 0' : undefined |
| 148 | + } |
| 149 | + className={className} |
| 150 | + color="var(--text, light-dark(#000, #fff))" |
| 151 | + cursor="pointer" |
| 152 | + data-radio-button |
| 153 | + display="flex" |
| 154 | + px={8} |
| 155 | + py={4} |
| 156 | + selectors={{ |
| 157 | + // checked |
| 158 | + '[data-radio-input]:checked + &:not([aria-disabled=true])': { |
| 159 | + fontWeight: 600, |
| 160 | + bg: `var(--checkedBg, light-dark(color-mix(in srgb, var(--primary) 10%, white 80%), color-mix(in srgb, var(--primary) 10%, black 80%)))`, |
| 161 | + borderColor: 'var(--checkedBorder, var(--primary))', |
| 162 | + color: 'var(--checkedColor, var(--primary))', |
| 163 | + }, |
| 164 | + // hover |
| 165 | + '&:hover:not([aria-disabled=true])': { |
| 166 | + bg: `var(--hoverBg, light-dark(color-mix(in srgb, var(--primary) 10%, white 90%), color-mix(in srgb, var(--primary) 10%, black 90%)))`, |
| 167 | + borderColor: 'var(--hoverBorder, var(--primary))', |
| 168 | + }, |
| 169 | + // disabled |
| 170 | + '[data-radio-input]:disabled + &': { |
| 171 | + bg: 'var(--disabledBg, light-dark(#F0F0F3, #47474A))', |
| 172 | + color: 'var(--disabledColor, light-dark(#D6D7DE, #373737))', |
| 173 | + }, |
| 174 | + }} |
| 175 | + style={style} |
| 176 | + styleOrder={1} |
| 177 | + styleVars={{ |
| 178 | + primary: colors?.primary, |
| 179 | + border: colors?.border, |
| 180 | + text: colors?.text, |
| 181 | + bg: colors?.bg, |
| 182 | + hoverBg: colors?.hoverBg, |
| 183 | + hoverBorder: colors?.hoverBorder, |
| 184 | + hoverColor: colors?.hoverColor, |
| 185 | + checkedBg: colors?.checkedBg, |
| 186 | + checkedBorder: colors?.checkedBorder, |
| 187 | + checkedColor: colors?.checkedColor, |
| 188 | + disabledBg: colors?.disabledBg, |
| 189 | + disabledColor: colors?.disabledColor, |
| 190 | + }} |
| 191 | + transition="background-color 0.25s, border-color 0.25s" |
| 192 | + w="fit-content" |
| 193 | + > |
| 194 | + {children} |
| 195 | + </Box> |
| 196 | + ) : ( |
| 197 | + <Text |
| 198 | + aria-disabled={disabled} |
| 199 | + className={classNames?.label} |
| 200 | + color="var(--text, light-dark(#000, #fff))" |
| 201 | + selectors={{ |
| 202 | + '&[aria-disabled=true]': { |
| 203 | + color: 'var(--disabledColor, light-dark(#D6D7DE, #373737))', |
| 204 | + }, |
| 205 | + }} |
| 206 | + style={style} |
| 207 | + styleOrder={1} |
| 208 | + styleVars={{ |
| 209 | + primary: colors?.primary, |
| 210 | + border: colors?.border, |
| 211 | + text: colors?.text, |
| 212 | + bg: colors?.bg, |
| 213 | + hoverBg: colors?.hoverBg, |
| 214 | + hoverBorder: colors?.hoverBorder, |
| 215 | + hoverColor: colors?.hoverColor, |
| 216 | + checkedBg: colors?.checkedBg, |
| 217 | + checkedBorder: colors?.checkedBorder, |
| 218 | + checkedColor: colors?.checkedColor, |
| 219 | + disabledBg: colors?.disabledBg, |
| 220 | + disabledColor: colors?.disabledColor, |
| 221 | + }} |
| 222 | + > |
| 223 | + {children} |
| 224 | + </Text> |
| 225 | + )} |
| 226 | + </Box> |
| 227 | + ) |
| 228 | +} |
0 commit comments