Skip to content

Commit f7aea0c

Browse files
pietervfacebook-github-bot
authored andcommitted
Fork onPress callbacks for ios highlighting (#44909)
Summary: Pull Request resolved: #44909 Today we wrap all `onPressIn` and `onPressOut` callbacks we pass to pressability so we can set the `highlighted` state. However highlighted state is only ever set to anything other that false on iOS. This change not only skips calling `setHighlighted(false)` on every press event but also skips wrapping the callback. Changelog: [Internal] Reviewed By: yungsters Differential Revision: D58391419 fbshipit-source-id: e79f51469609a59063098501f015f8078e3db79f
1 parent 0ad107b commit f7aea0c

1 file changed

Lines changed: 39 additions & 33 deletions

File tree

  • packages/react-native/Libraries/Text

packages/react-native/Libraries/Text/Text.js

Lines changed: 39 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -101,41 +101,47 @@ const Text: React.AbstractComponent<
101101
_disabled !== true;
102102

103103
const initialized = useLazyInitialization(isPressable);
104-
const config = useMemo(
105-
() =>
106-
initialized
107-
? {
108-
disabled: !isPressable,
109-
pressRectOffset: pressRetentionOffset,
110-
onLongPress,
111-
onPress,
112-
onPressIn(event: PressEvent) {
113-
// Updating isHighlighted causes unnecessary re-renders for platforms that don't use it
114-
// in the best case, and cause issues with text selection in the worst case. Forcing
115-
// the isHighlighted prop to false on all platforms except iOS.
116-
setHighlighted(
117-
(suppressHighlighting == null || !suppressHighlighting) &&
118-
Platform.OS === 'ios',
119-
);
120-
onPressIn?.(event);
121-
},
122-
onPressOut(event: PressEvent) {
123-
setHighlighted(false);
124-
onPressOut?.(event);
125-
},
126-
}
127-
: null,
128-
[
129-
initialized,
130-
isPressable,
131-
pressRetentionOffset,
104+
const config = useMemo(() => {
105+
if (!initialized) {
106+
return null;
107+
}
108+
109+
let _onPressIn = onPressIn;
110+
let _onPressOut = onPressOut;
111+
112+
// Updating isHighlighted causes unnecessary re-renders for platforms that don't use it
113+
// in the best case, and cause issues with text selection in the worst case. Forcing
114+
// the isHighlighted prop to false on all platforms except iOS.
115+
if (Platform.OS === 'ios') {
116+
_onPressIn = (event: PressEvent) => {
117+
setHighlighted(suppressHighlighting == null || !suppressHighlighting);
118+
onPressIn?.(event);
119+
};
120+
121+
_onPressOut = (event: PressEvent) => {
122+
setHighlighted(false);
123+
onPressOut?.(event);
124+
};
125+
}
126+
127+
return {
128+
disabled: !isPressable,
129+
pressRectOffset: pressRetentionOffset,
132130
onLongPress,
133131
onPress,
134-
onPressIn,
135-
onPressOut,
136-
suppressHighlighting,
137-
],
138-
);
132+
onPressIn: _onPressIn,
133+
onPressOut: _onPressOut,
134+
};
135+
}, [
136+
initialized,
137+
isPressable,
138+
pressRetentionOffset,
139+
onLongPress,
140+
onPress,
141+
onPressIn,
142+
onPressOut,
143+
suppressHighlighting,
144+
]);
139145

140146
const eventHandlers = usePressability(config);
141147
const eventHandlersForText = useMemo(

0 commit comments

Comments
 (0)