-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(client): add Animated component
- Loading branch information
1 parent
0ef3422
commit f1a8b44
Showing
11 changed files
with
189 additions
and
38 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
17 changes: 17 additions & 0 deletions
17
packages/client/src/elements/Animated/Animated.stories.tsx
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,17 @@ | ||
import { Meta, Story } from '@storybook/react/types-6-0'; | ||
import React from 'react'; | ||
|
||
import Animated from '.'; | ||
import { AnimatedProps } from './Animated.types'; | ||
|
||
export default { | ||
title: 'Elements/Animated', | ||
component: Animated, | ||
} as Meta; | ||
|
||
const Template: Story<AnimatedProps> = (args) => <Animated {...args} />; | ||
|
||
export const Default = Template.bind({}); | ||
Default.args = { | ||
children: <>Lorem Ipsum</>, | ||
}; |
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,7 @@ | ||
import { ReactNode } from 'react'; | ||
|
||
export interface AnimatedProps { | ||
children?: ReactNode; | ||
animateIn?: string; | ||
animateOut?: string; | ||
} |
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,73 @@ | ||
import anime, { AnimeInstance } from 'animejs'; | ||
import React, { useLayoutEffect, useRef } from 'react'; | ||
import styled from 'styled-components'; | ||
|
||
import { useOnScreen } from '../../hooks'; | ||
import { AnimatedProps } from './Animated.types'; | ||
|
||
const Wrapper = styled.div` | ||
display: flex; | ||
justify-content: center; | ||
align-items: center; | ||
height: 100%; | ||
width: 100%; | ||
`; | ||
|
||
const Animated = ({ children, animateIn, animateOut }: AnimatedProps) => { | ||
const animatedContainerRef = useRef<HTMLDivElement>(null); | ||
const isInViewPort = useOnScreen(animatedContainerRef); | ||
const animateInRef = useRef<AnimeInstance>(); | ||
const animateOutRef = useRef<AnimeInstance>(); | ||
|
||
useLayoutEffect(() => { | ||
// const xMax = 16; | ||
// animateInRef.current = anime({ | ||
// targets: animatedContainerRef.current, | ||
// easing: 'easeInOutSine', | ||
// duration: 550, | ||
// translateX: [ | ||
// { | ||
// value: xMax * -1, | ||
// }, | ||
// { | ||
// value: xMax, | ||
// }, | ||
// { | ||
// value: xMax / -2, | ||
// }, | ||
// { | ||
// value: xMax / 2, | ||
// }, | ||
// { | ||
// value: 0, | ||
// }, | ||
// ], | ||
// }); | ||
if (isInViewPort) { | ||
animateInRef.current = anime({ | ||
targets: animatedContainerRef.current, | ||
duration: 750, | ||
opacity: [0, 1], | ||
easing: 'cubicBezier(.5, .05, .1, .3)', | ||
}); | ||
} | ||
if (!isInViewPort) { | ||
animateOutRef.current = anime({ | ||
targets: animatedContainerRef.current, | ||
duration: 750, | ||
opacity: [1, 0], | ||
easing: 'cubicBezier(.5, .05, .1, .3)', | ||
}); | ||
} | ||
}, [isInViewPort]); | ||
|
||
return ( | ||
<Wrapper ref={animatedContainerRef} data-testid="animated"> | ||
{children} | ||
</Wrapper> | ||
); | ||
}; | ||
|
||
Animated.displayName = 'Animated'; | ||
|
||
export default Animated; |
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,35 @@ | ||
import { RefObject, useLayoutEffect, useState } from 'react'; | ||
|
||
const useOnScreen = (ref: RefObject<Element>, rootMargin = '0px') => { | ||
const [isIntersecting, setIntersecting] = useState(true); | ||
|
||
useLayoutEffect(() => { | ||
if (!ref && !window && !('IntersectionObserver' in window)) { | ||
return; | ||
} | ||
|
||
const observer = new IntersectionObserver( | ||
([entry]) => { | ||
setIntersecting(entry.isIntersecting); | ||
}, | ||
{ | ||
rootMargin, | ||
}, | ||
); | ||
|
||
if (ref.current) { | ||
observer.observe(ref.current); | ||
} | ||
|
||
return () => { | ||
if (ref.current) { | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
observer.unobserve(ref.current); | ||
} | ||
}; | ||
}, [ref, rootMargin]); | ||
|
||
return isIntersecting; | ||
}; | ||
|
||
export default useOnScreen; |
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 @@ | ||
declare module 'intersection-observer'; |
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