@delacour/react-native-ui 0.10.0-alpha.20261120132800 → 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 +3 -3
- package/src/components/screen/AGENTS.md +22 -0
- package/src/components/screen/screen-footer.tsx +33 -2
- package/src/components/screen/screen.context.tsx +10 -0
- package/src/components/screen/screen.render.test.tsx +90 -0
- package/src/components/screen/screen.variants.test.ts +6 -0
- package/src/components/screen/screen.variants.ts +8 -3
- package/src/components/screen/use-screen-scroll-insets.ts +26 -1
- package/src/components/switch/AGENTS.md +18 -3
- package/src/components/switch/switch-thumb.tsx +6 -2
- package/src/components/switch/switch.render.test.tsx +182 -1
- package/src/components/switch/switch.tsx +102 -50
- package/src/components/switch/switch.variants.ts +5 -3
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",
|
|
@@ -122,6 +122,28 @@ whole-screen states.
|
|
|
122
122
|
open keyboard and excludes the band the sticky shift parks behind it.
|
|
123
123
|
Conflating them is a real bug — the derivation is in the block comment above
|
|
124
124
|
them, and the tests pin both.
|
|
125
|
+
- **`keyboardOffset` stacks a sticky footer on a bar riding the keyboard.** A
|
|
126
|
+
form's previous / next / done toolbar sits on top of the keyboard, and a
|
|
127
|
+
sticky footer that only knew the keyboard's height parked its submit button
|
|
128
|
+
behind that bar. The footer's `offset.opened` becomes `bottom - keyboardOffset`,
|
|
129
|
+
and the footer publishes the value on the screen context
|
|
130
|
+
(`footer.keyboardOffset`) — reset to 0 on unmount — so
|
|
131
|
+
`useScreenFooterKeyboardClearance` adds the same distance and a keyboard-aware
|
|
132
|
+
scroll area keeps the focused field clear of the lifted footer. The value is
|
|
133
|
+
the distance from the keyboard's top edge to the bar's top edge — the bar's
|
|
134
|
+
height plus any gap it floats at (keyboard-controller's `KeyboardToolbar`
|
|
135
|
+
floats 11pt above a keyboard with rounded corners) — built from the bar's own
|
|
136
|
+
constants, never a literal: a literal drifts the day the bar is restyled, and
|
|
137
|
+
the footer then overlaps it or floats a gap above it. A footer that is not
|
|
138
|
+
sticky publishes 0, because it does not lift.
|
|
139
|
+
- **Two scrollables do not read `keyboardOffset` yet.** `resolveScrollBottomInset`
|
|
140
|
+
does not add it, so with the keyboard open and a list scrolled to its very end
|
|
141
|
+
the last `keyboardOffset` points of content sit under the footer. And
|
|
142
|
+
`Screen.ChatList` lifts its content by the keyboard minus its own `offset`
|
|
143
|
+
(the safe-area inset by default) and never reads the footer's, so a sticky
|
|
144
|
+
composer given a `keyboardOffset` covers the newest messages by that much —
|
|
145
|
+
pass the chat list `offset={bottom - keyboardOffset}` until it reads the
|
|
146
|
+
context.
|
|
125
147
|
|
|
126
148
|
## Lists
|
|
127
149
|
|
|
@@ -40,6 +40,19 @@ export type ScreenFooterProps = ScreenPlacementProps & {
|
|
|
40
40
|
* React Navigation both expose `useIsFocused()`.
|
|
41
41
|
*/
|
|
42
42
|
isFocused?: boolean;
|
|
43
|
+
/**
|
|
44
|
+
* Lift a sticky footer this many points further while the keyboard is open,
|
|
45
|
+
* so it sits on a bar that rides the keyboard — a form's previous / next /
|
|
46
|
+
* done toolbar — instead of hiding under it. A keyboard-aware scroll area
|
|
47
|
+
* clears the same extra distance, through the screen context.
|
|
48
|
+
*
|
|
49
|
+
* The distance from the keyboard's top edge to the bar's top edge: the bar's
|
|
50
|
+
* height plus any gap it floats at. Build it from the bar's own constants,
|
|
51
|
+
* never a literal. Only applies to a `sticky` footer.
|
|
52
|
+
*
|
|
53
|
+
* @default 0
|
|
54
|
+
*/
|
|
55
|
+
keyboardOffset?: number;
|
|
43
56
|
};
|
|
44
57
|
|
|
45
58
|
/**
|
|
@@ -80,12 +93,18 @@ export type ScreenFooterProps = ScreenPlacementProps & {
|
|
|
80
93
|
* <Screen.Footer sticky>
|
|
81
94
|
* <MessageComposer />
|
|
82
95
|
* </Screen.Footer>
|
|
96
|
+
*
|
|
97
|
+
* @example
|
|
98
|
+
* <Screen.Footer keyboardOffset={TOOLBAR_OFFSET} sticky>
|
|
99
|
+
* <Button onPress={submit}>Submit</Button>
|
|
100
|
+
* </Screen.Footer>
|
|
83
101
|
*/
|
|
84
102
|
export function ScreenFooter({
|
|
85
103
|
placement = "overlay",
|
|
86
104
|
sticky = false,
|
|
87
105
|
fadeBorderOnScroll = false,
|
|
88
106
|
isFocused = true,
|
|
107
|
+
keyboardOffset = 0,
|
|
89
108
|
className,
|
|
90
109
|
children,
|
|
91
110
|
style,
|
|
@@ -132,6 +151,17 @@ export function ScreenFooter({
|
|
|
132
151
|
footer.placement.value = placement;
|
|
133
152
|
}, [placement, footer.placement]);
|
|
134
153
|
|
|
154
|
+
// Published so a keyboard-aware scrollable clears the bar under the footer
|
|
155
|
+
// too, and cleared on unmount so a screen without the bar stops clearing it.
|
|
156
|
+
// A footer that is not sticky never lifts, so it publishes 0: otherwise the
|
|
157
|
+
// scrollable would scroll the focused field past a gap that is not there.
|
|
158
|
+
useEffect(() => {
|
|
159
|
+
footer.keyboardOffset.value = sticky ? keyboardOffset : 0;
|
|
160
|
+
return () => {
|
|
161
|
+
footer.keyboardOffset.value = 0;
|
|
162
|
+
};
|
|
163
|
+
}, [sticky, keyboardOffset, footer.keyboardOffset]);
|
|
164
|
+
|
|
135
165
|
useEffect(() => {
|
|
136
166
|
return () => {
|
|
137
167
|
footer.height.value = 0;
|
|
@@ -147,8 +177,9 @@ export function ScreenFooter({
|
|
|
147
177
|
// Shift by the inset rather than animating the padding away: the
|
|
148
178
|
// footer's safe-area band is meaningless once the keyboard covers it,
|
|
149
179
|
// and animating padding leaves a visible gap between the two
|
|
150
|
-
// mid-transition.
|
|
151
|
-
|
|
180
|
+
// mid-transition. `keyboardOffset` lifts it further, onto a bar that
|
|
181
|
+
// rides the keyboard.
|
|
182
|
+
offset={{ closed: 0, opened: bottom - keyboardOffset }}
|
|
152
183
|
style={{ paddingBottom: SCREEN_FLOATING_BOTTOM_GAP, paddingTop: SCREEN_FOOTER_PADDING }}
|
|
153
184
|
>
|
|
154
185
|
<ScreenFooterBackground fadeOnScroll={fadeBorderOnScroll} placement={placement} />
|
|
@@ -34,6 +34,13 @@ export type ScreenFooterMeasurements = {
|
|
|
34
34
|
* this — whoever drives it owns clearing it.
|
|
35
35
|
*/
|
|
36
36
|
overlayHeight: SharedValue<number>;
|
|
37
|
+
/**
|
|
38
|
+
* How far a sticky footer sits above the open keyboard's top edge — the
|
|
39
|
+
* height of a bar riding the keyboard under it, or 0. A keyboard-aware
|
|
40
|
+
* scrollable adds it to the clearance its focused input needs. The footer
|
|
41
|
+
* writes it from its `keyboardOffset` prop and resets it on unmount.
|
|
42
|
+
*/
|
|
43
|
+
keyboardOffset: SharedValue<number>;
|
|
37
44
|
};
|
|
38
45
|
|
|
39
46
|
/** Everything a screen's parts share, all of it readable from the UI thread. */
|
|
@@ -85,6 +92,7 @@ export function ScreenProvider({ parentContext = null, debug = false, children }
|
|
|
85
92
|
const footerHeight = useSharedValue<number>(0);
|
|
86
93
|
const footerInitialHeight = useSharedValue<number>(0);
|
|
87
94
|
const footerOverlayHeight = useSharedValue<number>(0);
|
|
95
|
+
const footerKeyboardOffset = useSharedValue<number>(0);
|
|
88
96
|
|
|
89
97
|
const scrollY = useSharedValue<number>(0);
|
|
90
98
|
const contentHeight = useSharedValue<number>(0);
|
|
@@ -100,6 +108,7 @@ export function ScreenProvider({ parentContext = null, debug = false, children }
|
|
|
100
108
|
height: footerHeight,
|
|
101
109
|
initialHeight: footerInitialHeight,
|
|
102
110
|
overlayHeight: footerOverlayHeight,
|
|
111
|
+
keyboardOffset: footerKeyboardOffset,
|
|
103
112
|
},
|
|
104
113
|
scrollY,
|
|
105
114
|
contentHeight,
|
|
@@ -114,6 +123,7 @@ export function ScreenProvider({ parentContext = null, debug = false, children }
|
|
|
114
123
|
footerHeight,
|
|
115
124
|
footerInitialHeight,
|
|
116
125
|
footerOverlayHeight,
|
|
126
|
+
footerKeyboardOffset,
|
|
117
127
|
scrollY,
|
|
118
128
|
contentHeight,
|
|
119
129
|
layoutHeight,
|
|
@@ -4,6 +4,7 @@ import { Text } from "react-native";
|
|
|
4
4
|
import { fireEvent, renderWithProviders, screen } from "../../test/render";
|
|
5
5
|
import { Button } from "../button";
|
|
6
6
|
import { Screen } from "./screen";
|
|
7
|
+
import { type ScreenContextValue, useScreen } from "./screen.context";
|
|
7
8
|
|
|
8
9
|
const tap = (name: string) => fireEvent(screen.getByRole("button", { name }), "accessibilityTap");
|
|
9
10
|
|
|
@@ -81,6 +82,95 @@ describe("Screen", () => {
|
|
|
81
82
|
expect(screen.getByText("Footer note")).toBeOnTheScreen();
|
|
82
83
|
});
|
|
83
84
|
|
|
85
|
+
test("a sticky footer's keyboardOffset lifts it that far above the open keyboard", async () => {
|
|
86
|
+
// The keyboard mock renders the sticky view as a plain View, so its offset is a prop on the
|
|
87
|
+
// nearest ancestor of the footer's content that carries one.
|
|
88
|
+
const stickyOffset = () => {
|
|
89
|
+
let node = screen.getByText("Footer note").parent;
|
|
90
|
+
while (node && node.props.offset === undefined) node = node.parent;
|
|
91
|
+
return node?.props.offset;
|
|
92
|
+
};
|
|
93
|
+
const view = await renderWithProviders(
|
|
94
|
+
<Screen>
|
|
95
|
+
<Screen.Footer sticky>
|
|
96
|
+
<Text>Footer note</Text>
|
|
97
|
+
</Screen.Footer>
|
|
98
|
+
</Screen>
|
|
99
|
+
);
|
|
100
|
+
expect(stickyOffset()).toEqual({ closed: 0, opened: 0 });
|
|
101
|
+
|
|
102
|
+
await view.rerender(
|
|
103
|
+
<Screen>
|
|
104
|
+
<Screen.Footer keyboardOffset={44} sticky>
|
|
105
|
+
<Text>Footer note</Text>
|
|
106
|
+
</Screen.Footer>
|
|
107
|
+
</Screen>
|
|
108
|
+
);
|
|
109
|
+
expect(stickyOffset()).toEqual({ closed: 0, opened: -44 });
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
test("a footer publishes its keyboardOffset on the screen context, and clears it when it unmounts", async () => {
|
|
113
|
+
const captured: { context: ScreenContextValue | null } = { context: null };
|
|
114
|
+
function Probe(): null {
|
|
115
|
+
captured.context = useScreen();
|
|
116
|
+
return null;
|
|
117
|
+
}
|
|
118
|
+
const view = await renderWithProviders(
|
|
119
|
+
<Screen>
|
|
120
|
+
<Probe />
|
|
121
|
+
<Screen.Footer keyboardOffset={44} sticky>
|
|
122
|
+
<Text>Footer note</Text>
|
|
123
|
+
</Screen.Footer>
|
|
124
|
+
</Screen>
|
|
125
|
+
);
|
|
126
|
+
expect(captured.context?.footer.keyboardOffset.value).toBe(44);
|
|
127
|
+
|
|
128
|
+
await view.rerender(
|
|
129
|
+
<Screen>
|
|
130
|
+
<Probe />
|
|
131
|
+
</Screen>
|
|
132
|
+
);
|
|
133
|
+
expect(captured.context?.footer.keyboardOffset.value).toBe(0);
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
test("a footer that is not sticky publishes no keyboardOffset, because it does not lift", async () => {
|
|
137
|
+
const captured: { context: ScreenContextValue | null } = { context: null };
|
|
138
|
+
function Probe(): null {
|
|
139
|
+
captured.context = useScreen();
|
|
140
|
+
return null;
|
|
141
|
+
}
|
|
142
|
+
await renderWithProviders(
|
|
143
|
+
<Screen>
|
|
144
|
+
<Probe />
|
|
145
|
+
<Screen.Footer keyboardOffset={44}>
|
|
146
|
+
<Text>Footer note</Text>
|
|
147
|
+
</Screen.Footer>
|
|
148
|
+
</Screen>
|
|
149
|
+
);
|
|
150
|
+
expect(captured.context?.footer.keyboardOffset.value).toBe(0);
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
test("a keyboard-aware scroll area clears a footer's keyboardOffset as well as the footer", async () => {
|
|
154
|
+
const clearance = () => screen.getByTestId("form").props.bottomOffset;
|
|
155
|
+
function Form({ keyboardOffset }: { keyboardOffset?: number }): ReactElement {
|
|
156
|
+
return (
|
|
157
|
+
<Screen>
|
|
158
|
+
<Screen.ScrollArea keyboardAware testID="form">
|
|
159
|
+
<Text>Field</Text>
|
|
160
|
+
</Screen.ScrollArea>
|
|
161
|
+
<Screen.Footer keyboardOffset={keyboardOffset} sticky>
|
|
162
|
+
<Text>Footer note</Text>
|
|
163
|
+
</Screen.Footer>
|
|
164
|
+
</Screen>
|
|
165
|
+
);
|
|
166
|
+
}
|
|
167
|
+
const view = await renderWithProviders(<Form />);
|
|
168
|
+
const base = clearance();
|
|
169
|
+
|
|
170
|
+
await view.rerender(<Form keyboardOffset={44} />);
|
|
171
|
+
expect(clearance()).toBe(base + 44);
|
|
172
|
+
});
|
|
173
|
+
|
|
84
174
|
test("a Screen.View renders its children", async () => {
|
|
85
175
|
await renderWithProviders(
|
|
86
176
|
<Screen>
|
|
@@ -87,6 +87,12 @@ describe("footerAboveKeyboard", () => {
|
|
|
87
87
|
expect(footerAboveKeyboard(58)).toBe(86);
|
|
88
88
|
expect(footerAboveKeyboard(0)).toBe(CHROME);
|
|
89
89
|
});
|
|
90
|
+
|
|
91
|
+
test("a keyboardOffset adds exactly that distance, for a bar riding the keyboard under the footer", () => {
|
|
92
|
+
for (const offset of [0, 42, 56]) {
|
|
93
|
+
expect(footerAboveKeyboard(58, offset) - footerAboveKeyboard(58)).toBe(offset);
|
|
94
|
+
}
|
|
95
|
+
});
|
|
90
96
|
});
|
|
91
97
|
|
|
92
98
|
describe("the layout constants", () => {
|
|
@@ -294,7 +294,9 @@ export function resolveScreenEdgePadding(
|
|
|
294
294
|
* the list's content bottom IS the screen bottom. Reserve = `c + m + b`.
|
|
295
295
|
* - OPEN: `KeyboardStickyView` translates by `-(K - b)` (its `offset.opened` is
|
|
296
296
|
* the inset), so the footer's `b` band ends up BEHIND the keyboard and its top
|
|
297
|
-
* edge is `keyboardTop - (c + m)`.
|
|
297
|
+
* edge is `keyboardTop - (c + m)`. A footer given a `keyboardOffset` `o` — a
|
|
298
|
+
* bar riding the keyboard under it — translates by `-(K - b + o)` instead, and
|
|
299
|
+
* its top edge is `keyboardTop - (c + m + o)`. The list lifts by `(K - b)`, because
|
|
298
300
|
* `KeyboardChatScrollView` scrolls by "distance (`keyboardHeight - offset`)
|
|
299
301
|
* instead of the full keyboard height" and `Screen.ChatList` passes the inset
|
|
300
302
|
* as that offset. Measured from the lifted content bottom, the footer's top is
|
|
@@ -352,10 +354,13 @@ export function footerOccupancy(measuredContentHeight: number, safeAreaBottom: n
|
|
|
352
354
|
*
|
|
353
355
|
* For `KeyboardAwareScrollView`'s `bottomOffset` — clearance between the focused
|
|
354
356
|
* input and the keyboard top, not clearance in content space.
|
|
357
|
+
*
|
|
358
|
+
* @param measuredContentHeight - `footer.initialHeight`, what `Screen.Footer` measures
|
|
359
|
+
* @param keyboardOffset - `footer.keyboardOffset`: how much further a sticky footer is lifted, onto a bar riding the keyboard
|
|
355
360
|
*/
|
|
356
|
-
export function footerAboveKeyboard(measuredContentHeight: number): number {
|
|
361
|
+
export function footerAboveKeyboard(measuredContentHeight: number, keyboardOffset = 0): number {
|
|
357
362
|
"worklet";
|
|
358
|
-
return measuredContentHeight + SCREEN_FOOTER_PADDING + SCREEN_FLOATING_BOTTOM_GAP;
|
|
363
|
+
return measuredContentHeight + SCREEN_FOOTER_PADDING + SCREEN_FLOATING_BOTTOM_GAP + keyboardOffset;
|
|
359
364
|
}
|
|
360
365
|
|
|
361
366
|
/** How a scrollable reserves space at its bottom edge. See {@link resolveScrollBottomInset}. */
|
|
@@ -49,6 +49,26 @@ function useFooterBaselineHeight(estimatedHeight = 0): number {
|
|
|
49
49
|
return measured;
|
|
50
50
|
}
|
|
51
51
|
|
|
52
|
+
/**
|
|
53
|
+
* The sticky footer's `keyboardOffset`, as JS state — mirrored from the UI
|
|
54
|
+
* thread for the same reason as {@link useFooterBaselineHeight}. Unlike the
|
|
55
|
+
* baseline, 0 is a real value here: the footer resets it on unmount.
|
|
56
|
+
*/
|
|
57
|
+
function useFooterKeyboardOffset(): number {
|
|
58
|
+
const { footer } = useScreenPart("a Screen scrollable");
|
|
59
|
+
const [offset, setOffset] = useState(0);
|
|
60
|
+
|
|
61
|
+
useAnimatedReaction(
|
|
62
|
+
() => Math.round(footer.keyboardOffset.value),
|
|
63
|
+
(current, previous) => {
|
|
64
|
+
if (current === previous) return;
|
|
65
|
+
scheduleOnRN(setOffset, current);
|
|
66
|
+
}
|
|
67
|
+
);
|
|
68
|
+
|
|
69
|
+
return offset;
|
|
70
|
+
}
|
|
71
|
+
|
|
52
72
|
/**
|
|
53
73
|
* The shared value a footer's own content drives when it expands VISUALLY above
|
|
54
74
|
* its measured layout box.
|
|
@@ -145,9 +165,14 @@ export function useChatComposerBaseSpacerHeight(estimatedFooterHeight = 0): Chat
|
|
|
145
165
|
* not just the keyboard. Deliberately NOT the full occupancy: the sticky shift
|
|
146
166
|
* parks the footer's safe-area band behind the keyboard, so counting it here
|
|
147
167
|
* would scroll the input further than it needs to go.
|
|
168
|
+
*
|
|
169
|
+
* A footer with a `keyboardOffset` sits that much higher, on a bar riding the
|
|
170
|
+
* keyboard, so the input clears that distance too.
|
|
148
171
|
*/
|
|
149
172
|
export function useScreenFooterKeyboardClearance(): number {
|
|
150
|
-
|
|
173
|
+
const baseline = useFooterBaselineHeight();
|
|
174
|
+
const keyboardOffset = useFooterKeyboardOffset();
|
|
175
|
+
return footerAboveKeyboard(baseline, keyboardOffset) + CHAT_COMPOSER_GAP;
|
|
151
176
|
}
|
|
152
177
|
|
|
153
178
|
/**
|
|
@@ -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;
|