@delacour/react-native-ui 0.11.0-alpha.20261124165126 → 0.11.0-alpha.20261124171114
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +3 -3
- package/src/components/flip-card/AGENTS.md +20 -9
- package/src/components/flip-card/flip-card-face.tsx +52 -7
- package/src/components/flip-card/flip-card.render.test.tsx +92 -23
- package/src/components/flip-card/flip-card.tsx +3 -13
- package/src/components/flip-card/flip-card.variants.test.ts +18 -0
- package/src/components/flip-card/flip-card.variants.ts +17 -0
- package/src/components/menu/AGENTS.md +18 -6
- package/src/components/menu/menu-content.tsx +18 -5
- package/src/components/menu/menu-item.tsx +17 -3
- package/src/components/menu/menu-radio-item.tsx +1 -1
- package/src/components/menu/menu.context.tsx +16 -3
- package/src/components/menu/menu.render.test.tsx +57 -13
- package/src/components/menu/menu.variants.test.ts +19 -2
- package/src/components/menu/menu.variants.ts +15 -2
- package/src/gesture-activation.test.ts +121 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@delacour/react-native-ui",
|
|
3
|
-
"version": "0.11.0-alpha.
|
|
3
|
+
"version": "0.11.0-alpha.20261124171114",
|
|
4
4
|
"description": "React Native UI components — Uniwind, Reanimated, Gesture Handler",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -186,8 +186,8 @@
|
|
|
186
186
|
"expo-blur": ">=15",
|
|
187
187
|
"@maplibre/maplibre-react-native": "^11.5.0",
|
|
188
188
|
"@central-icons-react-native/round-outlined-radius-1-stroke-1.5": "^1.1",
|
|
189
|
-
"@delacour/react-native-bottom-sheet": "0.11.0-alpha.
|
|
190
|
-
"@delacour/react-native-charts": "0.11.0-alpha.
|
|
189
|
+
"@delacour/react-native-bottom-sheet": "0.11.0-alpha.20261124171114",
|
|
190
|
+
"@delacour/react-native-charts": "0.11.0-alpha.20261124171114",
|
|
191
191
|
"@legendapp/list": ">=3.3",
|
|
192
192
|
"react-native-skia": ">=3.9.0",
|
|
193
193
|
"expo-linear-gradient": ">=15",
|
|
@@ -77,15 +77,26 @@ the back, a profile with details behind it. A compound root plus `FlipCard.Front
|
|
|
77
77
|
card, so a `FlipCard.Trigger` written beside the faces would be covered by it; the root splits
|
|
78
78
|
its children and stacks the rest under the card with a gap. A face must be a direct child to be
|
|
79
79
|
recognised — wrapped in a fragment or a component of its own, it lands beneath the card.
|
|
80
|
-
- **Screen readers always have a non-gesture path.** The card is a container
|
|
81
|
-
(`accessible={false}`)
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
80
|
+
- **Screen readers always have a non-gesture path.** The card root is a container
|
|
81
|
+
(`accessible={false}`) and lists no action — a container is never focused — so controls on its
|
|
82
|
+
faces stay reachable one by one. `resolveFaceMode` decides each face's part:
|
|
83
|
+
- **`"press"`** — the visible face stays a container, and its first child is a transparent,
|
|
84
|
+
absolutely-filling, `pointerEvents="none"` accessible **"Flip card" button** (hint "Shows the
|
|
85
|
+
back" / "Shows the front"). It sits behind the content, so explore-by-touch over a control
|
|
86
|
+
lands on the control. Its activation goes through `resolvePressableActivation`:
|
|
87
|
+
`onAccessibilityTap` on iOS, an `activate` action on Android. A disabled card keeps the
|
|
88
|
+
button, marks it disabled and consumes the activation without flipping. A hidden face has no
|
|
89
|
+
button.
|
|
90
|
+
- **`"swipe"`** — has no tap, and iOS only runs an action on an accessibility element, so the
|
|
91
|
+
**visible face** becomes one element holding `activate` ("Flip card") and the card's
|
|
92
|
+
`accessibilityLabel`; its content still reads as its label. Controls on a swipe face are
|
|
93
|
+
grouped into it, so use a `FlipCard.Trigger` beside the card to reach one separately. A
|
|
94
|
+
disabled card's face is announced disabled, lists no action and ignores `activate`.
|
|
95
|
+
- **`"manual"`** — leaves the path to the author's `FlipCard.Trigger`.
|
|
96
|
+
|
|
97
|
+
`resolveFlipCardActions` is tested to never return nothing for a gesture trigger. After a turn
|
|
98
|
+
settles, `AccessibilityInfo.announceForAccessibility` speaks `faceLabels[face]`; the card never
|
|
99
|
+
moves focus itself — `onFaceChange` is where an author does.
|
|
89
100
|
- **`FlipCard.Trigger` is a `Pressable` under every trigger.** A "Flip" button beneath a swipe
|
|
90
101
|
card is the accessible alternative to dragging. With no children it is an icon-only square on
|
|
91
102
|
the button scale with Central Icons' `IconArrowRotateLeftRight`, labelled "Flip card"; its hint
|
|
@@ -1,17 +1,22 @@
|
|
|
1
1
|
import type { ReactElement } from "react";
|
|
2
|
-
import type
|
|
2
|
+
import { type AccessibilityActionEvent, Platform, View } from "react-native";
|
|
3
3
|
import Animated, { useAnimatedStyle } from "react-native-reanimated";
|
|
4
|
+
import { dispatchPressableAccessibilityAction, resolvePressableActivation } from "../pressable/pressable.variants";
|
|
4
5
|
import { Surface } from "../surface";
|
|
5
6
|
import { useFlipCardPart } from "./flip-card.context";
|
|
6
7
|
import type { FlipCardFaceProps } from "./flip-card.types";
|
|
7
8
|
import {
|
|
9
|
+
FLIP_CARD_ACTION_LABEL,
|
|
8
10
|
type FlipCardFace,
|
|
9
11
|
faceRotation,
|
|
12
|
+
oppositeFace,
|
|
10
13
|
resolveFaceA11y,
|
|
14
|
+
resolveFaceMode,
|
|
11
15
|
resolveFaceOpacity,
|
|
12
16
|
resolveFaceTransform,
|
|
13
17
|
resolveFlipCardActions,
|
|
14
18
|
resolveFlipCardClasses,
|
|
19
|
+
resolveFlipCardTriggerHint,
|
|
15
20
|
} from "./flip-card.variants";
|
|
16
21
|
|
|
17
22
|
/** Set on both faces. Nothing depends on it — the angle decides visibility — but iOS honours it for free. */
|
|
@@ -28,8 +33,12 @@ const BACKFACE_STYLE = { backfaceVisibility: "hidden" } as const;
|
|
|
28
33
|
*
|
|
29
34
|
* Under `trigger="swipe"` the visible face is one accessibility element holding
|
|
30
35
|
* the "Flip card" action, because a drag has no screen-reader equivalent and an
|
|
31
|
-
* action needs an element to live on. Under
|
|
32
|
-
*
|
|
36
|
+
* action needs an element to live on. Under `"press"` it stays a container, so a
|
|
37
|
+
* button on a face is reached on its own, and a transparent "Flip card" button
|
|
38
|
+
* is its first child — behind the content, so explore-by-touch over a control
|
|
39
|
+
* still lands on the control. `resolvePressableActivation` routes its
|
|
40
|
+
* activation: `onAccessibilityTap` on iOS, an `activate` action on Android.
|
|
41
|
+
* Under `"manual"` the face is a plain container.
|
|
33
42
|
*
|
|
34
43
|
* Internal — rendered by `FlipCard.Front` and `FlipCard.Back`, never exported.
|
|
35
44
|
*/
|
|
@@ -40,12 +49,13 @@ export function FlipCardFaceBody({
|
|
|
40
49
|
className,
|
|
41
50
|
children,
|
|
42
51
|
}: FlipCardFaceProps & { side: FlipCardFace }): ReactElement {
|
|
43
|
-
const { accessibilityLabel, axis, face, flip, isCalm, perspective, rotation, trigger } = useFlipCardPart(
|
|
52
|
+
const { accessibilityLabel, axis, face, flip, isCalm, isDisabled, perspective, rotation, trigger } = useFlipCardPart(
|
|
44
53
|
side === "front" ? "FlipCard.Front" : "FlipCard.Back"
|
|
45
54
|
);
|
|
46
55
|
const classes = resolveFlipCardClasses({ className, face: side, padding, variant });
|
|
47
56
|
const isHidden = resolveFaceA11y(face)[side].hidden;
|
|
48
|
-
const
|
|
57
|
+
const mode = resolveFaceMode({ hidden: isHidden, trigger });
|
|
58
|
+
const isElement = mode === "grouped";
|
|
49
59
|
const rest = faceRotation(side);
|
|
50
60
|
|
|
51
61
|
const animatedStyle = useAnimatedStyle(() => ({
|
|
@@ -54,14 +64,35 @@ export function FlipCardFaceBody({
|
|
|
54
64
|
}));
|
|
55
65
|
|
|
56
66
|
const handleAction = (event: AccessibilityActionEvent) => {
|
|
57
|
-
if (event.nativeEvent.actionName === "activate") flip();
|
|
67
|
+
if (event.nativeEvent.actionName === "activate" && !isDisabled) flip();
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
const activation = resolvePressableActivation({
|
|
71
|
+
hasAccessibilityTap: false,
|
|
72
|
+
hasPress: true,
|
|
73
|
+
platform: Platform.OS,
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
const handleFlipButtonAction = (event: AccessibilityActionEvent) => {
|
|
77
|
+
dispatchPressableAccessibilityAction({
|
|
78
|
+
actionName: event.nativeEvent.actionName,
|
|
79
|
+
activation,
|
|
80
|
+
busy: false,
|
|
81
|
+
disabled: isDisabled,
|
|
82
|
+
onPress: flip,
|
|
83
|
+
});
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
const handleFlipButtonTap = () => {
|
|
87
|
+
if (!isDisabled) flip();
|
|
58
88
|
};
|
|
59
89
|
|
|
60
90
|
return (
|
|
61
91
|
<Animated.View
|
|
62
|
-
accessibilityActions={isElement ? resolveFlipCardActions(trigger) : undefined}
|
|
92
|
+
accessibilityActions={isElement ? (isDisabled ? [] : resolveFlipCardActions(trigger)) : undefined}
|
|
63
93
|
accessibilityElementsHidden={isHidden}
|
|
64
94
|
accessibilityLabel={isElement ? accessibilityLabel : undefined}
|
|
95
|
+
accessibilityState={isElement ? { disabled: isDisabled } : undefined}
|
|
65
96
|
accessible={isElement}
|
|
66
97
|
className={classes.wrapper}
|
|
67
98
|
importantForAccessibility={isHidden ? "no-hide-descendants" : "auto"}
|
|
@@ -69,6 +100,20 @@ export function FlipCardFaceBody({
|
|
|
69
100
|
pointerEvents={isHidden ? "none" : "auto"}
|
|
70
101
|
style={[BACKFACE_STYLE, animatedStyle]}
|
|
71
102
|
>
|
|
103
|
+
{mode === "flip-button" ? (
|
|
104
|
+
<View
|
|
105
|
+
accessibilityActions={activation.accessibilityActions}
|
|
106
|
+
accessibilityHint={resolveFlipCardTriggerHint(oppositeFace(face))}
|
|
107
|
+
accessibilityLabel={FLIP_CARD_ACTION_LABEL}
|
|
108
|
+
accessibilityRole="button"
|
|
109
|
+
accessibilityState={{ disabled: isDisabled }}
|
|
110
|
+
accessible
|
|
111
|
+
className="absolute inset-0"
|
|
112
|
+
onAccessibilityAction={handleFlipButtonAction}
|
|
113
|
+
onAccessibilityTap={activation.wiresAccessibilityTap ? handleFlipButtonTap : undefined}
|
|
114
|
+
pointerEvents="none"
|
|
115
|
+
/>
|
|
116
|
+
) : null}
|
|
72
117
|
<Surface className={classes.surface} padding={classes.padding} variant={classes.variant}>
|
|
73
118
|
{children}
|
|
74
119
|
</Surface>
|
|
@@ -1,12 +1,26 @@
|
|
|
1
1
|
import { describe, expect, jest, test } from "@jest/globals";
|
|
2
2
|
import { createRef } from "react";
|
|
3
|
-
import { Text } from "react-native";
|
|
3
|
+
import { Platform, Pressable, Text } from "react-native";
|
|
4
4
|
import { act, fireEvent, renderWithProviders, screen } from "../../test/render";
|
|
5
5
|
import { FlipCard, type FlipCardHandle, type FlipCardProps } from "./flip-card";
|
|
6
6
|
import { useFlipCard } from "./flip-card.context";
|
|
7
7
|
|
|
8
8
|
const activate = { nativeEvent: { actionName: "activate" } };
|
|
9
9
|
|
|
10
|
+
function flipButton() {
|
|
11
|
+
return screen.getByRole("button", { name: "Flip card" });
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
async function withPlatform(os: "ios" | "android", run: () => Promise<void>) {
|
|
15
|
+
const original = Platform.OS;
|
|
16
|
+
Platform.OS = os;
|
|
17
|
+
try {
|
|
18
|
+
await run();
|
|
19
|
+
} finally {
|
|
20
|
+
Platform.OS = original;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
10
24
|
function Faces() {
|
|
11
25
|
return (
|
|
12
26
|
<>
|
|
@@ -51,35 +65,70 @@ describe("FlipCard", () => {
|
|
|
51
65
|
expectBack();
|
|
52
66
|
});
|
|
53
67
|
|
|
54
|
-
test("
|
|
68
|
+
test("press: the Flip card button turns the card on Android's activate", async () => {
|
|
55
69
|
const onFaceChange = jest.fn();
|
|
56
|
-
await
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
70
|
+
await withPlatform("android", async () => {
|
|
71
|
+
await renderWithProviders(<Card onFaceChange={onFaceChange} />);
|
|
72
|
+
expect(flipButton()).toHaveProp("accessibilityActions", [{ name: "activate" }]);
|
|
73
|
+
expect(flipButton()).not.toHaveProp("onAccessibilityTap");
|
|
74
|
+
expect(flipButton()).toHaveProp("accessibilityHint", "Shows the back");
|
|
75
|
+
|
|
76
|
+
await fireEvent(flipButton(), "accessibilityAction", activate);
|
|
77
|
+
expectBack();
|
|
78
|
+
expect(onFaceChange).toHaveBeenLastCalledWith("back");
|
|
79
|
+
|
|
80
|
+
await fireEvent(flipButton(), "accessibilityAction", activate);
|
|
81
|
+
expectFront();
|
|
82
|
+
expect(onFaceChange).toHaveBeenCalledTimes(2);
|
|
83
|
+
});
|
|
84
|
+
});
|
|
61
85
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
86
|
+
test("press: VoiceOver's double-tap on the Flip card button turns the card on iOS", async () => {
|
|
87
|
+
const onFaceChange = jest.fn();
|
|
88
|
+
await withPlatform("ios", async () => {
|
|
89
|
+
await renderWithProviders(<Card onFaceChange={onFaceChange} />);
|
|
90
|
+
expect(flipButton()).not.toHaveProp("accessibilityActions", expect.arrayContaining([{ name: "activate" }]));
|
|
91
|
+
await fireEvent(flipButton(), "accessibilityTap");
|
|
92
|
+
expectBack();
|
|
93
|
+
expect(onFaceChange).toHaveBeenCalledWith("back");
|
|
94
|
+
});
|
|
66
95
|
});
|
|
67
96
|
|
|
68
|
-
test("
|
|
97
|
+
test("press: the root and the face stay containers; only the button is a new element", async () => {
|
|
69
98
|
const view = await renderWithProviders(<Card />);
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
99
|
+
const root = screen.getByLabelText("Flashcard");
|
|
100
|
+
expect(root).toHaveProp("accessible", false);
|
|
101
|
+
expect(root).not.toHaveProp("accessibilityActions");
|
|
102
|
+
expect(flipButton()).toHaveProp("pointerEvents", "none");
|
|
73
103
|
|
|
74
104
|
await view.rerender(<Card trigger="manual" />);
|
|
75
|
-
expect(screen.
|
|
105
|
+
expect(screen.queryByRole("button", { name: "Flip card" })).not.toBeOnTheScreen();
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
test("press: a control on a face is still its own element, and only the visible face has the button", async () => {
|
|
109
|
+
const onCopy = jest.fn();
|
|
110
|
+
await renderWithProviders(
|
|
111
|
+
<FlipCard accessibilityLabel="Flashcard" defaultFace="back">
|
|
112
|
+
<FlipCard.Front>
|
|
113
|
+
<Text>Bonjour</Text>
|
|
114
|
+
</FlipCard.Front>
|
|
115
|
+
<FlipCard.Back>
|
|
116
|
+
<Pressable accessibilityRole="button" onPress={onCopy}>
|
|
117
|
+
<Text>Copy</Text>
|
|
118
|
+
</Pressable>
|
|
119
|
+
</FlipCard.Back>
|
|
120
|
+
</FlipCard>
|
|
121
|
+
);
|
|
122
|
+
expect(screen.getByRole("button", { name: "Copy" })).toBeOnTheScreen();
|
|
123
|
+
expect(screen.getAllByRole("button")).toHaveLength(2);
|
|
124
|
+
expect(flipButton()).toHaveProp("accessibilityHint", "Shows the front");
|
|
76
125
|
});
|
|
77
126
|
|
|
78
127
|
test("controlled: reports the turn and waits for the face prop", async () => {
|
|
79
128
|
const onFaceChange = jest.fn();
|
|
80
129
|
const view = await renderWithProviders(<Card face="front" onFaceChange={onFaceChange} />);
|
|
81
130
|
|
|
82
|
-
await fireEvent(
|
|
131
|
+
await fireEvent(flipButton(), "accessibilityTap");
|
|
83
132
|
expect(onFaceChange).toHaveBeenCalledWith("back");
|
|
84
133
|
expectFront();
|
|
85
134
|
|
|
@@ -87,14 +136,23 @@ describe("FlipCard", () => {
|
|
|
87
136
|
expectBack();
|
|
88
137
|
});
|
|
89
138
|
|
|
90
|
-
test("isDisabled is announced
|
|
139
|
+
test("isDisabled is announced and the Flip card button consumes Android's activate without flipping", async () => {
|
|
91
140
|
const onFaceChange = jest.fn();
|
|
92
|
-
await
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
141
|
+
await withPlatform("android", async () => {
|
|
142
|
+
await renderWithProviders(<Card isDisabled onFaceChange={onFaceChange} />);
|
|
143
|
+
expect(flipButton()).toBeDisabled();
|
|
144
|
+
await fireEvent(flipButton(), "accessibilityAction", activate);
|
|
145
|
+
});
|
|
146
|
+
expect(onFaceChange).not.toHaveBeenCalled();
|
|
147
|
+
expectFront();
|
|
148
|
+
});
|
|
96
149
|
|
|
97
|
-
|
|
150
|
+
test("a disabled card consumes VoiceOver's double-tap without flipping", async () => {
|
|
151
|
+
const onFaceChange = jest.fn();
|
|
152
|
+
await withPlatform("ios", async () => {
|
|
153
|
+
await renderWithProviders(<Card isDisabled onFaceChange={onFaceChange} />);
|
|
154
|
+
await fireEvent(flipButton(), "accessibilityTap");
|
|
155
|
+
});
|
|
98
156
|
expect(onFaceChange).not.toHaveBeenCalled();
|
|
99
157
|
expectFront();
|
|
100
158
|
});
|
|
@@ -132,6 +190,17 @@ describe("FlipCard", () => {
|
|
|
132
190
|
expectBack();
|
|
133
191
|
});
|
|
134
192
|
|
|
193
|
+
test("swipe: a disabled card's face lists no action and ignores activate", async () => {
|
|
194
|
+
const onFaceChange = jest.fn();
|
|
195
|
+
await renderWithProviders(<Card isDisabled trigger="swipe" onFaceChange={onFaceChange} />);
|
|
196
|
+
|
|
197
|
+
const [, front] = screen.getAllByLabelText("Flashcard");
|
|
198
|
+
expect(front).toHaveProp("accessibilityActions", []);
|
|
199
|
+
expect(front).toBeDisabled();
|
|
200
|
+
await fireEvent(front, "accessibilityAction", activate);
|
|
201
|
+
expect(onFaceChange).not.toHaveBeenCalled();
|
|
202
|
+
});
|
|
203
|
+
|
|
135
204
|
test("FlipCard.Trigger turns to the other face and names itself", async () => {
|
|
136
205
|
const onFaceChange = jest.fn();
|
|
137
206
|
await renderWithProviders(
|
|
@@ -41,7 +41,6 @@ import {
|
|
|
41
41
|
flipCardVariants,
|
|
42
42
|
isFlipCardFaceName,
|
|
43
43
|
oppositeFace,
|
|
44
|
-
resolveFlipCardActions,
|
|
45
44
|
resolveFlipTarget,
|
|
46
45
|
resolveRubberBand,
|
|
47
46
|
resolveSwipeVelocity,
|
|
@@ -347,13 +346,6 @@ function FlipCardRoot(props: FlipCardProps): ReactElement {
|
|
|
347
346
|
[height, width]
|
|
348
347
|
);
|
|
349
348
|
|
|
350
|
-
const handleAccessibilityAction = useCallback(
|
|
351
|
-
(event: { nativeEvent: { actionName: string } }) => {
|
|
352
|
-
if (event.nativeEvent.actionName === "activate" && !isDisabled) flip();
|
|
353
|
-
},
|
|
354
|
-
[flip, isDisabled]
|
|
355
|
-
);
|
|
356
|
-
|
|
357
349
|
const context = useMemo<FlipCardContextValue>(
|
|
358
350
|
() => ({
|
|
359
351
|
accessibilityLabel,
|
|
@@ -373,19 +365,17 @@ function FlipCardRoot(props: FlipCardProps): ReactElement {
|
|
|
373
365
|
|
|
374
366
|
const { faces, rest } = splitFlipCardChildren(children);
|
|
375
367
|
const slots = flipCardVariants();
|
|
376
|
-
const actions = isDisabled ? [] : resolveFlipCardActions(trigger);
|
|
377
368
|
|
|
378
369
|
// The card is a container (`accessible={false}`) so the controls on its faces
|
|
379
|
-
// stay reachable one by one
|
|
380
|
-
//
|
|
370
|
+
// stay reachable one by one, and a container is never focused, so it lists no
|
|
371
|
+
// action. A press card's activation is a "Flip card" button inside its visible
|
|
372
|
+
// face; a swipe card's is its grouped visible face — see `FlipCardFaceBody`.
|
|
381
373
|
const card = (
|
|
382
374
|
<Animated.View
|
|
383
|
-
accessibilityActions={actions}
|
|
384
375
|
accessibilityLabel={accessibilityLabel}
|
|
385
376
|
accessibilityState={{ disabled: isDisabled }}
|
|
386
377
|
accessible={false}
|
|
387
378
|
className={slots.root({ className })}
|
|
388
|
-
onAccessibilityAction={handleAccessibilityAction}
|
|
389
379
|
onLayout={handleLayout}
|
|
390
380
|
style={pressStyle}
|
|
391
381
|
{...viewProps}
|
|
@@ -18,6 +18,7 @@ import {
|
|
|
18
18
|
normalizeRotation,
|
|
19
19
|
oppositeFace,
|
|
20
20
|
resolveFaceA11y,
|
|
21
|
+
resolveFaceMode,
|
|
21
22
|
resolveFaceOpacity,
|
|
22
23
|
resolveFaceTransform,
|
|
23
24
|
resolveFlipCardActions,
|
|
@@ -324,6 +325,23 @@ describe("resolveFlipCardActions", () => {
|
|
|
324
325
|
});
|
|
325
326
|
});
|
|
326
327
|
|
|
328
|
+
describe("resolveFaceMode", () => {
|
|
329
|
+
test("press: the visible face stays a container and gets a Flip card button behind its content", () => {
|
|
330
|
+
expect(resolveFaceMode({ hidden: false, trigger: "press" })).toBe("flip-button");
|
|
331
|
+
});
|
|
332
|
+
|
|
333
|
+
test("swipe: the visible face is one grouped element", () => {
|
|
334
|
+
expect(resolveFaceMode({ hidden: false, trigger: "swipe" })).toBe("grouped");
|
|
335
|
+
});
|
|
336
|
+
|
|
337
|
+
test("manual and any hidden face are plain containers", () => {
|
|
338
|
+
expect(resolveFaceMode({ hidden: false, trigger: "manual" })).toBe("container");
|
|
339
|
+
for (const trigger of FLIP_CARD_TRIGGERS) {
|
|
340
|
+
expect(resolveFaceMode({ hidden: true, trigger })).toBe("container");
|
|
341
|
+
}
|
|
342
|
+
});
|
|
343
|
+
});
|
|
344
|
+
|
|
327
345
|
describe("FLIP_CARD_MOTION", () => {
|
|
328
346
|
test("snappy is stiffer than default, which is stiffer than gentle", () => {
|
|
329
347
|
expect(FLIP_CARD_MOTION.snappy.stiffness).toBeGreaterThan(FLIP_CARD_MOTION.default.stiffness);
|
|
@@ -325,6 +325,23 @@ export function resolveFlipCardActions(trigger: FlipCardTrigger): Array<{ name:
|
|
|
325
325
|
return [{ label: FLIP_CARD_ACTION_LABEL, name: "activate" }];
|
|
326
326
|
}
|
|
327
327
|
|
|
328
|
+
/**
|
|
329
|
+
* How a face presents itself to a screen reader.
|
|
330
|
+
*
|
|
331
|
+
* - `"flip-button"` — `trigger="press"`'s visible face. The face stays a
|
|
332
|
+
* container so a control on it is reached on its own, and a transparent
|
|
333
|
+
* "Flip card" button sits behind its content as the card's activation.
|
|
334
|
+
* - `"grouped"` — `trigger="swipe"`'s visible face: one element holding the
|
|
335
|
+
* `activate` action.
|
|
336
|
+
* - `"container"` — a hidden face, and every face under `"manual"`.
|
|
337
|
+
*/
|
|
338
|
+
export type FlipCardFaceMode = "container" | "flip-button" | "grouped";
|
|
339
|
+
|
|
340
|
+
export function resolveFaceMode({ hidden, trigger }: { hidden: boolean; trigger: FlipCardTrigger }): FlipCardFaceMode {
|
|
341
|
+
if (hidden || trigger === "manual") return "container";
|
|
342
|
+
return trigger === "press" ? "flip-button" : "grouped";
|
|
343
|
+
}
|
|
344
|
+
|
|
328
345
|
/** What `FlipCard.Trigger` tells a screen reader a press will do. */
|
|
329
346
|
export function resolveFlipCardTriggerHint(destination: FlipCardFace): string {
|
|
330
347
|
return destination === "back" ? "Shows the back" : "Shows the front";
|
|
@@ -30,8 +30,8 @@ rows run actions, they never become the trigger's value. Compound root plus
|
|
|
30
30
|
| `menu.context.tsx` | The menu, submenu and radio-group contexts; `useMenu`, `useMenuRootValue`, `MenuRootProvider` |
|
|
31
31
|
| `menu.types.ts` | `MenuItemProps`, shared by every row |
|
|
32
32
|
| `menu.variants.ts` | The slotted `tv()`, the constants and the pure resolvers — no RN imports |
|
|
33
|
-
| `menu.render.test.tsx` | Mounted under Jest — open/close, items, disabled rows, checkbox and radio state, submenus, outside-part errors, Android `activate` |
|
|
34
|
-
| `menu.variants.test.ts` | Placement (flip, cap, clamp, RTL, the zero-size anchor), width, close rules, accessibility, tokens |
|
|
33
|
+
| `menu.render.test.tsx` | Mounted under Jest — open/close, items, disabled rows, checkbox and radio state, submenus, the accessibility escape, outside-part errors, Android `activate` |
|
|
34
|
+
| `menu.variants.test.ts` | Placement (flip, cap, clamp, RTL, the zero-size anchor), width, close rules, accessibility state and row roles, tokens |
|
|
35
35
|
|
|
36
36
|
## The panel
|
|
37
37
|
|
|
@@ -143,10 +143,22 @@ rows run actions, they never become the trigger's value. Compound root plus
|
|
|
143
143
|
## Accessibility
|
|
144
144
|
|
|
145
145
|
- Rows are activated by a screen reader too: `MenuRow` wires `onAccessibilityTap` (iOS) and an `activate` action (Android) to the same select as the tap gesture, behind the disabled guard, via `resolvePressableActivation`.
|
|
146
|
-
- Content: `accessibilityRole="menu"`, `accessibilityViewIsModal
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
146
|
+
- Content: `accessibilityRole="menu"`, `accessibilityViewIsModal`, and
|
|
147
|
+
`onAccessibilityEscape` — the VoiceOver scrub closes the menu like a tap
|
|
148
|
+
outside does (a caller's own handler runs after). Android's back button goes
|
|
149
|
+
through the `Modal`'s `onRequestClose`.
|
|
150
|
+
- **Focus moves in and comes back.** Once the panel has entered, screen-reader
|
|
151
|
+
focus moves to the first row; once it has gone, back to the trigger, if it is
|
|
152
|
+
still mounted. The first row to mount claims `firstRowRef` in the root
|
|
153
|
+
context and gives it up on unmount, so ContextMenu, which builds the same
|
|
154
|
+
value, gets both for free. Without it VoiceOver could stay on the trigger
|
|
155
|
+
behind a modal panel, and closing left focus wherever the platform put it.
|
|
156
|
+
- Rows: `accessibilityRole="menuitem"`, except a radio row, which is a
|
|
157
|
+
`radio` (`resolveMenuRowRole`). React Native has no `menuitemradio`, and a
|
|
158
|
+
`menuitem` announcing `selected` reads as a highlighted row rather than one
|
|
159
|
+
choice of several. Checkbox and radio → `checked`, sub-trigger → `expanded`,
|
|
160
|
+
all → `disabled`. One source: `resolveMenuItemAccessibility`, a
|
|
161
|
+
discriminated union on `kind`.
|
|
150
162
|
- Trigger: `accessibilityState={{ expanded }}`, hint "Opens a menu".
|
|
151
163
|
- A closed submenu is out of the accessibility tree, not just clipped.
|
|
152
164
|
|
|
@@ -14,7 +14,7 @@ import Animated, { useAnimatedStyle, useReducedMotion, useSharedValue, withTimin
|
|
|
14
14
|
import { useSafeAreaInsets } from "react-native-safe-area-context";
|
|
15
15
|
import { scheduleOnRN } from "react-native-worklets";
|
|
16
16
|
import { Pressable } from "../pressable";
|
|
17
|
-
import { useMenuPart } from "./menu.context";
|
|
17
|
+
import { focusMenuTarget, useMenuPart } from "./menu.context";
|
|
18
18
|
import {
|
|
19
19
|
MENU_DEFAULT_OFFSET,
|
|
20
20
|
MENU_EDGE_MARGIN,
|
|
@@ -95,9 +95,10 @@ export function MenuContent({
|
|
|
95
95
|
hasScrim = false,
|
|
96
96
|
anchor: anchorProp,
|
|
97
97
|
backdrop,
|
|
98
|
+
onAccessibilityEscape,
|
|
98
99
|
...props
|
|
99
100
|
}: MenuContentProps): ReactElement | null {
|
|
100
|
-
const { anchor: contextAnchor, close, isOpen } = useMenuPart("Menu.Content");
|
|
101
|
+
const { anchor: contextAnchor, close, firstRowRef, isOpen, triggerRef } = useMenuPart("Menu.Content");
|
|
101
102
|
const anchor = anchorProp ?? contextAnchor;
|
|
102
103
|
const window = useWindowDimensions();
|
|
103
104
|
const insets = useSafeAreaInsets();
|
|
@@ -146,18 +147,29 @@ export function MenuContent({
|
|
|
146
147
|
|
|
147
148
|
const handleContentSize = useCallback((_width: number, height: number) => setMeasuredHeight(height), []);
|
|
148
149
|
|
|
150
|
+
const handleEntered = useCallback(() => focusMenuTarget(firstRowRef.current), [firstRowRef]);
|
|
151
|
+
|
|
149
152
|
const handleExited = useCallback(() => {
|
|
150
153
|
setPresent(false);
|
|
151
154
|
setLocked(null);
|
|
152
155
|
setMeasuredHeight(null);
|
|
153
|
-
|
|
156
|
+
focusMenuTarget(triggerRef.current);
|
|
157
|
+
}, [triggerRef]);
|
|
158
|
+
|
|
159
|
+
const handleEscape = useCallback(() => {
|
|
160
|
+
close();
|
|
161
|
+
onAccessibilityEscape?.();
|
|
162
|
+
}, [close, onAccessibilityEscape]);
|
|
154
163
|
|
|
155
164
|
const progress = useSharedValue(0);
|
|
156
165
|
|
|
157
166
|
useEffect(() => {
|
|
158
167
|
if (isOpen) {
|
|
159
168
|
if (!locked) return;
|
|
160
|
-
progress.value = withTiming(1, { duration: MENU_MOTION.enterMs })
|
|
169
|
+
progress.value = withTiming(1, { duration: MENU_MOTION.enterMs }, (isFinished) => {
|
|
170
|
+
"worklet";
|
|
171
|
+
if (isFinished) scheduleOnRN(handleEntered);
|
|
172
|
+
});
|
|
161
173
|
return;
|
|
162
174
|
}
|
|
163
175
|
if (!isPresent) return;
|
|
@@ -165,7 +177,7 @@ export function MenuContent({
|
|
|
165
177
|
"worklet";
|
|
166
178
|
if (isFinished) scheduleOnRN(handleExited);
|
|
167
179
|
});
|
|
168
|
-
}, [handleExited, isOpen, isPresent, locked, progress]);
|
|
180
|
+
}, [handleEntered, handleExited, isOpen, isPresent, locked, progress]);
|
|
169
181
|
|
|
170
182
|
const { cap, origin, position } = resolvePanelFrame({
|
|
171
183
|
align,
|
|
@@ -222,6 +234,7 @@ export function MenuContent({
|
|
|
222
234
|
accessibilityRole="menu"
|
|
223
235
|
accessibilityViewIsModal
|
|
224
236
|
className={slots.panel()}
|
|
237
|
+
onAccessibilityEscape={handleEscape}
|
|
225
238
|
pointerEvents={locked ? "auto" : "none"}
|
|
226
239
|
style={[position, panelStyle]}
|
|
227
240
|
{...props}
|
|
@@ -7,7 +7,7 @@ import { Icon } from "../icon";
|
|
|
7
7
|
import { playHaptic } from "../pressable/pressable";
|
|
8
8
|
import { dispatchPressableAccessibilityAction, resolvePressableActivation } from "../pressable/pressable.variants";
|
|
9
9
|
import { Text } from "../text";
|
|
10
|
-
import { useMenuPart } from "./menu.context";
|
|
10
|
+
import { type MenuFocusTarget, useMenuPart } from "./menu.context";
|
|
11
11
|
import type { MenuItemProps } from "./menu.types";
|
|
12
12
|
import {
|
|
13
13
|
MENU_ICON_TOKEN,
|
|
@@ -17,6 +17,7 @@ import {
|
|
|
17
17
|
menuVariants,
|
|
18
18
|
resolveCloseOnSelect,
|
|
19
19
|
resolveMenuItemAccessibility,
|
|
20
|
+
resolveMenuRowRole,
|
|
20
21
|
} from "./menu.variants";
|
|
21
22
|
|
|
22
23
|
export type { MenuItemProps } from "./menu.types";
|
|
@@ -92,7 +93,7 @@ export function MenuRow({
|
|
|
92
93
|
accessibilityState,
|
|
93
94
|
...props
|
|
94
95
|
}: MenuRowProps): ReactElement {
|
|
95
|
-
const { close, haptic } = useMenuPart("Menu.Item");
|
|
96
|
+
const { close, firstRowRef, haptic } = useMenuPart("Menu.Item");
|
|
96
97
|
const isReducedMotion = useReducedMotion();
|
|
97
98
|
const slots = menuVariants({ isDisabled, isInset, variant });
|
|
98
99
|
const shouldClose = resolveCloseOnSelect(kind, isClosedOnSelect);
|
|
@@ -136,6 +137,18 @@ export function MenuRow({
|
|
|
136
137
|
handleSelect
|
|
137
138
|
);
|
|
138
139
|
|
|
140
|
+
// The first row to mount claims the panel's initial focus, and gives it up when it unmounts.
|
|
141
|
+
const claimFirstRow = useCallback(
|
|
142
|
+
(node: MenuFocusTarget | null) => {
|
|
143
|
+
if (node === null) return;
|
|
144
|
+
if (firstRowRef.current === null) firstRowRef.current = node;
|
|
145
|
+
return () => {
|
|
146
|
+
if (firstRowRef.current === node) firstRowRef.current = null;
|
|
147
|
+
};
|
|
148
|
+
},
|
|
149
|
+
[firstRowRef]
|
|
150
|
+
);
|
|
151
|
+
|
|
139
152
|
const rowStyle = useAnimatedStyle(() => ({
|
|
140
153
|
transform: [{ scale: 1 - pressed.value * (1 - pressedScale) }],
|
|
141
154
|
}));
|
|
@@ -157,13 +170,14 @@ export function MenuRow({
|
|
|
157
170
|
return (
|
|
158
171
|
<GestureDetector gesture={gesture}>
|
|
159
172
|
<Animated.View
|
|
160
|
-
accessibilityRole=
|
|
173
|
+
accessibilityRole={resolveMenuRowRole(kind)}
|
|
161
174
|
accessibilityState={accessibilityState ?? resolveMenuItemAccessibility({ isDisabled, kind: "item" })}
|
|
162
175
|
accessibilityActions={accessibilityActions}
|
|
163
176
|
accessible
|
|
164
177
|
className={slots.item({ className })}
|
|
165
178
|
onAccessibilityAction={onAccessibilityAction}
|
|
166
179
|
onAccessibilityTap={onAccessibilityTap}
|
|
180
|
+
ref={claimFirstRow}
|
|
167
181
|
style={rowStyle}
|
|
168
182
|
{...props}
|
|
169
183
|
>
|
|
@@ -23,7 +23,7 @@ export type MenuRadioItemProps = Omit<MenuItemProps, "onSelect"> & {
|
|
|
23
23
|
*
|
|
24
24
|
* Closes the menu on select by default — a choice made is a visit finished.
|
|
25
25
|
* The mark takes the leading column, so an `icon` moves to the row's end.
|
|
26
|
-
* Announced as `selected
|
|
26
|
+
* Announced as a `radio`, `checked` while selected.
|
|
27
27
|
*/
|
|
28
28
|
export function MenuRadioItem({
|
|
29
29
|
value,
|
|
@@ -10,7 +10,7 @@ import {
|
|
|
10
10
|
useRef,
|
|
11
11
|
useState,
|
|
12
12
|
} from "react";
|
|
13
|
-
import type
|
|
13
|
+
import { AccessibilityInfo, findNodeHandle, type ViewInstance } from "react-native";
|
|
14
14
|
import type { SharedValue } from "react-native-reanimated";
|
|
15
15
|
import { useControllableState } from "../../hooks/use-controllable-state";
|
|
16
16
|
import type { HapticFeedback } from "../pressable/pressable";
|
|
@@ -19,6 +19,16 @@ import type { MenuAnchorRect } from "./menu.variants";
|
|
|
19
19
|
/** Anything `measureInWindow` can be called on — a host view, or an animated one. */
|
|
20
20
|
export type MenuMeasurable = Pick<ViewInstance, "measureInWindow">;
|
|
21
21
|
|
|
22
|
+
/** Anything `AccessibilityInfo.setAccessibilityFocus` can be pointed at through `findNodeHandle`. */
|
|
23
|
+
export type MenuFocusTarget = NonNullable<Parameters<typeof findNodeHandle>[0]>;
|
|
24
|
+
|
|
25
|
+
/** Moves VoiceOver / TalkBack focus to a mounted element. A no-op when it has gone. */
|
|
26
|
+
export function focusMenuTarget(target: MenuFocusTarget | MenuMeasurable | null): void {
|
|
27
|
+
if (target === null) return;
|
|
28
|
+
const handle = findNodeHandle(target as MenuFocusTarget);
|
|
29
|
+
if (handle !== null && handle !== undefined) AccessibilityInfo.setAccessibilityFocus(handle);
|
|
30
|
+
}
|
|
31
|
+
|
|
22
32
|
export type MenuContextValue = {
|
|
23
33
|
isOpen: boolean;
|
|
24
34
|
/** Where the panel is anchored, set on open. `null` until the first open. */
|
|
@@ -31,8 +41,10 @@ export type MenuContextValue = {
|
|
|
31
41
|
close: () => void;
|
|
32
42
|
/** Closes an open menu, opens a closed one from the trigger. */
|
|
33
43
|
toggle: () => void;
|
|
34
|
-
/** The trigger's node, measured on open. */
|
|
44
|
+
/** The trigger's node, measured on open — and focused again once the panel has gone. */
|
|
35
45
|
triggerRef: RefObject<MenuMeasurable | null>;
|
|
46
|
+
/** The first row mounted in the panel, focused once the panel has entered. */
|
|
47
|
+
firstRowRef: RefObject<MenuFocusTarget | null>;
|
|
36
48
|
/** Played when a row is chosen. */
|
|
37
49
|
haptic: false | HapticFeedback;
|
|
38
50
|
};
|
|
@@ -97,6 +109,7 @@ export function useMenuRootValue({
|
|
|
97
109
|
|
|
98
110
|
const [anchor, setAnchor] = useState<MenuAnchorRect | null>(null);
|
|
99
111
|
const triggerRef = useRef<MenuMeasurable | null>(null);
|
|
112
|
+
const firstRowRef = useRef<MenuFocusTarget | null>(null);
|
|
100
113
|
|
|
101
114
|
const openRef = useRef(isOpen);
|
|
102
115
|
openRef.current = isOpen;
|
|
@@ -156,7 +169,7 @@ export function useMenuRootValue({
|
|
|
156
169
|
}, [close, open]);
|
|
157
170
|
|
|
158
171
|
return useMemo<MenuContextValue>(
|
|
159
|
-
() => ({ anchor, close, haptic, isOpen, open, toggle, triggerRef }),
|
|
172
|
+
() => ({ anchor, close, firstRowRef, haptic, isOpen, open, toggle, triggerRef }),
|
|
160
173
|
[anchor, close, haptic, isOpen, open, toggle]
|
|
161
174
|
);
|
|
162
175
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { afterEach, beforeEach, describe, expect, jest, test } from "@jest/globals";
|
|
2
2
|
import { type ReactElement, type ReactNode, useState } from "react";
|
|
3
3
|
import { Platform, View } from "react-native";
|
|
4
|
-
import { fireEvent, renderWithProviders, screen } from "../../test/render";
|
|
4
|
+
import { act, fireEvent, renderWithProviders, screen } from "../../test/render";
|
|
5
5
|
import { Button } from "../button";
|
|
6
6
|
import { OverlayProvider } from "../overlay";
|
|
7
7
|
import { Menu, type MenuProps } from "./menu";
|
|
@@ -39,6 +39,7 @@ const originalOS = Platform.OS;
|
|
|
39
39
|
const checked = (name: string) => screen.getByRole("menuitem", { name }).props.accessibilityState?.checked;
|
|
40
40
|
const press = (name: string) => fireEvent(screen.getByRole("button", { name }), "accessibilityTap");
|
|
41
41
|
const choose = (name: string) => fireEvent(screen.getByRole("menuitem", { name }), "accessibilityTap");
|
|
42
|
+
const chooseRadio = (name: string) => fireEvent(screen.getByRole("radio", { name }), "accessibilityTap");
|
|
42
43
|
|
|
43
44
|
/**
|
|
44
45
|
* The panel measures its trigger with `measureInWindow` and the rows with layout, and Jest has
|
|
@@ -253,10 +254,10 @@ describe("Menu", () => {
|
|
|
253
254
|
);
|
|
254
255
|
}
|
|
255
256
|
|
|
256
|
-
test("
|
|
257
|
+
test("each option is a radio, announced checked while selected", async () => {
|
|
257
258
|
await renderWithProviders(<DensityMenu defaultValue="compact" />, { wrapper: Overlays });
|
|
258
|
-
expect(screen.getByRole("
|
|
259
|
-
expect(screen.getByRole("
|
|
259
|
+
expect(screen.getByRole("radio", { name: "Compact" })).toBeChecked();
|
|
260
|
+
expect(screen.getByRole("radio", { name: "Comfortable" })).not.toBeChecked();
|
|
260
261
|
});
|
|
261
262
|
|
|
262
263
|
test("uncontrolled: choosing moves the selection, reports the value and closes", async () => {
|
|
@@ -267,7 +268,7 @@ describe("Menu", () => {
|
|
|
267
268
|
{ wrapper: Overlays }
|
|
268
269
|
);
|
|
269
270
|
|
|
270
|
-
await
|
|
271
|
+
await chooseRadio("Comfortable");
|
|
271
272
|
expect(onValueChange).toHaveBeenCalledWith("comfortable");
|
|
272
273
|
expect(onOpenChange).toHaveBeenLastCalledWith(false);
|
|
273
274
|
});
|
|
@@ -276,17 +277,17 @@ describe("Menu", () => {
|
|
|
276
277
|
await renderWithProviders(<DensityMenu defaultValue="compact" isClosedOnSelect={false} />, {
|
|
277
278
|
wrapper: Overlays,
|
|
278
279
|
});
|
|
279
|
-
await
|
|
280
|
-
expect(screen.getByRole("
|
|
281
|
-
expect(screen.getByRole("
|
|
280
|
+
await chooseRadio("Comfortable");
|
|
281
|
+
expect(screen.getByRole("radio", { name: "Comfortable" })).toBeChecked();
|
|
282
|
+
expect(screen.getByRole("radio", { name: "Compact" })).not.toBeChecked();
|
|
282
283
|
});
|
|
283
284
|
|
|
284
285
|
test("re-choosing the selected item keeps it selected", async () => {
|
|
285
286
|
await renderWithProviders(<DensityMenu defaultValue="compact" isClosedOnSelect={false} />, {
|
|
286
287
|
wrapper: Overlays,
|
|
287
288
|
});
|
|
288
|
-
await
|
|
289
|
-
expect(screen.getByRole("
|
|
289
|
+
await chooseRadio("Compact");
|
|
290
|
+
expect(screen.getByRole("radio", { name: "Compact" })).toBeChecked();
|
|
290
291
|
});
|
|
291
292
|
|
|
292
293
|
test("controlled: waits for the prop before the selection moves", async () => {
|
|
@@ -298,12 +299,12 @@ describe("Menu", () => {
|
|
|
298
299
|
}
|
|
299
300
|
);
|
|
300
301
|
|
|
301
|
-
await
|
|
302
|
+
await chooseRadio("Comfortable");
|
|
302
303
|
expect(onValueChange).toHaveBeenCalledWith("comfortable");
|
|
303
|
-
expect(screen.getByRole("
|
|
304
|
+
expect(screen.getByRole("radio", { name: "Compact" })).toBeChecked();
|
|
304
305
|
|
|
305
306
|
await view.rerender(<DensityMenu isClosedOnSelect={false} onValueChange={onValueChange} value="comfortable" />);
|
|
306
|
-
expect(screen.getByRole("
|
|
307
|
+
expect(screen.getByRole("radio", { name: "Comfortable" })).toBeChecked();
|
|
307
308
|
});
|
|
308
309
|
|
|
309
310
|
test("a RadioItem outside a RadioGroup throws", async () => {
|
|
@@ -419,6 +420,30 @@ describe("Menu", () => {
|
|
|
419
420
|
expect(screen.getByRole("button", { name: "Options" })).toBeCollapsed();
|
|
420
421
|
});
|
|
421
422
|
|
|
423
|
+
test("the panel is modal, and the accessibility escape closes the menu", async () => {
|
|
424
|
+
const onOpenChange = jest.fn();
|
|
425
|
+
const onAccessibilityEscape = jest.fn();
|
|
426
|
+
await renderWithProviders(
|
|
427
|
+
<Menu defaultOpen onOpenChange={onOpenChange}>
|
|
428
|
+
<Menu.Trigger asChild>
|
|
429
|
+
<Button>Options</Button>
|
|
430
|
+
</Menu.Trigger>
|
|
431
|
+
<Menu.Content onAccessibilityEscape={onAccessibilityEscape} testID="panel">
|
|
432
|
+
<Menu.Item>Rename</Menu.Item>
|
|
433
|
+
</Menu.Content>
|
|
434
|
+
</Menu>,
|
|
435
|
+
{ wrapper: Overlays }
|
|
436
|
+
);
|
|
437
|
+
const panel = screen.getByTestId("panel");
|
|
438
|
+
expect(panel.props.accessibilityViewIsModal).toBe(true);
|
|
439
|
+
await act(async () => {
|
|
440
|
+
panel.props.onAccessibilityEscape();
|
|
441
|
+
});
|
|
442
|
+
expect(onOpenChange).toHaveBeenLastCalledWith(false);
|
|
443
|
+
expect(onAccessibilityEscape).toHaveBeenCalledTimes(1);
|
|
444
|
+
expect(screen.getByRole("button", { name: "Options" })).toBeCollapsed();
|
|
445
|
+
});
|
|
446
|
+
|
|
422
447
|
describe("on Android", () => {
|
|
423
448
|
beforeEach(() => {
|
|
424
449
|
Platform.OS = "android";
|
|
@@ -437,6 +462,25 @@ describe("Menu", () => {
|
|
|
437
462
|
expect(onRename).toHaveBeenCalledTimes(1);
|
|
438
463
|
});
|
|
439
464
|
|
|
465
|
+
test("a radio row is activated through the activate accessibility action", async () => {
|
|
466
|
+
const onValueChange = jest.fn();
|
|
467
|
+
await renderWithProviders(
|
|
468
|
+
<Menu defaultOpen>
|
|
469
|
+
<Menu.Content>
|
|
470
|
+
<Menu.RadioGroup defaultValue="compact" onValueChange={onValueChange}>
|
|
471
|
+
<Menu.RadioItem value="comfortable">Comfortable</Menu.RadioItem>
|
|
472
|
+
<Menu.RadioItem value="compact">Compact</Menu.RadioItem>
|
|
473
|
+
</Menu.RadioGroup>
|
|
474
|
+
</Menu.Content>
|
|
475
|
+
</Menu>,
|
|
476
|
+
{ wrapper: Overlays }
|
|
477
|
+
);
|
|
478
|
+
await fireEvent(screen.getByRole("radio", { name: "Comfortable" }), "accessibilityAction", {
|
|
479
|
+
nativeEvent: { actionName: "activate" },
|
|
480
|
+
});
|
|
481
|
+
expect(onValueChange).toHaveBeenCalledWith("comfortable");
|
|
482
|
+
});
|
|
483
|
+
|
|
440
484
|
test("a disabled row ignores the activate action", async () => {
|
|
441
485
|
const onDelete = jest.fn();
|
|
442
486
|
await renderWithProviders(<ItemsMenu defaultOpen onDelete={onDelete} />, { wrapper: Overlays });
|
|
@@ -14,6 +14,7 @@ import {
|
|
|
14
14
|
resolveMenuOrigin,
|
|
15
15
|
resolveMenuPhysicalAlign,
|
|
16
16
|
resolveMenuPlacement,
|
|
17
|
+
resolveMenuRowRole,
|
|
17
18
|
resolveMenuWidth,
|
|
18
19
|
resolveRadioNext,
|
|
19
20
|
} from "./menu.variants";
|
|
@@ -273,10 +274,14 @@ describe("resolveMenuItemAccessibility", () => {
|
|
|
273
274
|
});
|
|
274
275
|
});
|
|
275
276
|
|
|
276
|
-
test("a radio announces
|
|
277
|
+
test("a radio announces checked, the state a radio role reads", () => {
|
|
277
278
|
expect(resolveMenuItemAccessibility({ isDisabled: false, isSelected: false, kind: "radio" })).toEqual({
|
|
279
|
+
checked: false,
|
|
278
280
|
disabled: false,
|
|
279
|
-
|
|
281
|
+
});
|
|
282
|
+
expect(resolveMenuItemAccessibility({ isDisabled: true, isSelected: true, kind: "radio" })).toEqual({
|
|
283
|
+
checked: true,
|
|
284
|
+
disabled: true,
|
|
280
285
|
});
|
|
281
286
|
});
|
|
282
287
|
|
|
@@ -288,6 +293,18 @@ describe("resolveMenuItemAccessibility", () => {
|
|
|
288
293
|
});
|
|
289
294
|
});
|
|
290
295
|
|
|
296
|
+
describe("resolveMenuRowRole", () => {
|
|
297
|
+
test("a radio row is a radio, so its checked state is read as a choice", () => {
|
|
298
|
+
expect(resolveMenuRowRole("radio")).toBe("radio");
|
|
299
|
+
});
|
|
300
|
+
|
|
301
|
+
test("every other row is a menuitem", () => {
|
|
302
|
+
expect(resolveMenuRowRole("item")).toBe("menuitem");
|
|
303
|
+
expect(resolveMenuRowRole("checkbox")).toBe("menuitem");
|
|
304
|
+
expect(resolveMenuRowRole("sub-trigger")).toBe("menuitem");
|
|
305
|
+
});
|
|
306
|
+
});
|
|
307
|
+
|
|
291
308
|
describe("motion constants", () => {
|
|
292
309
|
test("enter is slower than exit, and both are short", () => {
|
|
293
310
|
expect(MENU_MOTION.enterMs).toBe(160);
|
|
@@ -244,10 +244,23 @@ export type MenuItemAccessibilityInput =
|
|
|
244
244
|
export type MenuItemAccessibilityState = {
|
|
245
245
|
disabled: boolean;
|
|
246
246
|
checked?: boolean;
|
|
247
|
-
selected?: boolean;
|
|
248
247
|
expanded?: boolean;
|
|
249
248
|
};
|
|
250
249
|
|
|
250
|
+
/** The role a menu row announces. */
|
|
251
|
+
export type MenuRowRole = "menuitem" | "radio";
|
|
252
|
+
|
|
253
|
+
/**
|
|
254
|
+
* The role a row announces. React Native has no `menuitemradio`, and a
|
|
255
|
+
* `menuitem` with `selected` reads as a highlighted row rather than as one
|
|
256
|
+
* choice of several, so a radio row is a `radio` and announces `checked` —
|
|
257
|
+
* "Compact, radio button, checked" on VoiceOver and TalkBack alike. Every other
|
|
258
|
+
* row is a `menuitem`.
|
|
259
|
+
*/
|
|
260
|
+
export function resolveMenuRowRole(kind: MenuRowKind): MenuRowRole {
|
|
261
|
+
return kind === "radio" ? "radio" : "menuitem";
|
|
262
|
+
}
|
|
263
|
+
|
|
251
264
|
/** The `accessibilityState` a row announces — one source, so the matrix is testable. */
|
|
252
265
|
export function resolveMenuItemAccessibility(input: MenuItemAccessibilityInput): MenuItemAccessibilityState {
|
|
253
266
|
switch (input.kind) {
|
|
@@ -256,7 +269,7 @@ export function resolveMenuItemAccessibility(input: MenuItemAccessibilityInput):
|
|
|
256
269
|
case "checkbox":
|
|
257
270
|
return { checked: input.isChecked, disabled: input.isDisabled };
|
|
258
271
|
case "radio":
|
|
259
|
-
return {
|
|
272
|
+
return { checked: input.isSelected, disabled: input.isDisabled };
|
|
260
273
|
case "sub-trigger":
|
|
261
274
|
return { disabled: input.isDisabled, expanded: input.isExpanded };
|
|
262
275
|
default:
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import { describe, expect, test } from "bun:test";
|
|
2
|
+
import { readdirSync, readFileSync } from "node:fs";
|
|
3
|
+
import { dirname, join, relative } from "node:path";
|
|
4
|
+
|
|
5
|
+
const SRC = import.meta.dirname;
|
|
6
|
+
const COMPONENTS = join(SRC, "components");
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Every Gesture Handler tap a screen reader can land on has a way to be
|
|
10
|
+
* activated without a touch.
|
|
11
|
+
*
|
|
12
|
+
* A `Gesture.Tap()` or `Gesture.LongPress()` only ever sees touches. VoiceOver's
|
|
13
|
+
* double-tap reaches it by accident — UIKit synthesises a touch at the
|
|
14
|
+
* element's centre when nothing answers `accessibilityActivate` — and TalkBack's
|
|
15
|
+
* never does: its double-tap performs `ACTION_CLICK`, which React Native routes
|
|
16
|
+
* to `onAccessibilityAction` only when `activate` is listed, and otherwise falls
|
|
17
|
+
* to a `View.performClick` with no listener behind it. A Menu row shipped that
|
|
18
|
+
* way and could not be chosen with TalkBack on.
|
|
19
|
+
*
|
|
20
|
+
* So a component file that builds a tap or a long press in a `GestureDetector`
|
|
21
|
+
* and exposes an element to assistive technology (`accessible`,
|
|
22
|
+
* `accessibilityRole` or `role`) has to take one of the two activation paths:
|
|
23
|
+
*
|
|
24
|
+
* - **`resolvePressableActivation`** — the shared wiring `Pressable` uses:
|
|
25
|
+
* `onAccessibilityTap` on iOS, an `activate` (and `longpress`) action on
|
|
26
|
+
* Android, both routed through `dispatchPressableAccessibilityAction`.
|
|
27
|
+
* - **An `activate` action it owns** — listed in `accessibilityActions` (in the
|
|
28
|
+
* file, or by a resolver in its folder's `*.variants.ts`) and handled in its
|
|
29
|
+
* own `onAccessibilityAction`, as a `NumberInput` stepper, a `Flow.Node` or a
|
|
30
|
+
* `ContextMenu.Trigger` does.
|
|
31
|
+
*
|
|
32
|
+
* A file that takes neither is listed in {@link EXEMPT} with the reason its
|
|
33
|
+
* gesture needs no activation path — an `adjustable` whose increment and
|
|
34
|
+
* decrement already do what the tap does, or a tap that is a touch shortcut for
|
|
35
|
+
* an action the element exposes some other way. A reason is required, and an
|
|
36
|
+
* entry for a file that no longer needs one fails, so the list cannot rot.
|
|
37
|
+
*
|
|
38
|
+
* Reading text cannot tell which element an action sits on, so an owned
|
|
39
|
+
* `activate` on an `accessible={false}` container passes here and still reaches
|
|
40
|
+
* nothing — that is checked by each component's render suite and on a device.
|
|
41
|
+
*
|
|
42
|
+
* Like `docs.test.ts`, this reads source as text and imports nothing from the
|
|
43
|
+
* tree — `bun test` cannot parse React Native's Flow-typed source.
|
|
44
|
+
*/
|
|
45
|
+
const EXEMPT: Record<string, string> = {
|
|
46
|
+
"circular-text/circular-text.tsx":
|
|
47
|
+
"The hold only pauses the ring's turn while a finger rests on it; the text is read whole and nothing is activated. Pausing looping motion for a screen reader is the calm-motion work, not an activation.",
|
|
48
|
+
"flip-card/flip-card.tsx":
|
|
49
|
+
"The root is a container a screen reader never focuses. A press card's activation is the Flip card button its visible face renders, wired through resolvePressableActivation in flip-card-face.tsx; a swipe card's is the grouped visible face's activate action.",
|
|
50
|
+
"image-viewer/image-viewer-layer.tsx":
|
|
51
|
+
"The double-tap zoom is mirrored by the adjustable stage's zoomIn, zoomOut and resetZoom actions, and the single tap only hides chrome that starts visible and stays reachable.",
|
|
52
|
+
"splitter/splitter-handle.tsx":
|
|
53
|
+
"The double-tap reset is the adjustable handle's own reset action (Reset sizes), beside increment, decrement and expand or collapse.",
|
|
54
|
+
"swipe/swipe.tsx":
|
|
55
|
+
"The shield tap only closes a row a finger slid open. A screen reader never opens the row: each revealed tile is one of the row's named accessibility actions.",
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
function componentFiles(dir: string): string[] {
|
|
59
|
+
return readdirSync(dir, { withFileTypes: true }).flatMap((entry) => {
|
|
60
|
+
const path = join(dir, entry.name);
|
|
61
|
+
if (entry.isDirectory()) return componentFiles(path);
|
|
62
|
+
return entry.name.endsWith(".tsx") && !entry.name.endsWith(".test.tsx") ? [path] : [];
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const TAP = /Gesture\.(Tap|LongPress)\(/;
|
|
67
|
+
const EXPOSED = /\baccessible\b(?!\s*=\s*\{\s*false\s*\})|\baccessibilityRole\b|\brole=/;
|
|
68
|
+
const SHARED_PATH = /\bresolvePressableActivation\(/;
|
|
69
|
+
const OWNED_ACTIVATE = /name:\s*"activate"/;
|
|
70
|
+
|
|
71
|
+
/** The folder's `*.variants.ts` files, where a resolver may declare the actions a part lists. */
|
|
72
|
+
function variantsSource(dir: string): string {
|
|
73
|
+
return readdirSync(dir)
|
|
74
|
+
.filter((name) => name.endsWith(".variants.ts"))
|
|
75
|
+
.map((name) => readFileSync(join(dir, name), "utf-8"))
|
|
76
|
+
.join("\n");
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** Whether a file's source wires a screen-reader activation for its tap. */
|
|
80
|
+
function hasActivationPath({ source, variants }: { source: string; variants: string }): boolean {
|
|
81
|
+
if (SHARED_PATH.test(source)) return true;
|
|
82
|
+
const listsActivate = OWNED_ACTIVATE.test(source) || OWNED_ACTIVATE.test(variants);
|
|
83
|
+
return listsActivate && source.includes("onAccessibilityAction");
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const CANDIDATES = componentFiles(COMPONENTS)
|
|
87
|
+
.map((path) => ({
|
|
88
|
+
path: relative(COMPONENTS, path),
|
|
89
|
+
source: readFileSync(path, "utf-8"),
|
|
90
|
+
variants: variantsSource(dirname(path)),
|
|
91
|
+
}))
|
|
92
|
+
.filter(({ source }) => source.includes("GestureDetector") && TAP.test(source) && EXPOSED.test(source));
|
|
93
|
+
|
|
94
|
+
describe("gesture activation", () => {
|
|
95
|
+
// An empty walk would let every assertion below pass on nothing.
|
|
96
|
+
test("finds the tapping components", () => {
|
|
97
|
+
expect(CANDIDATES.length).toBeGreaterThan(5);
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
test("every exposed tap or long press has a screen-reader activation path", () => {
|
|
101
|
+
const offenders = CANDIDATES.filter(
|
|
102
|
+
(candidate) => !(candidate.path in EXEMPT) && !hasActivationPath(candidate)
|
|
103
|
+
).map(({ path }) => path);
|
|
104
|
+
expect(offenders).toEqual([]);
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
test("every exemption names a reason", () => {
|
|
108
|
+
const unexplained = Object.entries(EXEMPT)
|
|
109
|
+
.filter(([, reason]) => reason.trim().length < 20)
|
|
110
|
+
.map(([path]) => path);
|
|
111
|
+
expect(unexplained).toEqual([]);
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
test("every exemption is still needed", () => {
|
|
115
|
+
const stale = Object.keys(EXEMPT).filter((path) => {
|
|
116
|
+
const candidate = CANDIDATES.find((entry) => entry.path === path);
|
|
117
|
+
return candidate === undefined || hasActivationPath(candidate);
|
|
118
|
+
});
|
|
119
|
+
expect(stale).toEqual([]);
|
|
120
|
+
});
|
|
121
|
+
});
|