diff --git a/src/components/TouchableRipple/TouchableRipple.tsx b/src/components/TouchableRipple/TouchableRipple.tsx index c488634945..e523ac2395 100644 --- a/src/components/TouchableRipple/TouchableRipple.tsx +++ b/src/components/TouchableRipple/TouchableRipple.tsx @@ -7,11 +7,14 @@ import type { ViewStyle, } from 'react-native'; -import color from 'color'; - import type { PressableProps, PressableStateCallbackType } from './Pressable'; import { Pressable } from './Pressable'; -import { IDLE_STATE, getTouchableRippleColors, resolveChildren } from './utils'; +import { + IDLE_STATE, + getHoverColor, + getTouchableRippleColors, + resolveChildren, +} from './utils'; import { SettingsContext } from '../../core/settings'; import type { Settings } from '../../core/settings'; import { useInternalTheme } from '../../core/theming'; @@ -81,7 +84,9 @@ export type Props = PressableProps & { */ onPressOut?: (e: GestureResponderEvent) => void; /** - * Color of the ripple effect (Android >= 5.0 and Web). + * Color of the ripple effect (Android >= 5.0 and Web). On web, hovering + * shows this color at 0.8 times its opacity: the Material Design 3 ratio of + * the hovered to the pressed state layer. */ rippleColor?: ColorValue; /** @@ -153,12 +158,7 @@ const TouchableRipple = ({ theme, rippleColor, }); - // Web-only style. PlatformColor doesn't exist on web, so the calculated - // ripple color is effectively always a string here. - const hoverColor = - typeof calculatedRippleColor === 'string' - ? color(calculatedRippleColor).fade(0.5).rgb().string() - : calculatedRippleColor; + const hoverColor = getHoverColor(calculatedRippleColor); const { rippleEffectEnabled } = React.useContext(SettingsContext); const { onPress, onLongPress, onPressIn, onPressOut } = rest; diff --git a/src/components/TouchableRipple/utils.ts b/src/components/TouchableRipple/utils.ts index 07f41beb4a..6843678188 100644 --- a/src/components/TouchableRipple/utils.ts +++ b/src/components/TouchableRipple/utils.ts @@ -58,6 +58,28 @@ export const getTouchableRippleColors = ({ }; }; +/** + * Returns the web hover layer color for a ripple color. The ripple is the + * content color at the pressed-state opacity, so scaling its alpha by the + * hovered-to-pressed ratio gives the content color at the hovered-state + * opacity (per Material Design 3). A custom ripple color keeps that ratio. + * + * A color that is not a string (a `PlatformColor`, which web doesn't have) is + * returned as is. + */ +export const getHoverColor = (rippleColor: ColorValue): ColorValue => { + if (typeof rippleColor !== 'string') { + return rippleColor; + } + + const ripple = color(rippleColor); + + return ripple + .alpha((ripple.alpha() * stateOpacity.hovered) / stateOpacity.pressed) + .rgb() + .string(); +}; + // The plain View a touchable renders without a handler has nothing to press, so // its render prop and style function always see this idle state. Matches the // state shape Pressable passes. diff --git a/src/components/__tests__/TouchableRippleUtils.test.tsx b/src/components/__tests__/TouchableRippleUtils.test.tsx index 2f00f71250..4ccc2c89d1 100644 --- a/src/components/__tests__/TouchableRippleUtils.test.tsx +++ b/src/components/__tests__/TouchableRippleUtils.test.tsx @@ -4,7 +4,10 @@ import { describe, expect, it } from '@jest/globals'; import color from 'color'; import { tokens } from '../../theme/tokens'; -import { getRippleColorFromContent } from '../TouchableRipple/utils'; +import { + getHoverColor, + getRippleColorFromContent, +} from '../TouchableRipple/utils'; const stateOpacity = tokens.md.sys.state.opacity; @@ -23,3 +26,29 @@ describe('getRippleColorFromContent', () => { ).toBeUndefined(); }); }); + +describe('getHoverColor', () => { + it('gives the content color at the hovered-state opacity for a content ripple', () => { + const rippleColor = getRippleColorFromContent({ contentColor: '#123456' }); + + expect(rippleColor).toBeDefined(); + expect(getHoverColor(rippleColor ?? '')).toBe( + color('#123456').alpha(stateOpacity.hovered).rgb().string() + ); + }); + + it('keeps the hovered-to-pressed ratio for a custom ripple color', () => { + const hoverColor = color(String(getHoverColor('rgba(255, 0, 0, 0.5)'))); + + expect(hoverColor.rgb().array().slice(0, 3)).toEqual([255, 0, 0]); + expect(hoverColor.alpha()).toBeCloseTo( + (0.5 * stateOpacity.hovered) / stateOpacity.pressed + ); + }); + + it('returns a color that is not a plain string as is', () => { + const rippleColor = PlatformColor('?attr/colorPrimary'); + + expect(getHoverColor(rippleColor)).toBe(rippleColor); + }); +});