Skip to content

Commit ff059b5

Browse files
committed
Simplify AnimateWhenInViewport component
1 parent 6dd34d7 commit ff059b5

File tree

4 files changed

+16
-7
lines changed

4 files changed

+16
-7
lines changed

components/Atoms/AnimateWhenInViewport/AnimateWhenInViewport.tsx

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { Ref, ReactElement } from "react";
1+
import { Ref, ReactElement, cloneElement, DetailedReactHTMLElement, MutableRefObject } from "react";
22
import { useRef, useEffect, useState } from "react";
33

44
export interface Animation {
@@ -7,7 +7,7 @@ export interface Animation {
77
}
88

99
interface AnimateWhenInViewportProps<T> extends Animation {
10-
children: (ref: Ref<T>) => ReactElement<any, any>;
10+
children: JSX.Element;
1111
}
1212

1313
export default function AnimateWhenInViewport<T extends HTMLElement>({
@@ -64,7 +64,16 @@ export default function AnimateWhenInViewport<T extends HTMLElement>({
6464
return () => {
6565
observer.disconnect();
6666
};
67-
}, [ref]);
67+
}, [initDelay, initTime, ref, threshold]);
6868

69-
return <div className="animation-wrapper">{children(ref)}</div>;
69+
/**
70+
* Set `ref.current` to `current`, if not null.
71+
*/
72+
const updateRef = (current: T | null) => {
73+
if (current) {
74+
ref.current = current;
75+
}
76+
};
77+
78+
return <div className="animation-wrapper">{cloneElement(children, { ref: updateRef })}</div>;
7079
}

components/Atoms/LinkButtonAnimated/LinkButtonAnimated.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ interface LinkButtonAnimatedProps extends LinkButtonProps {
1111
export default function LinkButtonAnimated({ animation, ...props }: LinkButtonAnimatedProps) {
1212
return (
1313
<AnimateWhenInViewport {...animation}>
14-
{(ref: Ref<HTMLAnchorElement>) => <LinkButton {...props} ref={ref} />}
14+
<LinkButton {...props} />
1515
</AnimateWhenInViewport>
1616
);
1717
}

components/Atoms/TitleAnimated/TitleAnimated.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ interface TitleAnimatedProps extends TitleProps {
1010
export default function TitleAnimated({ animation, className, ...props }: TitleAnimatedProps) {
1111
return (
1212
<AnimateWhenInViewport {...animation}>
13-
{(ref: Ref<HTMLDivElement>) => <Title ref={ref} {...props}></Title>}
13+
<Title {...props} />
1414
</AnimateWhenInViewport>
1515
);
1616
}

components/Molecules/ProjectCardAnimated/ProjectCardAnimated.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ interface ProjectCardAnimatedProps extends ProjectCardProps {
1111
export default function ProjectCardAnimated({ animation, ...props }: ProjectCardAnimatedProps) {
1212
return (
1313
<AnimateWhenInViewport {...animation}>
14-
{(ref: Ref<HTMLAnchorElement>) => <ProjectCard {...props} ref={ref} />}
14+
<ProjectCard {...props} />
1515
</AnimateWhenInViewport>
1616
);
1717
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy