Skip to content
Merged
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
14 changes: 14 additions & 0 deletions docs/6.x/docs/guides/migration.md
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,20 @@ The misspelled `traileringIcon` props have been renamed:
/>
```

### Chip

The close button (`onClose`) now fills the trailing 34dp column reserved for it, instead of only the 26x18 area around its icon, and its touch target extends to 48dp in height. Taps in that column that used to activate the chip's `onPress` now activate `onClose`.

### IconButton

Corner radii set through `style` no longer shape the press, hover and disabled feedback.

### TouchableRipple

`borderless` has no effect on web. The web ripple is always clipped to the touchable's shape, including when `centered` is set, and the touchable no longer clips its own content. A child that needs a clipped or rounded shape should carry that shape itself.
Comment thread
Doberjohn marked this conversation as resolved.
Resolved

`hitSlop` now works on web: the touchable renders an invisible target that extends past its bounds. An ancestor with `overflow: 'hidden'` or a scroll view clips it, as on Android.

### 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
3 changes: 3 additions & 0 deletions src/components/BottomNavigation/BottomNavigationBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -788,6 +788,9 @@ const styles = StyleSheet.create({
// Top padding is 6 and bottom padding is 10
// The extra 4dp bottom padding is offset by label's height
paddingVertical: 6,
// `borderless` doesn't clip on web, so a label wider than its tab would
// spill into the neighbouring tabs.
...(Platform.OS === 'web' ? { overflow: 'hidden' } : null),
},
v3Item: {
paddingVertical: 0,
Expand Down
115 changes: 111 additions & 4 deletions src/components/Chip/Chip.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,28 @@ import { Platform, StyleSheet, Pressable, View } from 'react-native';
import type {
ColorValue,
GestureResponderEvent,
NativeSyntheticEvent,
PressableAndroidRippleConfig,
StyleProp,
TargetedEvent,
TextProps,
TextStyle,
ViewProps,
ViewStyle,
} from 'react-native';

import useLatestCallback from 'use-latest-callback';

import { getChipColors } from './helpers';
import type { ChipAvatarProps } from './helpers';
import { ChipTokens } from './tokens';
import { useInternalTheme } from '../../core/theming';
import { white } from '../../theme/colors';
import { tokens } from '../../theme/tokens';
import type { ThemeProp } from '../../theme/types';
import getMinInteractiveSizeHitSlop from '../../utils/getMinInteractiveSizeHitSlop';
import hasTouchHandler from '../../utils/hasTouchHandler';
import { isKeyboardFocusEvent } from '../../utils/isKeyboardFocusEvent';
import type { IconSource } from '../Icon';
import Icon from '../Icon';
import MaterialCommunityIcon from '../MaterialCommunityIcon';
Expand Down Expand Up @@ -156,6 +163,38 @@ export type Props = Omit<ViewProps, 'style'> & {
ref?: React.Ref<React.ComponentRef<typeof View>>;
};

/**
* Width reserved on the right for the close button.
*/
Comment thread
Doberjohn marked this conversation as resolved.
Resolved
const CLOSE_AFFORDANCE_WIDTH = 34;

/**
* Floor for the clamp below. The 18dp glyph is centered in the column, so at
* this width it sits 4dp from each side. Any narrower and part of the visible
* icon would activate the chip instead of removing it.
*/
const CLOSE_AFFORDANCE_MIN_WIDTH = 26;

/**
* The label makes a default chip 32dp tall, so the slop to reach the 48dp
* minimum is a constant rather than something to measure. Width grows with the
* label and the whole pill is already the target, so only the vertical axis
* needs it.
*/
const { containerHeight: CHIP_BODY_HEIGHT } = ChipTokens;
const CHIP_BODY_HIT_SLOP = getMinInteractiveSizeHitSlop({
height: CHIP_BODY_HEIGHT,
});
// The close column spans the chip's height, so it gets the same vertical slop.
const CLOSE_BUTTON_WEB_TOUCH_TARGET_INSET = CHIP_BODY_HIT_SLOP?.top ?? 0;

const CLOSE_ICON_SIZE = 18;
const CLOSE_FOCUS_THICKNESS = tokens.md.sys.state.focusIndicator.thickness;

// Web-only style; `outline` is outside ViewStyle.
// eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
const webNoOutline = { outline: 'none' } as unknown as ViewStyle;

/**
* Chips are compact elements that can represent inputs, attributes, or actions.
* They can have an icon or avatar on the left, and a close button icon on the right.
Expand Down Expand Up @@ -214,6 +253,7 @@ const Chip = ({
const theme = useInternalTheme(themeOverrides);

const [pressed, setPressed] = React.useState(false);
const [closeFocused, setCloseFocused] = React.useState(false);
const elevation = elevated ? (pressed ? 2 : 1) : 0;

const hasPassedTouchHandler = hasTouchHandler({
Expand All @@ -235,6 +275,18 @@ const Chip = ({
setPressed(false);
});

const handleCloseFocus = useLatestCallback(
(e: NativeSyntheticEvent<TargetedEvent>) => {
if (Platform.OS === 'web' && isKeyboardFocusEvent(e)) {
setCloseFocused(true);
}
}
);

const handleCloseBlur = useLatestCallback(() => {
setCloseFocused(false);
});

const elevationTransitionDuration =
theme.motion.duration[pressed ? 'short4' : 'short3'] *
theme.animation.scale;
Expand Down Expand Up @@ -270,7 +322,7 @@ const Chip = ({
};

const contentSpacings = {
paddingRight: onClose ? 34 : 0,
paddingRight: onClose ? CLOSE_AFFORDANCE_WIDTH : 0,
};

const labelTextStyle = {
Expand Down Expand Up @@ -304,7 +356,13 @@ const Chip = ({
aria-disabled={disabled}
testID={testID}
theme={theme}
hitSlop={hitSlop}
hitSlop={
Comment thread
Doberjohn marked this conversation as resolved.
Resolved
hitSlop !== undefined
? hitSlop
: disabled || !hasPassedTouchHandler
? undefined
: CHIP_BODY_HIT_SLOP
}
>
<View
style={[
Expand Down Expand Up @@ -391,8 +449,32 @@ const Chip = ({
role="button"
aria-label={closeIconAccessibilityLabel}
testID={closeIconTestID}
onFocus={handleCloseFocus}
onBlur={handleCloseBlur}
// The browser outline would wrap the whole column and cover the
// label, so a ring is drawn around the glyph instead.
style={[
styles.closeButton,
Platform.OS === 'web' ? webNoOutline : undefined,
]}
hitSlop={disabled ? undefined : CHIP_BODY_HIT_SLOP}
>
{/* react-native-web removed `hitSlop` in 0.13.0,
so web needs a real element the browser can
hit-test instead of a native responder inset. */}
{Platform.OS === 'web' && !disabled && (
<View aria-hidden style={styles.closeButtonWebTouchTarget} />
)}
<View style={[styles.icon, styles.closeIcon, styles.md3CloseIcon]}>
{closeFocused && !disabled ? (
<View
pointerEvents="none"
style={[
styles.closeFocusRing,
{ borderColor: theme.colors.secondary },
]}
/>
) : null}
{closeIcon ? (
<Icon source={closeIcon} color={iconColor} size={iconSize} />
) : (
Expand Down Expand Up @@ -441,7 +523,7 @@ const styles = StyleSheet.create({
marginRight: 4,
},
md3CloseIcon: {
marginRight: 8,
marginRight: 0,
padding: 0,
},
md3LabelText: {
Expand Down Expand Up @@ -474,9 +556,34 @@ const styles = StyleSheet.create({
closeButtonStyle: {
position: 'absolute',
right: 0,
width: CLOSE_AFFORDANCE_WIDTH,
// A chip narrower than this column would hand the whole thing to the close
// button. Never more than half, never less than the glyph needs; minWidth
// wins over maxWidth.
minWidth: CLOSE_AFFORDANCE_MIN_WIDTH,
maxWidth: '50%',
height: '100%',
},
closeButton: {
width: '100%',
height: '100%',
justifyContent: 'center',
alignItems: 'center',
},
closeFocusRing: {
position: 'absolute',
top: -CLOSE_FOCUS_THICKNESS,
left: -CLOSE_FOCUS_THICKNESS,
width: CLOSE_ICON_SIZE + 2 * CLOSE_FOCUS_THICKNESS,
height: CLOSE_ICON_SIZE + 2 * CLOSE_FOCUS_THICKNESS,
borderRadius: CLOSE_ICON_SIZE / 2 + CLOSE_FOCUS_THICKNESS,
borderWidth: CLOSE_FOCUS_THICKNESS,
},
closeButtonWebTouchTarget: {
position: 'absolute',
top: -CLOSE_BUTTON_WEB_TOUCH_TARGET_INSET,
bottom: -CLOSE_BUTTON_WEB_TOUCH_TARGET_INSET,
left: 0,
right: 0,
},
touchable: {
width: '100%',
Expand Down
7 changes: 7 additions & 0 deletions src/components/Chip/tokens.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* MD3 Chip spec dimensions.
* @see https://m3.material.io/components/chips/specs
*/
export const ChipTokens = {
containerHeight: 32,
} as const;
40 changes: 30 additions & 10 deletions src/components/IconButton/IconButton.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as React from 'react';
import { StyleSheet, View } from 'react-native';
import { Platform, StyleSheet, View } from 'react-native';
import type {
ColorValue,
GestureResponderEvent,
Expand All @@ -12,6 +12,7 @@ import Animated, { type AnimatedStyle } from 'react-native-reanimated';
import { getIconButtonColor } from './utils';
import { useInternalTheme } from '../../core/theming';
import type { ThemeProp } from '../../theme/types';
import getMinInteractiveSizeHitSlop from '../../utils/getMinInteractiveSizeHitSlop';
import ActivityIndicator from '../ActivityIndicator';
import CrossFadeIcon from '../CrossFadeIcon';
import Icon from '../Icon';
Expand Down Expand Up @@ -128,6 +129,7 @@ const IconButton = ({
testID,
loading = false,
contentStyle,
hitSlop,
ref,
...rest
}: Props) => {
Expand All @@ -151,13 +153,25 @@ const IconButton = ({
});

const buttonSize = size + 2 * PADDING;
const borderWidth = mode === 'outlined' && !selected ? 1 : 0;

const shapeStyle = { borderRadius: buttonSize / 2 };

const borderStyles = {
borderWidth: mode === 'outlined' && !selected ? 1 : 0,
borderRadius: buttonSize / 2,
borderWidth,
borderColor,
...shapeStyle,
};

const touchableSize = buttonSize - 2 * borderWidth;

const defaultHitSlop = disabled
? undefined
: getMinInteractiveSizeHitSlop({
width: touchableSize,
height: touchableSize,
});

return (
<Animated.View
ref={ref}
Expand All @@ -178,6 +192,7 @@ const IconButton = ({
style={[
StyleSheet.absoluteFill,
{ backgroundColor, opacity: backgroundOpacity },
shapeStyle,
]}
/>
)}
Expand All @@ -186,16 +201,19 @@ const IconButton = ({
centered
onPress={onPress}
aria-label={ariaLabel}
style={[styles.touchable, contentStyle]}
style={[
styles.touchable,
shapeStyle,
// Clip the ripple to the button shape. Not on web, where
// `overflow: 'hidden'` would also clip the hit slop target.
Platform.OS !== 'web' && styles.clipToShape,
contentStyle,
]}
role="button"
aria-disabled={disabled}
disabled={disabled}
hitSlop={
TouchableRipple.supported
? { top: 10, left: 10, bottom: 10, right: 10 }
: { top: 6, left: 6, bottom: 6, right: 6 }
}
testID={testID}
hitSlop={hitSlop !== undefined ? hitSlop : defaultHitSlop}
{...rest}
>
<View style={{ opacity: iconOpacity }}>
Expand All @@ -213,13 +231,15 @@ const IconButton = ({
const styles = StyleSheet.create({
container: {
margin: 6,
overflow: 'hidden',
},
touchable: {
flexGrow: 1,
justifyContent: 'center',
alignItems: 'center',
},
clipToShape: {
overflow: 'hidden',
},
});

export default IconButton;
7 changes: 6 additions & 1 deletion src/components/SegmentedButtons/SegmentedButtonItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
import { useInternalTheme } from '../../core/theming';
import { useReduceMotion } from '../../theme/accessibility/ReduceMotionContext';
import type { ThemeProp } from '../../theme/types';
import getMinInteractiveSizeHitSlop from '../../utils/getMinInteractiveSizeHitSlop';
import type { IconSource } from '../Icon';
import Icon from '../Icon';
import TouchableRipple from '../TouchableRipple/TouchableRipple';
Expand Down Expand Up @@ -192,6 +193,10 @@ const SegmentedButtonItem = ({
};

const paddingVertical = getSegmentedButtonDensityPadding({ density });
const contentHeight = 2 * paddingVertical + iconSize;
const defaultHitSlop = disabled
? undefined
: getMinInteractiveSizeHitSlop({ height: contentHeight });

const rippleStyle: ViewStyle = {
borderRadius,
Expand All @@ -217,7 +222,7 @@ const SegmentedButtonItem = ({
style={rippleStyle}
background={background}
theme={theme}
hitSlop={hitSlop}
hitSlop={hitSlop !== undefined ? hitSlop : defaultHitSlop}
>
<View
style={[styles.content, { paddingVertical, opacity: textOpacity }]}
Expand Down
10 changes: 10 additions & 0 deletions src/components/TextInput/TextInputIcon.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,17 @@ import { ACCESSORY_SIZE } from './constants';
import { styles } from './styles';
import { getIconColor } from './utils';
import { useInternalTheme } from '../../core/theming';
import getMinInteractiveSizeHitSlop from '../../utils/getMinInteractiveSizeHitSlop';
import IconButton from '../IconButton/IconButton';
import type { Props as IconButtonProps } from '../IconButton/IconButton';

// TextInput shrinks the button to ACCESSORY_SIZE through `style`, which
// IconButton can't see when computing its own slop.
const ACCESSORY_HIT_SLOP = getMinInteractiveSizeHitSlop({
width: ACCESSORY_SIZE,
height: ACCESSORY_SIZE,
});

export type TextInputAccessoryProps = {
style: StyleProp<ViewStyle>;
multiline: boolean;
Expand Down Expand Up @@ -64,6 +72,7 @@ const TextInputIcon = ({
disabled,
theme: themeOverride,
onPress,
hitSlop,
...rest
}: TextInputIconProps) => {
const theme = useInternalTheme(themeOverride);
Expand All @@ -83,6 +92,7 @@ const TextInputIcon = ({
<View style={styles.iconWrapper}>
<IconButton
{...rest}
hitSlop={hitSlop !== undefined ? hitSlop : ACCESSORY_HIT_SLOP}
icon={icon}
iconColor={color}
size={iconSize}
Expand Down
Loading
Loading