-
-
Notifications
You must be signed in to change notification settings - Fork 212
Expand file tree
/
Copy pathColorInput.tsx
More file actions
170 lines (156 loc) · 4.09 KB
/
ColorInput.tsx
File metadata and controls
170 lines (156 loc) · 4.09 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import React, { forwardRef } from 'react';
import styled, { css } from 'styled-components';
import { StyledButton } from '../Button/Button';
import { focusOutline } from '../common';
import useControlledOrUncontrolled from '../common/hooks/useControlledOrUncontrolled';
import { noOp } from '../common/utils';
import { Separator } from '../Separator/Separator';
import { CommonStyledProps } from '../types';
type ColorInputProps = {
defaultValue?: string;
disabled?: boolean;
onChange?: React.ChangeEventHandler<HTMLInputElement>;
value?: string;
variant?: 'default' | 'flat';
} & Omit<
React.InputHTMLAttributes<HTMLInputElement>,
'defaultValue' | 'disabled' | 'onChange' | 'value'
> &
CommonStyledProps;
const Trigger = styled(StyledButton)`
padding-left: 8px;
`;
const StyledSeparator = styled(Separator)`
height: 21px;
position: relative;
top: 0;
`;
export const StyledColorInput = styled.input`
box-sizing: border-box;
width: 100%;
height: 100%;
position: absolute;
left: 0;
top: 0;
opacity: 0;
z-index: 1;
cursor: pointer;
&:disabled {
cursor: default;
}
`;
// TODO replace with SVG icon
const ColorPreview = styled.div<{
color: string;
$disabled: boolean;
}>`
box-sizing: border-box;
height: 19px;
display: inline-block;
width: 35px;
margin-right: 5px;
background: ${({ color }) => color};
${({ $disabled }) =>
$disabled
? css`
border: 2px solid ${({ theme }) => theme.materialTextDisabled};
filter: drop-shadow(
1px 1px 0px ${({ theme }) => theme.materialTextDisabledShadow}
);
`
: css`
border: 2px solid ${({ theme }) => theme.materialText};
`}
${StyledColorInput}:focus:not(:active) + &:after {
content: '';
position: absolute;
left: 0;
top: 0;
width: 100%;
height: 100%;
${focusOutline}
outline-offset: -8px;
}
`;
const ChevronIcon = styled.span<
Required<Pick<ColorInputProps, 'variant'>> & {
$disabled: boolean;
}
>`
width: 0px;
height: 0px;
border-left: 6px solid transparent;
border-right: 6px solid transparent;
display: inline-block;
margin-left: 6px;
${({ $disabled }) =>
$disabled
? css`
border-top: 6px solid ${({ theme }) => theme.materialTextDisabled};
filter: drop-shadow(
1px 1px 0px ${({ theme }) => theme.materialTextDisabledShadow}
);
`
: css`
border-top: 6px solid ${({ theme }) => theme.materialText};
`}
&:after {
content: '';
box-sizing: border-box;
position: absolute;
top: ${({ variant }) => (variant === 'flat' ? '6px' : '8px')};
right: 8px;
width: 16px;
height: 19px;
}
`;
// TODO make sure all aria and role attributes are in place
const ColorInput = forwardRef<HTMLInputElement, ColorInputProps>(
(
{
value,
defaultValue,
onChange = noOp,
disabled = false,
variant = 'default',
...otherProps
},
ref
) => {
const [valueDerived, setValueState] = useControlledOrUncontrolled({
defaultValue,
onChange,
readOnly: otherProps.readOnly ?? disabled,
value
});
const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
const color = e.target.value;
setValueState(color);
onChange(e);
};
return (
// we only need button styles, so we display
// it as a div and reset type attribute
<Trigger $disabled={disabled} as='div' $variant={variant} $size='md'>
<StyledColorInput
onChange={handleChange}
readOnly={disabled}
disabled={disabled}
value={valueDerived ?? '#008080'}
type='color'
ref={ref}
{...otherProps}
/>
<ColorPreview
$disabled={disabled}
color={valueDerived ?? '#008080'}
role='presentation'
/>
{variant === 'default' && <StyledSeparator orientation='vertical' />}
<ChevronIcon $disabled={disabled} variant={variant} />
</Trigger>
);
}
);
ColorInput.displayName = 'ColorInput';
export { ColorInput, ColorInputProps };