|
1 | | -import {Checkbox as AriaCheckbox, CheckboxProps} from 'react-aria-components'; |
| 1 | +import {Checkbox as AriaCheckbox, CheckboxProps as AriaCheckboxProps, CheckboxRenderProps} from 'react-aria-components'; |
| 2 | +import {style, baseColor} from '../style-macro/spectrum-theme.ts' with {type: 'macro'}; |
| 3 | +import {focusRing} from './style-utils.ts' with {type: 'macro'}; |
| 4 | +import {CenterBaseline} from './CenterBaseline.tsx'; |
| 5 | +import CheckmarkIcon from '../ui-icons/S2_CheckmarkSize100.svg'; |
| 6 | +import DashIcon from '../ui-icons/S2_DashSize100.svg'; |
| 7 | +import {useRef} from 'react'; |
| 8 | +import {pressScale} from './pressScale'; |
| 9 | +import {mergeStyles} from '../style-macro/runtime.ts'; |
2 | 10 |
|
| 11 | +interface CheckboxStyleProps { |
| 12 | + size?: 'S' | 'M' | 'L' | 'XL', |
| 13 | + isEmphasized?: boolean |
| 14 | +} |
| 15 | + |
| 16 | +interface RenderProps extends CheckboxRenderProps, CheckboxStyleProps {} |
| 17 | + |
| 18 | +interface CheckboxProps extends Omit<AriaCheckboxProps, 'className'>, CheckboxStyleProps { |
| 19 | + className?: string |
| 20 | +} |
| 21 | + |
| 22 | +const wrapper = style<RenderProps>({ |
| 23 | + display: 'flex', |
| 24 | + columnGap: 'text-to-control', |
| 25 | + alignItems: 'baseline', |
| 26 | + fontFamily: 'sans', |
| 27 | + fontSize: 'control', |
| 28 | + transition: 'colors', |
| 29 | + color: { |
| 30 | + default: 'neutral', |
| 31 | + isDisabled: { |
| 32 | + default: 'disabled', |
| 33 | + forcedColors: 'GrayText' |
| 34 | + } |
| 35 | + } |
| 36 | +}); |
| 37 | + |
| 38 | +const box = style<RenderProps>({ |
| 39 | + ...focusRing(), |
| 40 | + size: 'control-sm', |
| 41 | + borderRadius: 'control-sm', |
| 42 | + flexShrink: 0, |
| 43 | + display: 'flex', |
| 44 | + alignItems: 'center', |
| 45 | + justifyContent: 'center', |
| 46 | + borderWidth: 2, |
| 47 | + boxSizing: 'border-box', |
| 48 | + borderStyle: 'solid', |
| 49 | + transition: 'default', |
| 50 | + forcedColorAdjust: 'none', |
| 51 | + backgroundColor: { |
| 52 | + default: 'gray-25', |
| 53 | + isSelected: { |
| 54 | + default: 'neutral', |
| 55 | + isEmphasized: baseColor('accent-900'), |
| 56 | + forcedColors: 'Highlight', |
| 57 | + isInvalid: { |
| 58 | + default: baseColor('negative-900'), |
| 59 | + forcedColors: 'Mark' |
| 60 | + }, |
| 61 | + isDisabled: { |
| 62 | + default: 'disabled', |
| 63 | + forcedColors: 'GrayText' |
| 64 | + } |
| 65 | + } |
| 66 | + }, |
| 67 | + borderColor: { |
| 68 | + default: baseColor('gray-800'), |
| 69 | + forcedColors: 'ButtonBorder', |
| 70 | + isInvalid: { |
| 71 | + default: 'negative', |
| 72 | + forcedColors: 'Mark' |
| 73 | + }, |
| 74 | + isDisabled: { |
| 75 | + default: 'disabled', |
| 76 | + forcedColors: 'GrayText' |
| 77 | + }, |
| 78 | + isSelected: 'transparent' |
| 79 | + } |
| 80 | +}); |
| 81 | + |
| 82 | +const iconStyles = style<RenderProps>({ |
| 83 | + size: { |
| 84 | + default: 2.5, |
| 85 | + size: { |
| 86 | + XL: 3 |
| 87 | + } |
| 88 | + }, |
| 89 | + '--iconPrimary': { |
| 90 | + type: 'color', |
| 91 | + value: { |
| 92 | + default: 'gray-25', |
| 93 | + forcedColors: 'HighlightText', |
| 94 | + isDisabled: 'gray-400' |
| 95 | + } |
| 96 | + } |
| 97 | +}); |
3 | 98 |
|
4 | | -export function Checkbox({children, ...props}: CheckboxProps) { |
| 99 | +export function Checkbox({children, ...props}: CheckboxProps & CheckboxStyleProps) { |
| 100 | + let boxRef = useRef(null); |
5 | 101 | return ( |
6 | | - <AriaCheckbox {...props}> |
7 | | - {({isIndeterminate}) => ( |
| 102 | + <AriaCheckbox |
| 103 | + {...props} |
| 104 | + className={renderProps => mergeStyles(props.className, wrapper({...renderProps, size: props.size || 'M'}))}> |
| 105 | + {renderProps => ( |
8 | 106 | <> |
9 | | - <div className="checkbox"> |
10 | | - <svg viewBox="0 0 18 18" aria-hidden="true"> |
11 | | - {isIndeterminate |
12 | | - ? <rect x={1} y={7.5} width={15} height={3} /> |
13 | | - : <polyline points="1 9 7 14 15 4" />} |
14 | | - </svg> |
15 | | - </div> |
| 107 | + <CenterBaseline> |
| 108 | + <div |
| 109 | + ref={boxRef} |
| 110 | + style={pressScale(boxRef)(renderProps)} |
| 111 | + className={box({ |
| 112 | + ...renderProps, |
| 113 | + isSelected: renderProps.isSelected || renderProps.isIndeterminate, |
| 114 | + size: props.size || 'M', |
| 115 | + isEmphasized: props.isEmphasized |
| 116 | + })}> |
| 117 | + {renderProps.isIndeterminate && |
| 118 | + <DashIcon className={iconStyles({...renderProps, size: props.size})} /> |
| 119 | + } |
| 120 | + {renderProps.isSelected && !renderProps.isIndeterminate && |
| 121 | + <CheckmarkIcon className={iconStyles({...renderProps, size: props.size})} /> |
| 122 | + } |
| 123 | + </div> |
| 124 | + </CenterBaseline> |
16 | 125 | {children} |
17 | 126 | </> |
18 | 127 | )} |
|
0 commit comments