-
Notifications
You must be signed in to change notification settings - Fork 90
/
StyledTextInput.tsx
315 lines (270 loc) · 8.75 KB
/
StyledTextInput.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
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/**
* Copyright Zendesk, Inc.
*
* Use of this source code is governed under the Apache License, Version 2.0
* found at http://www.apache.org/licenses/LICENSE-2.0.
*/
import React from 'react';
import { renderToString } from 'react-dom/server';
import styled, { css, ThemeProps, DefaultTheme } from 'styled-components';
import { em, math } from 'polished';
import {
retrieveComponentStyles,
getLineHeight,
focusStyles,
getColor
} from '@zendeskgarden/react-theming';
import ChevronIcon from '@zendeskgarden/svg-icons/src/16/chevron-down-stroke.svg';
import { Validation } from '../../types';
import { StyledHint } from '../common/StyledHint';
import { StyledLabel } from '../common/StyledLabel';
import { StyledMessage } from '../common/StyledMessage';
const COMPONENT_ID = 'forms.input';
const isInvalid = (validation?: Validation) => {
return validation === 'warning' || validation === 'error';
};
const colorStyles = ({
theme,
$isBare,
$isHovered,
$focusInset,
$validation
}: IStyledTextInputProps & ThemeProps<DefaultTheme>) => {
const foregroundColor = getColor({ theme, variable: 'foreground.default' });
const backgroundColor = $isBare
? 'transparent'
: getColor({ theme, variable: 'background.default' });
let borderColor: string | undefined;
let hoverBorderColor: string | undefined;
let borderVariable: string | undefined;
let focusBorderColor: string | undefined;
if ($validation) {
if ($validation === 'success') {
borderVariable = 'border.successEmphasis';
} else if ($validation === 'warning') {
borderVariable = 'border.warningEmphasis';
} else if ($validation === 'error') {
borderVariable = 'border.dangerEmphasis';
}
borderColor = getColor({ theme, variable: borderVariable });
hoverBorderColor = borderColor;
focusBorderColor = borderColor;
} else {
borderColor = getColor({
theme,
variable: 'border.default',
dark: { offset: -100 },
light: { offset: 100 }
});
borderVariable = 'border.primaryEmphasis';
hoverBorderColor = getColor({ theme, variable: borderVariable });
focusBorderColor = hoverBorderColor;
}
const disabledBackgroundColor = $isBare
? undefined
: getColor({ theme, variable: 'background.disabled' });
const disabledBorderColor = getColor({ theme, variable: 'border.disabled' });
const disabledForegroundColor = getColor({ theme, variable: 'foreground.disabled' });
const placeholderColor = disabledForegroundColor;
const readOnlyBackgroundColor = disabledBackgroundColor;
const calendarPickerColor = getColor({ theme, variable: 'foreground.subtle' });
const calendarPickerIcon = renderToString(<ChevronIcon color={calendarPickerColor} />);
const calendarPickerBackgroundImage = `url("data:image/svg+xml,${encodeURIComponent(calendarPickerIcon)}")`;
return css`
border-color: ${$isHovered ? hoverBorderColor : borderColor};
background-color: ${backgroundColor};
color: ${foregroundColor};
&::placeholder {
opacity: 1;
color: ${placeholderColor};
}
&::-webkit-datetime-edit {
color: ${placeholderColor};
}
&::-webkit-calendar-picker-indicator {
background-image: ${calendarPickerBackgroundImage};
}
&[readonly],
&[aria-readonly='true'] {
background-color: ${readOnlyBackgroundColor};
}
&:hover {
border-color: ${hoverBorderColor};
}
${focusStyles({
theme,
inset: $focusInset,
color: { variable: borderVariable },
styles: { borderColor: focusBorderColor },
condition: !$isBare
})}
&::-webkit-calendar-picker-indicator:focus-visible {
${focusStyles({ theme })}
}
&:disabled,
&[aria-disabled='true'] {
border-color: ${disabledBorderColor};
background-color: ${disabledBackgroundColor};
color: ${disabledForegroundColor};
}
`;
};
const sizeStyles = ({
theme,
$isBare,
$isCompact
}: IStyledTextInputProps & ThemeProps<DefaultTheme>) => {
const fontSize = theme.fontSizes.md;
const paddingHorizontal = `${theme.space.base * 3}px`;
let height;
let paddingVertical;
let browseFontSize;
let swatchHeight;
if ($isCompact) {
height = `${theme.space.base * 8}px`;
paddingVertical = `${theme.space.base * 1.5}px`;
browseFontSize = math(`${theme.fontSizes.sm} - 1`);
swatchHeight = `${theme.space.base * 6}px`;
} else {
height = `${theme.space.base * 10}px`;
paddingVertical = `${theme.space.base * 2.5}px`;
browseFontSize = theme.fontSizes.sm;
swatchHeight = `${theme.space.base * 7}px`;
}
const lineHeight = math(`${height} - (${paddingVertical} * 2) - (${theme.borderWidths.sm} * 2)`);
const padding = $isBare
? '0'
: `${em(paddingVertical, fontSize)} ${em(paddingHorizontal, fontSize)}`;
const swatchMarginVertical = math(`(${lineHeight} - ${swatchHeight}) / 2`);
const swatchMarginHorizontal = math(
`${paddingVertical} + ${swatchMarginVertical} - ${paddingHorizontal}`
);
const calendarPickerSize = `${theme.space.base * 5}px`;
const calendarPickerBackgroundSize = theme.iconSizes.md;
return css`
padding: ${padding};
min-height: ${$isBare ? '1em' : height};
line-height: ${getLineHeight(lineHeight, fontSize)};
font-size: ${fontSize};
&::-ms-browse {
font-size: ${browseFontSize};
}
&[type='date'],
&[type='datetime-local'],
&[type='file'],
&[type='month'],
&[type='time'],
&[type='week'] {
max-height: ${height};
}
&[type='file'] {
line-height: 1; /* align file prompt text */
}
@supports (-ms-ime-align: auto) {
&[type='color'] {
padding: ${$isCompact ? '0 2px' : '1px 3px'}; /* correct color swatch size for Edge */
}
}
&::-moz-color-swatch {
margin-top: ${swatchMarginVertical};
margin-left: ${swatchMarginHorizontal};
width: calc(100% + ${math(`${swatchMarginHorizontal} * -2`)});
height: ${swatchHeight};
}
&::-webkit-calendar-picker-indicator {
background-position: center;
background-size: ${calendarPickerBackgroundSize};
padding: 0;
width: ${calendarPickerSize};
height: ${calendarPickerSize};
}
&::-webkit-color-swatch {
margin: ${swatchMarginVertical} ${swatchMarginHorizontal};
}
${StyledLabel}:not([hidden]) + &&,
${StyledHint} + &&,
${StyledMessage} + &&,
&& + ${StyledHint},
&& ~ ${StyledMessage} {
margin-top: ${theme.space.base * ($isCompact ? 1 : 2)}px;
}
`;
};
export interface IStyledTextInputProps {
$isCompact?: boolean;
$isBare?: boolean;
$isHovered?: boolean;
$isFocused?: boolean;
$focusInset?: boolean;
$validation?: Validation;
}
export const StyledTextInput = styled.input.attrs<IStyledTextInputProps>(props => ({
'data-garden-id': COMPONENT_ID,
'data-garden-version': PACKAGE_VERSION,
'aria-invalid': isInvalid(props.$validation)
}))<IStyledTextInputProps>`
appearance: none;
/* prettier-ignore */
transition:
border-color 0.25s ease-in-out,
box-shadow 0.1s ease-in-out,
background-color 0.25s ease-in-out,
color 0.25s ease-in-out,
z-index 0.25s ease-in-out;
direction: ${props => props.theme.rtl && 'rtl'};
border: ${props => (props.$isBare ? 'none' : props.theme.borders.sm)};
border-radius: ${props => (props.$isBare ? '0' : props.theme.borderRadii.md)};
width: 100%; /* vs. display: block to limit mouse interaction area */
box-sizing: border-box;
vertical-align: middle; /* support inline label */
font-family: inherit;
&::-ms-browse {
border-radius: ${props => props.theme.borderRadii.sm};
}
&::-ms-clear,
&::-ms-reveal {
display: none; /* remove clear button and password reveal in IE */
}
&::-moz-color-swatch {
border: none;
border-radius: ${props => props.theme.borderRadii.sm};
}
&::-webkit-color-swatch {
border: none;
border-radius: ${props => props.theme.borderRadii.sm};
}
&::-webkit-color-swatch-wrapper {
padding: 0;
}
&::-webkit-clear-button,
&::-webkit-inner-spin-button,
&::-webkit-search-cancel-button,
&::-webkit-search-results-button {
display: none; /* remove non-standard browser features */
}
&::-webkit-datetime-edit {
direction: ${props => props.theme.rtl && 'rtl'};
line-height: 1;
}
&::-webkit-calendar-picker-indicator {
border-radius: 100%;
}
&:invalid {
box-shadow: none; /* prevent FireFox validation styling */
}
&[type='file']::-ms-value {
display: none; /* remove file entry in IE */
}
/* stylelint-disable-next-line */
@media screen and (min--moz-device-pixel-ratio: 0) {
&[type='number'] {
appearance: textfield; /* remove number spinner in FireFox */
}
}
${sizeStyles};
${colorStyles};
&:disabled {
cursor: default;
}
${props => retrieveComponentStyles(COMPONENT_ID, props)};
`;