@devries-cameron20 / t3code-1 / commits / 79b304367e
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

refactor(mobile): use native iOS sliders (#20016)

Julius Marminge committed Oct 15, 2026 at 15:51 UTC 79b304367ed5482949ba088a5dbdaecb7bfc8536
3 files changed +89 -206
apps/mobile/src/components/SliderRow.android.tsx
+12 -4
@@ -1,18 +1,22 @@
1 import { Slider } from "@expo/ui/jetpack-compose";
2 import { fillMaxWidth } from "@expo/ui/jetpack-compose/modifiers";
3 import * as Haptics from "expo-haptics";
4 -import { useRef, type ComponentProps } from "react";
4 +import { useEffect, useRef } from "react";
5 import { View } from "react-native";
6
7 import { AppText as Text } from "./AppText";
8 import { SymbolView } from "./AppSymbol";
9 import { NativeThemeHost } from "./NativeThemeHost.android";
10 import { useControlTheme } from "./useControlTheme";
11 -import type { SliderRow as SharedSliderRow } from "./SliderRow";
11 +import type { SliderRowProps } from "./SliderRow.types";
12
13 -export function SliderRow(props: ComponentProps<typeof SharedSliderRow>) {
13 +export function SliderRow(props: SliderRowProps) {
14 const { colors } = useControlTheme();
15 const draft = useRef(props.value);
16 + const dragging = useRef(false);
17 + useEffect(() => {
18 + if (!dragging.current) draft.current = props.value;
19 + }, [props.value]);
20 const commit = (value: number) => {
21 if (props.disabled) return;
22 const next = Math.min(
@@ -62,9 +66,13 @@ export function SliderRow(props: ComponentProps<typeof SharedSliderRow>) {
66 inactiveTrackColor: colors["--color-secondary"],
67 }}
68 onValueChange={(value) => {
69 + dragging.current = true;
70 draft.current = value;
71 }}
67 - onValueChangeFinished={() => commit(draft.current)}
72 + onValueChangeFinished={() => {
73 + dragging.current = false;
74 + commit(draft.current);
75 + }}
76 />
77 </NativeThemeHost>
78 </View>
apps/mobile/src/components/SliderRow.tsx
+59 -202
@@ -1,220 +1,77 @@
1 -import { MOBILE_MOTION } from "../lib/designTokens";
1 +import { Slider } from "@expo/ui/swift-ui";
2 +import {
3 + accessibilityLabel,
4 + accessibilityValue,
5 + disabled,
6 + tint,
7 +} from "@expo/ui/swift-ui/modifiers";
8 import * as Haptics from "expo-haptics";
3 -import { SymbolView } from "./AppSymbol";
4 -import { useCallback, useEffect, useMemo, useRef } from "react";
5 -import { View, type AccessibilityActionEvent } from "react-native";
6 -import { Gesture, GestureDetector } from "react-native-gesture-handler";
7 -import Animated, {
8 - runOnJS,
9 - useAnimatedStyle,
10 - useSharedValue,
11 - withTiming,
12 - ReduceMotion,
13 -} from "react-native-reanimated";
14 -import type { ComponentProps } from "react";
15 -
16 -import { AppText as Text } from "./AppText";
17 -
18 -type SymbolName = ComponentProps<typeof SymbolView>["name"];
19 -
20 -const THUMB_SIZE = 26;
21 -const TRACK_HEIGHT = 4;
22 -const SNAP_ANIMATION = {
23 - duration: MOBILE_MOTION.adjust,
24 - reduceMotion: ReduceMotion.System,
25 -} as const;
26 -
27 -function clampFraction(value: number): number {
28 - "worklet";
29 - return Math.min(1, Math.max(0, value));
30 -}
31 -
32 -export function SliderRow(props: {
33 - readonly disabled?: boolean;
34 - readonly icon: SymbolName;
35 - readonly minimumIcon?: SymbolName;
36 - readonly maximumIcon?: SymbolName;
37 - readonly label: string;
38 - readonly valueLabel: string;
39 - readonly min: number;
40 - readonly max: number;
41 - readonly step: number;
42 - readonly value: number;
43 - readonly onChange: (value: number) => void;
44 -}) {
45 - const latest = useRef(props);
46 - latest.current = props;
9 +import { useEffect, useRef } from "react";
10 +import { View } from "react-native";
11
48 - const { min, max, step, value, disabled } = props;
49 - const fraction = (value - min) / (max - min);
12 +import { AppText } from "./AppText";
13 +import { SymbolView } from "./AppSymbol";
14 +import { NativeThemeHost } from "./NativeThemeHost";
15 +import type { SliderRowProps } from "./SliderRow.types";
16 +import { useControlTheme } from "./useControlTheme";
17
51 - const progress = useSharedValue(clampFraction(fraction));
52 - const trackWidth = useSharedValue(0);
53 - const dragging = useSharedValue(false);
18 +export type { SliderRowProps } from "./SliderRow.types";
19
20 +export function SliderRow(props: SliderRowProps) {
21 + const { colors } = useControlTheme();
22 + const draft = useRef(props.value);
23 + const editing = useRef(false);
24 useEffect(() => {
56 - if (!dragging.value) {
57 - progress.value = withTiming(clampFraction(fraction), SNAP_ANIMATION);
58 - }
59 - }, [dragging, fraction, progress]);
60 -
61 - const commit = useCallback((next: number) => {
62 - const current = latest.current;
63 - if (current.disabled || next === current.value) {
64 - return;
65 - }
66 - Haptics.selectionAsync().catch(() => undefined);
67 - current.onChange(next);
68 - }, []);
69 -
70 - const gesture = useMemo(() => {
71 - const snapValue = (raw: number): number => {
72 - "worklet";
73 - const stepped = Math.round((raw - min) / step) * step + min;
74 - return Math.min(max, Math.max(min, stepped));
75 - };
76 - const fractionAt = (x: number): number => {
77 - "worklet";
78 - const usable = trackWidth.value - THUMB_SIZE;
79 - if (usable <= 0) {
80 - return 0;
81 - }
82 - return clampFraction((x - THUMB_SIZE / 2) / usable);
83 - };
84 - const valueAtFraction = (f: number): number => {
85 - "worklet";
86 - return snapValue(min + f * (max - min));
87 - };
88 - const fractionOfValue = (v: number): number => {
89 - "worklet";
90 - return clampFraction((v - min) / (max - min));
91 - };
92 -
93 - const pan = Gesture.Pan()
94 - .enabled(!disabled)
95 - .activeOffsetX([-8, 8])
96 - .failOffsetY([-12, 12])
97 - .onUpdate((event) => {
98 - dragging.value = true;
99 - const f = fractionAt(event.x);
100 - progress.value = f;
101 - })
102 - .onFinalize((_event, success) => {
103 - if (!dragging.value) {
104 - return;
105 - }
106 - dragging.value = false;
107 - if (!success) {
108 - progress.value = withTiming(fractionOfValue(value), SNAP_ANIMATION);
109 - return;
110 - }
111 - const next = valueAtFraction(progress.value);
112 - progress.value = withTiming(fractionOfValue(next), SNAP_ANIMATION);
113 - runOnJS(commit)(next);
114 - });
115 -
116 - const tap = Gesture.Tap()
117 - .enabled(!disabled)
118 - .onEnd((event) => {
119 - const next = valueAtFraction(fractionAt(event.x));
120 - progress.value = withTiming(fractionOfValue(next), SNAP_ANIMATION);
121 - runOnJS(commit)(next);
122 - });
123 -
124 - return Gesture.Race(pan, tap);
125 - }, [commit, disabled, dragging, max, min, progress, step, trackWidth, value]);
126 -
127 - const fillStyle = useAnimatedStyle(() => ({
128 - width: THUMB_SIZE / 2 + progress.value * Math.max(0, trackWidth.value - THUMB_SIZE),
129 - }));
130 - const thumbStyle = useAnimatedStyle(() => ({
131 - transform: [{ translateX: progress.value * Math.max(0, trackWidth.value - THUMB_SIZE) }],
132 - }));
133 -
134 - const handleAccessibilityAction = (event: AccessibilityActionEvent) => {
135 - if (event.nativeEvent.actionName === "increment") {
136 - commit(Math.min(max, value + step));
137 - } else if (event.nativeEvent.actionName === "decrement") {
138 - commit(Math.max(min, value - step));
139 - }
25 + if (!editing.current) draft.current = props.value;
26 + }, [props.value]);
27 + const commit = (value: number) => {
28 + if (props.disabled) return;
29 + const next = Math.min(
30 + props.max,
31 + Math.max(props.min, props.min + Math.round((value - props.min) / props.step) * props.step),
32 + );
33 + if (next === props.value) return;
34 + void Haptics.selectionAsync().catch(() => undefined);
35 + props.onChange(next);
36 };
141 -
37 return (
143 - <View className={disabled ? "gap-1 p-4 opacity-[0.45]" : "gap-1 p-4"}>
38 + <View className={props.disabled ? "gap-1 p-4 opacity-[0.45]" : "gap-1 p-4"}>
39 <View className="flex-row items-center gap-4">
145 - <SymbolView
146 - name={props.icon}
147 - size={22}
148 - tintColorClassName={"accent-icon"}
149 - type="monochrome"
150 - weight="regular"
151 - />
152 - <Text className="flex-1 text-lg text-foreground">{props.label}</Text>
153 - <Text className="text-base font-t3-medium text-foreground-muted">{props.valueLabel}</Text>
40 + <SymbolView name={props.icon} size={22} tintColorClassName="accent-icon" />
41 + <AppText className="flex-1 text-lg text-foreground">{props.label}</AppText>
42 + <AppText className="text-base font-t3-medium text-foreground-muted">
43 + {props.valueLabel}
44 + </AppText>
45 </View>
46 <View className="flex-row items-center gap-3">
47 {props.minimumIcon ? (
157 - <SymbolView
158 - name={props.minimumIcon}
159 - size={15}
160 - tintColorClassName={"accent-icon-muted"}
161 - type="monochrome"
162 - weight="regular"
163 - />
48 + <SymbolView name={props.minimumIcon} size={15} tintColorClassName="accent-icon-muted" />
49 ) : null}
165 - <GestureDetector gesture={gesture}>
166 - <View
167 - accessible
168 - accessibilityActions={[
169 - { name: "increment", label: `Increase ${props.label}` },
170 - { name: "decrement", label: `Decrease ${props.label}` },
50 + <NativeThemeHost style={{ flex: 1, height: 44 }}>
51 + <Slider
52 + min={props.min}
53 + max={props.max}
54 + step={props.step}
55 + value={props.value}
56 + modifiers={[
57 + tint(colors["--color-primary"]),
58 + disabled(Boolean(props.disabled)),
59 + accessibilityLabel(props.label),
60 + accessibilityValue(props.valueLabel),
61 ]}
172 - accessibilityLabel={props.label}
173 - accessibilityRole="adjustable"
174 - accessibilityState={{ disabled: Boolean(disabled) }}
175 - accessibilityValue={{ min, max, now: value, text: props.valueLabel }}
176 - className="h-11 flex-1 justify-center"
177 - onAccessibilityAction={handleAccessibilityAction}
178 - onLayout={(event) => {
179 - trackWidth.value = event.nativeEvent.layout.width;
62 + onValueChange={(value) => {
63 + draft.current = value;
64 + // Accessibility adjustments do not begin a touch editing session.
65 + if (!editing.current) commit(value);
66 + }}
67 + onEditingChanged={(isEditing) => {
68 + editing.current = isEditing;
69 + if (!isEditing) commit(draft.current);
70 }}
181 - >
182 - <View
183 - className="w-full rounded-full bg-secondary-border"
184 - style={{ height: TRACK_HEIGHT }}
185 - >
186 - <Animated.View
187 - className="absolute inset-y-0 left-0 rounded-full bg-primary"
188 - style={fillStyle}
189 - />
190 - </View>
191 - <Animated.View
192 - className="absolute left-0 rounded-full border-border bg-primary"
193 - style={[
194 - {
195 - borderWidth: 1,
196 - height: THUMB_SIZE,
197 - marginTop: -THUMB_SIZE / 2,
198 - shadowColor: "#000000",
199 - shadowOffset: { height: 2, width: 0 },
200 - shadowOpacity: 0.18,
201 - shadowRadius: 3,
202 - top: "50%",
203 - width: THUMB_SIZE,
204 - },
205 - thumbStyle,
206 - ]}
207 - />
208 - </View>
209 - </GestureDetector>
210 - {props.maximumIcon ? (
211 - <SymbolView
212 - name={props.maximumIcon}
213 - size={22}
214 - tintColorClassName={"accent-icon-muted"}
215 - type="monochrome"
216 - weight="regular"
71 />
72 + </NativeThemeHost>
73 + {props.maximumIcon ? (
74 + <SymbolView name={props.maximumIcon} size={22} tintColorClassName="accent-icon-muted" />
75 ) : null}
76 </View>
77 </View>
apps/mobile/src/components/SliderRow.types.ts new
+18
@@ -0,0 +1,18 @@
1 +import type { ComponentProps } from "react";
2 +import type { SymbolView } from "./AppSymbol";
3 +
4 +type SymbolName = ComponentProps<typeof SymbolView>["name"];
5 +
6 +export interface SliderRowProps {
7 + readonly disabled?: boolean;
8 + readonly icon: SymbolName;
9 + readonly minimumIcon?: SymbolName;
10 + readonly maximumIcon?: SymbolName;
11 + readonly label: string;
12 + readonly valueLabel: string;
13 + readonly min: number;
14 + readonly max: number;
15 + readonly step: number;
16 + readonly value: number;
17 + readonly onChange: (value: number) => void;
18 +}