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
3 changes: 2 additions & 1 deletion src/components/Appbar/AppbarContent.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as React from 'react';
import { StyleSheet, Pressable, View } from 'react-native';
import { StyleSheet, View } from 'react-native';
import type {
GestureResponderEvent,
LayoutChangeEvent,
Expand All @@ -18,6 +18,7 @@ import {
} from './utils';
import type { ThemeProp } from '../../theme/types';
import { mergeDataSetProps, useFocusRing } from '../../utils/useFocusRing';
import { Pressable } from '../TouchableRipple/Pressable';
import Text from '../Typography/Text';
import type { TextRef } from '../Typography/Text';

Expand Down
3 changes: 2 additions & 1 deletion src/components/Card/Card.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as React from 'react';
import { StyleSheet, Pressable, View } from 'react-native';
import { StyleSheet, View } from 'react-native';
import type {
ColorValue,
GestureResponderEvent,
Expand All @@ -22,6 +22,7 @@ import hasTouchHandler from '../../utils/hasTouchHandler';
import { mergeDataSetProps, useFocusRing } from '../../utils/useFocusRing';
import Surface from '../Surface';
import type { SurfaceStyle } from '../Surface';
import { Pressable } from '../TouchableRipple/Pressable';

type OutlinedCardProps = {
mode: 'outlined';
Expand Down
3 changes: 2 additions & 1 deletion src/components/Chip/Chip.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as React from 'react';
import { Platform, StyleSheet, Pressable, View } from 'react-native';
import { Platform, StyleSheet, View } from 'react-native';
import type {
ColorValue,
GestureResponderEvent,
Expand Down Expand Up @@ -28,6 +28,7 @@ import Icon from '../Icon';
import MaterialCommunityIcon from '../MaterialCommunityIcon';
import Surface from '../Surface';
import type { SurfaceStyle } from '../Surface';
import { Pressable } from '../TouchableRipple/Pressable';
import TouchableRipple from '../TouchableRipple/TouchableRipple';
import type { Props as TouchableRippleProps } from '../TouchableRipple/TouchableRipple';
import Text from '../Typography/Text';
Expand Down
3 changes: 2 additions & 1 deletion src/components/DataTable/DataTableTitle.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as React from 'react';
import { PixelRatio, Pressable, StyleSheet, View } from 'react-native';
import { PixelRatio, StyleSheet, View } from 'react-native';
import type {
GestureResponderEvent,
PressableProps,
Expand All @@ -25,6 +25,7 @@ import hasTouchHandler, {
} from '../../utils/hasTouchHandler';
import { mergeDataSetProps, useFocusRing } from '../../utils/useFocusRing';
import MaterialCommunityIcon from '../MaterialCommunityIcon';
import { Pressable } from '../TouchableRipple/Pressable';
import Text from '../Typography/Text';

export type Props = PressableProps & {
Expand Down
3 changes: 2 additions & 1 deletion src/components/Drawer/DrawerCollapsedItem.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as React from 'react';
import { Platform, Pressable, StyleSheet, View } from 'react-native';
import { Platform, StyleSheet, View } from 'react-native';
import type {
GestureResponderEvent,
StyleProp,
Expand All @@ -25,6 +25,7 @@ import { useFocusRing } from '../../utils/useFocusRing';
import Badge from '../Badge';
import Icon from '../Icon';
import type { IconSource } from '../Icon';
import { Pressable } from '../TouchableRipple/Pressable';
import Text from '../Typography/Text';

export type Props = ViewProps & {
Expand Down
10 changes: 8 additions & 2 deletions src/components/Modal.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import * as React from 'react';
import { Platform, StyleSheet, Pressable, View } from 'react-native';
import {
Platform,
Pressable as NativePressable,
StyleSheet,
View,
} from 'react-native';
import type { StyleProp, ViewStyle } from 'react-native';

import Animated, {
Expand All @@ -12,6 +17,7 @@ import useLatestCallback from 'use-latest-callback';
import Portal from './Portal/Portal';
import Surface from './Surface';
import type { Props as SurfaceProps, SurfaceStyle } from './Surface';
import { Pressable } from './TouchableRipple/Pressable';
import { useInternalTheme } from '../core/theming';
import {
useMotionDuration,
Expand Down Expand Up @@ -139,7 +145,7 @@ const DEFAULT_DURATION = 220;
// Having the handler at all keeps the screen reader escape gesture on this
// modal, instead of passing it on to the modal behind it.
const absorbAccessibilityEscape = () => {};
const AnimatedPressable = Animated.createAnimatedComponent(Pressable);
const AnimatedPressable = Animated.createAnimatedComponent(NativePressable);

/**
* The Modal component is a simple way to present content above an enclosing view.
Expand Down
12 changes: 12 additions & 0 deletions src/components/Portal/PortalInertContext.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import * as React from 'react';

/**
* Whether the subtree sits behind an open modal, such as the screen behind a
* `Dialog`.
*
* On the web the portal layer is `inert`, and `aria-hidden` hides it from
* TalkBack and VoiceOver. On Android neither stops a hardware keyboard or D-pad
* from focusing and pressing views inside it, so Paper's pressables read this
* to step out of keyboard focus themselves.
*/
export const PortalInertContext = React.createContext(false);
10 changes: 9 additions & 1 deletion src/components/Portal/PortalLayer.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,27 @@
import * as React from 'react';
import { Platform, View } from 'react-native';
import type { ViewProps } from 'react-native';

import { PortalInertContext } from './PortalInertContext';

export type Props = ViewProps & {
inert: boolean;
};

export default function PortalLayer({ inert, children, ...rest }: Props) {
// A `Portal.Host` nested in a layer behind a modal is behind it too.
const isInsideInertLayer = React.useContext(PortalInertContext);

return (
<View
aria-hidden={inert || undefined}
// @ts-expect-error: `ViewProps` doesn't expose the web-specific `inert` prop.
inert={Platform.OS === 'web' ? inert || undefined : undefined}
{...rest}
>
{children}
<PortalInertContext.Provider value={isInsideInertLayer || inert}>
{children}
</PortalInertContext.Provider>
</View>
);
}
2 changes: 1 addition & 1 deletion src/components/Switch/Switch.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import * as React from 'react';
import {
Platform,
Pressable,
StyleSheet,
type StyleProp,
View,
Expand Down Expand Up @@ -33,6 +32,7 @@ import { isKeyboardFocusEvent } from '../../utils/isKeyboardFocusEvent';
import { useFocusRing } from '../../utils/useFocusRing';
import { useSpaceKeyPress } from '../../utils/useSpaceKeyPress';
import Icon, { type IconSource } from '../Icon';
import { Pressable } from '../TouchableRipple/Pressable';

export type Props = {
/**
Expand Down
35 changes: 31 additions & 4 deletions src/components/TouchableRipple/Pressable.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import type * as React from 'react';
import * as React from 'react';
import type {
PressableProps as PressableNativeProps,
StyleProp,
View,
ViewStyle,
} from 'react-native';
import { Pressable as PressableNative } from 'react-native';
import { Platform, Pressable as PressableNative } from 'react-native';

import { PortalInertContext } from '../Portal/PortalInertContext';

// This component is added to support type-safe hover and focus states on web
// https://necolas.github.io/react-native-web/docs/pressable/
Expand All @@ -20,7 +22,7 @@ export type PressableProps = Omit<
PressableNativeProps,
'children' | 'style'
> & {
children:
children?:
| React.ReactNode
| ((state: PressableStateCallbackType) => React.ReactNode)
| undefined;
Expand All @@ -30,7 +32,32 @@ export type PressableProps = Omit<
| undefined;
};

// On Android `accessible` is what makes a view keyboard focusable, and
// `focusable` is what lets Enter press it, so both have to go. The layer
// behind a modal is already hidden from TalkBack, so nothing else changes.
const androidInertProps = {
accessible: false,
focusable: false,
} as const;

function PaperPressable({
ref,
...rest
}: PressableNativeProps & {
ref?: React.Ref<React.ComponentRef<typeof View>>;
}) {
const inert = React.useContext(PortalInertContext);

return (
<PressableNative
{...rest}
{...(Platform.OS === 'android' && inert && androidInertProps)}
ref={ref}
/>
);
}

// @ts-expect-error React Native Web adds focused and hovered callback state fields that React Native omits.
export const Pressable: React.ForwardRefExoticComponent<
PressableProps & React.RefAttributes<React.ComponentRef<typeof View>>
> = PressableNative;
> = PaperPressable;
Loading
Loading