diff --git a/src/components/Appbar/AppbarContent.tsx b/src/components/Appbar/AppbarContent.tsx
index 92fae42e7d..f655d1fdfd 100644
--- a/src/components/Appbar/AppbarContent.tsx
+++ b/src/components/Appbar/AppbarContent.tsx
@@ -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,
@@ -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';
diff --git a/src/components/Card/Card.tsx b/src/components/Card/Card.tsx
index 876ba099c0..98b3389471 100644
--- a/src/components/Card/Card.tsx
+++ b/src/components/Card/Card.tsx
@@ -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,
@@ -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';
diff --git a/src/components/Chip/Chip.tsx b/src/components/Chip/Chip.tsx
index 68fb7b70ca..74c143e0cc 100644
--- a/src/components/Chip/Chip.tsx
+++ b/src/components/Chip/Chip.tsx
@@ -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,
@@ -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';
diff --git a/src/components/DataTable/DataTableTitle.tsx b/src/components/DataTable/DataTableTitle.tsx
index 76daec7f8b..f13473829c 100644
--- a/src/components/DataTable/DataTableTitle.tsx
+++ b/src/components/DataTable/DataTableTitle.tsx
@@ -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,
@@ -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 & {
diff --git a/src/components/Drawer/DrawerCollapsedItem.tsx b/src/components/Drawer/DrawerCollapsedItem.tsx
index 257a6a3e54..4ead36346f 100644
--- a/src/components/Drawer/DrawerCollapsedItem.tsx
+++ b/src/components/Drawer/DrawerCollapsedItem.tsx
@@ -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,
@@ -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 & {
diff --git a/src/components/Modal.tsx b/src/components/Modal.tsx
index fa0c7eea6c..a15bc9b2e0 100644
--- a/src/components/Modal.tsx
+++ b/src/components/Modal.tsx
@@ -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, {
@@ -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,
@@ -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.
diff --git a/src/components/Portal/PortalInertContext.ts b/src/components/Portal/PortalInertContext.ts
new file mode 100644
index 0000000000..2a8778293a
--- /dev/null
+++ b/src/components/Portal/PortalInertContext.ts
@@ -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);
diff --git a/src/components/Portal/PortalLayer.tsx b/src/components/Portal/PortalLayer.tsx
index 543685eded..1e4f545328 100644
--- a/src/components/Portal/PortalLayer.tsx
+++ b/src/components/Portal/PortalLayer.tsx
@@ -1,11 +1,17 @@
+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 (
- {children}
+
+ {children}
+
);
}
diff --git a/src/components/Switch/Switch.tsx b/src/components/Switch/Switch.tsx
index c1024f29f0..ab6589b9ed 100644
--- a/src/components/Switch/Switch.tsx
+++ b/src/components/Switch/Switch.tsx
@@ -1,7 +1,6 @@
import * as React from 'react';
import {
Platform,
- Pressable,
StyleSheet,
type StyleProp,
View,
@@ -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 = {
/**
diff --git a/src/components/TouchableRipple/Pressable.tsx b/src/components/TouchableRipple/Pressable.tsx
index c68e2567be..e471e9cef7 100644
--- a/src/components/TouchableRipple/Pressable.tsx
+++ b/src/components/TouchableRipple/Pressable.tsx
@@ -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/
@@ -20,7 +22,7 @@ export type PressableProps = Omit<
PressableNativeProps,
'children' | 'style'
> & {
- children:
+ children?:
| React.ReactNode
| ((state: PressableStateCallbackType) => React.ReactNode)
| undefined;
@@ -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>;
+}) {
+ const inert = React.useContext(PortalInertContext);
+
+ return (
+
+ );
+}
+
// @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>
-> = PressableNative;
+> = PaperPressable;
diff --git a/src/components/__tests__/PortalKeyboardFocus.test.tsx b/src/components/__tests__/PortalKeyboardFocus.test.tsx
new file mode 100644
index 0000000000..b863317541
--- /dev/null
+++ b/src/components/__tests__/PortalKeyboardFocus.test.tsx
@@ -0,0 +1,199 @@
+import { Platform } from 'react-native';
+
+import { afterEach, beforeEach, describe, expect, it } from '@jest/globals';
+
+import { render, screen } from '../../test-utils';
+import Button from '../Button/Button';
+import Card from '../Card/Card';
+import Chip from '../Chip/Chip';
+import Dialog from '../Dialog/Dialog';
+import Portal from '../Portal/Portal';
+
+const noop = () => {};
+
+const okAction = (
+
+);
+
+describe('on Android', () => {
+ const originalOS = Platform.OS;
+
+ beforeEach(() => {
+ Platform.OS = 'android';
+ });
+
+ afterEach(() => {
+ Platform.OS = originalOS;
+ });
+
+ it('takes the buttons behind an open dialog out of keyboard focus', async () => {
+ await render(
+
+
+
+
+ );
+
+ const behind = screen.getByTestId('behind', {
+ includeHiddenElements: true,
+ });
+
+ // `accessible` makes a view keyboard focusable on Android, and
+ // `focusable` lets Enter press it.
+ expect(behind).toHaveProp('accessible', false);
+ expect(behind).toHaveProp('focusable', false);
+ });
+
+ it("keeps the dialog's own buttons in keyboard focus", async () => {
+ await render(
+
+
+
+
+ );
+
+ const ok = screen.getByTestId('ok');
+
+ expect(ok).toHaveProp('accessible', true);
+ expect(ok).toHaveProp('focusable', true);
+ });
+
+ it('covers Paper controls that render their own pressable', async () => {
+ await render(
+
+
+ Chip
+
+
+
+
+
+
+ );
+
+ for (const testID of ['chip', 'card']) {
+ const control = screen.getByTestId(testID, {
+ includeHiddenElements: true,
+ });
+
+ expect(control).toHaveProp('focusable', false);
+ }
+ });
+
+ it('gives the buttons behind keyboard focus back once the dialog is gone', async () => {
+ const { rerender } = await render(
+
+
+
+
+ );
+
+ await rerender(
+
+
+
+ );
+
+ const behind = screen.getByTestId('behind');
+
+ expect(behind).toHaveProp('accessible', true);
+ expect(behind).toHaveProp('focusable', true);
+ });
+
+ it('takes a dialog under another dialog out of keyboard focus', async () => {
+ await render(
+
+
+ );
+
+ expect(
+ screen.getByTestId('lower', { includeHiddenElements: true })
+ ).toHaveProp('focusable', false);
+ expect(screen.getByTestId('upper')).toHaveProp('focusable', true);
+ });
+
+ it('covers a Portal.Host nested in the content behind the dialog', async () => {
+ await render(
+
+
+
+
+
+
+ );
+
+ expect(
+ screen.getByTestId('nested', { includeHiddenElements: true })
+ ).toHaveProp('focusable', false);
+ });
+
+ it('leaves the content behind a portal that is not a modal in keyboard focus', async () => {
+ await render(
+
+
+
+
+
+
+ );
+
+ expect(screen.getByTestId('behind')).toHaveProp('focusable', true);
+ });
+});
+
+it('leaves keyboard focus behind a dialog to the platform on iOS', async () => {
+ const originalOS = Platform.OS;
+ Platform.OS = 'ios';
+
+ try {
+ await render(
+
+
+
+
+ );
+
+ const behind = screen.getByTestId('behind', {
+ includeHiddenElements: true,
+ });
+
+ expect(behind).toHaveProp('accessible', true);
+ expect(behind).toHaveProp('focusable', true);
+ } finally {
+ Platform.OS = originalOS;
+ }
+});