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 {Pressable, PressableProps, StyleProp, ViewStyle} from 'react-native'
import Animated, { import Animated, {
cancelAnimation, cancelAnimation,
runOnJS, runOnUI,
useAnimatedStyle, useAnimatedStyle,
useReducedMotion, useReducedMotion,
useSharedValue, useSharedValue,
@@ -28,7 +28,6 @@ export function PressableScale({
style?: StyleProp<ViewStyle> style?: StyleProp<ViewStyle>
} & Exclude<PressableProps, 'onPressIn' | 'onPressOut' | 'style'>) { } & Exclude<PressableProps, 'onPressIn' | 'onPressOut' | 'style'>) {
const reducedMotion = useReducedMotion() const reducedMotion = useReducedMotion()
const scale = useSharedValue(1) const scale = useSharedValue(1)
const animatedStyle = useAnimatedStyle(() => ({ const animatedStyle = useAnimatedStyle(() => ({
@@ -38,21 +37,21 @@ export function PressableScale({
return ( return (
<AnimatedPressable <AnimatedPressable
accessibilityRole="button" accessibilityRole="button"
onPressIn={e => { onPressIn={evt => {
'worklet' onPressIn?.(evt)
if (onPressIn) { runOnUI(() => {
runOnJS(onPressIn)(e) 'worklet'
} cancelAnimation(scale)
cancelAnimation(scale) scale.set(withTiming(targetScale, {duration: 100}))
scale.value = withTiming(targetScale, {duration: 100}) })()
}} }}
onPressOut={e => { onPressOut={evt => {
'worklet' onPressOut?.(evt)
if (onPressOut) { runOnUI(() => {
runOnJS(onPressOut)(e) 'worklet'
} cancelAnimation(scale)
cancelAnimation(scale) scale.set(withTiming(1, {duration: 100}))
scale.value = withTiming(1, {duration: 100}) })()
}} }}
style={[!reducedMotion && animatedStyle, style]} style={[!reducedMotion && animatedStyle, style]}
{...rest}> {...rest}>