Skip to content
Open
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
20 changes: 10 additions & 10 deletions src/components/TouchableRipple/TouchableRipple.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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;
/**
Expand Down Expand Up @@ -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<Settings>(SettingsContext);

const { onPress, onLongPress, onPressIn, onPressOut } = rest;
Expand Down
22 changes: 22 additions & 0 deletions src/components/TouchableRipple/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
31 changes: 30 additions & 1 deletion src/components/__tests__/TouchableRippleUtils.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
});
});
Loading