Skip to content

Commit

Permalink
fix(experience): shrink input field when autofilled by the browser
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaoyijun committed Jul 19, 2024
1 parent 4ac4f8c commit 1604171
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,15 @@
transition: background-color 999999s ease-in-out 0s;
background-color: transparent;
}

&:-webkit-autofill {
/*
* Define a void transition css rule on the desired <input> element once it is :-webkit-autofilled.
* JavaScript will then be able to hook onto the 'animationstart' event.
* see https://stackoverflow.com/questions/11708092/detecting-browser-autofill/41530164#41530164
*/
animation-name: onAutoFillStart;
}
}

.suffix {
Expand Down Expand Up @@ -115,3 +124,18 @@
}
}
}


/*
* Define a void transition css rule on the desired <input> element once it is :-webkit-autofilled.
* JavaScript will then be able to hook onto the 'animationstart' event.
* see https://stackoverflow.com/questions/11708092/detecting-browser-autofill/41530164#41530164
*/
@keyframes onAutoFillStart {
/* stylelint-disable-next-line block-no-empty */
from {}

/* stylelint-disable-next-line block-no-empty */
to {}
}

Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,15 @@ const InputField = (

/**
* Fix the issue that the input field doesn't have the active style when the autofill value is set.
* Modern browsers will trigger an animation event when the input field is autofilled.
* We have define a void transition css rule on the input element once it is `:-webkit-autofill`ed.
* Hook onto this 'animationstart' event to detect the autofill start.
* see https://stackoverflow.com/questions/11708092/detecting-browser-autofill/41530164#41530164
*/
const handleAnimationStart = useCallback((event: AnimationEvent<HTMLInputElement>) => {
if (event.animationName === 'onautofillstart') {
/**
* Because SCSS adds some random characters to the ‘onAutoFillStart’ animation name during the build process, we can’t use the exact name here.
*/
if (event.animationName.includes('onAutoFillStart')) {
setHasValue(true);
}
}, []);
Expand Down

0 comments on commit 1604171

Please sign in to comment.