@delacour/react-native-ui 0.10.0-alpha.20261120132923 → 0.10.0-alpha.20261120160127
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
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@delacour/react-native-ui",
|
|
3
|
-
"version": "0.10.0-alpha.
|
|
3
|
+
"version": "0.10.0-alpha.20261120160127",
|
|
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.10.0-alpha.
|
|
190
|
-
"@delacour/react-native-charts": "0.10.0-alpha.
|
|
189
|
+
"@delacour/react-native-bottom-sheet": "0.10.0-alpha.20261120160127",
|
|
190
|
+
"@delacour/react-native-charts": "0.10.0-alpha.20261120160127",
|
|
191
191
|
"@legendapp/list": ">=3.3",
|
|
192
192
|
"react-native-skia": ">=3.9.0",
|
|
193
193
|
"expo-linear-gradient": ">=15",
|
|
@@ -21,7 +21,7 @@ section to read first — what follows is only where a switch differs.
|
|
|
21
21
|
| `switch.types.ts` | Prop types shared by two or more parts |
|
|
22
22
|
| `switch.variants.ts` | Pure `tv()` slots + the release worklet, no RN imports |
|
|
23
23
|
| `switch.variants.test.ts` | |
|
|
24
|
-
| `switch.render.test.tsx` | Mounted under Jest — role, checked and disabled state, controlled and uncontrolled, assistive activation |
|
|
24
|
+
| `switch.render.test.tsx` | Mounted under Jest — role, checked and disabled state, controlled and uncontrolled, assistive activation, the `Field.Label` name, a caller's `accessibilityState` and `Switch.Thumb` `onLayout`. The pan is driven with `fireGestureHandler` — tap, drag out and back on either axis, cancel, disabled |
|
|
25
25
|
|
|
26
26
|
## Design
|
|
27
27
|
|
|
@@ -50,11 +50,24 @@ section to read first — what follows is only where a switch differs.
|
|
|
50
50
|
the control a tap and toggle it. Reading both means such a swipe is movement
|
|
51
51
|
with no travel, which settles back to the state it started in — the switch is
|
|
52
52
|
left alone.
|
|
53
|
+
- **`distance` is the peak over the whole gesture, not the translation at
|
|
54
|
+
release.** The root tracks the furthest the finger got on either axis, resets
|
|
55
|
+
it in `onBegin`, and passes it at release. A slow drag out and back to near its
|
|
56
|
+
start is still a drag, and it settles by position instead of toggling.
|
|
53
57
|
- **The value is settled in `onFinalize`, and only there.** Unlike a slider there
|
|
54
58
|
is nothing to write on `onBegin`: a switch has no position to move a handle to,
|
|
55
59
|
only a state to end up in. `onFinalize` is still the callback because it is the
|
|
56
60
|
one that fires on every path, the never-activated one included — which is
|
|
57
61
|
exactly the path a stationary tap takes.
|
|
62
|
+
- **A cancelled pan commits nothing.** `onFinalize` also runs when the system,
|
|
63
|
+
a parent, or a disable under the finger cancels the pan. On `CANCELLED`, or
|
|
64
|
+
when the switch is disabled, it springs the thumb back to the current state,
|
|
65
|
+
releases the press scale and clears the drag flag, with no haptic and no
|
|
66
|
+
change. It checks `CANCELLED` only, never `success`: on iOS a stationary press
|
|
67
|
+
never activates the pan and finalizes as `FAILED`, and that press is the tap.
|
|
68
|
+
- **The pan carries ``.withTestId(`${testID}-pan`)``** when the root has a
|
|
69
|
+
`testID`, so a render test can drive it with `fireGestureHandler`. No visual
|
|
70
|
+
effect.
|
|
58
71
|
- **`isDisabled` is checked in `toggle`, not only on the pan.** `.enabled()` gates
|
|
59
72
|
the gesture, but a screen reader's activation and an enclosing `Field`'s label
|
|
60
73
|
press both arrive through `toggle`, so a guard on the pan alone let a disabled
|
|
@@ -188,8 +201,10 @@ section to read first — what follows is only where a switch differs.
|
|
|
188
201
|
and its layers into the single element a control should be.
|
|
189
202
|
- **There is no `Switch.Label` and no `Switch.Group`.** The track is a fixed pill
|
|
190
203
|
and a label cannot sit inside it, so the name is a `Field.Label` or a
|
|
191
|
-
`ListGroup.ItemTitle` a row away
|
|
192
|
-
|
|
204
|
+
`ListGroup.ItemTitle` a row away. Inside a `Field`, the switch takes the
|
|
205
|
+
`Field.Label` text as its `accessibilityLabel` unless one is passed, because a
|
|
206
|
+
sibling label is outside the switch's accessibility element. Unlike `Slider`,
|
|
207
|
+
the switch **does** register `field.registerPress`, because a row-wide press means exactly what a
|
|
193
208
|
tap on the pill means. A switch is a binary preference, not one of a set, so
|
|
194
209
|
there is nothing for a group to own; the axis ladder is two rungs, the switch's
|
|
195
210
|
own props then an enclosing `Field`.
|
|
@@ -52,14 +52,18 @@ export type SwitchThumbProps = Omit<ViewProps, "children" | "style"> & {
|
|
|
52
52
|
* of the knob rather than an acknowledgement of the press. The outermost node is
|
|
53
53
|
* the one thing nothing can crop, so that is where the press feedback lives.
|
|
54
54
|
*/
|
|
55
|
-
export function SwitchThumb({ className, children, ...props }: SwitchThumbProps): ReactElement {
|
|
55
|
+
export function SwitchThumb({ className, children, onLayout, ...props }: SwitchThumbProps): ReactElement {
|
|
56
56
|
const { color, size, isInvalid, isDisabled, progress, trackWidth, thumbWidth } = useSwitchPart("Switch.Thumb");
|
|
57
57
|
|
|
58
|
+
// The caller's handler runs as well as the measurement, never instead of it:
|
|
59
|
+
// with the width left at zero the travel is the whole track, and an on-state
|
|
60
|
+
// knob slides out past the clipped end.
|
|
58
61
|
const handleLayout = useCallback(
|
|
59
62
|
(event: LayoutChangeEvent) => {
|
|
60
63
|
thumbWidth.value = event.nativeEvent.layout.width;
|
|
64
|
+
onLayout?.(event);
|
|
61
65
|
},
|
|
62
|
-
[thumbWidth]
|
|
66
|
+
[onLayout, thumbWidth]
|
|
63
67
|
);
|
|
64
68
|
|
|
65
69
|
const tokens = resolveSwitchThumbTokens({ color, isInvalid });
|
|
@@ -1,6 +1,49 @@
|
|
|
1
1
|
import { describe, expect, jest, test } from "@jest/globals";
|
|
2
|
-
import {
|
|
2
|
+
import { State } from "react-native-gesture-handler";
|
|
3
|
+
import { fireGestureHandler, getByGestureTestId } from "react-native-gesture-handler/jest-utils";
|
|
4
|
+
import { act, fireEvent, renderWithProviders, screen, waitFor } from "../../test/render";
|
|
5
|
+
import { Field } from "../field";
|
|
3
6
|
import { Switch } from "./switch";
|
|
7
|
+
import { SWITCH_THUMB_INSET } from "./switch.variants";
|
|
8
|
+
|
|
9
|
+
const layout = (width: number) => ({ nativeEvent: { layout: { height: 0, width, x: 0, y: 0 } } });
|
|
10
|
+
|
|
11
|
+
type Instance = ReturnType<typeof screen.getByTestId>;
|
|
12
|
+
|
|
13
|
+
/** The track is the root's only child: the node that clips the thumb and reports the travel. */
|
|
14
|
+
const track = (id = "switch"): Instance => {
|
|
15
|
+
const [first] = screen.getByTestId(id).children;
|
|
16
|
+
if (typeof first === "string" || first === undefined) throw new Error("the switch root holds no track");
|
|
17
|
+
return first;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
const translateX = (element: Instance): number | undefined => {
|
|
21
|
+
const { jestAnimatedStyle } = element.props as {
|
|
22
|
+
jestAnimatedStyle?: { value: { transform?: { translateX?: number }[] } };
|
|
23
|
+
};
|
|
24
|
+
return jestAnimatedStyle?.value.transform?.[0]?.translateX;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
type PanStep = { state: State; translationX?: number; translationY?: number };
|
|
28
|
+
|
|
29
|
+
/** Sends one pan to the switch, filling in what each step leaves out. */
|
|
30
|
+
const pan = async (steps: PanStep[], id = "switch"): Promise<void> => {
|
|
31
|
+
await act(async () => {
|
|
32
|
+
fireGestureHandler(
|
|
33
|
+
getByGestureTestId(`${id}-pan`),
|
|
34
|
+
steps.map((step) => ({ translationX: 0, translationY: 0, velocityX: 0, velocityY: 0, ...step }))
|
|
35
|
+
);
|
|
36
|
+
});
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
type MeasuredProps = { defaultSelected?: boolean; isDisabled?: boolean; onSelectedChange: (next: boolean) => void };
|
|
40
|
+
|
|
41
|
+
/** A switch with a measured track, so a drag moves the thumb the way it does on a device. */
|
|
42
|
+
const renderMeasured = async (props: MeasuredProps) => {
|
|
43
|
+
const view = await renderWithProviders(<Switch {...props} testID="switch" />);
|
|
44
|
+
await fireEvent(track(), "layout", layout(52));
|
|
45
|
+
return view;
|
|
46
|
+
};
|
|
4
47
|
|
|
5
48
|
describe("Switch", () => {
|
|
6
49
|
test("is a switch whose state is announced", async () => {
|
|
@@ -44,3 +87,141 @@ describe("Switch", () => {
|
|
|
44
87
|
expect(onSelectedChange).not.toHaveBeenCalled();
|
|
45
88
|
});
|
|
46
89
|
});
|
|
90
|
+
|
|
91
|
+
describe("Switch.Thumb", () => {
|
|
92
|
+
test("calls a caller's onLayout and still measures itself", async () => {
|
|
93
|
+
const onLayout = jest.fn();
|
|
94
|
+
await renderWithProviders(
|
|
95
|
+
<Switch defaultSelected testID="switch">
|
|
96
|
+
<Switch.Thumb onLayout={onLayout} testID="thumb" />
|
|
97
|
+
</Switch>
|
|
98
|
+
);
|
|
99
|
+
|
|
100
|
+
await fireEvent(track(), "layout", layout(52));
|
|
101
|
+
await fireEvent(screen.getByTestId("thumb"), "layout", layout(24));
|
|
102
|
+
|
|
103
|
+
expect(onLayout).toHaveBeenCalledTimes(1);
|
|
104
|
+
// An on-state thumb sits one travel along. With its own width unmeasured
|
|
105
|
+
// the travel would be the whole track less the insets, and the knob would
|
|
106
|
+
// slide out past the clipped end. Waited for, because under Jest Reanimated
|
|
107
|
+
// runs the style mapper on the next animation frame, not inside the event.
|
|
108
|
+
await waitFor(() => expect(translateX(screen.getByTestId("thumb"))).toBe(52 - 24 - 2 * SWITCH_THUMB_INSET));
|
|
109
|
+
});
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
describe("Switch accessibility", () => {
|
|
113
|
+
test("keeps reporting checked and disabled when the caller passes its own accessibilityState", async () => {
|
|
114
|
+
await renderWithProviders(
|
|
115
|
+
<Switch accessibilityState={{ busy: true }} defaultSelected isDisabled testID="switch" />
|
|
116
|
+
);
|
|
117
|
+
|
|
118
|
+
expect(screen.getByTestId("switch").props.accessibilityState).toEqual({
|
|
119
|
+
busy: true,
|
|
120
|
+
checked: true,
|
|
121
|
+
disabled: true,
|
|
122
|
+
});
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
test("is named by a sibling Field.Label", async () => {
|
|
126
|
+
await renderWithProviders(
|
|
127
|
+
<Field>
|
|
128
|
+
<Field.Label>Notifications</Field.Label>
|
|
129
|
+
<Switch />
|
|
130
|
+
</Field>
|
|
131
|
+
);
|
|
132
|
+
|
|
133
|
+
expect(screen.getByRole("switch", { name: "Notifications" })).toBeOnTheScreen();
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
test("keeps an explicit accessibilityLabel over the Field.Label", async () => {
|
|
137
|
+
await renderWithProviders(
|
|
138
|
+
<Field>
|
|
139
|
+
<Field.Label>Notifications</Field.Label>
|
|
140
|
+
<Switch accessibilityLabel="Push alerts" />
|
|
141
|
+
</Field>
|
|
142
|
+
);
|
|
143
|
+
|
|
144
|
+
expect(screen.getByRole("switch", { name: "Push alerts" })).toBeOnTheScreen();
|
|
145
|
+
});
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
describe("Switch gesture", () => {
|
|
149
|
+
test("a stationary press toggles", async () => {
|
|
150
|
+
const onSelectedChange = jest.fn();
|
|
151
|
+
await renderMeasured({ onSelectedChange });
|
|
152
|
+
|
|
153
|
+
await pan([{ state: State.BEGAN }, { state: State.END }]);
|
|
154
|
+
|
|
155
|
+
expect(onSelectedChange).toHaveBeenCalledWith(true);
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
test("a horizontal drag out and back is not a tap", async () => {
|
|
159
|
+
const onSelectedChange = jest.fn();
|
|
160
|
+
await renderMeasured({ onSelectedChange });
|
|
161
|
+
|
|
162
|
+
await pan([
|
|
163
|
+
{ state: State.BEGAN },
|
|
164
|
+
{ state: State.ACTIVE },
|
|
165
|
+
{ state: State.ACTIVE, translationX: 12 },
|
|
166
|
+
{ state: State.ACTIVE, translationX: 1 },
|
|
167
|
+
{ state: State.END, translationX: 1 },
|
|
168
|
+
]);
|
|
169
|
+
|
|
170
|
+
expect(onSelectedChange).not.toHaveBeenCalled();
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
test("a vertical drag out and back is not a tap", async () => {
|
|
174
|
+
const onSelectedChange = jest.fn();
|
|
175
|
+
await renderMeasured({ defaultSelected: true, onSelectedChange });
|
|
176
|
+
|
|
177
|
+
await pan([
|
|
178
|
+
{ state: State.BEGAN },
|
|
179
|
+
{ state: State.ACTIVE },
|
|
180
|
+
{ state: State.ACTIVE, translationY: 30 },
|
|
181
|
+
{ state: State.ACTIVE, translationY: 1 },
|
|
182
|
+
{ state: State.END, translationY: 1 },
|
|
183
|
+
]);
|
|
184
|
+
|
|
185
|
+
expect(onSelectedChange).not.toHaveBeenCalled();
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
test("a pan cancelled before it moved changes nothing", async () => {
|
|
189
|
+
const onSelectedChange = jest.fn();
|
|
190
|
+
await renderMeasured({ onSelectedChange });
|
|
191
|
+
|
|
192
|
+
await pan([{ state: State.BEGAN }, { state: State.CANCELLED }]);
|
|
193
|
+
|
|
194
|
+
expect(onSelectedChange).not.toHaveBeenCalled();
|
|
195
|
+
expect(screen.getByTestId("switch").props.accessibilityState).toMatchObject({ checked: false });
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
test("a pan cancelled after a drag past half way changes nothing", async () => {
|
|
199
|
+
const onSelectedChange = jest.fn();
|
|
200
|
+
await renderMeasured({ onSelectedChange });
|
|
201
|
+
|
|
202
|
+
await pan([
|
|
203
|
+
{ state: State.BEGAN },
|
|
204
|
+
{ state: State.ACTIVE },
|
|
205
|
+
{ state: State.ACTIVE, translationX: 40 },
|
|
206
|
+
{ state: State.CANCELLED, translationX: 40 },
|
|
207
|
+
]);
|
|
208
|
+
|
|
209
|
+
expect(onSelectedChange).not.toHaveBeenCalled();
|
|
210
|
+
expect(screen.getByTestId("switch").props.accessibilityState).toMatchObject({ checked: false });
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
// `fireGestureHandler` ends every sequence it is given, so one gesture cannot
|
|
214
|
+
// span the rerender, and it skips a disabled pan. This checks that a pan sent
|
|
215
|
+
// to a switch disabled after mount commits nothing. The `onFinalize` guard for
|
|
216
|
+
// a native pan disabled under the finger is not reachable from here.
|
|
217
|
+
test("a pan that finalizes after the switch was disabled changes nothing", async () => {
|
|
218
|
+
const onSelectedChange = jest.fn();
|
|
219
|
+
const view = await renderMeasured({ onSelectedChange });
|
|
220
|
+
|
|
221
|
+
await view.rerender(<Switch isDisabled onSelectedChange={onSelectedChange} testID="switch" />);
|
|
222
|
+
await pan([{ state: State.BEGAN }, { state: State.CANCELLED }]);
|
|
223
|
+
await pan([{ state: State.BEGAN }, { state: State.END }]);
|
|
224
|
+
|
|
225
|
+
expect(onSelectedChange).not.toHaveBeenCalled();
|
|
226
|
+
});
|
|
227
|
+
});
|
|
@@ -10,7 +10,7 @@ import {
|
|
|
10
10
|
useState,
|
|
11
11
|
} from "react";
|
|
12
12
|
import type { AccessibilityActionEvent, LayoutChangeEvent, ViewProps } from "react-native";
|
|
13
|
-
import { Gesture, GestureDetector } from "react-native-gesture-handler";
|
|
13
|
+
import { Gesture, GestureDetector, State } from "react-native-gesture-handler";
|
|
14
14
|
import Animated, {
|
|
15
15
|
cancelAnimation,
|
|
16
16
|
interpolateColor,
|
|
@@ -71,6 +71,8 @@ function SwitchRoot({
|
|
|
71
71
|
isInvalid,
|
|
72
72
|
haptic = "selection",
|
|
73
73
|
className,
|
|
74
|
+
accessibilityLabel,
|
|
75
|
+
accessibilityState,
|
|
74
76
|
children,
|
|
75
77
|
...props
|
|
76
78
|
}: SwitchProps): ReactElement {
|
|
@@ -96,6 +98,10 @@ function SwitchRoot({
|
|
|
96
98
|
// rather than state: nothing renders differently for it, so a commit on every
|
|
97
99
|
// touch would be pure cost.
|
|
98
100
|
const isPressed = useSharedValue(0);
|
|
101
|
+
// The furthest the finger got from where it landed, on either axis, over the
|
|
102
|
+
// whole gesture. Read at release rather than the final translation, so a drag
|
|
103
|
+
// out and back to its start is still a drag and not a tap.
|
|
104
|
+
const peakDistance = useSharedValue(0);
|
|
99
105
|
|
|
100
106
|
// A ref rather than state: the pan reads it twice a drag and nothing renders
|
|
101
107
|
// differently for it, so a re-render on touch-down would be pure cost.
|
|
@@ -139,56 +145,90 @@ function SwitchRoot({
|
|
|
139
145
|
[trackWidth]
|
|
140
146
|
);
|
|
141
147
|
|
|
142
|
-
const
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
148
|
+
const testID = props.testID;
|
|
149
|
+
const isGestureDisabled = axes.isDisabled;
|
|
150
|
+
const gesture = useMemo(() => {
|
|
151
|
+
const pan = Gesture.Pan()
|
|
152
|
+
.enabled(!isGestureDisabled)
|
|
153
|
+
.minDistance(0)
|
|
154
|
+
.shouldCancelWhenOutside(false)
|
|
155
|
+
.onBegin(() => {
|
|
156
|
+
"worklet";
|
|
157
|
+
grabbed.value = progress.value;
|
|
158
|
+
peakDistance.value = 0;
|
|
159
|
+
isPressed.value = withSpring(1, SWITCH_PRESS_SPRING);
|
|
160
|
+
scheduleOnRN(setDragging, true);
|
|
161
|
+
})
|
|
162
|
+
.onUpdate((event) => {
|
|
163
|
+
"worklet";
|
|
164
|
+
// Before the travel check: a vertical swipe moves no thumb but still
|
|
165
|
+
// counts as movement.
|
|
166
|
+
peakDistance.value = Math.max(peakDistance.value, Math.abs(event.translationX), Math.abs(event.translationY));
|
|
167
|
+
|
|
168
|
+
const travel = switchTravel({
|
|
169
|
+
inset: SWITCH_THUMB_INSET,
|
|
170
|
+
thumbWidth: thumbWidth.value,
|
|
171
|
+
trackWidth: trackWidth.value,
|
|
172
|
+
});
|
|
173
|
+
if (travel <= 0) return;
|
|
174
|
+
|
|
175
|
+
progress.value = Math.min(1, Math.max(0, grabbed.value + event.translationX / travel));
|
|
176
|
+
})
|
|
177
|
+
.onFinalize((event) => {
|
|
178
|
+
"worklet";
|
|
179
|
+
// A cancelled pan (the system took the touch, a parent claimed it, or
|
|
180
|
+
// the switch was disabled under the finger) commits nothing. Only
|
|
181
|
+
// CANCELLED: on iOS a stationary press never activates the pan and
|
|
182
|
+
// finalizes as FAILED, and that press is the tap.
|
|
183
|
+
if (event.state === PAN_CANCELLED || isGestureDisabled) {
|
|
184
|
+
progress.value = withSpring(selected ? 1 : 0, SWITCH_THUMB_SPRING);
|
|
178
185
|
isPressed.value = withSpring(0, SWITCH_PRESS_SPRING);
|
|
179
|
-
// At the commit, never at the grab. A drag taken half way and
|
|
180
|
-
// released back has changed nothing, and a switch that buzzed for it
|
|
181
|
-
// would be reporting a state change that did not happen.
|
|
182
|
-
if (haptic !== false && target !== selected) playHaptic(haptic);
|
|
183
|
-
|
|
184
|
-
// Order matters: the root must have stopped treating this as a live
|
|
185
|
-
// drag before `commit` asks it to re-sync. `scheduleOnRN` keeps them
|
|
186
|
-
// in the order they were queued.
|
|
187
186
|
scheduleOnRN(setDragging, false);
|
|
188
|
-
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
|
|
187
|
+
return;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
// The furthest the gesture got on whichever axis moved further. A
|
|
191
|
+
// vertical swipe that began on the switch has to count as movement, or
|
|
192
|
+
// every attempt to scroll past the control would read as a tap and
|
|
193
|
+
// toggle it. The peak, not the final translation, so a slow drag that
|
|
194
|
+
// returns near its start does not read as a tap either.
|
|
195
|
+
const target = resolveSwitchRelease({
|
|
196
|
+
distance: Math.max(peakDistance.value, Math.abs(event.translationX), Math.abs(event.translationY)),
|
|
197
|
+
progress: progress.value,
|
|
198
|
+
velocity: event.velocityX,
|
|
199
|
+
wasSelected: selected,
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
progress.value = withSpring(target ? 1 : 0, SWITCH_THUMB_SPRING);
|
|
203
|
+
isPressed.value = withSpring(0, SWITCH_PRESS_SPRING);
|
|
204
|
+
// At the commit, never at the grab. A drag taken half way and
|
|
205
|
+
// released back has changed nothing, and a switch that buzzed for it
|
|
206
|
+
// would be reporting a state change that did not happen.
|
|
207
|
+
if (haptic !== false && target !== selected) playHaptic(haptic);
|
|
208
|
+
|
|
209
|
+
// Order matters: the root must have stopped treating this as a live
|
|
210
|
+
// drag before `commit` asks it to re-sync. `scheduleOnRN` keeps them
|
|
211
|
+
// in the order they were queued.
|
|
212
|
+
scheduleOnRN(setDragging, false);
|
|
213
|
+
scheduleOnRN(commit, target);
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
// Lets a render test drive the pan through `fireGestureHandler`.
|
|
217
|
+
return testID ? pan.withTestId(`${testID}-pan`) : pan;
|
|
218
|
+
}, [
|
|
219
|
+
commit,
|
|
220
|
+
grabbed,
|
|
221
|
+
haptic,
|
|
222
|
+
isGestureDisabled,
|
|
223
|
+
isPressed,
|
|
224
|
+
peakDistance,
|
|
225
|
+
progress,
|
|
226
|
+
selected,
|
|
227
|
+
setDragging,
|
|
228
|
+
testID,
|
|
229
|
+
thumbWidth,
|
|
230
|
+
trackWidth,
|
|
231
|
+
]);
|
|
192
232
|
|
|
193
233
|
// Guarded here rather than at each caller: the pan is gated by `.enabled()`,
|
|
194
234
|
// but a screen reader's activation and an enclosing `Field`'s label press
|
|
@@ -264,13 +304,20 @@ function SwitchRoot({
|
|
|
264
304
|
[toggle]
|
|
265
305
|
);
|
|
266
306
|
|
|
307
|
+
// `accessibilityState` is merged, not replaced: a caller's `busy` must not
|
|
308
|
+
// drop the checked and disabled state a screen reader announces.
|
|
309
|
+
//
|
|
310
|
+
// The name falls back to the enclosing `Field.Label`. That label is a sibling,
|
|
311
|
+
// outside this element, so `accessible` alone would leave the switch unnamed.
|
|
312
|
+
// An explicit `accessibilityLabel` still wins.
|
|
267
313
|
return (
|
|
268
314
|
<SwitchProvider value={context}>
|
|
269
315
|
<GestureDetector gesture={gesture}>
|
|
270
316
|
<Animated.View
|
|
271
317
|
accessibilityActions={ACCESSIBILITY_ACTIONS}
|
|
318
|
+
accessibilityLabel={accessibilityLabel ?? field?.label ?? undefined}
|
|
272
319
|
accessibilityRole="switch"
|
|
273
|
-
accessibilityState={{ checked: selected, disabled: axes.isDisabled }}
|
|
320
|
+
accessibilityState={{ ...accessibilityState, checked: selected, disabled: axes.isDisabled }}
|
|
274
321
|
// Without this the view is not an accessibility element on iOS, and
|
|
275
322
|
// the role and the state above never reach VoiceOver. It also merges
|
|
276
323
|
// the parts into one element, which is what a control should be.
|
|
@@ -302,6 +349,9 @@ function SwitchRoot({
|
|
|
302
349
|
*/
|
|
303
350
|
const ACCESSIBILITY_ACTIONS = [{ name: "activate" }] as const;
|
|
304
351
|
|
|
352
|
+
/** Read inside the pan's worklets, so it is a plain number captured once. */
|
|
353
|
+
const PAN_CANCELLED = State.CANCELLED;
|
|
354
|
+
|
|
305
355
|
/**
|
|
306
356
|
* Composes a `Switch.Thumb` in, and puts it last however it was written.
|
|
307
357
|
*
|
|
@@ -386,6 +436,8 @@ function isSwitchThumbElement(child: ReactNode): boolean {
|
|
|
386
436
|
*
|
|
387
437
|
* There is no `Switch.Label`: the track is a fixed pill and a label cannot sit
|
|
388
438
|
* inside it, so the name is a `Field.Label` or a `ListGroup.ItemTitle` a row away.
|
|
439
|
+
* Inside a `Field`, the `Field.Label` text becomes the switch's accessible name
|
|
440
|
+
* unless an `accessibilityLabel` is passed.
|
|
389
441
|
* A switch with no label anywhere near it needs an `accessibilityLabel`, the same
|
|
390
442
|
* rule an icon-sized `Button` follows.
|
|
391
443
|
*
|
|
@@ -444,8 +444,10 @@ export function switchTravel({
|
|
|
444
444
|
* racing a `Tap` against it. `distance` is deliberately not the along-track
|
|
445
445
|
* translation: a vertical swipe that began on the switch moves nothing
|
|
446
446
|
* horizontally, and reading only that axis would turn every attempt to scroll
|
|
447
|
-
* past the control into a toggle.
|
|
448
|
-
*
|
|
447
|
+
* past the control into a toggle. It is also the peak over the gesture, not the
|
|
448
|
+
* translation at release, so a drag out and back is not a tap. A release
|
|
449
|
+
* travelling fast is a **flick**, and goes the way the finger was going however
|
|
450
|
+
* short of half way it stopped.
|
|
449
451
|
* Anything else is settled by position.
|
|
450
452
|
*
|
|
451
453
|
* Marked `"worklet"` and written flat — it calls nothing, not even to clamp.
|
|
@@ -463,7 +465,7 @@ export function resolveSwitchRelease({
|
|
|
463
465
|
wasSelected,
|
|
464
466
|
}: {
|
|
465
467
|
progress: number;
|
|
466
|
-
/**
|
|
468
|
+
/** The furthest the finger got over the gesture, in points, on whichever axis moved further. */
|
|
467
469
|
distance: number;
|
|
468
470
|
velocity: number;
|
|
469
471
|
wasSelected: boolean;
|