-
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 useAnimated hook, fix useOnScreen
- Loading branch information
1 parent
f1a8b44
commit d7d338d
Showing
9 changed files
with
228 additions
and
144 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
17 changes: 0 additions & 17 deletions
17
packages/client/src/elements/Animated/Animated.stories.tsx
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,72 @@ | ||
import anime from 'animejs'; | ||
|
||
import { AnimationType, GetAnimationOptions } from './useAnimated.typed'; | ||
|
||
const getAnimation = ( | ||
type: AnimationType, | ||
{ targets, duration, delay, maxDebounceSize }: GetAnimationOptions, | ||
autoplay = true, | ||
) => { | ||
const baseConfig = { | ||
targets, | ||
duration, | ||
autoplay, | ||
delay, | ||
}; | ||
|
||
const createAnimation = ({ | ||
duration: baseDuration = 500, | ||
delay: baseDelay = 0, | ||
...rest | ||
}: anime.AnimeParams) => | ||
anime({ | ||
...baseConfig, | ||
duration: baseDuration, | ||
delay: baseDelay, | ||
...rest, | ||
}); | ||
|
||
switch (type) { | ||
case 'shake': { | ||
const internalMaxDebounceSize = maxDebounceSize || 16; | ||
return createAnimation({ | ||
easing: 'easeInOutSine', | ||
translateX: [ | ||
internalMaxDebounceSize * -1, | ||
internalMaxDebounceSize, | ||
internalMaxDebounceSize / -2, | ||
internalMaxDebounceSize / 2, | ||
0, | ||
], | ||
}); | ||
} | ||
case 'fadeIn': | ||
return createAnimation({ | ||
opacity: [0, 1], | ||
easing: 'cubicBezier(.5, .05, .1, .3)', | ||
}); | ||
case 'fadeOut': | ||
return createAnimation({ | ||
opacity: [1, 0], | ||
easing: 'cubicBezier(.5, .05, .1, .3)', | ||
}); | ||
case 'slideInUp': { | ||
return createAnimation({ | ||
translateY: ['100%', 0], | ||
easing: 'cubicBezier(0.68, -0.6, 0.32, 1.6)', | ||
}); | ||
} | ||
case 'slideInOut': | ||
return createAnimation({ | ||
translateY: [0, '100%'], | ||
easing: 'cubicBezier(0.68, -0.6, 0.32, 1.6)', | ||
}); | ||
default: | ||
return createAnimation({ | ||
opacity: [0, 1], | ||
easing: 'cubicBezier(.5, .05, .1, .3)', | ||
}); | ||
} | ||
}; | ||
|
||
export default getAnimation; |
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,49 @@ | ||
import { AnimeInstance } from 'animejs'; | ||
import { useLayoutEffect, useRef } from 'react'; | ||
|
||
import useOnScreen from '../useOnScreen'; | ||
import getAnimation from './getAnimation'; | ||
import { AnimationType, UseAnimatedProps, UseAnimatedReturnProps } from './useAnimated.typed'; | ||
|
||
const useAnimated = ({ targets, type, opt = {} }: UseAnimatedProps): UseAnimatedReturnProps => { | ||
const { root, rootMargin, animationOpt, autoTrigger = true, autoTriggerOnce = false } = opt; | ||
const isInOut = Array.isArray(type); | ||
const animateInType = (isInOut ? type[0] : type) as AnimationType; | ||
const animateOutType = isInOut ? (type[1] as AnimationType) : undefined; | ||
|
||
const animateInRef = useRef<AnimeInstance>(); | ||
const animateOutRef = useRef<AnimeInstance>(); | ||
const containerRefInner = useRef<Element>(null); | ||
const isInViewPort = useOnScreen(containerRefInner, { | ||
root, | ||
rootMargin, | ||
triggerOnce: autoTriggerOnce, | ||
disabled: !autoTrigger, | ||
}); | ||
|
||
useLayoutEffect(() => { | ||
const innerTargets = targets || containerRefInner.current; | ||
const options = { | ||
targets: innerTargets, | ||
...animationOpt, | ||
}; | ||
if (!autoTrigger) { | ||
animateInRef.current = getAnimation(animateInType, options, false); | ||
} | ||
|
||
if (autoTrigger && isInViewPort) { | ||
animateInRef.current = getAnimation(animateInType, options); | ||
} | ||
if (autoTrigger && !isInViewPort && animateOutType) { | ||
animateOutRef.current = getAnimation(animateOutType, options); | ||
} | ||
}, [animateInType, animateOutType, animationOpt, autoTrigger, isInViewPort, targets]); | ||
|
||
const triggerAnimation = () => { | ||
animateInRef.current?.play(); | ||
}; | ||
|
||
return { triggerAnimation, ref: containerRefInner }; | ||
}; | ||
|
||
export default useAnimated; |
36 changes: 36 additions & 0 deletions
36
packages/client/src/hooks/useAnimated/useAnimated.typed.d.ts
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,36 @@ | ||
import { Ref } from 'react'; | ||
|
||
export type AnimationType = 'fadeIn' | 'fadeOut' | 'shake' | 'slideInUp' | 'slideInOut'; | ||
export type UseAnimatedType = AnimationType | [AnimationType, AnimationType]; | ||
|
||
export type AnimationTargets = Element | ReadonlyArray<Element>; | ||
|
||
export interface AnimationOptions { | ||
maxDebounceSize?: number; | ||
duration?: number | FunctionBasedParameter; | ||
delay?: number | FunctionBasedParameter; | ||
} | ||
|
||
export interface GetAnimationOptions extends AnimationOptions { | ||
targets: AnimationTargets | null; | ||
} | ||
|
||
export interface UseAnimatedOptions { | ||
root?: Element | null; | ||
rootMargin?: string; | ||
autoTrigger?: boolean; | ||
autoTriggerOnce?: boolean; | ||
animationOpt?: AnimationOptions; | ||
} | ||
|
||
export interface UseAnimatedProps { | ||
targets?: AnimationTargets; | ||
type: UseAnimatedType; | ||
opt?: UseAnimatedOptions; | ||
} | ||
|
||
export interface UseAnimatedReturnProps { | ||
triggerAnimation: () => void; | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
ref: Ref<any>; | ||
} |
Oops, something went wrong.