optimise PressableScale for React Compiler

This commit is contained in:
Samuel Newman
2024-11-12 19:50:51 +00:00
parent 5c4fc4737f
commit 4f52841d44
+15 -16
View File
@@ -2,7 +2,7 @@ import React from 'react'
import {Pressable, PressableProps, StyleProp, ViewStyle} from 'react-native'
import Animated, {
cancelAnimation,
runOnJS,
runOnUI,
useAnimatedStyle,
useReducedMotion,
useSharedValue,
@@ -28,7 +28,6 @@ export function PressableScale({
style?: StyleProp<ViewStyle>
} & Exclude<PressableProps, 'onPressIn' | 'onPressOut' | 'style'>) {
const reducedMotion = useReducedMotion()
const scale = useSharedValue(1)
const animatedStyle = useAnimatedStyle(() => ({
@@ -38,21 +37,21 @@ export function PressableScale({
return (
<AnimatedPressable
accessibilityRole="button"
onPressIn={e => {
'worklet'
if (onPressIn) {
runOnJS(onPressIn)(e)
}
cancelAnimation(scale)
scale.value = withTiming(targetScale, {duration: 100})
onPressIn={evt => {
onPressIn?.(evt)
runOnUI(() => {
'worklet'
cancelAnimation(scale)
scale.set(withTiming(targetScale, {duration: 100}))
})()
}}
onPressOut={e => {
'worklet'
if (onPressOut) {
runOnJS(onPressOut)(e)
}
cancelAnimation(scale)
scale.value = withTiming(1, {duration: 100})
onPressOut={evt => {
onPressOut?.(evt)
runOnUI(() => {
'worklet'
cancelAnimation(scale)
scale.set(withTiming(1, {duration: 100}))
})()
}}
style={[!reducedMotion && animatedStyle, style]}
{...rest}>