-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reduce render cost of post controls and improve perceived responsiven…
…ess (#132) * Move post control animations into conditional render and increase perceived responsiveness * Remove log
- Loading branch information
Showing
6 changed files
with
159 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
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,73 @@ | ||
import React from 'react' | ||
import {Animated, StyleProp, View, ViewStyle} from 'react-native' | ||
import {useAnimatedValue} from '../../../lib/hooks/useAnimatedValue' | ||
|
||
type CreateAnimFn = (interp: Animated.Value) => Animated.CompositeAnimation | ||
type FinishCb = () => void | ||
|
||
interface TriggeredAnimation { | ||
start: CreateAnimFn | ||
style: ( | ||
interp: Animated.Value, | ||
) => Animated.WithAnimatedValue<StyleProp<ViewStyle>> | ||
} | ||
|
||
export interface TriggerableAnimatedRef { | ||
trigger: (anim: TriggeredAnimation, onFinish?: FinishCb) => void | ||
} | ||
|
||
type TriggerableAnimatedProps = React.PropsWithChildren<{}> | ||
|
||
type PropsInner = TriggerableAnimatedProps & { | ||
anim: TriggeredAnimation | ||
onFinish: () => void | ||
} | ||
|
||
export const TriggerableAnimated = React.forwardRef< | ||
TriggerableAnimatedRef, | ||
TriggerableAnimatedProps | ||
>(({children, ...props}, ref) => { | ||
const [anim, setAnim] = React.useState<TriggeredAnimation | undefined>( | ||
undefined, | ||
) | ||
const [finishCb, setFinishCb] = React.useState<FinishCb | undefined>( | ||
undefined, | ||
) | ||
React.useImperativeHandle(ref, () => ({ | ||
trigger(v: TriggeredAnimation, cb?: FinishCb) { | ||
setFinishCb(() => cb) // note- wrap in function due to react behaviors around setstate | ||
setAnim(v) | ||
}, | ||
})) | ||
const onFinish = () => { | ||
finishCb?.() | ||
setAnim(undefined) | ||
setFinishCb(undefined) | ||
} | ||
return ( | ||
<View key="triggerable"> | ||
{anim ? ( | ||
<AnimatingView anim={anim} onFinish={onFinish} {...props}> | ||
{children} | ||
</AnimatingView> | ||
) : ( | ||
children | ||
)} | ||
</View> | ||
) | ||
}) | ||
|
||
function AnimatingView({ | ||
anim, | ||
onFinish, | ||
children, | ||
}: React.PropsWithChildren<PropsInner>) { | ||
const interp = useAnimatedValue(0) | ||
React.useEffect(() => { | ||
anim?.start(interp).start(() => { | ||
onFinish() | ||
}) | ||
}) | ||
const animStyle = anim?.style(interp) | ||
return <Animated.View style={animStyle}>{children}</Animated.View> | ||
} |
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