Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Android Pressable foreground ripple #30466

Closed
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 23 additions & 13 deletions Libraries/Components/Pressable/useAndroidRippleForView.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ import invariant from 'invariant';
import {Commands} from '../View/ViewNativeComponent';
import type {ColorValue} from '../../StyleSheet/StyleSheet';
import type {PressEvent} from '../../Types/CoreEventTypes';
import {Platform, View, processColor} from 'react-native';
import {
Platform,
View,
processColor,
TouchableNativeFeedback,
} from 'react-native';
import * as React from 'react';
import {useMemo} from 'react';

Expand All @@ -29,6 +34,7 @@ export type RippleConfig = {|
color?: ColorValue,
borderless?: boolean,
radius?: number,
foreground?: boolean,
|};

/**
Expand All @@ -43,10 +49,11 @@ export default function useAndroidRippleForView(
onPressMove: (event: PressEvent) => void,
onPressOut: (event: PressEvent) => void,
viewProps: $ReadOnly<{|
nativeBackgroundAndroid: NativeBackgroundProp,
nativeBackgroundAndroid?: NativeBackgroundProp,
nativeForegroundAndroid?: NativeBackgroundProp,
|}>,
|}> {
const {color, borderless, radius} = rippleConfig ?? {};
const {color, borderless, foreground, radius} = rippleConfig ?? {};

return useMemo(() => {
if (
Expand All @@ -60,16 +67,19 @@ export default function useAndroidRippleForView(
'Unexpected color given for Ripple color',
);

const background = {
type: 'RippleAndroid',
color: processedColor,
borderless: borderless === true,
rippleRadius: radius,
};

return {
viewProps: {
// Consider supporting `nativeForegroundAndroid`
nativeBackgroundAndroid: {
type: 'RippleAndroid',
color: processedColor,
borderless: borderless === true,
rippleRadius: radius,
},
},
viewProps:
foreground === true &&
TouchableNativeFeedback.canUseNativeForeground()
? {nativeForegroundAndroid: background}
: {nativeBackgroundAndroid: background},
onPressIn(event: PressEvent): void {
const view = viewRef.current;
if (view != null) {
Expand Down Expand Up @@ -100,5 +110,5 @@ export default function useAndroidRippleForView(
};
}
return null;
}, [color, borderless, radius, viewRef]);
}, [color, borderless, foreground, radius, viewRef]);
}