From 57399c2d405ba01c9e7fd032b19c47de35b25f72 Mon Sep 17 00:00:00 2001 From: Jacek Paciorek Date: Thu, 19 Nov 2026 14:45:21 +0100 Subject: [PATCH 1/6] fix(touchable-ripple): paint web hover on top of style background --- .../TouchableRipple/TouchableRipple.tsx | 14 ++- src/components/TouchableRipple/utils.ts | 11 +- .../TouchableRippleHoverWeb.test.tsx | 100 ++++++++++++++++++ .../TouchableRippleWeb.test.tsx.snap | 10 +- 4 files changed, 125 insertions(+), 10 deletions(-) create mode 100644 src/components/__tests__/TouchableRippleHoverWeb.test.tsx diff --git a/src/components/TouchableRipple/TouchableRipple.tsx b/src/components/TouchableRipple/TouchableRipple.tsx index c488634945..0d10952018 100644 --- a/src/components/TouchableRipple/TouchableRipple.tsx +++ b/src/components/TouchableRipple/TouchableRipple.tsx @@ -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'; @@ -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. // 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, + state.hovered && isInteractive && getHoverStyle(hoverColor), ...ring.style, ]} > diff --git a/src/components/TouchableRipple/utils.ts b/src/components/TouchableRipple/utils.ts index 07f41beb4a..5a6d6676d6 100644 --- a/src/components/TouchableRipple/utils.ts +++ b/src/components/TouchableRipple/utils.ts @@ -1,4 +1,4 @@ -import type { ColorValue } from 'react-native'; +import type { ColorValue, ViewStyle } from 'react-native'; import color from 'color'; @@ -96,3 +96,12 @@ export const getRippleColorFromContent = ({ return undefined; } }; + +/** + * 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. + */ +export const getHoverStyle = (hoverColor: ColorValue): ViewStyle => ({ + backgroundImage: `linear-gradient(${String(hoverColor)}, ${String(hoverColor)})`, +}); diff --git a/src/components/__tests__/TouchableRippleHoverWeb.test.tsx b/src/components/__tests__/TouchableRippleHoverWeb.test.tsx new file mode 100644 index 0000000000..28ca7e48ed --- /dev/null +++ b/src/components/__tests__/TouchableRippleHoverWeb.test.tsx @@ -0,0 +1,100 @@ +import * as React from 'react'; +import { Text } from 'react-native'; + +import { describe, expect, it, jest } from '@jest/globals'; +import color from 'color'; + +import { render, screen } from '../../test-utils'; +import { LightTheme } from '../../theme/schemes'; +// 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 }: any, ref: React.Ref) => ( + + {typeof children === 'function' ? children(hovered) : children} + + ) + ), + }; +}); + +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)', () => { + it('paints the hover layer when style sets no background', async () => { + await render( + {}} testID="ripple"> + Button + + ); + + expect(screen.getByTestId('ripple')).toHaveStyle({ + backgroundImage: hoverLayer, + }); + }); + + it('paints the hover layer on top of a background set through style', async () => { + await render( + {}} + style={{ backgroundColor: rowColor }} + testID="ripple" + > + Button + + ); + + 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( + {}} + style={() => ({ backgroundColor: rowColor })} + testID="ripple" + > + Button + + ); + + expect(screen.getByTestId('ripple')).toHaveStyle({ + backgroundColor: rowColor, + backgroundImage: hoverLayer, + }); + }); + + it('does not paint the hover layer on a disabled touchable', async () => { + await render( + {}} disabled testID="ripple"> + Button + + ); + + expect(screen.getByTestId('ripple')).not.toHaveStyle({ + backgroundImage: hoverLayer, + }); + }); +}); diff --git a/src/components/__tests__/__snapshots__/TouchableRippleWeb.test.tsx.snap b/src/components/__tests__/__snapshots__/TouchableRippleWeb.test.tsx.snap index 453d77f958..6dd48d9b88 100644 --- a/src/components/__tests__/__snapshots__/TouchableRippleWeb.test.tsx.snap +++ b/src/components/__tests__/__snapshots__/TouchableRippleWeb.test.tsx.snap @@ -36,9 +36,9 @@ exports[`TouchableRipple (web) does not clip its content when borderless 1`] = ` { "position": "relative", }, - undefined, false, undefined, + undefined, ] } > @@ -84,9 +84,9 @@ exports[`TouchableRipple (web) renders a touch target behind the children sized { "position": "relative", }, - undefined, false, undefined, + undefined, ] } > @@ -144,9 +144,9 @@ exports[`TouchableRipple (web) renders a touch target behind the children sized { "position": "relative", }, - undefined, false, undefined, + undefined, ] } > @@ -204,9 +204,9 @@ exports[`TouchableRipple (web) renders no touch target when it is disabled 1`] = { "position": "relative", }, - undefined, {}, undefined, + undefined, ] } > @@ -270,9 +270,9 @@ exports[`TouchableRipple (web) renders no touch target when there is no hitSlop { "position": "relative", }, - undefined, false, undefined, + undefined, ] } > From 2ecf362c57ca486b788a6dd85431509f5c6eacd7 Mon Sep 17 00:00:00 2001 From: Jacek Paciorek Date: Thu, 19 Nov 2026 16:50:11 +0100 Subject: [PATCH 2/6] fix(touchable-ripple): skip web hover for a non-string ripple color --- src/components/TouchableRipple/utils.ts | 11 ++++++++--- .../__tests__/TouchableRippleHoverWeb.test.tsx | 18 +++++++++++++++++- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/src/components/TouchableRipple/utils.ts b/src/components/TouchableRipple/utils.ts index 5a6d6676d6..21791ce76f 100644 --- a/src/components/TouchableRipple/utils.ts +++ b/src/components/TouchableRipple/utils.ts @@ -101,7 +101,12 @@ export const getRippleColorFromContent = ({ * 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 => ({ - backgroundImage: `linear-gradient(${String(hoverColor)}, ${String(hoverColor)})`, -}); +export const getHoverStyle = (hoverColor: ColorValue): ViewStyle | undefined => + typeof hoverColor === 'string' + ? { backgroundImage: `linear-gradient(${hoverColor}, ${hoverColor})` } + : undefined; diff --git a/src/components/__tests__/TouchableRippleHoverWeb.test.tsx b/src/components/__tests__/TouchableRippleHoverWeb.test.tsx index 28ca7e48ed..aeb1cada31 100644 --- a/src/components/__tests__/TouchableRippleHoverWeb.test.tsx +++ b/src/components/__tests__/TouchableRippleHoverWeb.test.tsx @@ -1,5 +1,5 @@ import * as React from 'react'; -import { Text } from 'react-native'; +import { PlatformColor, Text } from 'react-native'; import { describe, expect, it, jest } from '@jest/globals'; import color from 'color'; @@ -86,6 +86,22 @@ describe('TouchableRipple hover (web)', () => { }); }); + it('does not paint a hover layer for a ripple color that is not a string', async () => { + await render( + {}} + rippleColor={PlatformColor('systemBlue')} + testID="ripple" + > + Button + + ); + + expect(screen.getByTestId('ripple')).toHaveStyle({ + backgroundImage: undefined, + }); + }); + it('does not paint the hover layer on a disabled touchable', async () => { await render( {}} disabled testID="ripple"> From b44658ca92d69963a046a3acc6800d2b91ed434e Mon Sep 17 00:00:00 2001 From: Jacek Paciorek Date: Thu, 19 Nov 2026 17:32:10 +0100 Subject: [PATCH 3/6] fix(touchable-ripple): adress sentinel comments --- .../TouchableRipple/TouchableRipple.tsx | 2 +- .../TouchableRippleHoverWeb.test.tsx | 19 +++++++++++++++++-- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/components/TouchableRipple/TouchableRipple.tsx b/src/components/TouchableRipple/TouchableRipple.tsx index 0d10952018..dcf6343ccc 100644 --- a/src/components/TouchableRipple/TouchableRipple.tsx +++ b/src/components/TouchableRipple/TouchableRipple.tsx @@ -360,10 +360,10 @@ const TouchableRipple = ({ !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), // 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 && getHoverStyle(hoverColor), ...ring.style, ]} > diff --git a/src/components/__tests__/TouchableRippleHoverWeb.test.tsx b/src/components/__tests__/TouchableRippleHoverWeb.test.tsx index aeb1cada31..40a41fbf00 100644 --- a/src/components/__tests__/TouchableRippleHoverWeb.test.tsx +++ b/src/components/__tests__/TouchableRippleHoverWeb.test.tsx @@ -1,7 +1,14 @@ import * as React from 'react'; -import { PlatformColor, Text } from 'react-native'; +import { Platform, PlatformColor, Text } from 'react-native'; -import { describe, expect, it, jest } from '@jest/globals'; +import { + afterEach, + beforeEach, + describe, + expect, + it, + jest, +} from '@jest/globals'; import color from 'color'; import { render, screen } from '../../test-utils'; @@ -40,6 +47,14 @@ 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( {}} testID="ripple"> From e3b261edd0203c396eac52bf195daa67a5ba0564 Mon Sep 17 00:00:00 2001 From: Jacek Paciorek Date: Mon, 23 Nov 2026 02:05:20 +0100 Subject: [PATCH 4/6] fix(touchable-ripple): adress sentinel comments --- docs/6.x/docs/guides/migration.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/6.x/docs/guides/migration.md b/docs/6.x/docs/guides/migration.md index 83cead4660..3fd9c7f3f4 100644 --- a/docs/6.x/docs/guides/migration.md +++ b/docs/6.x/docs/guides/migration.md @@ -1024,6 +1024,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`. + ### Drawer The `Drawer` API is unchanged in Paper 6.x. No props were renamed, removed, or given new defaults. `Drawer.Item`, `Drawer.CollapsedItem` and `Drawer.Section` were realigned with the current Material Design 3 navigation drawer spec, so their appearance changes: From f7073add69c1b3ed5d923716008c72349d339112 Mon Sep 17 00:00:00 2001 From: Jacek Paciorek Date: Mon, 23 Nov 2026 12:54:00 +0100 Subject: [PATCH 5/6] docs(touchable-ripple): note that the web hover no longer fades --- docs/6.x/docs/guides/migration.md | 2 +- src/components/TouchableRipple/TouchableRipple.tsx | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/docs/6.x/docs/guides/migration.md b/docs/6.x/docs/guides/migration.md index 3fd9c7f3f4..9f7dda027a 100644 --- a/docs/6.x/docs/guides/migration.md +++ b/docs/6.x/docs/guides/migration.md @@ -1024,7 +1024,7 @@ 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`. +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. ### Drawer diff --git a/src/components/TouchableRipple/TouchableRipple.tsx b/src/components/TouchableRipple/TouchableRipple.tsx index dcf6343ccc..53a945bd9c 100644 --- a/src/components/TouchableRipple/TouchableRipple.tsx +++ b/src/components/TouchableRipple/TouchableRipple.tsx @@ -410,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', }), }, From 1607ad6cd29798378449b6da5de44753986d82a0 Mon Sep 17 00:00:00 2001 From: Jacek Paciorek Date: Mon, 23 Nov 2026 12:54:27 +0100 Subject: [PATCH 6/6] test(touchable-ripple): type the Pressable mock with PressableProps --- src/components/__tests__/TouchableRippleHoverWeb.test.tsx | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/components/__tests__/TouchableRippleHoverWeb.test.tsx b/src/components/__tests__/TouchableRippleHoverWeb.test.tsx index 40a41fbf00..9440993dae 100644 --- a/src/components/__tests__/TouchableRippleHoverWeb.test.tsx +++ b/src/components/__tests__/TouchableRippleHoverWeb.test.tsx @@ -13,6 +13,7 @@ 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'; @@ -26,7 +27,10 @@ jest.mock('../TouchableRipple/Pressable', () => { return { Pressable: React.forwardRef( - ({ style, children, ...rest }: any, ref: React.Ref) => ( + ( + { style, children, ...rest }: PressableProps, + ref: React.Ref + ) => (