Skip to content

Commit

Permalink
fix(clerk-js): Form error animation polishing
Browse files Browse the repository at this point in the history
  • Loading branch information
desiprisg committed Sep 28, 2023
1 parent b6c7cbd commit 3d5378e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 9 deletions.
22 changes: 13 additions & 9 deletions packages/clerk-js/src/ui/elements/FormControl.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,16 +94,16 @@ function useFormTextAnimation() {
const prefersReducedMotion = usePrefersReducedMotion();

const getFormTextAnimation = useCallback(
(enterAnimation: boolean): ThemableCssProp => {
(enterAnimation: boolean, inDelay?: boolean): ThemableCssProp => {
if (prefersReducedMotion) {
return {
animation: 'none',
};
}
return t => ({
animation: `${enterAnimation ? animations.inAnimation : animations.outAnimation} ${
t.transitionDuration.$textField
} ${t.transitionTiming.$common}`,
animation: `${
enterAnimation ? (inDelay ? animations.inDelayAnimation : animations.inAnimation) : animations.outAnimation
} ${t.transitionDuration.$textField} ${t.transitionTiming.$common}`,
transition: `height ${t.transitionDuration.$slow} ${t.transitionTiming.$common}`, // This is expensive but required for a smooth layout shift
});
},
Expand All @@ -121,7 +121,8 @@ const useCalculateErrorTextHeight = ({ feedback }: { feedback: string }) => {
const calculateHeight = useCallback(
(element: HTMLElement | null) => {
if (element) {
setHeight(element.scrollHeight);
const computedStyles = getComputedStyle(element);
setHeight(element.scrollHeight + parseInt(computedStyles.marginTop.replace('px', '')));
}
},
[feedback],
Expand Down Expand Up @@ -228,7 +229,7 @@ export const FormFeedback = (props: FormFeedbackProps) => {
() => ({
visibility: feedbacks.a?.shouldEnter ? 'visible' : 'hidden',
}),
getFormTextAnimation(!!feedbacks.a?.shouldEnter),
getFormTextAnimation(!!feedbacks.a?.shouldEnter, true),
]}
localizationKey={feedbacks.a?.feedback}
/>
Expand All @@ -239,7 +240,7 @@ export const FormFeedback = (props: FormFeedbackProps) => {
() => ({
visibility: feedbacks.b?.shouldEnter ? 'visible' : 'hidden',
}),
getFormTextAnimation(!!feedbacks.b?.shouldEnter),
getFormTextAnimation(!!feedbacks.b?.shouldEnter, true),
]}
localizationKey={feedbacks.b?.feedback}
/>
Expand All @@ -255,6 +256,7 @@ export const FormControl = forwardRef<HTMLInputElement, PropsWithChildren<FormCo
const {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
hasPassedComplexity,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
infoText,
id,
isRequired,
Expand Down Expand Up @@ -391,15 +393,17 @@ export const FormControl = forwardRef<HTMLInputElement, PropsWithChildren<FormCo
{...inputElementProps}
onFocus={e => {
inputElementProps.onFocus?.(e);
setIsFocused(true);
setTimeout(() => {
setIsFocused(true);
}, 300);
}}
onBlur={e => {
inputElementProps.onBlur?.(e);
// set a timeout because new errors might appear
// and we don't want to spam layout shifts
setTimeout(() => {
setIsFocused(false);
}, 500);
}, 300);
}}
ref={ref}
placeholder={t(placeholder)}
Expand Down
19 changes: 19 additions & 0 deletions packages/clerk-js/src/ui/styledSystem/animations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,24 @@ const inAnimation = keyframes`
}
`;

const inDelayAnimation = keyframes`
0% {
opacity: 0;
transform: translateY(-5px);
max-height: 0;
}
50% {
opacity: 0;
transform: translateY(-5px);
max-height: 0;
}
100% {
opacity: 1;
transform: translateY(0px);
max-height: 6rem;
}
`;

const notificationAnimation = keyframes`
0% {
opacity: 0;
Expand Down Expand Up @@ -117,6 +135,7 @@ export const animations = {
expandIn,
navbarSlideIn,
inAnimation,
inDelayAnimation,
outAnimation,
notificationAnimation,
};

0 comments on commit 3d5378e

Please sign in to comment.