Skip to content

Commit cae078c

Browse files
authored
Move placeholder up when input is autofilled (#175)
1 parent b821112 commit cae078c

File tree

2 files changed

+17
-11
lines changed

2 files changed

+17
-11
lines changed

src/elements/forms/text-field.css

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -77,14 +77,6 @@ label {
7777
}
7878
}
7979

80-
input:invalid,
81-
input:invalid + label {
82-
--form-control-color: var(--danger-dark);
83-
84-
/* For some reason --form-control-color change is not enough */
85-
--form-label-color: var(--form-control-color);
86-
}
87-
8880
/* Size variations */
8981

9082
.small {
@@ -99,6 +91,7 @@ input:invalid + label {
9991

10092
:matches(input, select, textarea):focus + label,
10193
:matches(input, select, textarea):not(:placeholder-shown) + label,
94+
:matches(input, select, textarea):-webkit-autofill + label,
10295
.focus label {
10396
font-size: var(--form-label-font-size);
10497
background: var(--white, #fff);
@@ -109,7 +102,7 @@ input:invalid + label {
109102
);
110103
}
111104

112-
.focus ::placeholder,
105+
.focus::placeholder,
113106
:focus::placeholder {
114107
opacity: 1;
115108
transform: none;
@@ -132,3 +125,9 @@ input:invalid + label {
132125
/* For some reason --form-control-color change is not enough */
133126
--form-label-color: var(--form-control-color);
134127
}
128+
129+
.touched:invalid,
130+
.touched:invalid + label {
131+
--form-control-color: var(--danger-dark);
132+
--form-label-color: var(--form-control-color);
133+
}

src/elements/forms/text-field.jsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React from 'react'
1+
import React, { useState } from 'react'
22
import PropTypes from 'prop-types'
33

44
import styles from './text-field.css'
@@ -35,7 +35,7 @@ const TextField = React.forwardRef(
3535
ref
3636
) => {
3737
const controlId = [id, 'control'].join('-')
38-
38+
const [isTouched, setIsTouched] = useState(false)
3939
return (
4040
<Tag
4141
id={id}
@@ -44,15 +44,22 @@ const TextField = React.forwardRef(
4444
[size]: size !== 'default',
4545
[variant]: variant !== 'normal',
4646
focus: variant !== 'normal',
47+
touched: isTouched,
4748
})
4849
.from(styles)
4950
.join(className)}
5051
>
5152
<input
5253
ref={ref}
5354
id={controlId}
55+
onBlur={() => !isTouched && setIsTouched(true)}
5456
aria-invalid={variant === 'error' ? 'true' : 'false'}
5557
aria-describedby={`${id}-helper`}
58+
className={classNames
59+
.use({
60+
touched: isTouched,
61+
})
62+
.from(styles)}
5663
{...inputProps}
5764
/>
5865
<label htmlFor={controlId}>{label}</label>

0 commit comments

Comments
 (0)