@@ -18,6 +18,7 @@ import {
1818 isTouchWithinInset ,
1919 adaptTouchEvent ,
2020 addInsets ,
21+ splitStyles ,
2122} from './utils' ;
2223import { PressabilityDebugView } from '../../handlers/PressabilityDebugView' ;
2324import { GestureTouchEvent } from '../../handlers/gestureHandlerCommon' ;
@@ -35,15 +36,21 @@ export default function Pressable(props: PressableProps) {
3536 const isPressCallbackEnabled = useRef < boolean > ( true ) ;
3637 const isPressedDown = useRef < boolean > ( false ) ;
3738
38- const normalizedHitSlop : Insets =
39- typeof props . hitSlop === 'number'
40- ? numberAsInset ( props . hitSlop )
41- : props . hitSlop ?? { } ;
39+ const normalizedHitSlop : Insets = useMemo (
40+ ( ) =>
41+ typeof props . hitSlop === 'number'
42+ ? numberAsInset ( props . hitSlop )
43+ : props . hitSlop ?? { } ,
44+ [ props . hitSlop ]
45+ ) ;
4246
43- const normalizedPressRetentionOffset : Insets =
44- typeof props . pressRetentionOffset === 'number'
45- ? numberAsInset ( props . pressRetentionOffset )
46- : props . pressRetentionOffset ?? { } ;
47+ const normalizedPressRetentionOffset : Insets = useMemo (
48+ ( ) =>
49+ typeof props . pressRetentionOffset === 'number'
50+ ? numberAsInset ( props . pressRetentionOffset )
51+ : props . pressRetentionOffset ?? { } ,
52+ [ props . pressRetentionOffset ]
53+ ) ;
4754
4855 const pressGesture = useMemo (
4956 ( ) =>
@@ -53,7 +60,7 @@ export default function Pressable(props: PressableProps) {
5360 isPressCallbackEnabled . current = false ;
5461 }
5562 } ) ,
56- [ isPressCallbackEnabled , props . onLongPress , isPressedDown ]
63+ [ props ]
5764 ) ;
5865
5966 const hoverInTimeout = useRef < number | null > ( null ) ;
@@ -88,41 +95,48 @@ export default function Pressable(props: PressableProps) {
8895 }
8996 props . onHoverOut ?.( adaptStateChangeEvent ( event ) ) ;
9097 } ) ,
91- [ props . onHoverIn , props . onHoverOut , props . delayHoverIn , props . delayHoverOut ]
98+ [ props ]
9299 ) ;
93100
94101 const pressDelayTimeoutRef = useRef < number | null > ( null ) ;
95- const pressInHandler = useCallback ( ( event : GestureTouchEvent ) => {
96- props . onPressIn ?.( adaptTouchEvent ( event ) ) ;
97- isPressCallbackEnabled . current = true ;
98- pressDelayTimeoutRef . current = null ;
99- setPressedState ( true ) ;
100- } , [ ] ) ;
101- const pressOutHandler = useCallback ( ( event : GestureTouchEvent ) => {
102- if (
103- ! isPressedDown . current ||
104- event . allTouches . length > event . changedTouches . length
105- ) {
106- return ;
107- }
108-
109- if ( props . unstable_pressDelay && pressDelayTimeoutRef . current !== null ) {
110- // When delay is preemptively finished by lifting touches,
111- // we want to immediately activate it's effects - pressInHandler,
112- // even though we are located at the pressOutHandler
113- clearTimeout ( pressDelayTimeoutRef . current ) ;
114- pressInHandler ( event ) ;
115- }
116-
117- props . onPressOut ?.( adaptTouchEvent ( event ) ) ;
118-
119- if ( isPressCallbackEnabled . current ) {
120- props . onPress ?.( adaptTouchEvent ( event ) ) ;
121- }
102+ const pressInHandler = useCallback (
103+ ( event : GestureTouchEvent ) => {
104+ props . onPressIn ?.( adaptTouchEvent ( event ) ) ;
105+ isPressCallbackEnabled . current = true ;
106+ pressDelayTimeoutRef . current = null ;
107+ setPressedState ( true ) ;
108+ } ,
109+ [ props ]
110+ ) ;
122111
123- isPressedDown . current = false ;
124- setPressedState ( false ) ;
125- } , [ ] ) ;
112+ const pressOutHandler = useCallback (
113+ ( event : GestureTouchEvent ) => {
114+ if (
115+ ! isPressedDown . current ||
116+ event . allTouches . length > event . changedTouches . length
117+ ) {
118+ return ;
119+ }
120+
121+ if ( props . unstable_pressDelay && pressDelayTimeoutRef . current !== null ) {
122+ // When delay is preemptively finished by lifting touches,
123+ // we want to immediately activate it's effects - pressInHandler,
124+ // even though we are located at the pressOutHandler
125+ clearTimeout ( pressDelayTimeoutRef . current ) ;
126+ pressInHandler ( event ) ;
127+ }
128+
129+ props . onPressOut ?.( adaptTouchEvent ( event ) ) ;
130+
131+ if ( isPressCallbackEnabled . current ) {
132+ props . onPress ?.( adaptTouchEvent ( event ) ) ;
133+ }
134+
135+ isPressedDown . current = false ;
136+ setPressedState ( false ) ;
137+ } ,
138+ [ pressInHandler , props ]
139+ ) ;
126140
127141 const handlingOnTouchesDown = useRef < boolean > ( false ) ;
128142 const onEndHandlingTouchesDown = useRef < ( ( ) => void ) | null > ( null ) ;
@@ -192,14 +206,10 @@ export default function Pressable(props: PressableProps) {
192206 pressOutHandler ( event ) ;
193207 } ) ,
194208 [
195- props . onPress ,
196- props . onPressIn ,
197- props . onPressOut ,
198- setPressedState ,
199- isPressedDown ,
200- isPressCallbackEnabled ,
201209 normalizedHitSlop ,
202- pressDelayTimeoutRef ,
210+ props . unstable_pressDelay ,
211+ pressInHandler ,
212+ pressOutHandler ,
203213 ]
204214 ) ;
205215
@@ -256,8 +266,12 @@ export default function Pressable(props: PressableProps) {
256266 ? props . children ( { pressed : pressedState } )
257267 : props . children ;
258268
269+ const flattenedStyles = StyleSheet . flatten ( styleProp ?? { } ) ;
270+
271+ const [ innerStyles , outerStyles ] = splitStyles ( flattenedStyles ) ;
272+
259273 return (
260- < View style = { styleProp } >
274+ < View style = { outerStyles } >
261275 < GestureDetector gesture = { gesture } >
262276 < NativeButton
263277 ref = { pressableRef }
@@ -269,7 +283,7 @@ export default function Pressable(props: PressableProps) {
269283 props . android_ripple ?. color ?? defaultRippleColor
270284 ) }
271285 rippleRadius = { props . android_ripple ?. radius ?? undefined }
272- style = { [ StyleSheet . absoluteFill , pointerStyle ] } >
286+ style = { [ StyleSheet . absoluteFill , pointerStyle , innerStyles ] } >
273287 { childrenProp }
274288 { __DEV__ ? (
275289 < PressabilityDebugView color = "red" hitSlop = { normalizedHitSlop } />
0 commit comments