Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 2 additions & 1 deletion src/components/BottomNavigation/BottomNavigationBar.tsx
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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';
Expand Down
27 changes: 18 additions & 9 deletions src/components/TouchableRipple/TouchableRipple.native.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -31,8 +31,6 @@ export type Props = PressableProps & {
onPressOut?: (e: GestureResponderEvent) => void;
rippleColor?: ColorValue;
underlayColor?: string;
children: React.ReactNode;
style?: StyleProp<ViewStyle>;
ref?: React.Ref<React.ComponentRef<typeof View>>;
theme?: ThemeProp;
};
Expand Down Expand Up @@ -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<ViewStyle>
): PressableProps['style'] =>
typeof style === 'function'
? (state) => [baseStyle, style(state)]
: [baseStyle, style];

if (TouchableRipple.supported) {
const androidRipple = rippleEffectEnabled
? (background ?? {
Expand All @@ -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)}
</Pressable>
);
}
Expand All @@ -105,19 +114,19 @@ const TouchableRipple = ({
{...rest}
ref={ref}
disabled={disabled}
style={[borderless && styles.overflowHidden, style]}
style={resolveStyle(borderless && styles.overflowHidden)}
>
{({ pressed }) => (
{(state) => (
<>
{pressed && rippleEffectEnabled && (
{state.pressed && rippleEffectEnabled && (
<View
style={[
styles.underlay,
{ backgroundColor: calculatedUnderlayColor },
]}
/>
)}
{React.Children.only(children)}
{resolveChildren(children, state)}
</>
)}
</Pressable>
Expand Down
11 changes: 4 additions & 7 deletions src/components/TouchableRipple/TouchableRipple.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)}
</Pressable>
);
};
Expand Down
10 changes: 10 additions & 0 deletions src/components/TouchableRipple/utils.ts
Original file line number Diff line number Diff line change
@@ -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 = ({
Expand Down Expand Up @@ -51,3 +53,11 @@ export const getTouchableRippleColors = ({
}),
};
};

export const resolveChildren = (
children: PressableProps['children'],
state: PressableStateCallbackType
) =>
React.Children.only(
typeof children === 'function' ? children(state) : children
);
69 changes: 68 additions & 1 deletion src/components/__tests__/TouchableRipple.test.tsx
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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(
<TouchableRipple>
{({ pressed }) => <Text>{pressed ? 'pressed' : 'idle'}</Text>}
</TouchableRipple>
);

expect(screen.getByText('idle')).toBeOnTheScreen();
});

it('passes the pressed state to the children render function', async () => {
await render(
<TouchableRipple testOnly_pressed>
{({ pressed }) => <Text>{pressed ? 'pressed' : 'idle'}</Text>}
</TouchableRipple>
);

expect(screen.getByText('pressed')).toBeOnTheScreen();
});

it('applies the style returned by a style function', async () => {
await render(
<TouchableRipple
testID="touchable-ripple"
testOnly_pressed
style={({ pressed }) => ({ opacity: pressed ? 0.5 : 1 })}
>
<Text>Press me!</Text>
</TouchableRipple>
);

expect(screen.getByTestId('touchable-ripple')).toHaveStyle({
opacity: 0.5,
});
});

it('keeps rendering element children with a style object', async () => {
await render(
<TouchableRipple testID="touchable-ripple" style={{ opacity: 0.5 }}>
<Text>Press me!</Text>
</TouchableRipple>
);

expect(screen.getByText('Press me!')).toBeOnTheScreen();
expect(screen.getByTestId('touchable-ripple')).toHaveStyle({
opacity: 0.5,
});
});
});
});
Loading