Skip to content
2 changes: 2 additions & 0 deletions docs/6.x/docs/guides/migration.md
Original file line number Diff line number Diff line change
Expand Up @@ -939,6 +939,8 @@ The close button (`onClose`) now fills the trailing 34dp column reserved for it,

`TouchableRipple` draws the keyboard focus ring and takes a new `focusRing` prop. See [Focus indicator](#focus-indicator).

On web, the hover layer is now a CSS background image. It paints on top of a `backgroundColor` that you set through `style`. While the touchable is hovered, it replaces a `backgroundImage`, such as a gradient, that you set through `style`. The hover layer no longer fades in and out. It shows and hides at once, as in Material Web.

### TextInput

The Paper 6.x `TextInput` is a complete rewrite with a new API. Import the component the same way, but note that the props and behavior have changed significantly.
Expand Down
17 changes: 13 additions & 4 deletions src/components/TouchableRipple/TouchableRipple.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ import color from 'color';

import type { PressableProps, PressableStateCallbackType } from './Pressable';
import { Pressable } from './Pressable';
import { IDLE_STATE, getTouchableRippleColors, resolveChildren } from './utils';
import {
IDLE_STATE,
getHoverStyle,
getTouchableRippleColors,
resolveChildren,
} from './utils';
import { SettingsContext } from '../../core/settings';
import type { Settings } from '../../core/settings';
import { useInternalTheme } from '../../core/theming';
Expand Down Expand Up @@ -352,12 +357,13 @@ const TouchableRipple = ({
{...mergeDataSetProps(rest, ring.dataSetProps)}
style={(state) => [
styles.touchable,
!isInteractive && styles.disabled,
typeof style === 'function' ? style(state) : style,
// After `style`, so a background set there does not hide the hover.
state.hovered && isInteractive && getHoverStyle(hoverColor),
Comment thread
jacekAtCK marked this conversation as resolved.
Resolved
// RNW's own `state.focused` fires for mouse clicks too, which is
// exactly the distinction `:focus-visible` exists to make.
// https://github.com/necolas/react-native-web/issues/1849
state.hovered && isInteractive && { backgroundColor: hoverColor },
!isInteractive && styles.disabled,
typeof style === 'function' ? style(state) : style,
...ring.style,
]}
>
Expand Down Expand Up @@ -404,6 +410,9 @@ const styles = StyleSheet.create({
position: 'relative',
...(Platform.OS === 'web' && {
cursor: 'pointer',
// Eases a `backgroundColor` change from `style`, such as a row that
// becomes selected. The hover layer is a gradient, which CSS can't
// transition, so hover shows at once, as in Material Web (15ms).
transition: '150ms background-color',
}),
},
Expand Down
16 changes: 15 additions & 1 deletion src/components/TouchableRipple/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ColorValue } from 'react-native';
import type { ColorValue, ViewStyle } from 'react-native';

import type { PressableProps, PressableStateCallbackType } from './Pressable';
import type { InternalTheme } from '../../theme/types';
Expand Down Expand Up @@ -66,3 +66,17 @@ export const resolveChildren = (
children: PressableProps['children'],
state: PressableStateCallbackType
) => (typeof children === 'function' ? children(state) : children);

/**
* The hover layer of a touchable on web. It is a background image, so it
* paints on top of a background color that `style` sets, such as the color of
* a selected row, and it keeps the border radius of the touchable.
*
* While the touchable is hovered, this layer replaces a `backgroundImage` that
* `style` sets. A color that is not a string cannot go in a CSS gradient, so
* it gets no hover layer.
*/
export const getHoverStyle = (hoverColor: ColorValue): ViewStyle | undefined =>
typeof hoverColor === 'string'
? { backgroundImage: `linear-gradient(${hoverColor}, ${hoverColor})` }
: undefined;
Comment thread
jacekAtCK marked this conversation as resolved.
Resolved
Comment thread
jacekAtCK marked this conversation as resolved.
Resolved
135 changes: 135 additions & 0 deletions src/components/__tests__/TouchableRippleHoverWeb.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
import * as React from 'react';
import { Platform, PlatformColor, Text } from 'react-native';

import {
afterEach,
beforeEach,
describe,
expect,
it,
jest,
} from '@jest/globals';
import color from 'color';

import { render, screen } from '../../test-utils';
import { LightTheme } from '../../theme/schemes';
import type { PressableProps } from '../TouchableRipple/Pressable';
// The bare specifier resolves to .native under the jest preset, so the web file
// needs the extension or it never runs.
import TouchableRipple from '../TouchableRipple/TouchableRipple.tsx';

// Jest has no pointer, so the mock renders Pressable as hovered. That is the
// state React Native Web passes to the style function under a mouse.
jest.mock('../TouchableRipple/Pressable', () => {
const React = require('react');
const { View } = require('react-native');
const hovered = { hovered: true, pressed: false, focused: false };

return {
Pressable: React.forwardRef(
(
{ style, children, ...rest }: PressableProps,
ref: React.Ref<unknown>
) => (
<View
{...rest}
ref={ref}
style={typeof style === 'function' ? style(hovered) : style}
>
{typeof children === 'function' ? children(hovered) : children}
</View>
)
),
};
});
Comment thread
jacekAtCK marked this conversation as resolved.
Resolved

const hoverColor = color(String(LightTheme.colors.stateLayerPressed))
.fade(0.5)
.rgb()
.string();
const hoverLayer = `linear-gradient(${hoverColor}, ${hoverColor})`;
const rowColor = LightTheme.colors.secondaryContainer;

describe('TouchableRipple hover (web)', () => {
const original = Platform.OS;
beforeEach(() => {
Platform.OS = 'web';
});
afterEach(() => {
Platform.OS = original;
});

it('paints the hover layer when style sets no background', async () => {
await render(
<TouchableRipple onPress={() => {}} testID="ripple">
<Text>Button</Text>
</TouchableRipple>
);

expect(screen.getByTestId('ripple')).toHaveStyle({
backgroundImage: hoverLayer,
});
});

it('paints the hover layer on top of a background set through style', async () => {
await render(
<TouchableRipple
onPress={() => {}}
style={{ backgroundColor: rowColor }}
testID="ripple"
>
<Text>Button</Text>
</TouchableRipple>
);

expect(screen.getByTestId('ripple')).toHaveStyle({
backgroundColor: rowColor,
backgroundImage: hoverLayer,
});
});

it('paints the hover layer on top of a background from a style function', async () => {
await render(
<TouchableRipple
onPress={() => {}}
style={() => ({ backgroundColor: rowColor })}
testID="ripple"
>
<Text>Button</Text>
</TouchableRipple>
);

expect(screen.getByTestId('ripple')).toHaveStyle({
backgroundColor: rowColor,
backgroundImage: hoverLayer,
});
});

it('does not paint a hover layer for a ripple color that is not a string', async () => {
await render(
<TouchableRipple
onPress={() => {}}
rippleColor={PlatformColor('systemBlue')}
testID="ripple"
>
<Text>Button</Text>
</TouchableRipple>
);

expect(screen.getByTestId('ripple')).toHaveStyle({
backgroundImage: undefined,
});
});

it('does not paint the hover layer on a disabled touchable', async () => {
await render(
<TouchableRipple onPress={() => {}} disabled testID="ripple">
<Text>Button</Text>
</TouchableRipple>
);

expect(screen.getByTestId('ripple')).not.toHaveStyle({
backgroundImage: hoverLayer,
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ exports[`TouchableRipple (web) does not clip its content when borderless 1`] = `
{
"position": "relative",
},
undefined,
false,
undefined,
undefined,
]
}
>
Expand Down Expand Up @@ -84,9 +84,9 @@ exports[`TouchableRipple (web) renders a touch target behind the children sized
{
"position": "relative",
},
undefined,
false,
undefined,
undefined,
]
}
>
Expand Down Expand Up @@ -144,9 +144,9 @@ exports[`TouchableRipple (web) renders a touch target behind the children sized
{
"position": "relative",
},
undefined,
false,
undefined,
undefined,
]
}
>
Expand Down Expand Up @@ -204,9 +204,9 @@ exports[`TouchableRipple (web) renders no touch target when it is disabled 1`] =
{
"position": "relative",
},
undefined,
{},
undefined,
undefined,
]
}
>
Expand Down Expand Up @@ -270,9 +270,9 @@ exports[`TouchableRipple (web) renders no touch target when there is no hitSlop
{
"position": "relative",
},
undefined,
false,
undefined,
undefined,
]
}
>
Expand Down
Loading