diff --git a/src/components/BottomNavigation/BottomNavigationBar.tsx b/src/components/BottomNavigation/BottomNavigationBar.tsx index 63aaf74df9..bc64cbab99 100644 --- a/src/components/BottomNavigation/BottomNavigationBar.tsx +++ b/src/components/BottomNavigation/BottomNavigationBar.tsx @@ -1,6 +1,6 @@ import * as React from 'react'; // eslint-disable-next-line no-restricted-imports -- TODO: migrate BottomNavigation to Reanimated. -import { Animated, Platform, StyleSheet, Pressable, View } from 'react-native'; +import { Animated, Platform, StyleSheet, View } from 'react-native'; import type { ColorValue, EasingFunction, @@ -24,6 +24,7 @@ import useLayout from '../../utils/useLayout'; import Badge from '../Badge'; import Icon from '../Icon'; import type { IconSource } from '../Icon'; +import { Pressable } from '../TouchableRipple/Pressable'; import TouchableRipple from '../TouchableRipple/TouchableRipple'; import type { Props as TouchableRippleProps } from '../TouchableRipple/TouchableRipple'; import Text from '../Typography/Text'; diff --git a/src/components/TouchableRipple/TouchableRipple.native.tsx b/src/components/TouchableRipple/TouchableRipple.native.tsx index 42e69ddd70..519fc41c0c 100644 --- a/src/components/TouchableRipple/TouchableRipple.native.tsx +++ b/src/components/TouchableRipple/TouchableRipple.native.tsx @@ -10,7 +10,7 @@ import type { import type { PressableProps } from './Pressable'; import { Pressable } from './Pressable'; -import { getTouchableRippleColors } from './utils'; +import { getTouchableRippleColors, resolveChildren } from './utils'; import { SettingsContext } from '../../core/settings'; import type { Settings } from '../../core/settings'; import { useInternalTheme } from '../../core/theming'; @@ -31,8 +31,6 @@ export type Props = PressableProps & { onPressOut?: (e: GestureResponderEvent) => void; rippleColor?: ColorValue; underlayColor?: string; - children: React.ReactNode; - style?: StyleProp; ref?: React.Ref>; theme?: ThemeProp; }; @@ -78,6 +76,15 @@ const TouchableRipple = ({ const useForeground = Platform.OS === 'android' && Platform.Version >= ANDROID_VERSION_PIE; + // Keep the static form when possible: `Pressable` only tracks the pressed + // state when `children` or `style` is a function. + const resolveStyle = ( + baseStyle: StyleProp + ): PressableProps['style'] => + typeof style === 'function' + ? (state) => [baseStyle, style(state)] + : [baseStyle, style]; + if (TouchableRipple.supported) { const androidRipple = rippleEffectEnabled ? (background ?? { @@ -92,10 +99,12 @@ const TouchableRipple = ({ {...rest} ref={ref} disabled={disabled} - style={[useForeground && styles.overflowHidden, style]} + style={resolveStyle(useForeground && styles.overflowHidden)} android_ripple={androidRipple} > - {React.Children.only(children)} + {typeof children === 'function' + ? (state) => resolveChildren(children, state) + : React.Children.only(children)} ); } @@ -105,11 +114,11 @@ const TouchableRipple = ({ {...rest} ref={ref} disabled={disabled} - style={[borderless && styles.overflowHidden, style]} + style={resolveStyle(borderless && styles.overflowHidden)} > - {({ pressed }) => ( + {(state) => ( <> - {pressed && rippleEffectEnabled && ( + {state.pressed && rippleEffectEnabled && ( )} - {React.Children.only(children)} + {resolveChildren(children, state)} )} diff --git a/src/components/TouchableRipple/TouchableRipple.tsx b/src/components/TouchableRipple/TouchableRipple.tsx index 7f6e6d23ad..87cc32b9c3 100644 --- a/src/components/TouchableRipple/TouchableRipple.tsx +++ b/src/components/TouchableRipple/TouchableRipple.tsx @@ -11,7 +11,7 @@ import color from 'color'; import type { PressableProps, PressableStateCallbackType } from './Pressable'; import { Pressable } from './Pressable'; -import { getTouchableRippleColors } from './utils'; +import { getTouchableRippleColors, resolveChildren } from './utils'; import { SettingsContext } from '../../core/settings'; import type { Settings } from '../../core/settings'; import { useInternalTheme } from '../../core/theming'; @@ -61,7 +61,8 @@ export type Props = PressableProps & { */ underlayColor?: string; /** - * Content of the `TouchableRipple`. + * Content of the `TouchableRipple`. Can be a function that receives the pressable state. + * `hovered` and `focused` are only available on the web. */ children: | ((state: PressableStateCallbackType) => React.ReactNode) @@ -290,11 +291,7 @@ const TouchableRipple = ({ typeof style === 'function' ? style(state) : style, ]} > - {(state) => - React.Children.only( - typeof children === 'function' ? children(state) : children - ) - } + {(state) => resolveChildren(children, state)} ); }; diff --git a/src/components/TouchableRipple/utils.ts b/src/components/TouchableRipple/utils.ts index 94aa42e513..d58b7f083d 100644 --- a/src/components/TouchableRipple/utils.ts +++ b/src/components/TouchableRipple/utils.ts @@ -1,5 +1,7 @@ +import * as React from 'react'; import type { ColorValue } from 'react-native'; +import type { PressableProps, PressableStateCallbackType } from './Pressable'; import type { InternalTheme } from '../../theme/types'; const getUnderlayColor = ({ @@ -51,3 +53,11 @@ export const getTouchableRippleColors = ({ }), }; }; + +export const resolveChildren = ( + children: PressableProps['children'], + state: PressableStateCallbackType +) => + React.Children.only( + typeof children === 'function' ? children(state) : children + ); diff --git a/src/components/__tests__/TouchableRipple.test.tsx b/src/components/__tests__/TouchableRipple.test.tsx index 663d540bec..d826586e1f 100644 --- a/src/components/__tests__/TouchableRipple.test.tsx +++ b/src/components/__tests__/TouchableRipple.test.tsx @@ -1,7 +1,7 @@ import { Platform, Text } from 'react-native'; import type { GestureResponderEvent } from 'react-native'; -import { describe, expect, it, jest } from '@jest/globals'; +import { afterAll, beforeAll, describe, expect, it, jest } from '@jest/globals'; import { userEvent } from '@testing-library/react-native'; import { render, screen } from '../../test-utils'; @@ -67,4 +67,71 @@ describe('TouchableRipple', () => { expect(toJSON()).toMatchSnapshot(); }); }); + + // Both branches of the component need to behave the same, so the tests below + // are run once with the native ripple effect enabled (Android >= Lollipop) + // and once with the highlight fallback used everywhere else. + describe.each([ + { supported: false, label: 'with the highlight fallback' }, + { supported: true, label: 'with the native ripple effect' }, + ])('$label', ({ supported }) => { + const originalSupported = TouchableRipple.supported; + + beforeAll(() => { + TouchableRipple.supported = supported; + }); + + afterAll(() => { + TouchableRipple.supported = originalSupported; + }); + + it('supports children as a render function', async () => { + await render( + + {({ pressed }) => {pressed ? 'pressed' : 'idle'}} + + ); + + expect(screen.getByText('idle')).toBeOnTheScreen(); + }); + + it('passes the pressed state to the children render function', async () => { + await render( + + {({ pressed }) => {pressed ? 'pressed' : 'idle'}} + + ); + + expect(screen.getByText('pressed')).toBeOnTheScreen(); + }); + + it('applies the style returned by a style function', async () => { + await render( + ({ opacity: pressed ? 0.5 : 1 })} + > + Press me! + + ); + + expect(screen.getByTestId('touchable-ripple')).toHaveStyle({ + opacity: 0.5, + }); + }); + + it('keeps rendering element children with a style object', async () => { + await render( + + Press me! + + ); + + expect(screen.getByText('Press me!')).toBeOnTheScreen(); + expect(screen.getByTestId('touchable-ripple')).toHaveStyle({ + opacity: 0.5, + }); + }); + }); });