optimise PressableScale for React Compiler
This commit is contained in:
@@ -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}>
|
||||||
|
|||||||
Reference in New Issue
Block a user