Skip to content

Commit 0823f29

Browse files
Foreground ripple support in Pressable (#31632)
Summary: This PR aims to enable support for foreground ripple in Pressable. This makes it possible to show ripple on top of custom child components like Image as shown in the below example. ## Changelog <!-- Help reviewers and the release process by writing your own changelog entry. For an example, see: https://github.com/facebook/react-native/wiki/Changelog --> [Android] [Added] - Support for foreground ripple in Pressable Pull Request resolved: #31632 Test Plan: - Pass property useForeground: true in android_ripple config to verify the changes. https://user-images.githubusercontent.com/23293248/120111371-4cecbf00-c18f-11eb-8acb-d10718d5483c.mov Reviewed By: kacieb Differential Revision: D28926493 Pulled By: yungsters fbshipit-source-id: 12a6ba71a7dc6ed60fbaeb651f015cace38e03b1
1 parent 1cc2229 commit 0823f29

File tree

2 files changed

+38
-14
lines changed

2 files changed

+38
-14
lines changed

Libraries/Components/Pressable/useAndroidRippleForView.js

+17-14
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export type RippleConfig = {|
2727
color?: ColorValue,
2828
borderless?: boolean,
2929
radius?: number,
30+
foreground?: boolean,
3031
|};
3132

3233
/**
@@ -40,11 +41,11 @@ export default function useAndroidRippleForView(
4041
onPressIn: (event: PressEvent) => void,
4142
onPressMove: (event: PressEvent) => void,
4243
onPressOut: (event: PressEvent) => void,
43-
viewProps: $ReadOnly<{|
44-
nativeBackgroundAndroid: NativeBackgroundProp,
45-
|}>,
44+
viewProps:
45+
| $ReadOnly<{|nativeBackgroundAndroid: NativeBackgroundProp|}>
46+
| $ReadOnly<{|nativeForegroundAndroid: NativeBackgroundProp|}>,
4647
|}> {
47-
const {color, borderless, radius} = rippleConfig ?? {};
48+
const {color, borderless, radius, foreground} = rippleConfig ?? {};
4849

4950
return useMemo(() => {
5051
if (
@@ -58,16 +59,18 @@ export default function useAndroidRippleForView(
5859
'Unexpected color given for Ripple color',
5960
);
6061

62+
const nativeRippleValue = {
63+
type: 'RippleAndroid',
64+
color: processedColor,
65+
borderless: borderless === true,
66+
rippleRadius: radius,
67+
};
68+
6169
return {
62-
viewProps: {
63-
// Consider supporting `nativeForegroundAndroid`
64-
nativeBackgroundAndroid: {
65-
type: 'RippleAndroid',
66-
color: processedColor,
67-
borderless: borderless === true,
68-
rippleRadius: radius,
69-
},
70-
},
70+
viewProps:
71+
foreground === true
72+
? {nativeForegroundAndroid: nativeRippleValue}
73+
: {nativeBackgroundAndroid: nativeRippleValue},
7174
onPressIn(event: PressEvent): void {
7275
const view = viewRef.current;
7376
if (view != null) {
@@ -98,5 +101,5 @@ export default function useAndroidRippleForView(
98101
};
99102
}
100103
return null;
101-
}, [color, borderless, radius, viewRef]);
104+
}, [borderless, color, foreground, radius, viewRef]);
102105
}

packages/rn-tester/js/examples/Pressable/PressableExample.js

+21
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import * as React from 'react';
1212
import {
1313
Animated,
14+
Image,
1415
Pressable,
1516
StyleSheet,
1617
Text,
@@ -310,6 +311,10 @@ const styles = StyleSheet.create({
310311
fontWeight: '500',
311312
color: 'blue',
312313
},
314+
image: {
315+
height: 100,
316+
width: 100,
317+
},
313318
});
314319

315320
exports.displayName = (undefined: ?string);
@@ -425,6 +430,22 @@ exports.examples = [
425430
</Text>
426431
</View>
427432
</Pressable>
433+
434+
<View style={{alignItems: 'center'}}>
435+
<Pressable
436+
android_ripple={{
437+
borderless: false,
438+
foreground: true,
439+
}}>
440+
<Image
441+
source={{
442+
uri: 'https://www.facebook.com/ads/pics/successstories.png',
443+
}}
444+
style={styles.image}
445+
/>
446+
</Pressable>
447+
<Text>use foreground</Text>
448+
</View>
428449
</View>
429450
);
430451
},

0 commit comments

Comments
 (0)