This repository has been archived by the owner on Mar 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 177
/
CheckboxButton.tsx
66 lines (59 loc) · 1.56 KB
/
CheckboxButton.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import React from 'react';
import styled from 'styled-components';
import css from '@styled-system/css';
import omit from 'lodash.omit';
import pick from 'lodash.pick';
import { margin, MarginProps } from '@modulz/radix-system';
export type CheckboxButtonProps = MarginProps &
React.ComponentPropsWithRef<'input'> & { children: React.ReactNode };
const marginPropNames = margin.propNames;
export const CheckboxButton = React.forwardRef<HTMLLabelElement, CheckboxButtonProps>(
({ children, ...props }, forwardedRef) => {
const marginProps = pick(props, marginPropNames);
const inputProps = omit(props, marginPropNames);
return (
<CheckboxWrapper {...marginProps} ref={forwardedRef}>
<Input type="checkbox" {...inputProps} />
<FakeCheckbox>{children}</FakeCheckbox>
</CheckboxWrapper>
);
}
);
const CheckboxWrapper = styled('label')<MarginProps>(
{
position: 'relative',
},
margin
);
const Input = styled('input')({
appearance: 'none',
position: 'absolute',
top: 0,
left: 0,
width: '100%',
height: '100%',
zIndex: 0,
outline: 'none',
margin: 0,
opacity: 0,
WebkitTapHighlightColor: 'rgba(0, 0, 0, 0)',
'&:hover': {
cursor: 'pointer',
},
});
const FakeCheckbox = styled('div')(
css({
display: 'inline-flex',
verticalAlign: 'middle',
alignItems: 'center',
justifyContent: 'center',
color: 'gray700',
WebkitTapHighlightColor: 'rgba(0, 0, 0, 0)',
[`${Input}:focus + &`]: {
color: 'gray800',
},
[`${Input}:checked + &`]: {
color: 'blue600',
},
})
);