-
Notifications
You must be signed in to change notification settings - Fork 202
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[GEN-1772]: fix "Transition" re-renders (#1819)
- Loading branch information
1 parent
6ef2a04
commit 28c3eee
Showing
7 changed files
with
82 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import React, { PropsWithChildren, useCallback, useEffect, useState } from 'react'; | ||
import styled from 'styled-components'; | ||
import type { IStyledComponentBase, Keyframes, Substitute } from 'styled-components/dist/types'; | ||
|
||
interface HookProps { | ||
container: IStyledComponentBase<'web', Substitute<React.DetailedHTMLProps<React.HTMLAttributes<HTMLElement>, HTMLElement>, {}>> & string; | ||
animateIn: Keyframes; | ||
animateOut?: Keyframes; | ||
duration?: number; // in milliseconds | ||
} | ||
|
||
type TransitionProps = PropsWithChildren<{ | ||
enter: boolean; | ||
[key: string]: any; | ||
}>; | ||
|
||
export const useTransition = ({ container, animateIn, animateOut, duration = 300 }: HookProps) => { | ||
const Animated = styled(container)<{ $isEntering: boolean; $isLeaving: boolean }>` | ||
animation-name: ${({ $isEntering, $isLeaving }) => ($isEntering ? animateIn : $isLeaving ? animateOut : 'none')}; | ||
animation-duration: ${duration}ms; | ||
animation-fill-mode: forwards; | ||
`; | ||
|
||
const Transition = useCallback(({ children, enter, ...props }: TransitionProps) => { | ||
const [mounted, setMounted] = useState(false); | ||
|
||
useEffect(() => { | ||
const t = setTimeout(() => setMounted(enter), duration + 50); // +50ms to ensure the animation is finished | ||
return () => clearTimeout(t); | ||
}, [enter, duration]); | ||
|
||
return ( | ||
<Animated $isEntering={enter} $isLeaving={!enter && mounted} {...props}> | ||
{children} | ||
</Animated> | ||
); | ||
|
||
// do not add dependencies here, it will cause re-renders which we want to avoid | ||
}, []); | ||
|
||
return Transition; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.