diff --git a/docs/6.x/docs/guides/migration.md b/docs/6.x/docs/guides/migration.md index 0b0b13c650..2db373f8a9 100644 --- a/docs/6.x/docs/guides/migration.md +++ b/docs/6.x/docs/guides/migration.md @@ -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. diff --git a/src/components/TouchableRipple/TouchableRipple.tsx b/src/components/TouchableRipple/TouchableRipple.tsx index c488634945..53a945bd9c 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. + 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 && { backgroundColor: hoverColor }, - !isInteractive && styles.disabled, - typeof style === 'function' ? style(state) : style, ...ring.style, ]} > @@ -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', }), }, diff --git a/src/components/TouchableRipple/utils.ts b/src/components/TouchableRipple/utils.ts index 9aef807629..31f7f35e35 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 type { PressableProps, PressableStateCallbackType } from './Pressable'; import type { InternalTheme } from '../../theme/types'; @@ -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; diff --git a/src/components/__tests__/TouchableRippleHoverWeb.test.tsx b/src/components/__tests__/TouchableRippleHoverWeb.test.tsx new file mode 100644 index 0000000000..9440993dae --- /dev/null +++ b/src/components/__tests__/TouchableRippleHoverWeb.test.tsx @@ -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 + ) => ( + + {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)', () => { + 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"> + 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 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"> + 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, ] } >