mobile-design 0.0.0-stage → 0.1.1
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/COMPONENTS.md +3776 -0
- package/LICENSE +21 -0
- package/LICENSE-Apache-2.0.txt +55 -0
- package/NOTICE +11 -0
- package/README.md +1347 -2
- package/assets/fonts/MaterialSymbolsRounded.ttf +0 -0
- package/assets/fonts/MaterialSymbolsRounded.woff2 +0 -0
- package/assets/fonts/roboto/OFL.txt +93 -0
- package/assets/fonts/roboto/Roboto_400Regular.ttf +0 -0
- package/assets/fonts/roboto/Roboto_400Regular.woff2 +0 -0
- package/assets/fonts/roboto/Roboto_400Regular_Italic.ttf +0 -0
- package/assets/fonts/roboto/Roboto_400Regular_Italic.woff2 +0 -0
- package/assets/fonts/roboto/Roboto_500Medium.ttf +0 -0
- package/assets/fonts/roboto/Roboto_500Medium.woff2 +0 -0
- package/assets/fonts/roboto/Roboto_500Medium_Italic.ttf +0 -0
- package/assets/fonts/roboto/Roboto_500Medium_Italic.woff2 +0 -0
- package/assets/fonts/roboto/Roboto_600SemiBold.ttf +0 -0
- package/assets/fonts/roboto/Roboto_600SemiBold.woff2 +0 -0
- package/assets/fonts/roboto/Roboto_600SemiBold_Italic.ttf +0 -0
- package/assets/fonts/roboto/Roboto_600SemiBold_Italic.woff2 +0 -0
- package/assets/fonts/roboto/Roboto_700Bold.ttf +0 -0
- package/assets/fonts/roboto/Roboto_700Bold.woff2 +0 -0
- package/assets/fonts/roboto/Roboto_700Bold_Italic.ttf +0 -0
- package/assets/fonts/roboto/Roboto_700Bold_Italic.woff2 +0 -0
- package/assets/fonts/roboto/Roboto_800ExtraBold.ttf +0 -0
- package/assets/fonts/roboto/Roboto_800ExtraBold.woff2 +0 -0
- package/assets/fonts/roboto/Roboto_800ExtraBold_Italic.ttf +0 -0
- package/assets/fonts/roboto/Roboto_800ExtraBold_Italic.woff2 +0 -0
- package/assets/logo.svg +13 -0
- package/package.json +63 -3
- package/src/catalog.ts +683 -0
- package/src/color-scheme.ts +68 -0
- package/src/color.ts +35 -0
- package/src/components/action-surface.tsx +83 -0
- package/src/components/appbar-layout.ts +85 -0
- package/src/components/appbar-scroll-behavior.tsx +386 -0
- package/src/components/appbar.tsx +474 -0
- package/src/components/avatar.tsx +135 -0
- package/src/components/bottom-sheet-text-field.tsx +13 -0
- package/src/components/bottom-sheet-text-input.tsx +2 -0
- package/src/components/bottom-sheet-text-input.web.tsx +2 -0
- package/src/components/button-group.tsx +996 -0
- package/src/components/button.tsx +504 -0
- package/src/components/card.tsx +284 -0
- package/src/components/carousel-full-screen.tsx +374 -0
- package/src/components/carousel-layout.ts +418 -0
- package/src/components/carousel-scroll-controller.tsx +303 -0
- package/src/components/carousel-vertical-layout.ts +92 -0
- package/src/components/carousel-vertical.tsx +505 -0
- package/src/components/carousel.tsx +678 -0
- package/src/components/choice-motion.tsx +108 -0
- package/src/components/choice-types.ts +52 -0
- package/src/components/choice.tsx +400 -0
- package/src/components/choice.web.ts +1 -0
- package/src/components/circular-progress-status.tsx +218 -0
- package/src/components/container-transform.tsx +304 -0
- package/src/components/content.tsx +1450 -0
- package/src/components/date-picker-calendar.tsx +933 -0
- package/src/components/date-picker-months.tsx +102 -0
- package/src/components/date-picker-utils.ts +96 -0
- package/src/components/date-picker.tsx +641 -0
- package/src/components/dialog.tsx +340 -0
- package/src/components/docked-date-picker.tsx +525 -0
- package/src/components/drawer.tsx +248 -0
- package/src/components/fab.tsx +1221 -0
- package/src/components/focus-order.ts +40 -0
- package/src/components/full-screen-dialog.tsx +234 -0
- package/src/components/icon-button.tsx +72 -0
- package/src/components/list-focus.tsx +182 -0
- package/src/components/list-reorder.tsx +349 -0
- package/src/components/loading-indicator.tsx +378 -0
- package/src/components/menu.tsx +1091 -0
- package/src/components/navigation-bar.tsx +390 -0
- package/src/components/navigation-focus.ts +45 -0
- package/src/components/navigation-rail-layout.ts +53 -0
- package/src/components/navigation.tsx +682 -0
- package/src/components/popup.tsx +624 -0
- package/src/components/press-scope.ts +17 -0
- package/src/components/progress-indicator-paths.ts +254 -0
- package/src/components/progress-indicator.tsx +570 -0
- package/src/components/progress.tsx +39 -0
- package/src/components/pull-to-refresh.tsx +379 -0
- package/src/components/rich-tooltip.tsx +183 -0
- package/src/components/search-icon-button.tsx +8 -0
- package/src/components/search-trigger.tsx +28 -0
- package/src/components/search-view.tsx +790 -0
- package/src/components/searchbar.tsx +190 -0
- package/src/components/segmented-buttons.tsx +456 -0
- package/src/components/selection-controls.tsx +760 -0
- package/src/components/sheets.tsx +1027 -0
- package/src/components/slider.tsx +1290 -0
- package/src/components/snackbar.tsx +430 -0
- package/src/components/split-button.tsx +525 -0
- package/src/components/swipe-list-item.tsx +793 -0
- package/src/components/switch.tsx +319 -0
- package/src/components/tabs-pager.tsx +241 -0
- package/src/components/tabs-pager.web.tsx +110 -0
- package/src/components/tabs.tsx +759 -0
- package/src/components/text-field.tsx +673 -0
- package/src/components/time-picker.tsx +1248 -0
- package/src/components/toolbar.tsx +590 -0
- package/src/components/tooltip.tsx +273 -0
- package/src/components/web-choice.tsx +572 -0
- package/src/core.tsx +169 -0
- package/src/font-assets.ts +28 -0
- package/src/font-assets.web.ts +25 -0
- package/src/index.ts +139 -0
- package/src/material-symbol-codepoints.ts +67 -0
- package/src/material-symbol-paths.ts +340 -0
- package/src/material-symbols.tsx +155 -0
- package/src/native-modal.tsx +50 -0
- package/src/outside-interaction.tsx +39 -0
- package/src/paper-icon-aliases.ts +147 -0
- package/src/paper-internals.d.ts +15 -0
- package/src/paper-motion-bridge.tsx +11 -0
- package/src/paper-motion.tsx +24 -0
- package/src/primitives.tsx +13 -0
- package/src/ripple.tsx +7 -0
- package/src/ripple.web.tsx +108 -0
- package/src/scroll-indicators.ts +12 -0
- package/src/typography.ts +9 -0
- package/src/web-accessibility.ts +19 -0
|
@@ -0,0 +1,1290 @@
|
|
|
1
|
+
import { sliderSizeTokens } from '@mobile-design/tokens';
|
|
2
|
+
import { webAccessibility } from '../web-accessibility';
|
|
3
|
+
import React, { useCallback, useEffect, useLayoutEffect, useMemo, useRef, useState } from 'react';
|
|
4
|
+
import { Platform, StyleSheet, TextInput, View, useWindowDimensions, type StyleProp, type ViewStyle } from 'react-native';
|
|
5
|
+
import { Icon, useLocale, useTheme, type Theme } from 'react-native-paper';
|
|
6
|
+
import { GestureDetector, GestureStateManager, useExclusiveGestures, usePanGesture, useTapGesture } from 'react-native-gesture-handler';
|
|
7
|
+
import Animated, {
|
|
8
|
+
cancelAnimation,
|
|
9
|
+
Easing,
|
|
10
|
+
ReduceMotion,
|
|
11
|
+
useAnimatedProps,
|
|
12
|
+
useAnimatedReaction,
|
|
13
|
+
useAnimatedStyle,
|
|
14
|
+
useSharedValue,
|
|
15
|
+
withDelay,
|
|
16
|
+
withTiming,
|
|
17
|
+
type SharedValue,
|
|
18
|
+
} from 'react-native-reanimated';
|
|
19
|
+
import { scheduleOnRN } from 'react-native-worklets';
|
|
20
|
+
|
|
21
|
+
export interface SliderBaseProps {
|
|
22
|
+
minimumValue?: number;
|
|
23
|
+
maximumValue?: number;
|
|
24
|
+
/** 0 is continuous; a positive step includes both endpoints as selectable values. */
|
|
25
|
+
step?: number;
|
|
26
|
+
disabled?: boolean;
|
|
27
|
+
showTicks?: boolean;
|
|
28
|
+
showStopIndicator?: boolean;
|
|
29
|
+
valueIndicator?: 'on-drag' | 'always' | 'never';
|
|
30
|
+
valueSuffix?: string;
|
|
31
|
+
/** Optional live value readout. Use an empty string to show only the value. */
|
|
32
|
+
valueLabel?: string;
|
|
33
|
+
/** Decimal places in the value indicator and accessible text, independent of the slider's step. */
|
|
34
|
+
valuePrecision?: number;
|
|
35
|
+
orientation?: 'horizontal' | 'vertical';
|
|
36
|
+
size?: 'xs' | 'sm' | 'md' | 'lg' | 'xl';
|
|
37
|
+
/** Width for horizontal sliders, height for vertical sliders (default240dp). */
|
|
38
|
+
length?: number;
|
|
39
|
+
/** Optional, evenly spaced visual marks on a continuous track; does not add detents. */
|
|
40
|
+
continuousTickCount?: number;
|
|
41
|
+
/** Inset icons are available on standard md/lg/xl sliders. */
|
|
42
|
+
leadingIcon?: React.ComponentProps<typeof Icon>['source'];
|
|
43
|
+
trailingIcon?: React.ComponentProps<typeof Icon>['source'];
|
|
44
|
+
accessibilityLabel?: string;
|
|
45
|
+
accessibilityHint?: string;
|
|
46
|
+
/** Mirrors the track direction, without changing the numeric values. */
|
|
47
|
+
reversed?: boolean;
|
|
48
|
+
style?: StyleProp<ViewStyle>;
|
|
49
|
+
testID?: string;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export interface SliderProps extends SliderBaseProps {
|
|
53
|
+
/** Active track extends from the middle of the domain; useful for positive/negative values. */
|
|
54
|
+
centered?: boolean;
|
|
55
|
+
value: number;
|
|
56
|
+
onValueChange: (value: number) => void;
|
|
57
|
+
onSlidingStart?: (value: number) => void;
|
|
58
|
+
onSlidingComplete?: (value: number) => void;
|
|
59
|
+
/** An interrupted interaction ends without committing; the consumer may restore its accepted value. */
|
|
60
|
+
onSlidingCancel?: (value: number) => void;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export interface RangeSliderProps extends SliderBaseProps {
|
|
64
|
+
value: readonly [number, number];
|
|
65
|
+
onValueChange: (value: [number, number]) => void;
|
|
66
|
+
onSlidingStart?: (value: [number, number]) => void;
|
|
67
|
+
onSlidingComplete?: (value: [number, number]) => void;
|
|
68
|
+
onSlidingCancel?: (value: [number, number]) => void;
|
|
69
|
+
minimumRange?: number;
|
|
70
|
+
startAccessibilityLabel?: string;
|
|
71
|
+
endAccessibilityLabel?: string;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
type InternalProps = SliderBaseProps & {
|
|
75
|
+
value: readonly [number, number];
|
|
76
|
+
range: boolean;
|
|
77
|
+
centered?: boolean;
|
|
78
|
+
minimumRange?: number;
|
|
79
|
+
onValueChange: (value: [number, number]) => void;
|
|
80
|
+
onSlidingStart?: (value: [number, number]) => void;
|
|
81
|
+
onSlidingComplete?: (value: [number, number]) => void;
|
|
82
|
+
onSlidingCancel?: (value: [number, number]) => void;
|
|
83
|
+
startAccessibilityLabel?: string;
|
|
84
|
+
endAccessibilityLabel?: string;
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
/** MD3 slider, horizontal or vertical: continuous or discrete, with UI-thread tracking. */
|
|
88
|
+
export function Slider({ value, onValueChange, onSlidingStart, onSlidingComplete, onSlidingCancel, ...props }: SliderProps) {
|
|
89
|
+
const min = props.minimumValue ?? 0;
|
|
90
|
+
return (
|
|
91
|
+
<SliderControl
|
|
92
|
+
{...props}
|
|
93
|
+
value={[min, value]}
|
|
94
|
+
range={false}
|
|
95
|
+
onValueChange={([, next]) => onValueChange(next)}
|
|
96
|
+
onSlidingStart={onSlidingStart ? ([, next]) => onSlidingStart(next) : undefined}
|
|
97
|
+
onSlidingComplete={onSlidingComplete ? ([, next]) => onSlidingComplete(next) : undefined}
|
|
98
|
+
onSlidingCancel={onSlidingCancel ? ([, next]) => onSlidingCancel(next) : undefined}
|
|
99
|
+
/>
|
|
100
|
+
);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** Two independently adjustable thumbs; thumbs never cross. */
|
|
104
|
+
export function RangeSlider(props: RangeSliderProps) {
|
|
105
|
+
return <SliderControl {...props} range />;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
function clamp(value: number, min: number, max: number) {
|
|
109
|
+
'worklet';
|
|
110
|
+
return Math.min(max, Math.max(min, value));
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function snap(value: number, min: number, max: number, step: number) {
|
|
114
|
+
'worklet';
|
|
115
|
+
if (!step) return clamp(value, min, max);
|
|
116
|
+
const stepped = clamp(min + Math.round((value - min) / step) * step, min, max);
|
|
117
|
+
// A final partial step still permits selecting maximumValue.
|
|
118
|
+
return Math.abs(value - max) < Math.abs(value - stepped) ? max : Number(stepped.toFixed(10));
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function constrainedSnap(value: number, min: number, max: number, step: number, lower: number, upper: number) {
|
|
122
|
+
'worklet';
|
|
123
|
+
let next = snap(value, min, max, step);
|
|
124
|
+
if (step && next < lower) next = Math.min(max, min + Math.ceil((lower - min) / step) * step);
|
|
125
|
+
if (step && next > upper) next = min + Math.floor((upper - min) / step) * step;
|
|
126
|
+
return clamp(Number(next.toFixed(10)), lower, upper);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
function decimalPlaces(number: number) {
|
|
130
|
+
const [mantissa, exponent = '0'] = String(number).split('e');
|
|
131
|
+
return Math.max(0, (mantissa.split('.')[1]?.length ?? 0) - Number(exponent));
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
const INDICATOR_GAP = 4;
|
|
135
|
+
const INDICATOR_PADDING = 8;
|
|
136
|
+
|
|
137
|
+
function formatSliderValue(value: number, precision: number, suffix: string) {
|
|
138
|
+
'worklet';
|
|
139
|
+
return `${precision === 0 ? Math.round(value) : Number(value.toFixed(precision))}${suffix}`;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
function indicatorWidthFor(minimum: number, maximum: number, precision: number, suffix: string, fontScale: number) {
|
|
143
|
+
const digits = Math.max(String(Math.trunc(minimum)).length, String(Math.trunc(maximum)).length) + (precision ? precision + 1 : 0);
|
|
144
|
+
return Math.max(48, (digits + suffix.length) * 8 * fontScale + INDICATOR_PADDING * 2);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
function SliderControl({
|
|
148
|
+
value,
|
|
149
|
+
onValueChange,
|
|
150
|
+
onSlidingStart,
|
|
151
|
+
onSlidingComplete,
|
|
152
|
+
onSlidingCancel,
|
|
153
|
+
range,
|
|
154
|
+
centered = false,
|
|
155
|
+
minimumValue = 0,
|
|
156
|
+
maximumValue = 1,
|
|
157
|
+
step = 0,
|
|
158
|
+
minimumRange = 0,
|
|
159
|
+
disabled = false,
|
|
160
|
+
showTicks = step > 0,
|
|
161
|
+
showStopIndicator = true,
|
|
162
|
+
valueIndicator = 'never',
|
|
163
|
+
valueSuffix = '',
|
|
164
|
+
valueLabel,
|
|
165
|
+
valuePrecision,
|
|
166
|
+
orientation = 'horizontal',
|
|
167
|
+
size = 'xs',
|
|
168
|
+
length: componentLength,
|
|
169
|
+
continuousTickCount = 0,
|
|
170
|
+
leadingIcon,
|
|
171
|
+
trailingIcon,
|
|
172
|
+
accessibilityLabel = 'Slider',
|
|
173
|
+
accessibilityHint,
|
|
174
|
+
startAccessibilityLabel,
|
|
175
|
+
endAccessibilityLabel,
|
|
176
|
+
reversed: reversedProp,
|
|
177
|
+
style,
|
|
178
|
+
testID = range ? 'range-slider' : 'slider',
|
|
179
|
+
}: InternalProps) {
|
|
180
|
+
const theme = useTheme<Theme>();
|
|
181
|
+
const { fontScale } = useWindowDimensions();
|
|
182
|
+
const { direction } = useLocale();
|
|
183
|
+
const rtl = direction === 'rtl';
|
|
184
|
+
const vertical = orientation === 'vertical';
|
|
185
|
+
const reversed = reversedProp ?? (vertical || rtl);
|
|
186
|
+
const dimensions = sliderSizeTokens[size];
|
|
187
|
+
const crossSize = Math.max(48, dimensions.handle);
|
|
188
|
+
// Material Android increases side padding with track thickness; keep at least24dp for touch.
|
|
189
|
+
const axisPadding = Math.max(24, 16 + (dimensions.track - 4) / 2);
|
|
190
|
+
if (
|
|
191
|
+
!Number.isFinite(minimumValue) ||
|
|
192
|
+
!Number.isFinite(maximumValue) ||
|
|
193
|
+
maximumValue <= minimumValue ||
|
|
194
|
+
!Number.isFinite(step) ||
|
|
195
|
+
step < 0 ||
|
|
196
|
+
!Number.isFinite(minimumRange) ||
|
|
197
|
+
minimumRange < 0 ||
|
|
198
|
+
minimumRange > maximumValue - minimumValue ||
|
|
199
|
+
value.some((entry) => !Number.isFinite(entry))
|
|
200
|
+
) {
|
|
201
|
+
throw new Error('Slider requires finite, ordered bounds, finite values and a non-negative step/minimumRange.');
|
|
202
|
+
}
|
|
203
|
+
if ((leadingIcon || trailingIcon) && (range || centered || !dimensions.icon))
|
|
204
|
+
throw new Error('Inset icons require a standard Slider in md, lg or xl size.');
|
|
205
|
+
if (
|
|
206
|
+
!Number.isInteger(continuousTickCount) ||
|
|
207
|
+
continuousTickCount < 0 ||
|
|
208
|
+
(componentLength !== undefined && (!Number.isFinite(componentLength) || componentLength <= axisPadding * 2))
|
|
209
|
+
)
|
|
210
|
+
throw new Error('Slider length must fit its track; continuousTickCount must be a non-negative integer.');
|
|
211
|
+
if (valuePrecision !== undefined && (!Number.isInteger(valuePrecision) || valuePrecision < 0 || valuePrecision > 10))
|
|
212
|
+
throw new Error('Slider valuePrecision must be an integer between 0 and 10.');
|
|
213
|
+
const span = maximumValue - minimumValue;
|
|
214
|
+
const precision =
|
|
215
|
+
valuePrecision ?? Math.min(10, Math.max(step ? decimalPlaces(step) : 2, decimalPlaces(minimumValue), decimalPlaces(maximumValue)));
|
|
216
|
+
const initialEnd = constrainedSnap(value[1], minimumValue, maximumValue, step, minimumValue + (range ? minimumRange : 0), maximumValue);
|
|
217
|
+
const initialStart = range
|
|
218
|
+
? constrainedSnap(value[0], minimumValue, maximumValue, step, minimumValue, initialEnd - minimumRange)
|
|
219
|
+
: minimumValue;
|
|
220
|
+
const start = useSharedValue(clamp(initialStart, minimumValue, maximumValue));
|
|
221
|
+
const end = useSharedValue(initialEnd);
|
|
222
|
+
// Semantic values commit immediately. Only a track tap animates the geometry;
|
|
223
|
+
// dragging stays attached to the finger, including when it interrupts a tap.
|
|
224
|
+
const displayStart = useSharedValue(initialStart);
|
|
225
|
+
const displayEnd = useSharedValue(initialEnd);
|
|
226
|
+
const active = useSharedValue(-1);
|
|
227
|
+
const tapThumb = useSharedValue(-1);
|
|
228
|
+
const focusedThumb = useSharedValue(-1);
|
|
229
|
+
const indicatorOwner = useSharedValue(range ? 0 : 1);
|
|
230
|
+
const dragging = useSharedValue(false);
|
|
231
|
+
const pendingDirection = useSharedValue(false);
|
|
232
|
+
const tapMultiplePointers = useSharedValue(false);
|
|
233
|
+
const width = useSharedValue(0);
|
|
234
|
+
const [layoutWidth, setLayoutWidth] = useState(0);
|
|
235
|
+
const [a11yValue, setA11yValue] = useState<[number, number]>([initialStart, initialEnd]);
|
|
236
|
+
const [interactionRevision, setInteractionRevision] = useState(0);
|
|
237
|
+
const callbacks = useRef({ onValueChange, onSlidingStart, onSlidingComplete, onSlidingCancel });
|
|
238
|
+
const disposed = useRef(false);
|
|
239
|
+
const lastEmitted = useRef<[number, number]>([initialStart, initialEnd]);
|
|
240
|
+
const gestureSequence = useSharedValue(0);
|
|
241
|
+
const settledGesture = useRef(0);
|
|
242
|
+
const activeGesture = useRef<number | null>(null);
|
|
243
|
+
// Controlled value updates during a drag are expected. Changes to the
|
|
244
|
+
// domain, direction or geometry revoke that gesture and its queued callbacks.
|
|
245
|
+
const configuration = JSON.stringify([
|
|
246
|
+
minimumValue,
|
|
247
|
+
maximumValue,
|
|
248
|
+
step,
|
|
249
|
+
minimumRange,
|
|
250
|
+
range,
|
|
251
|
+
vertical,
|
|
252
|
+
reversed,
|
|
253
|
+
size,
|
|
254
|
+
componentLength,
|
|
255
|
+
layoutWidth,
|
|
256
|
+
rtl,
|
|
257
|
+
valueIndicator,
|
|
258
|
+
disabled,
|
|
259
|
+
]);
|
|
260
|
+
const configurationRef = useRef(configuration);
|
|
261
|
+
const configurationUI = useSharedValue(configuration);
|
|
262
|
+
const cancel = useCallback(
|
|
263
|
+
(lower: number, upper: number, gesture: number, owner: string, updateState = true, revokeOwned = false) => {
|
|
264
|
+
if (disposed.current || owner !== configurationRef.current || gesture <= settledGesture.current) return;
|
|
265
|
+
if (
|
|
266
|
+
!revokeOwned &&
|
|
267
|
+
(gesture !== gestureSequence.get() || (activeGesture.current !== null && activeGesture.current !== gesture))
|
|
268
|
+
)
|
|
269
|
+
return;
|
|
270
|
+
settledGesture.current = gesture;
|
|
271
|
+
activeGesture.current = null;
|
|
272
|
+
if (updateState) {
|
|
273
|
+
setA11yValue([lower, upper]);
|
|
274
|
+
setInteractionRevision((revision) => revision + 1);
|
|
275
|
+
}
|
|
276
|
+
callbacks.current.onSlidingCancel?.([lower, upper]);
|
|
277
|
+
},
|
|
278
|
+
[gestureSequence]
|
|
279
|
+
);
|
|
280
|
+
const cancelOwnedGesture = useCallback(
|
|
281
|
+
(updateState = true) => {
|
|
282
|
+
tapThumb.set(-1);
|
|
283
|
+
const gesture = gestureSequence.get();
|
|
284
|
+
if (dragging.get() || activeGesture.current !== null) {
|
|
285
|
+
dragging.set(false);
|
|
286
|
+
active.set(-1);
|
|
287
|
+
pendingDirection.set(false);
|
|
288
|
+
cancelAnimation(displayStart);
|
|
289
|
+
cancelAnimation(displayEnd);
|
|
290
|
+
displayStart.set(start.get());
|
|
291
|
+
displayEnd.set(end.get());
|
|
292
|
+
// The owner can revoke a UI drag whose begin notification is still
|
|
293
|
+
// queued. Retire the latest sequence without leaving an older RN
|
|
294
|
+
// interaction active; ordinary native cancel callbacks stay guarded.
|
|
295
|
+
cancel(start.get(), end.get(), gesture, configurationRef.current, updateState, true);
|
|
296
|
+
}
|
|
297
|
+
},
|
|
298
|
+
[active, cancel, dragging, end, gestureSequence, pendingDirection, start, displayStart, displayEnd, tapThumb]
|
|
299
|
+
);
|
|
300
|
+
useLayoutEffect(() => {
|
|
301
|
+
callbacks.current = { onValueChange, onSlidingStart, onSlidingComplete, onSlidingCancel };
|
|
302
|
+
if (configurationRef.current !== configuration) {
|
|
303
|
+
cancelOwnedGesture();
|
|
304
|
+
cancelAnimation(displayStart);
|
|
305
|
+
cancelAnimation(displayEnd);
|
|
306
|
+
displayStart.set(start.get());
|
|
307
|
+
displayEnd.set(end.get());
|
|
308
|
+
configurationRef.current = configuration;
|
|
309
|
+
configurationUI.set(configuration);
|
|
310
|
+
}
|
|
311
|
+
}, [
|
|
312
|
+
configuration,
|
|
313
|
+
configurationUI,
|
|
314
|
+
cancelOwnedGesture,
|
|
315
|
+
displayStart,
|
|
316
|
+
displayEnd,
|
|
317
|
+
start,
|
|
318
|
+
end,
|
|
319
|
+
onValueChange,
|
|
320
|
+
onSlidingStart,
|
|
321
|
+
onSlidingComplete,
|
|
322
|
+
onSlidingCancel,
|
|
323
|
+
]);
|
|
324
|
+
useEffect(() => {
|
|
325
|
+
disposed.current = false;
|
|
326
|
+
return () => {
|
|
327
|
+
cancelOwnedGesture(false);
|
|
328
|
+
cancelAnimation(displayStart);
|
|
329
|
+
cancelAnimation(displayEnd);
|
|
330
|
+
disposed.current = true;
|
|
331
|
+
};
|
|
332
|
+
}, [cancelOwnedGesture, displayStart, displayEnd]);
|
|
333
|
+
const indicatorHeight = Math.max(44, 20 * fontScale + 24);
|
|
334
|
+
const preferredWidth = indicatorWidthFor(minimumValue, maximumValue, precision, valueSuffix, fontScale);
|
|
335
|
+
const indicatorWidth = !vertical && layoutWidth > 0 ? Math.min(preferredWidth, layoutWidth) : preferredWidth;
|
|
336
|
+
// Keep the slot at rest so feedback cannot cover adjacent text or move the track.
|
|
337
|
+
const indicatorSpace =
|
|
338
|
+
valueIndicator === 'never' ? 0 : (vertical ? indicatorWidth : indicatorHeight) + INDICATOR_GAP + (dimensions.handle - crossSize) / 2;
|
|
339
|
+
const readoutTypography = vertical ? theme.fonts.bodySmall : theme.fonts.bodyLarge;
|
|
340
|
+
const readoutHeight = (readoutTypography.lineHeight ?? (vertical ? 16 : 24)) * fontScale;
|
|
341
|
+
const verticalLength = componentLength ?? 240;
|
|
342
|
+
|
|
343
|
+
useEffect(() => {
|
|
344
|
+
if (dragging.get()) return;
|
|
345
|
+
const upper = constrainedSnap(value[1], minimumValue, maximumValue, step, minimumValue + (range ? minimumRange : 0), maximumValue);
|
|
346
|
+
const lower = range
|
|
347
|
+
? constrainedSnap(value[0], minimumValue, maximumValue, step, minimumValue, upper - minimumRange)
|
|
348
|
+
: minimumValue;
|
|
349
|
+
if (start.get() !== lower) displayStart.set(lower);
|
|
350
|
+
if (end.get() !== upper) displayEnd.set(upper);
|
|
351
|
+
start.set(lower);
|
|
352
|
+
end.set(upper);
|
|
353
|
+
setA11yValue([lower, upper]);
|
|
354
|
+
lastEmitted.current = [lower, upper];
|
|
355
|
+
}, [
|
|
356
|
+
value[0],
|
|
357
|
+
value[1],
|
|
358
|
+
minimumValue,
|
|
359
|
+
maximumValue,
|
|
360
|
+
step,
|
|
361
|
+
minimumRange,
|
|
362
|
+
range,
|
|
363
|
+
start,
|
|
364
|
+
end,
|
|
365
|
+
displayStart,
|
|
366
|
+
displayEnd,
|
|
367
|
+
dragging,
|
|
368
|
+
interactionRevision,
|
|
369
|
+
]);
|
|
370
|
+
|
|
371
|
+
const publishValue = useCallback(
|
|
372
|
+
(lower: number, upper: number, gesture?: number, owner?: string) => {
|
|
373
|
+
if (
|
|
374
|
+
disposed.current ||
|
|
375
|
+
(gesture !== undefined &&
|
|
376
|
+
(gesture !== gestureSequence.get() || gesture <= settledGesture.current || owner !== configurationRef.current))
|
|
377
|
+
)
|
|
378
|
+
return;
|
|
379
|
+
if (lastEmitted.current[0] === lower && lastEmitted.current[1] === upper) return;
|
|
380
|
+
lastEmitted.current = [lower, upper];
|
|
381
|
+
setA11yValue([lower, upper]);
|
|
382
|
+
callbacks.current.onValueChange([lower, upper]);
|
|
383
|
+
},
|
|
384
|
+
[gestureSequence]
|
|
385
|
+
);
|
|
386
|
+
const begin = useCallback(
|
|
387
|
+
(lower: number, upper: number, gesture?: number, owner?: string) => {
|
|
388
|
+
if (
|
|
389
|
+
disposed.current ||
|
|
390
|
+
(gesture !== undefined &&
|
|
391
|
+
(gesture !== gestureSequence.get() || gesture <= settledGesture.current || owner !== configurationRef.current))
|
|
392
|
+
)
|
|
393
|
+
return;
|
|
394
|
+
activeGesture.current = gesture ?? null;
|
|
395
|
+
callbacks.current.onSlidingStart?.([lower, upper]);
|
|
396
|
+
},
|
|
397
|
+
[gestureSequence]
|
|
398
|
+
);
|
|
399
|
+
const finish = useCallback(
|
|
400
|
+
(lower: number, upper: number, gesture?: number, owner?: string) => {
|
|
401
|
+
if (
|
|
402
|
+
disposed.current ||
|
|
403
|
+
(gesture !== undefined &&
|
|
404
|
+
(gesture !== gestureSequence.get() || gesture <= settledGesture.current || owner !== configurationRef.current))
|
|
405
|
+
)
|
|
406
|
+
return;
|
|
407
|
+
if (gesture !== undefined) settledGesture.current = gesture;
|
|
408
|
+
activeGesture.current = null;
|
|
409
|
+
setA11yValue([lower, upper]);
|
|
410
|
+
setInteractionRevision((revision) => revision + 1);
|
|
411
|
+
callbacks.current.onSlidingComplete?.([lower, upper]);
|
|
412
|
+
},
|
|
413
|
+
[gestureSequence]
|
|
414
|
+
);
|
|
415
|
+
|
|
416
|
+
// Report discrete numeric changes, never use React state to position a thumb/track.
|
|
417
|
+
// Continuous callbacks use 1/1000 of the domain: enough precision without per-pixel JS traffic.
|
|
418
|
+
useAnimatedReaction(
|
|
419
|
+
() => {
|
|
420
|
+
if (!dragging.get()) return null;
|
|
421
|
+
const unit = step || span / 1000;
|
|
422
|
+
return [Math.round((start.get() - minimumValue) / unit), Math.round((end.get() - minimumValue) / unit)];
|
|
423
|
+
},
|
|
424
|
+
(next, previous) => {
|
|
425
|
+
if (next && (!previous || next[0] !== previous[0] || next[1] !== previous[1])) {
|
|
426
|
+
scheduleOnRN(publishValue, start.get(), end.get(), gestureSequence.get(), configuration);
|
|
427
|
+
}
|
|
428
|
+
},
|
|
429
|
+
Platform.OS === 'web' ? [publishValue, step, span, minimumValue, configuration, gestureSequence] : undefined
|
|
430
|
+
);
|
|
431
|
+
|
|
432
|
+
const updateAt = (x: number, animate = false) => {
|
|
433
|
+
'worklet';
|
|
434
|
+
const length = Math.max(1, width.get() - axisPadding * 2);
|
|
435
|
+
const fraction = clamp((x - axisPadding) / length, 0, 1);
|
|
436
|
+
const next = snap(minimumValue + (reversed ? 1 - fraction : fraction) * span, minimumValue, maximumValue, step);
|
|
437
|
+
if (active.get() === 0 && range)
|
|
438
|
+
start.set(constrainedSnap(next, minimumValue, maximumValue, step, minimumValue, end.get() - minimumRange));
|
|
439
|
+
else
|
|
440
|
+
end.set(
|
|
441
|
+
constrainedSnap(next, minimumValue, maximumValue, step, range ? start.get() + minimumRange : minimumValue, maximumValue)
|
|
442
|
+
);
|
|
443
|
+
const motion = {
|
|
444
|
+
duration: theme.motion.duration.short3 * theme.animation.scale,
|
|
445
|
+
easing: Easing.bezier(...theme.motion.easing.standard),
|
|
446
|
+
reduceMotion: ReduceMotion.Never,
|
|
447
|
+
};
|
|
448
|
+
displayStart.set(animate ? withTiming(start.get(), motion) : start.get());
|
|
449
|
+
displayEnd.set(animate ? withTiming(end.get(), motion) : end.get());
|
|
450
|
+
};
|
|
451
|
+
const closestThumb = (coordinate: number) => {
|
|
452
|
+
'worklet';
|
|
453
|
+
const fraction = clamp((coordinate - axisPadding) / Math.max(1, width.get() - axisPadding * 2), 0, 1);
|
|
454
|
+
const touchValue = minimumValue + (reversed ? 1 - fraction : fraction) * span;
|
|
455
|
+
const distanceStart = Math.abs(start.get() - touchValue);
|
|
456
|
+
const distanceEnd = Math.abs(end.get() - touchValue);
|
|
457
|
+
return range && (distanceStart < distanceEnd || (distanceStart === distanceEnd && touchValue <= start.get())) ? 0 : 1;
|
|
458
|
+
};
|
|
459
|
+
const activateAt = (coordinate: number, animate = false) => {
|
|
460
|
+
'worklet';
|
|
461
|
+
if (disabled || configurationUI.get() !== configuration) return;
|
|
462
|
+
const fraction = clamp((coordinate - axisPadding) / Math.max(1, width.get() - axisPadding * 2), 0, 1);
|
|
463
|
+
const touchValue = minimumValue + (reversed ? 1 - fraction : fraction) * span;
|
|
464
|
+
const distanceStart = Math.abs(start.get() - touchValue);
|
|
465
|
+
const distanceEnd = Math.abs(end.get() - touchValue);
|
|
466
|
+
// Coincident thumbs: left half moves the lower thumb, right half the upper thumb.
|
|
467
|
+
active.set(closestThumb(coordinate));
|
|
468
|
+
pendingDirection.set(
|
|
469
|
+
range &&
|
|
470
|
+
Math.abs(start.get() - end.get()) < span / 100000 &&
|
|
471
|
+
Math.min(distanceStart, distanceEnd) < (span * 24) / Math.max(1, width.get() - axisPadding * 2)
|
|
472
|
+
);
|
|
473
|
+
dragging.set(true);
|
|
474
|
+
gestureSequence.set(gestureSequence.get() + 1);
|
|
475
|
+
scheduleOnRN(begin, start.get(), end.get(), gestureSequence.get(), configuration);
|
|
476
|
+
if (!pendingDirection.get()) updateAt(coordinate, animate);
|
|
477
|
+
};
|
|
478
|
+
const completeAt = (coordinate: number, canceled: boolean, animate = false) => {
|
|
479
|
+
'worklet';
|
|
480
|
+
if (!dragging.get() || configurationUI.get() !== configuration) return;
|
|
481
|
+
if (!canceled) updateAt(coordinate, animate);
|
|
482
|
+
if (canceled) scheduleOnRN(cancel, start.get(), end.get(), gestureSequence.get(), configuration);
|
|
483
|
+
else {
|
|
484
|
+
scheduleOnRN(publishValue, start.get(), end.get(), gestureSequence.get(), configuration);
|
|
485
|
+
scheduleOnRN(finish, start.get(), end.get(), gestureSequence.get(), configuration);
|
|
486
|
+
}
|
|
487
|
+
dragging.set(false);
|
|
488
|
+
pendingDirection.set(false);
|
|
489
|
+
active.set(-1);
|
|
490
|
+
};
|
|
491
|
+
const claimVerticalTouch = vertical && Platform.OS !== 'web';
|
|
492
|
+
const pan = usePanGesture({
|
|
493
|
+
enabled: !disabled,
|
|
494
|
+
manualActivation: claimVerticalTouch,
|
|
495
|
+
// Horizontal sliders leave cross-axis movement to the surrounding scroll.
|
|
496
|
+
activeOffsetX: vertical ? undefined : [-8, 8],
|
|
497
|
+
activeOffsetY: vertical ? [-8, 8] : undefined,
|
|
498
|
+
failOffsetX: vertical ? [-8, 8] : undefined,
|
|
499
|
+
failOffsetY: vertical ? undefined : [-8, 8],
|
|
500
|
+
maxPointers: 1,
|
|
501
|
+
onBegin: (event) => {
|
|
502
|
+
'worklet';
|
|
503
|
+
if (disabled || configurationUI.get() !== configuration) return;
|
|
504
|
+
tapThumb.set(-1);
|
|
505
|
+
active.set(closestThumb(vertical ? event.y : event.x));
|
|
506
|
+
},
|
|
507
|
+
// Native pan recognition must claim this axis in a touch callback.
|
|
508
|
+
// A begin notification can precede the recognizer's activation gate.
|
|
509
|
+
onTouchesDown: (event) => {
|
|
510
|
+
'worklet';
|
|
511
|
+
if (claimVerticalTouch && !disabled && configurationUI.get() === configuration) {
|
|
512
|
+
GestureStateManager.activate(event.handlerTag);
|
|
513
|
+
}
|
|
514
|
+
},
|
|
515
|
+
onTouchesMove: (event) => {
|
|
516
|
+
'worklet';
|
|
517
|
+
if (claimVerticalTouch && !disabled && configurationUI.get() === configuration && !dragging.get()) {
|
|
518
|
+
GestureStateManager.activate(event.handlerTag);
|
|
519
|
+
}
|
|
520
|
+
},
|
|
521
|
+
onActivate: (event) => {
|
|
522
|
+
'worklet';
|
|
523
|
+
activateAt(vertical ? event.y : event.x);
|
|
524
|
+
},
|
|
525
|
+
onUpdate: (event) => {
|
|
526
|
+
'worklet';
|
|
527
|
+
if (!dragging.get() || disabled || configurationUI.get() !== configuration) return;
|
|
528
|
+
if (pendingDirection.get()) {
|
|
529
|
+
const movement = (vertical ? event.translationY : event.translationX) * (reversed ? -1 : 1);
|
|
530
|
+
if (Math.abs(movement) < 1) return;
|
|
531
|
+
active.set(movement > 0 ? 1 : 0);
|
|
532
|
+
pendingDirection.set(false);
|
|
533
|
+
}
|
|
534
|
+
updateAt(vertical ? event.y : event.x);
|
|
535
|
+
},
|
|
536
|
+
onFinalize: (event) => {
|
|
537
|
+
'worklet';
|
|
538
|
+
completeAt(vertical ? event.y : event.x, event.canceled);
|
|
539
|
+
if (!dragging.get()) active.set(-1);
|
|
540
|
+
},
|
|
541
|
+
});
|
|
542
|
+
const tap = useTapGesture({
|
|
543
|
+
enabled: !disabled,
|
|
544
|
+
maxDistance: 8,
|
|
545
|
+
onBegin: (event) => {
|
|
546
|
+
'worklet';
|
|
547
|
+
tapMultiplePointers.set(event.numberOfPointers > 1);
|
|
548
|
+
},
|
|
549
|
+
onTouchesDown: (event) => {
|
|
550
|
+
'worklet';
|
|
551
|
+
if (event.numberOfTouches > 1) tapMultiplePointers.set(true);
|
|
552
|
+
},
|
|
553
|
+
onActivate: (event) => {
|
|
554
|
+
'worklet';
|
|
555
|
+
if (disabled || tapMultiplePointers.get() || configurationUI.get() !== configuration) return;
|
|
556
|
+
const coordinate = vertical ? event.y : event.x;
|
|
557
|
+
activateAt(coordinate, true);
|
|
558
|
+
if (valueIndicator === 'on-drag') {
|
|
559
|
+
tapThumb.set(active.get());
|
|
560
|
+
tapThumb.set(
|
|
561
|
+
withDelay(theme.motion.duration.medium4, withTiming(-1, { duration: 0, reduceMotion: ReduceMotion.Never }), ReduceMotion.Never)
|
|
562
|
+
);
|
|
563
|
+
}
|
|
564
|
+
completeAt(coordinate, false, true);
|
|
565
|
+
},
|
|
566
|
+
});
|
|
567
|
+
const gesture = useExclusiveGestures(pan, tap);
|
|
568
|
+
|
|
569
|
+
const adjust = (thumb: 0 | 1, increment: boolean, count = 1, boundary = false) => {
|
|
570
|
+
if (disabled) return;
|
|
571
|
+
const current = thumb === 0 ? start.get() : end.get();
|
|
572
|
+
const offset = increment ? count : -count;
|
|
573
|
+
let next = current + (span / 100) * offset;
|
|
574
|
+
if (boundary) {
|
|
575
|
+
next = increment ? maximumValue : minimumValue;
|
|
576
|
+
} else if (step) {
|
|
577
|
+
let index = Math.round((current - minimumValue) / step);
|
|
578
|
+
// A maximum off the grid is its own stop, even when rounded to the preceding regular stop.
|
|
579
|
+
const regularValue = Number((minimumValue + index * step).toFixed(10));
|
|
580
|
+
if (current === maximumValue && regularValue < Number(maximumValue.toFixed(10))) index += 1;
|
|
581
|
+
next = minimumValue + (index + offset) * step;
|
|
582
|
+
}
|
|
583
|
+
const lower =
|
|
584
|
+
thumb === 0 ? constrainedSnap(next, minimumValue, maximumValue, step, minimumValue, end.get() - minimumRange) : start.get();
|
|
585
|
+
const upper =
|
|
586
|
+
thumb === 1
|
|
587
|
+
? constrainedSnap(next, minimumValue, maximumValue, step, lower + (range ? minimumRange : 0), maximumValue)
|
|
588
|
+
: end.get();
|
|
589
|
+
begin(start.get(), end.get());
|
|
590
|
+
start.set(lower);
|
|
591
|
+
end.set(upper);
|
|
592
|
+
displayStart.set(lower);
|
|
593
|
+
displayEnd.set(upper);
|
|
594
|
+
publishValue(lower, upper);
|
|
595
|
+
finish(lower, upper);
|
|
596
|
+
};
|
|
597
|
+
const tickFractions = useMemo(() => {
|
|
598
|
+
if ((!showTicks && !continuousTickCount) || (!step && continuousTickCount < 2) || layoutWidth <= axisPadding * 2) return [];
|
|
599
|
+
const numberOfSteps = step ? Math.ceil(span / step) : continuousTickCount - 1;
|
|
600
|
+
// Ticks closer than 4dp merge visually; keep all numerical detents in snapping.
|
|
601
|
+
const stride = Math.max(1, Math.ceil(numberOfSteps / Math.max(1, (layoutWidth - axisPadding * 2 - dimensions.corner * 2) / 4)));
|
|
602
|
+
const ticks = [];
|
|
603
|
+
for (let index = 0; index <= numberOfSteps; index += stride)
|
|
604
|
+
ticks.push(step ? Math.min((index * step) / span, 1) : index / numberOfSteps);
|
|
605
|
+
if (ticks[ticks.length - 1] !== 1) ticks.push(1);
|
|
606
|
+
return ticks;
|
|
607
|
+
}, [showTicks, step, span, layoutWidth, continuousTickCount, axisPadding, dimensions]);
|
|
608
|
+
const trackProps = {
|
|
609
|
+
start,
|
|
610
|
+
end,
|
|
611
|
+
displayStart,
|
|
612
|
+
displayEnd,
|
|
613
|
+
width,
|
|
614
|
+
range,
|
|
615
|
+
centered,
|
|
616
|
+
minimumValue,
|
|
617
|
+
span,
|
|
618
|
+
reversed,
|
|
619
|
+
disabled,
|
|
620
|
+
theme,
|
|
621
|
+
vertical,
|
|
622
|
+
crossSize,
|
|
623
|
+
axisPadding,
|
|
624
|
+
dimensions,
|
|
625
|
+
active,
|
|
626
|
+
focusedThumb,
|
|
627
|
+
indicatorOwner,
|
|
628
|
+
};
|
|
629
|
+
|
|
630
|
+
const readout = valueLabel === undefined ? null : (
|
|
631
|
+
<View
|
|
632
|
+
style={
|
|
633
|
+
vertical
|
|
634
|
+
? { marginTop: 8, width: Math.max(crossSize, indicatorWidth), alignSelf: 'flex-start' }
|
|
635
|
+
: { paddingBottom: indicatorSpace + 8 }
|
|
636
|
+
}
|
|
637
|
+
pointerEvents="none"
|
|
638
|
+
accessibilityElementsHidden
|
|
639
|
+
importantForAccessibility="no-hide-descendants"
|
|
640
|
+
{...webAccessibility({ hidden: true })}
|
|
641
|
+
>
|
|
642
|
+
<SliderReadout
|
|
643
|
+
label={valueLabel}
|
|
644
|
+
range={range}
|
|
645
|
+
start={start}
|
|
646
|
+
end={end}
|
|
647
|
+
value={a11yValue}
|
|
648
|
+
precision={precision}
|
|
649
|
+
suffix={valueSuffix}
|
|
650
|
+
typography={readoutTypography}
|
|
651
|
+
height={readoutHeight}
|
|
652
|
+
vertical={vertical}
|
|
653
|
+
testID={`${testID}-value`}
|
|
654
|
+
/>
|
|
655
|
+
</View>
|
|
656
|
+
);
|
|
657
|
+
|
|
658
|
+
return (
|
|
659
|
+
<View
|
|
660
|
+
style={[
|
|
661
|
+
vertical
|
|
662
|
+
? {
|
|
663
|
+
direction: 'ltr',
|
|
664
|
+
width: crossSize + indicatorSpace,
|
|
665
|
+
paddingLeft: rtl ? indicatorSpace : 0,
|
|
666
|
+
height: verticalLength + (readout ? readoutHeight + 8 : 0),
|
|
667
|
+
alignSelf: 'flex-start',
|
|
668
|
+
}
|
|
669
|
+
: { width: componentLength, minHeight: crossSize + indicatorSpace, paddingTop: readout ? 0 : indicatorSpace },
|
|
670
|
+
style,
|
|
671
|
+
]}
|
|
672
|
+
testID={testID}
|
|
673
|
+
>
|
|
674
|
+
{!vertical && readout}
|
|
675
|
+
<GestureDetector gesture={gesture} touchAction={vertical ? 'pan-x' : 'pan-y'}>
|
|
676
|
+
<Animated.View
|
|
677
|
+
collapsable={false}
|
|
678
|
+
style={[styles.touchArea, vertical ? { width: crossSize, height: readout ? verticalLength : '100%' } : { height: crossSize }]}
|
|
679
|
+
onLayout={(event) => {
|
|
680
|
+
const length = vertical ? event.nativeEvent.layout.height : event.nativeEvent.layout.width;
|
|
681
|
+
if (length !== width.get()) cancelOwnedGesture();
|
|
682
|
+
width.set(length);
|
|
683
|
+
setLayoutWidth(length);
|
|
684
|
+
}}
|
|
685
|
+
>
|
|
686
|
+
<TrackSegment {...trackProps} segment="before" />
|
|
687
|
+
{(range || centered) && <TrackSegment {...trackProps} segment="active" />}
|
|
688
|
+
<TrackSegment {...trackProps} segment="after" />
|
|
689
|
+
{tickFractions.map((fraction, index) => (
|
|
690
|
+
<Tick key={index} {...trackProps} fraction={fraction} stop={false} showStopIndicator={showStopIndicator} />
|
|
691
|
+
))}
|
|
692
|
+
{showStopIndicator && (
|
|
693
|
+
<>
|
|
694
|
+
<Tick {...trackProps} fraction={1} stop showStopIndicator />
|
|
695
|
+
{(range || centered) && <Tick {...trackProps} fraction={0} stop showStopIndicator />}
|
|
696
|
+
</>
|
|
697
|
+
)}
|
|
698
|
+
{leadingIcon && <InsetIcon {...trackProps} source={leadingIcon} endIcon={false} hasLeadingIcon />}
|
|
699
|
+
{trailingIcon && <InsetIcon {...trackProps} source={trailingIcon} endIcon hasLeadingIcon={Boolean(leadingIcon)} />}
|
|
700
|
+
{range && (
|
|
701
|
+
<Thumb
|
|
702
|
+
{...trackProps}
|
|
703
|
+
active={active}
|
|
704
|
+
index={0}
|
|
705
|
+
valueIndicator={valueIndicator}
|
|
706
|
+
indicatorWidth={indicatorWidth}
|
|
707
|
+
indicatorHeight={indicatorHeight}
|
|
708
|
+
indicatorOnLeft={rtl}
|
|
709
|
+
tapThumb={tapThumb}
|
|
710
|
+
suffix={valueSuffix}
|
|
711
|
+
precision={precision}
|
|
712
|
+
testID={`${testID}-start`}
|
|
713
|
+
label={startAccessibilityLabel ?? `${accessibilityLabel}, start`}
|
|
714
|
+
hint={accessibilityHint}
|
|
715
|
+
value={a11yValue[0]}
|
|
716
|
+
maximum={a11yValue[1] - minimumRange}
|
|
717
|
+
minimum={minimumValue}
|
|
718
|
+
onAdjust={(increment, count) => adjust(0, increment, count)}
|
|
719
|
+
onBoundary={(end) => adjust(0, end, 1, true)}
|
|
720
|
+
/>
|
|
721
|
+
)}
|
|
722
|
+
<Thumb
|
|
723
|
+
{...trackProps}
|
|
724
|
+
active={active}
|
|
725
|
+
index={1}
|
|
726
|
+
valueIndicator={valueIndicator}
|
|
727
|
+
indicatorWidth={indicatorWidth}
|
|
728
|
+
indicatorHeight={indicatorHeight}
|
|
729
|
+
indicatorOnLeft={rtl}
|
|
730
|
+
tapThumb={tapThumb}
|
|
731
|
+
suffix={valueSuffix}
|
|
732
|
+
precision={precision}
|
|
733
|
+
testID={`${testID}-${range ? 'end' : 'thumb'}`}
|
|
734
|
+
label={endAccessibilityLabel ?? accessibilityLabel}
|
|
735
|
+
hint={accessibilityHint}
|
|
736
|
+
value={a11yValue[1]}
|
|
737
|
+
minimum={range ? a11yValue[0] + minimumRange : minimumValue}
|
|
738
|
+
maximum={maximumValue}
|
|
739
|
+
onAdjust={(increment, count) => adjust(1, increment, count)}
|
|
740
|
+
onBoundary={(end) => adjust(1, end, 1, true)}
|
|
741
|
+
/>
|
|
742
|
+
</Animated.View>
|
|
743
|
+
</GestureDetector>
|
|
744
|
+
{vertical && readout}
|
|
745
|
+
</View>
|
|
746
|
+
);
|
|
747
|
+
}
|
|
748
|
+
|
|
749
|
+
type TrackProps = {
|
|
750
|
+
start: SharedValue<number>;
|
|
751
|
+
end: SharedValue<number>;
|
|
752
|
+
displayStart: SharedValue<number>;
|
|
753
|
+
displayEnd: SharedValue<number>;
|
|
754
|
+
width: SharedValue<number>;
|
|
755
|
+
range: boolean;
|
|
756
|
+
centered: boolean;
|
|
757
|
+
minimumValue: number;
|
|
758
|
+
span: number;
|
|
759
|
+
reversed: boolean;
|
|
760
|
+
disabled: boolean;
|
|
761
|
+
theme: Theme;
|
|
762
|
+
vertical: boolean;
|
|
763
|
+
crossSize: number;
|
|
764
|
+
axisPadding: number;
|
|
765
|
+
dimensions: (typeof sliderSizeTokens)[keyof typeof sliderSizeTokens];
|
|
766
|
+
active: SharedValue<number>;
|
|
767
|
+
focusedThumb: SharedValue<number>;
|
|
768
|
+
indicatorOwner: SharedValue<number>;
|
|
769
|
+
};
|
|
770
|
+
|
|
771
|
+
function TrackSegment({
|
|
772
|
+
displayStart,
|
|
773
|
+
displayEnd,
|
|
774
|
+
width,
|
|
775
|
+
range,
|
|
776
|
+
centered,
|
|
777
|
+
minimumValue,
|
|
778
|
+
span,
|
|
779
|
+
reversed,
|
|
780
|
+
disabled,
|
|
781
|
+
theme,
|
|
782
|
+
vertical,
|
|
783
|
+
crossSize,
|
|
784
|
+
axisPadding,
|
|
785
|
+
dimensions,
|
|
786
|
+
active: activeThumb,
|
|
787
|
+
focusedThumb,
|
|
788
|
+
segment,
|
|
789
|
+
}: TrackProps & { segment: 'before' | 'active' | 'after' }) {
|
|
790
|
+
const active = segment === 'active' || (segment === 'before' && !range && !centered);
|
|
791
|
+
const animated = useAnimatedStyle(() => {
|
|
792
|
+
const length = Math.max(0, width.get() - axisPadding * 2);
|
|
793
|
+
const first = ((displayStart.get() - minimumValue) / span) * length;
|
|
794
|
+
const last = ((displayEnd.get() - minimumValue) / span) * length;
|
|
795
|
+
const center = length / 2;
|
|
796
|
+
const low = centered ? Math.min(last, center) : first;
|
|
797
|
+
const high = centered ? Math.max(last, center) : last;
|
|
798
|
+
const interaction = activeThumb.get() >= 0 ? activeThumb.get() : focusedThumb.get();
|
|
799
|
+
const startGap = interaction === 0 ? 7 : 8,
|
|
800
|
+
endGap = interaction === 1 ? 7 : 8;
|
|
801
|
+
const lowGap = centered && last >= center ? 6 : range ? startGap : endGap;
|
|
802
|
+
const highGap = centered && last <= center ? 6 : endGap;
|
|
803
|
+
const from = segment === 'before' ? 0 : segment === 'active' ? low + lowGap : high + highGap;
|
|
804
|
+
const to = segment === 'before' ? (range || centered ? low : last) - lowGap : segment === 'active' ? high - highGap : length;
|
|
805
|
+
const size = Math.max(0, to - from);
|
|
806
|
+
const outerStart = segment === 'before';
|
|
807
|
+
const outerEnd = segment === 'after';
|
|
808
|
+
const fromCorner = (reversed ? outerEnd : outerStart) ? Math.min(dimensions.corner, size / 2) : 2;
|
|
809
|
+
const toCorner = (reversed ? outerStart : outerEnd) ? Math.min(dimensions.corner, size / 2) : 2;
|
|
810
|
+
return {
|
|
811
|
+
left: vertical ? (crossSize - dimensions.track) / 2 : axisPadding + (reversed ? length - to : from),
|
|
812
|
+
top: vertical ? axisPadding + (reversed ? length - to : from) : (crossSize - dimensions.track) / 2,
|
|
813
|
+
width: vertical ? dimensions.track : size,
|
|
814
|
+
height: vertical ? size : dimensions.track,
|
|
815
|
+
opacity: disabled ? (active ? 0.38 : 0.12) : 1,
|
|
816
|
+
borderTopLeftRadius: fromCorner,
|
|
817
|
+
borderBottomLeftRadius: vertical ? toCorner : fromCorner,
|
|
818
|
+
borderTopRightRadius: vertical ? fromCorner : toCorner,
|
|
819
|
+
borderBottomRightRadius: toCorner,
|
|
820
|
+
};
|
|
821
|
+
});
|
|
822
|
+
return (
|
|
823
|
+
<Animated.View
|
|
824
|
+
pointerEvents="none"
|
|
825
|
+
style={[
|
|
826
|
+
styles.track,
|
|
827
|
+
{ backgroundColor: disabled ? theme.colors.onSurface : active ? theme.colors.primary : theme.colors.secondaryContainer },
|
|
828
|
+
animated,
|
|
829
|
+
]}
|
|
830
|
+
/>
|
|
831
|
+
);
|
|
832
|
+
}
|
|
833
|
+
|
|
834
|
+
function Tick({
|
|
835
|
+
displayStart,
|
|
836
|
+
displayEnd,
|
|
837
|
+
width,
|
|
838
|
+
range,
|
|
839
|
+
centered,
|
|
840
|
+
minimumValue,
|
|
841
|
+
span,
|
|
842
|
+
reversed,
|
|
843
|
+
disabled,
|
|
844
|
+
theme,
|
|
845
|
+
vertical,
|
|
846
|
+
crossSize,
|
|
847
|
+
axisPadding,
|
|
848
|
+
dimensions,
|
|
849
|
+
fraction,
|
|
850
|
+
stop,
|
|
851
|
+
showStopIndicator,
|
|
852
|
+
}: TrackProps & { fraction: number; stop: boolean; showStopIndicator: boolean }) {
|
|
853
|
+
const animated = useAnimatedStyle(() => {
|
|
854
|
+
const length = Math.max(0, width.get() - axisPadding * 2);
|
|
855
|
+
const corner = dimensions.corner;
|
|
856
|
+
const x = fraction === 0 ? corner : fraction === 1 ? length - corner : fraction * length;
|
|
857
|
+
const lower = ((displayStart.get() - minimumValue) / span) * length;
|
|
858
|
+
const upper = ((displayEnd.get() - minimumValue) / span) * length;
|
|
859
|
+
const inside = centered
|
|
860
|
+
? x >= Math.min(upper, length / 2) && x <= Math.max(upper, length / 2)
|
|
861
|
+
: (!range || x >= lower) && x <= upper;
|
|
862
|
+
const onGap = Math.abs(x - upper) <= 10 || (range && Math.abs(x - lower) <= 10) || (centered && Math.abs(x - length / 2) <= 6);
|
|
863
|
+
const hideStop = stop && (fraction === 0 ? (centered ? upper <= corner + 8 : lower <= corner + 8) : upper >= length - corner - 8);
|
|
864
|
+
const hideDuplicate = !stop && showStopIndicator && (fraction === 1 || ((range || centered) && fraction === 0));
|
|
865
|
+
return {
|
|
866
|
+
left: vertical ? crossSize / 2 - 2 : axisPadding + (reversed ? length - x : x) - 2,
|
|
867
|
+
top: vertical ? axisPadding + (reversed ? length - x : x) - 2 : crossSize / 2 - 2,
|
|
868
|
+
opacity: onGap || hideStop || hideDuplicate ? 0 : 1,
|
|
869
|
+
backgroundColor: disabled
|
|
870
|
+
? inside
|
|
871
|
+
? theme.colors.inverseOnSurface
|
|
872
|
+
: theme.colors.onSurface
|
|
873
|
+
: inside
|
|
874
|
+
? theme.colors.onPrimary
|
|
875
|
+
: theme.colors.onSecondaryContainer,
|
|
876
|
+
};
|
|
877
|
+
});
|
|
878
|
+
return <Animated.View pointerEvents="none" style={[styles.tick, animated]} />;
|
|
879
|
+
}
|
|
880
|
+
|
|
881
|
+
function InsetIcon({
|
|
882
|
+
displayEnd,
|
|
883
|
+
width,
|
|
884
|
+
minimumValue,
|
|
885
|
+
span,
|
|
886
|
+
reversed,
|
|
887
|
+
disabled,
|
|
888
|
+
theme,
|
|
889
|
+
vertical,
|
|
890
|
+
crossSize,
|
|
891
|
+
axisPadding,
|
|
892
|
+
dimensions,
|
|
893
|
+
source,
|
|
894
|
+
endIcon,
|
|
895
|
+
hasLeadingIcon,
|
|
896
|
+
}: TrackProps & { source: React.ComponentProps<typeof Icon>['source']; endIcon: boolean; hasLeadingIcon: boolean }) {
|
|
897
|
+
const animated = useAnimatedStyle(() => {
|
|
898
|
+
const length = Math.max(0, width.get() - axisPadding * 2);
|
|
899
|
+
const value = ((displayEnd.get() - minimumValue) / span) * length;
|
|
900
|
+
const activeFits = value - 8 >= dimensions.icon + 20;
|
|
901
|
+
const inactiveFits = length - value - 8 >= dimensions.icon + 20;
|
|
902
|
+
const inactive = endIcon || !activeFits;
|
|
903
|
+
const position = endIcon ? length - 10 - dimensions.icon : inactive ? value + 8 + 10 : 10;
|
|
904
|
+
const physical = reversed ? length - position - dimensions.icon : position;
|
|
905
|
+
return {
|
|
906
|
+
left: vertical ? (crossSize - dimensions.icon) / 2 : axisPadding + physical,
|
|
907
|
+
top: vertical ? axisPadding + physical : (crossSize - dimensions.icon) / 2,
|
|
908
|
+
opacity: (inactive ? !inactiveFits : !activeFits) || (endIcon && hasLeadingIcon && !activeFits) ? 0 : disabled ? 0.38 : 1,
|
|
909
|
+
};
|
|
910
|
+
});
|
|
911
|
+
const activeColor = useAnimatedStyle(() => ({
|
|
912
|
+
opacity:
|
|
913
|
+
!endIcon && ((displayEnd.get() - minimumValue) / span) * Math.max(0, width.get() - axisPadding * 2) - 8 >= dimensions.icon + 20
|
|
914
|
+
? 1
|
|
915
|
+
: 0,
|
|
916
|
+
}));
|
|
917
|
+
const inactiveColor = useAnimatedStyle(() => ({
|
|
918
|
+
opacity:
|
|
919
|
+
endIcon || ((displayEnd.get() - minimumValue) / span) * Math.max(0, width.get() - axisPadding * 2) - 8 < dimensions.icon + 20
|
|
920
|
+
? 1
|
|
921
|
+
: 0,
|
|
922
|
+
}));
|
|
923
|
+
return (
|
|
924
|
+
<Animated.View
|
|
925
|
+
pointerEvents="none"
|
|
926
|
+
accessible={false}
|
|
927
|
+
style={[{ position: 'absolute', width: dimensions.icon, height: dimensions.icon }, animated]}
|
|
928
|
+
>
|
|
929
|
+
<Animated.View style={[StyleSheet.absoluteFill, activeColor]}>
|
|
930
|
+
<Icon source={source} size={dimensions.icon} color={disabled ? theme.colors.onSurface : theme.colors.onPrimary} />
|
|
931
|
+
</Animated.View>
|
|
932
|
+
<Animated.View style={[StyleSheet.absoluteFill, inactiveColor]}>
|
|
933
|
+
<Icon
|
|
934
|
+
source={source}
|
|
935
|
+
size={dimensions.icon}
|
|
936
|
+
color={disabled ? theme.colors.onSurface : theme.colors.onSecondaryContainer}
|
|
937
|
+
/>
|
|
938
|
+
</Animated.View>
|
|
939
|
+
</Animated.View>
|
|
940
|
+
);
|
|
941
|
+
}
|
|
942
|
+
|
|
943
|
+
const AnimatedInput = Animated.createAnimatedComponent(TextInput);
|
|
944
|
+
|
|
945
|
+
function formatSliderReadout(label: string, range: boolean, start: number, end: number, precision: number, suffix: string) {
|
|
946
|
+
'worklet';
|
|
947
|
+
const upper = formatSliderValue(end, precision, suffix);
|
|
948
|
+
const value = range ? `${formatSliderValue(start, precision, suffix)} – ${upper}` : upper;
|
|
949
|
+
return label ? `${label}: ${value}` : value;
|
|
950
|
+
}
|
|
951
|
+
|
|
952
|
+
function SliderReadout({
|
|
953
|
+
label,
|
|
954
|
+
range,
|
|
955
|
+
start,
|
|
956
|
+
end,
|
|
957
|
+
value,
|
|
958
|
+
precision,
|
|
959
|
+
suffix,
|
|
960
|
+
typography,
|
|
961
|
+
height,
|
|
962
|
+
vertical,
|
|
963
|
+
testID,
|
|
964
|
+
}: {
|
|
965
|
+
label: string;
|
|
966
|
+
range: boolean;
|
|
967
|
+
start: SharedValue<number>;
|
|
968
|
+
end: SharedValue<number>;
|
|
969
|
+
value: [number, number];
|
|
970
|
+
precision: number;
|
|
971
|
+
suffix: string;
|
|
972
|
+
typography: Theme['fonts']['bodyLarge'];
|
|
973
|
+
height: number;
|
|
974
|
+
vertical: boolean;
|
|
975
|
+
testID: string;
|
|
976
|
+
}) {
|
|
977
|
+
const theme = useTheme<Theme>();
|
|
978
|
+
const animatedProps = useAnimatedProps<React.ComponentProps<typeof TextInput> & { text: string }>(
|
|
979
|
+
() => {
|
|
980
|
+
const text = formatSliderReadout(label, range, start.get(), end.get(), precision, suffix);
|
|
981
|
+
return { text, defaultValue: text };
|
|
982
|
+
},
|
|
983
|
+
Platform.OS === 'web' ? [label, range, start, end, precision, suffix] : undefined
|
|
984
|
+
);
|
|
985
|
+
return (
|
|
986
|
+
<AnimatedInput
|
|
987
|
+
accessible={false}
|
|
988
|
+
editable={false}
|
|
989
|
+
focusable={false}
|
|
990
|
+
{...(Platform.OS === 'web' ? { tabIndex: -1 } : {})}
|
|
991
|
+
caretHidden
|
|
992
|
+
underlineColorAndroid="transparent"
|
|
993
|
+
animatedProps={animatedProps}
|
|
994
|
+
defaultValue={formatSliderReadout(label, range, value[0], value[1], precision, suffix)}
|
|
995
|
+
style={[typography, { height, padding: 0, color: theme.colors.onSurface, textAlign: vertical ? 'center' : 'auto' }]}
|
|
996
|
+
testID={testID}
|
|
997
|
+
/>
|
|
998
|
+
);
|
|
999
|
+
}
|
|
1000
|
+
|
|
1001
|
+
function Thumb({
|
|
1002
|
+
start,
|
|
1003
|
+
end,
|
|
1004
|
+
displayStart,
|
|
1005
|
+
displayEnd,
|
|
1006
|
+
width,
|
|
1007
|
+
minimumValue,
|
|
1008
|
+
span,
|
|
1009
|
+
reversed,
|
|
1010
|
+
disabled,
|
|
1011
|
+
theme,
|
|
1012
|
+
vertical,
|
|
1013
|
+
crossSize,
|
|
1014
|
+
axisPadding,
|
|
1015
|
+
dimensions,
|
|
1016
|
+
active,
|
|
1017
|
+
focusedThumb,
|
|
1018
|
+
indicatorOwner,
|
|
1019
|
+
range,
|
|
1020
|
+
index,
|
|
1021
|
+
valueIndicator,
|
|
1022
|
+
indicatorWidth,
|
|
1023
|
+
indicatorHeight,
|
|
1024
|
+
indicatorOnLeft,
|
|
1025
|
+
tapThumb,
|
|
1026
|
+
suffix,
|
|
1027
|
+
precision,
|
|
1028
|
+
testID,
|
|
1029
|
+
label,
|
|
1030
|
+
hint,
|
|
1031
|
+
value,
|
|
1032
|
+
minimum,
|
|
1033
|
+
maximum,
|
|
1034
|
+
onAdjust,
|
|
1035
|
+
onBoundary,
|
|
1036
|
+
}: TrackProps & {
|
|
1037
|
+
index: 0 | 1;
|
|
1038
|
+
valueIndicator: 'on-drag' | 'always' | 'never';
|
|
1039
|
+
indicatorWidth: number;
|
|
1040
|
+
indicatorHeight: number;
|
|
1041
|
+
indicatorOnLeft: boolean;
|
|
1042
|
+
tapThumb: SharedValue<number>;
|
|
1043
|
+
suffix: string;
|
|
1044
|
+
precision: number;
|
|
1045
|
+
testID: string;
|
|
1046
|
+
label: string;
|
|
1047
|
+
hint?: string;
|
|
1048
|
+
value: number;
|
|
1049
|
+
minimum: number;
|
|
1050
|
+
maximum: number;
|
|
1051
|
+
onAdjust: (increment: boolean, count?: number) => void;
|
|
1052
|
+
onBoundary: (end: boolean) => void;
|
|
1053
|
+
}) {
|
|
1054
|
+
const [focused, setFocused] = useState(false);
|
|
1055
|
+
const { fontScale } = useWindowDimensions();
|
|
1056
|
+
const spaceHeld = useRef(false);
|
|
1057
|
+
const state = useSharedValue(0);
|
|
1058
|
+
const valueText = formatSliderValue(value, precision, suffix);
|
|
1059
|
+
useEffect(() => {
|
|
1060
|
+
if (disabled) {
|
|
1061
|
+
focusedThumb.set(-1);
|
|
1062
|
+
spaceHeld.current = false;
|
|
1063
|
+
setFocused(false);
|
|
1064
|
+
state.set(0);
|
|
1065
|
+
}
|
|
1066
|
+
return () => cancelAnimation(state);
|
|
1067
|
+
}, [disabled, focusedThumb, state]);
|
|
1068
|
+
useAnimatedReaction(
|
|
1069
|
+
() => {
|
|
1070
|
+
if (disabled) return false;
|
|
1071
|
+
const pressed = active.get();
|
|
1072
|
+
const tapped = tapThumb.get();
|
|
1073
|
+
return index === (pressed >= 0 ? pressed : tapped >= 0 ? tapped : focusedThumb.get());
|
|
1074
|
+
},
|
|
1075
|
+
(pressed) => {
|
|
1076
|
+
if (pressed) indicatorOwner.set(index);
|
|
1077
|
+
// Paper resolves auto/on/off into scale; it is the single motion authority.
|
|
1078
|
+
// MDC BaseSlider changes handle width immediately; label reveal has a separate duration.
|
|
1079
|
+
state.set(
|
|
1080
|
+
withTiming(pressed ? 1 : 0, {
|
|
1081
|
+
duration: (pressed ? theme.motion.duration.medium4 : theme.motion.duration.short3) * theme.animation.scale,
|
|
1082
|
+
easing: Easing.bezier(...(pressed ? theme.motion.easing.emphasized : theme.motion.easing.emphasizedAccelerate)),
|
|
1083
|
+
reduceMotion: ReduceMotion.Never,
|
|
1084
|
+
})
|
|
1085
|
+
);
|
|
1086
|
+
},
|
|
1087
|
+
Platform.OS === 'web' ? [active, disabled, focusedThumb, indicatorOwner, index, state, tapThumb, theme] : undefined
|
|
1088
|
+
);
|
|
1089
|
+
const position = useAnimatedStyle(() => {
|
|
1090
|
+
const fraction = ((index === 0 ? displayStart.get() : displayEnd.get()) - minimumValue) / span;
|
|
1091
|
+
const position = axisPadding + (reversed ? 1 - fraction : fraction) * Math.max(0, width.get() - axisPadding * 2) - 24;
|
|
1092
|
+
return { left: vertical ? 0 : position, top: vertical ? position : 0 };
|
|
1093
|
+
});
|
|
1094
|
+
const handle = useAnimatedStyle(() => {
|
|
1095
|
+
const height = dimensions.handle - (focused && !disabled ? 6 : 0);
|
|
1096
|
+
const pressed = active.get();
|
|
1097
|
+
const interaction = pressed >= 0 ? pressed : focusedThumb.get();
|
|
1098
|
+
const thickness = !disabled && interaction === index ? 2 : 4;
|
|
1099
|
+
return { width: vertical ? height : thickness, height: vertical ? thickness : height };
|
|
1100
|
+
});
|
|
1101
|
+
const indicator = useAnimatedStyle(() => {
|
|
1102
|
+
const reveal = valueIndicator === 'always' ? 1 : state.get();
|
|
1103
|
+
const length = width.get();
|
|
1104
|
+
const fraction = ((index === 0 ? displayStart.get() : displayEnd.get()) - minimumValue) / span;
|
|
1105
|
+
const center = axisPadding + (reversed ? 1 - fraction : fraction) * Math.max(0, length - axisPadding * 2);
|
|
1106
|
+
const extent = vertical ? indicatorHeight : indicatorWidth;
|
|
1107
|
+
const offset = clamp(center - extent / 2, 0, Math.max(0, length - extent));
|
|
1108
|
+
return {
|
|
1109
|
+
...(vertical ? { top: offset - center + 24 } : { left: offset - center + 24 }),
|
|
1110
|
+
opacity:
|
|
1111
|
+
disabled || length <= 0 || valueIndicator === 'never' || (range && indicatorOwner.get() !== index)
|
|
1112
|
+
? 0
|
|
1113
|
+
: Math.min(1, Math.max(0, (reveal - 0.19) / 0.81)),
|
|
1114
|
+
transform: [{ scale: reveal }],
|
|
1115
|
+
transformOrigin: vertical ? `${indicatorOnLeft ? 'right' : 'left'} ${center - offset}px` : `${center - offset}px bottom`,
|
|
1116
|
+
};
|
|
1117
|
+
});
|
|
1118
|
+
const text = useAnimatedProps<React.ComponentProps<typeof TextInput> & { text: string }>(
|
|
1119
|
+
() => {
|
|
1120
|
+
const number = index === 0 ? start.get() : end.get();
|
|
1121
|
+
const formatted = formatSliderValue(number, precision, suffix);
|
|
1122
|
+
return { text: formatted, defaultValue: formatted };
|
|
1123
|
+
},
|
|
1124
|
+
Platform.OS === 'web' ? [suffix, index, precision] : undefined
|
|
1125
|
+
);
|
|
1126
|
+
return (
|
|
1127
|
+
<Animated.View
|
|
1128
|
+
accessible
|
|
1129
|
+
accessibilityRole="adjustable"
|
|
1130
|
+
{...(Platform.OS === 'web'
|
|
1131
|
+
? { tabIndex: disabled ? -1 : 0, 'aria-orientation': vertical ? ('vertical' as const) : ('horizontal' as const) }
|
|
1132
|
+
: {})}
|
|
1133
|
+
accessibilityLabel={label}
|
|
1134
|
+
accessibilityHint={hint}
|
|
1135
|
+
accessibilityState={{ disabled }}
|
|
1136
|
+
accessibilityValue={{ min: minimum, max: maximum, now: value, text: valueText }}
|
|
1137
|
+
accessibilityActions={[{ name: 'increment' }, { name: 'decrement' }]}
|
|
1138
|
+
onAccessibilityAction={(event) => {
|
|
1139
|
+
if (event.nativeEvent.actionName === 'increment') onAdjust(true);
|
|
1140
|
+
if (event.nativeEvent.actionName === 'decrement') onAdjust(false);
|
|
1141
|
+
}}
|
|
1142
|
+
onKeyDown={(event) => {
|
|
1143
|
+
const key = event.nativeEvent.key;
|
|
1144
|
+
if (event.nativeEvent.altKey || event.nativeEvent.ctrlKey || event.nativeEvent.metaKey) return;
|
|
1145
|
+
if (key === ' ' || key === 'Spacebar') {
|
|
1146
|
+
if (!disabled) {
|
|
1147
|
+
spaceHeld.current = true;
|
|
1148
|
+
event.preventDefault?.();
|
|
1149
|
+
}
|
|
1150
|
+
return;
|
|
1151
|
+
}
|
|
1152
|
+
if (disabled || !['ArrowRight', 'ArrowLeft', 'ArrowUp', 'ArrowDown', 'Home', 'End', 'PageUp', 'PageDown'].includes(key))
|
|
1153
|
+
return;
|
|
1154
|
+
event.preventDefault?.();
|
|
1155
|
+
if (key === 'Home' || key === 'End') onBoundary(key === 'End');
|
|
1156
|
+
else if (key === 'PageUp' || key === 'PageDown') onAdjust(key === 'PageUp', 10);
|
|
1157
|
+
else
|
|
1158
|
+
onAdjust(
|
|
1159
|
+
key === 'ArrowUp'
|
|
1160
|
+
? vertical
|
|
1161
|
+
? reversed
|
|
1162
|
+
: true
|
|
1163
|
+
: key === 'ArrowDown'
|
|
1164
|
+
? vertical
|
|
1165
|
+
? !reversed
|
|
1166
|
+
: false
|
|
1167
|
+
: (key === 'ArrowRight' && !reversed) || (key === 'ArrowLeft' && reversed),
|
|
1168
|
+
spaceHeld.current ? 10 : 1
|
|
1169
|
+
);
|
|
1170
|
+
}}
|
|
1171
|
+
onKeyUp={(event) => {
|
|
1172
|
+
if (event.nativeEvent.key === ' ' || event.nativeEvent.key === 'Spacebar') spaceHeld.current = false;
|
|
1173
|
+
}}
|
|
1174
|
+
onFocus={() => {
|
|
1175
|
+
focusedThumb.set(index);
|
|
1176
|
+
setFocused(true);
|
|
1177
|
+
}}
|
|
1178
|
+
onBlur={() => {
|
|
1179
|
+
focusedThumb.set(-1);
|
|
1180
|
+
spaceHeld.current = false;
|
|
1181
|
+
setFocused(false);
|
|
1182
|
+
}}
|
|
1183
|
+
focusable={!disabled}
|
|
1184
|
+
style={[
|
|
1185
|
+
styles.thumbTarget,
|
|
1186
|
+
{ width: vertical ? crossSize : 48, height: vertical ? 48 : crossSize },
|
|
1187
|
+
Platform.OS === 'web' && {
|
|
1188
|
+
outlineWidth: 0,
|
|
1189
|
+
outlineStyle: 'solid',
|
|
1190
|
+
outlineColor: 'transparent',
|
|
1191
|
+
cursor: disabled ? 'auto' : 'pointer',
|
|
1192
|
+
},
|
|
1193
|
+
position,
|
|
1194
|
+
]}
|
|
1195
|
+
testID={testID}
|
|
1196
|
+
{...webAccessibility({
|
|
1197
|
+
role: 'adjustable',
|
|
1198
|
+
state: { disabled },
|
|
1199
|
+
value: { min: minimum, max: maximum, now: value, text: valueText },
|
|
1200
|
+
hidden: undefined,
|
|
1201
|
+
})}
|
|
1202
|
+
>
|
|
1203
|
+
{focused && !disabled && (
|
|
1204
|
+
<View
|
|
1205
|
+
pointerEvents="none"
|
|
1206
|
+
accessible={false}
|
|
1207
|
+
testID={`${testID}-focus-ring`}
|
|
1208
|
+
style={{
|
|
1209
|
+
position: 'absolute',
|
|
1210
|
+
borderColor: theme.colors.secondary,
|
|
1211
|
+
borderWidth: 3,
|
|
1212
|
+
borderRadius: 24,
|
|
1213
|
+
width: vertical ? dimensions.handle + 4 : 12,
|
|
1214
|
+
height: vertical ? 12 : dimensions.handle + 4,
|
|
1215
|
+
}}
|
|
1216
|
+
/>
|
|
1217
|
+
)}
|
|
1218
|
+
<Animated.View
|
|
1219
|
+
pointerEvents="none"
|
|
1220
|
+
testID={`${testID}-handle`}
|
|
1221
|
+
style={[
|
|
1222
|
+
styles.thumb,
|
|
1223
|
+
{ backgroundColor: disabled ? theme.colors.onSurface : theme.colors.primary, opacity: disabled ? 0.38 : 1 },
|
|
1224
|
+
handle,
|
|
1225
|
+
]}
|
|
1226
|
+
/>
|
|
1227
|
+
<Animated.View
|
|
1228
|
+
pointerEvents="none"
|
|
1229
|
+
accessible={false}
|
|
1230
|
+
accessibilityElementsHidden
|
|
1231
|
+
importantForAccessibility="no-hide-descendants"
|
|
1232
|
+
{...webAccessibility({ hidden: true })}
|
|
1233
|
+
testID={`${testID}-indicator`}
|
|
1234
|
+
style={[
|
|
1235
|
+
styles.indicator,
|
|
1236
|
+
vertical
|
|
1237
|
+
? {
|
|
1238
|
+
left: indicatorOnLeft
|
|
1239
|
+
? (crossSize - dimensions.handle) / 2 - INDICATOR_GAP - indicatorWidth
|
|
1240
|
+
: (crossSize + dimensions.handle) / 2 + INDICATOR_GAP,
|
|
1241
|
+
}
|
|
1242
|
+
: { bottom: (crossSize + dimensions.handle) / 2 + INDICATOR_GAP },
|
|
1243
|
+
{
|
|
1244
|
+
backgroundColor: theme.colors.inverseSurface,
|
|
1245
|
+
width: indicatorWidth,
|
|
1246
|
+
height: indicatorHeight,
|
|
1247
|
+
borderRadius: indicatorHeight / 2,
|
|
1248
|
+
},
|
|
1249
|
+
indicator,
|
|
1250
|
+
]}
|
|
1251
|
+
>
|
|
1252
|
+
<AnimatedInput
|
|
1253
|
+
accessible={false}
|
|
1254
|
+
editable={false}
|
|
1255
|
+
focusable={false}
|
|
1256
|
+
{...(Platform.OS === 'web' ? { tabIndex: -1 } : {})}
|
|
1257
|
+
caretHidden
|
|
1258
|
+
underlineColorAndroid="transparent"
|
|
1259
|
+
animatedProps={text}
|
|
1260
|
+
defaultValue={valueText}
|
|
1261
|
+
style={[
|
|
1262
|
+
theme.fonts.labelLarge,
|
|
1263
|
+
styles.indicatorText,
|
|
1264
|
+
{
|
|
1265
|
+
color: theme.colors.inverseOnSurface,
|
|
1266
|
+
height: Math.max(24, 20 * fontScale),
|
|
1267
|
+
// UIKit centers the line box; the Roboto digits sit below its center.
|
|
1268
|
+
transform: Platform.OS === 'ios' ? [{ translateY: -3 * fontScale }] : undefined,
|
|
1269
|
+
},
|
|
1270
|
+
]}
|
|
1271
|
+
/>
|
|
1272
|
+
</Animated.View>
|
|
1273
|
+
</Animated.View>
|
|
1274
|
+
);
|
|
1275
|
+
}
|
|
1276
|
+
|
|
1277
|
+
const styles = StyleSheet.create({
|
|
1278
|
+
touchArea: { minHeight: 48, direction: 'ltr' },
|
|
1279
|
+
track: { position: 'absolute' },
|
|
1280
|
+
tick: { position: 'absolute', width: 4, height: 4, borderRadius: 2 },
|
|
1281
|
+
thumbTarget: { position: 'absolute', top: 0, width: 48, height: 48, alignItems: 'center', justifyContent: 'center' },
|
|
1282
|
+
thumb: { height: 44, borderRadius: 2 },
|
|
1283
|
+
indicator: {
|
|
1284
|
+
position: 'absolute',
|
|
1285
|
+
paddingHorizontal: INDICATOR_PADDING,
|
|
1286
|
+
alignItems: 'center',
|
|
1287
|
+
justifyContent: 'center',
|
|
1288
|
+
},
|
|
1289
|
+
indicatorText: { padding: 0, textAlign: 'center', minWidth: 24, height: 24, fontSize: 14, lineHeight: 20 },
|
|
1290
|
+
});
|