Skip to content

Commit

Permalink
fix: animation on mount not stable
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristiaanScheermeijer committed Sep 19, 2024
1 parent 0f4491a commit 29855b9
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion packages/ui-react/src/components/Animation/Animation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ type Props = {

export type Status = 'opening' | 'open' | 'closing' | 'closed';

const triggerReflow = (element: HTMLElement | null) => element?.scrollTop;

const Animation: React.FC<Props> = ({
className,
createStyle,
Expand All @@ -26,6 +28,7 @@ const Animation: React.FC<Props> = ({
keepMounted = false,
children,
}) => {
const nodeRef = useRef<HTMLDivElement>(null);
const [status, setStatus] = useState<Status>('closed');
const [hasOpenedBefore, setHasOpenedBefore] = useState<boolean>(false);

Expand All @@ -35,6 +38,9 @@ const Animation: React.FC<Props> = ({
// use event callbacks to ignore reactive dependencies
const openEvent = useEventCallback(() => {
setHasOpenedBefore(true);
// trigger a reflow to ensure the transition is respected after mount
triggerReflow(nodeRef.current);

timeout.current = window.setTimeout(() => setStatus('opening'), delay);
timeout2.current = window.setTimeout(() => {
setStatus('open');
Expand Down Expand Up @@ -70,7 +76,7 @@ const Animation: React.FC<Props> = ({
}

return (
<div style={createStyle(status)} className={className}>
<div style={createStyle(status)} className={className} ref={nodeRef}>
{children}
</div>
);
Expand Down

0 comments on commit 29855b9

Please sign in to comment.