@vvedantb/native-ui 0.40.0 → 0.41.0

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.
@@ -0,0 +1,180 @@
1
+ import { useEffect, useState } from "react";
2
+ import { PanResponder, View } from "react-native";
3
+ import Animated, { Easing, useAnimatedStyle, useSharedValue, withTiming } from "react-native-reanimated";
4
+ import { cn } from "./cn";
5
+ import { useGlass } from "./glass";
6
+ import { selectionHaptic } from "./haptics";
7
+ import { palette } from "./palette";
8
+ import { useControllableState } from "./primitives/use-controllable-state";
9
+ import { snapSliderValue, sliderPercent, sliderValueFromOffset } from "./sliderMath";
10
+ import { useAccentColor } from "./theme";
11
+ import { tokens } from "./tokens";
12
+ import { useReduceMotion } from "./useReduceMotion";
13
+ import { useScheme } from "./useScheme";
14
+ const THUMB_LONG = 32;
15
+ const THUMB_SHORT = 20;
16
+ const DRAG_SCALE = 1.45;
17
+
18
+ /** Track 6 thick, glass pill thumb 32 × 20. The thumb grows to 1.45× and turns translucent while dragged. `surface="flat"` drops the glass shadow. */
19
+ export function Slider({
20
+ value,
21
+ defaultValue = 0,
22
+ onValueChange,
23
+ onSlidingComplete,
24
+ min = 0,
25
+ max = 100,
26
+ step = 1,
27
+ disabled = false,
28
+ orientation = "horizontal",
29
+ className,
30
+ style,
31
+ ...props
32
+ }) {
33
+ "use no memo";
34
+
35
+ // PanResponder reads a mutable gesture box that the compiler cannot model.
36
+ const vertical = orientation === "vertical";
37
+ const scheme = useScheme();
38
+ const accent = useAccentColor();
39
+ const glass = useGlass("small");
40
+ const reduceMotion = useReduceMotion();
41
+ const [current, setCurrent] = useControllableState({
42
+ value,
43
+ defaultValue,
44
+ onChange: onValueChange
45
+ });
46
+ const [length, setLength] = useState(0);
47
+ const [dragging, setDragging] = useState(false);
48
+ const scale = useSharedValue(1);
49
+ const [box] = useState(() => ({
50
+ min,
51
+ max,
52
+ step,
53
+ vertical,
54
+ disabled,
55
+ value: current,
56
+ length: 0,
57
+ setValue: setCurrent,
58
+ onSlidingComplete
59
+ }));
60
+ useEffect(() => {
61
+ box.min = min;
62
+ box.max = max;
63
+ box.step = step;
64
+ box.vertical = vertical;
65
+ box.disabled = disabled;
66
+ box.value = current;
67
+ box.setValue = setCurrent;
68
+ box.onSlidingComplete = onSlidingComplete;
69
+ });
70
+ useEffect(() => {
71
+ const target = dragging ? DRAG_SCALE : 1;
72
+ scale.set(reduceMotion ? target : withTiming(target, {
73
+ duration: 200,
74
+ easing: Easing.bezier(0.22, 1, 0.36, 1)
75
+ }));
76
+ }, [dragging, reduceMotion, scale]);
77
+ const [responder] = useState(() => {
78
+ const commit = offset => {
79
+ const next = snapSliderValue(sliderValueFromOffset(offset, box.length, box.min, box.max, box.vertical), box.min, box.max, box.step);
80
+ if (next === box.value) return;
81
+ box.value = next;
82
+ selectionHaptic();
83
+ box.setValue(next);
84
+ };
85
+ const offsetOf = event => box.vertical ? event.nativeEvent.locationY : event.nativeEvent.locationX;
86
+ const finish = () => {
87
+ setDragging(false);
88
+ box.onSlidingComplete?.(box.value);
89
+ };
90
+ return PanResponder.create({
91
+ onStartShouldSetPanResponder: () => !box.disabled,
92
+ onMoveShouldSetPanResponder: () => !box.disabled,
93
+ onPanResponderTerminationRequest: () => false,
94
+ onPanResponderGrant: event_0 => {
95
+ setDragging(true);
96
+ commit(offsetOf(event_0));
97
+ },
98
+ onPanResponderMove: event_1 => commit(offsetOf(event_1)),
99
+ onPanResponderRelease: finish,
100
+ onPanResponderTerminate: finish
101
+ });
102
+ });
103
+ const onLayout = event_2 => {
104
+ const next_0 = vertical ? event_2.nativeEvent.layout.height : event_2.nativeEvent.layout.width;
105
+ box.length = next_0;
106
+ setLength(next_0);
107
+ };
108
+ const onAccessibilityAction = event_3 => {
109
+ const direction = event_3.nativeEvent.actionName === "increment" ? 1 : -1;
110
+ const next_1 = snapSliderValue(box.value + direction * (step > 0 ? step : (max - min) / 10), min, max, step);
111
+ if (next_1 === box.value) return;
112
+ box.value = next_1;
113
+ setCurrent(next_1);
114
+ onSlidingComplete?.(next_1);
115
+ };
116
+ const percent = sliderPercent(current, min, max);
117
+ const travel = Math.max(0, length - THUMB_LONG);
118
+ const thumbOffset = percent / 100 * travel;
119
+ const thumbW = vertical ? THUMB_SHORT : THUMB_LONG;
120
+ const thumbH = vertical ? THUMB_LONG : THUMB_SHORT;
121
+ const thumbStyle = useAnimatedStyle(() => ({
122
+ transform: [{
123
+ scale: scale.get()
124
+ }],
125
+ opacity: 1 - (scale.get() - 1) / (DRAG_SCALE - 1) * 0.15
126
+ }));
127
+ const trackColor = palette.default[scheme];
128
+ const thumbColor = scheme === "dark" ? tokens.color.foreground.dark : tokens.color.surface.light;
129
+ return <View accessibilityRole="adjustable" accessibilityState={{
130
+ disabled
131
+ }} accessibilityValue={{
132
+ min,
133
+ max,
134
+ now: current
135
+ }} accessibilityActions={[{
136
+ name: "increment"
137
+ }, {
138
+ name: "decrement"
139
+ }]} onAccessibilityAction={onAccessibilityAction} onLayout={onLayout} className={cn(vertical ? "h-full min-h-40 w-8 items-center" : "h-8 w-full justify-center", disabled && "opacity-50", className)} style={style} {...responder.panHandlers} {...props}>
140
+ <View pointerEvents="none" className="overflow-hidden rounded-full" style={{
141
+ backgroundColor: trackColor,
142
+ ...(vertical ? {
143
+ width: 6,
144
+ height: "100%"
145
+ } : {
146
+ height: 6,
147
+ width: "100%"
148
+ })
149
+ }}>
150
+ <View style={{
151
+ position: "absolute",
152
+ backgroundColor: accent,
153
+ ...(vertical ? {
154
+ bottom: 0,
155
+ left: 0,
156
+ right: 0,
157
+ height: `${percent}%`
158
+ } : {
159
+ left: 0,
160
+ top: 0,
161
+ bottom: 0,
162
+ width: `${percent}%`
163
+ })
164
+ }} />
165
+ </View>
166
+ <Animated.View pointerEvents="none" style={[{
167
+ position: "absolute",
168
+ width: thumbW,
169
+ height: thumbH,
170
+ borderRadius: 999,
171
+ backgroundColor: thumbColor,
172
+ ...(vertical ? {
173
+ bottom: thumbOffset,
174
+ alignSelf: "center"
175
+ } : {
176
+ left: thumbOffset
177
+ })
178
+ }, glass, thumbStyle]} />
179
+ </View>;
180
+ }
@@ -8,7 +8,7 @@ import { tokens } from "../tokens";
8
8
  import { useScheme } from "../useScheme";
9
9
 
10
10
  /** The phone chat icon button: 40 square with an 18 icon (hit slop reaches 44). */
11
- export const CHAT_ICON_BUTTON_SIZE = 40;
11
+ const CHAT_ICON_BUTTON_SIZE = 40;
12
12
  const CHAT_ICON_BUTTON = {
13
13
  xs: {
14
14
  side: 36,
package/dist/index.js CHANGED
@@ -40,6 +40,7 @@ export { Stepper } from "./Stepper";
40
40
  export { CaptureButton } from "./CaptureButton";
41
41
  export { Separator } from "./Separator";
42
42
  export { Progress } from "./Progress";
43
+ export { Slider } from "./Slider";
43
44
  export { Spinner, CenteredSpinner } from "./Spinner";
44
45
  export { KeyboardAvoidingView } from "./primitives/keyboard-parts";
45
46
  export { LoadingState } from "./LoadingState";
@@ -0,0 +1,16 @@
1
+ export function snapSliderValue(raw, min, max, step) {
2
+ const clamped = Math.min(max, Math.max(min, raw));
3
+ if (step <= 0) return clamped;
4
+ const snapped = min + Math.round((clamped - min) / step) * step;
5
+ const decimals = (String(step).split(".")[1] ?? "").length;
6
+ return Math.min(max, Number(snapped.toFixed(decimals)));
7
+ }
8
+ export function sliderValueFromOffset(offset, length, min, max, vertical) {
9
+ if (length <= 0) return min;
10
+ const ratio = Math.min(1, Math.max(0, offset / length));
11
+ return min + (vertical ? 1 - ratio : ratio) * (max - min);
12
+ }
13
+ export function sliderPercent(value, min, max) {
14
+ if (max <= min) return 0;
15
+ return Math.min(100, Math.max(0, (value - min) / (max - min) * 100));
16
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vvedantb/native-ui",
3
- "version": "0.40.0",
3
+ "version": "0.41.0",
4
4
  "license": "MIT",
5
5
  "description": "Shared React Native / Expo UI primitives. Floating surfaces, forms, overlays, toasts, settings chrome, tab bar, AI chat parts, the family app marks, a NativeWind preset, the Expo web root, the app root shell helpers and Clerk + Convex auth screens.",
6
6
  "type": "module",
package/src/Slider.tsx ADDED
@@ -0,0 +1,208 @@
1
+ import { useEffect, useState } from "react";
2
+ import {
3
+ PanResponder,
4
+ type GestureResponderEvent,
5
+ View,
6
+ type LayoutChangeEvent,
7
+ type ViewProps,
8
+ } from "react-native";
9
+ import Animated, {
10
+ Easing,
11
+ useAnimatedStyle,
12
+ useSharedValue,
13
+ withTiming,
14
+ } from "react-native-reanimated";
15
+ import { cn } from "./cn";
16
+ import { useGlass } from "./glass";
17
+ import { selectionHaptic } from "./haptics";
18
+ import { palette } from "./palette";
19
+ import { useControllableState } from "./primitives/use-controllable-state";
20
+ import { snapSliderValue, sliderPercent, sliderValueFromOffset } from "./sliderMath";
21
+ import { useAccentColor } from "./theme";
22
+ import { tokens } from "./tokens";
23
+ import { useReduceMotion } from "./useReduceMotion";
24
+ import { useScheme } from "./useScheme";
25
+
26
+ export type SliderProps = Omit<ViewProps, "children"> & {
27
+ value?: number;
28
+ defaultValue?: number;
29
+ onValueChange?: (value: number) => void;
30
+ onSlidingComplete?: (value: number) => void;
31
+ min?: number;
32
+ max?: number;
33
+ step?: number;
34
+ disabled?: boolean;
35
+ orientation?: "horizontal" | "vertical";
36
+ className?: string;
37
+ };
38
+
39
+ const THUMB_LONG = 32;
40
+ const THUMB_SHORT = 20;
41
+ const DRAG_SCALE = 1.45;
42
+
43
+ /** Track 6 thick, glass pill thumb 32 × 20. The thumb grows to 1.45× and turns translucent while dragged. `surface="flat"` drops the glass shadow. */
44
+ export function Slider({
45
+ value,
46
+ defaultValue = 0,
47
+ onValueChange,
48
+ onSlidingComplete,
49
+ min = 0,
50
+ max = 100,
51
+ step = 1,
52
+ disabled = false,
53
+ orientation = "horizontal",
54
+ className,
55
+ style,
56
+ ...props
57
+ }: SliderProps) {
58
+ "use no memo"; // PanResponder reads a mutable gesture box that the compiler cannot model.
59
+ const vertical = orientation === "vertical";
60
+ const scheme = useScheme();
61
+ const accent = useAccentColor();
62
+ const glass = useGlass("small");
63
+ const reduceMotion = useReduceMotion();
64
+ const [current, setCurrent] = useControllableState({ value, defaultValue, onChange: onValueChange });
65
+ const [length, setLength] = useState(0);
66
+ const [dragging, setDragging] = useState(false);
67
+ const scale = useSharedValue(1);
68
+ const [box] = useState(() => ({
69
+ min,
70
+ max,
71
+ step,
72
+ vertical,
73
+ disabled,
74
+ value: current,
75
+ length: 0,
76
+ setValue: setCurrent,
77
+ onSlidingComplete,
78
+ }));
79
+
80
+ useEffect(() => {
81
+ box.min = min;
82
+ box.max = max;
83
+ box.step = step;
84
+ box.vertical = vertical;
85
+ box.disabled = disabled;
86
+ box.value = current;
87
+ box.setValue = setCurrent;
88
+ box.onSlidingComplete = onSlidingComplete;
89
+ });
90
+
91
+ useEffect(() => {
92
+ const target = dragging ? DRAG_SCALE : 1;
93
+ scale.set(
94
+ reduceMotion ? target : withTiming(target, { duration: 200, easing: Easing.bezier(0.22, 1, 0.36, 1) }),
95
+ );
96
+ }, [dragging, reduceMotion, scale]);
97
+
98
+ const [responder] = useState(() => {
99
+ const commit = (offset: number) => {
100
+ const next = snapSliderValue(
101
+ sliderValueFromOffset(offset, box.length, box.min, box.max, box.vertical),
102
+ box.min,
103
+ box.max,
104
+ box.step,
105
+ );
106
+ if (next === box.value) return;
107
+ box.value = next;
108
+ selectionHaptic();
109
+ box.setValue(next);
110
+ };
111
+ const offsetOf = (event: GestureResponderEvent) =>
112
+ box.vertical ? event.nativeEvent.locationY : event.nativeEvent.locationX;
113
+ const finish = () => {
114
+ setDragging(false);
115
+ box.onSlidingComplete?.(box.value);
116
+ };
117
+ return PanResponder.create({
118
+ onStartShouldSetPanResponder: () => !box.disabled,
119
+ onMoveShouldSetPanResponder: () => !box.disabled,
120
+ onPanResponderTerminationRequest: () => false,
121
+ onPanResponderGrant: (event) => {
122
+ setDragging(true);
123
+ commit(offsetOf(event));
124
+ },
125
+ onPanResponderMove: (event) => commit(offsetOf(event)),
126
+ onPanResponderRelease: finish,
127
+ onPanResponderTerminate: finish,
128
+ });
129
+ });
130
+
131
+ const onLayout = (event: LayoutChangeEvent) => {
132
+ const next = vertical ? event.nativeEvent.layout.height : event.nativeEvent.layout.width;
133
+ box.length = next;
134
+ setLength(next);
135
+ };
136
+
137
+ const onAccessibilityAction = (event: { nativeEvent: { actionName: string } }) => {
138
+ const direction = event.nativeEvent.actionName === "increment" ? 1 : -1;
139
+ const next = snapSliderValue(box.value + direction * (step > 0 ? step : (max - min) / 10), min, max, step);
140
+ if (next === box.value) return;
141
+ box.value = next;
142
+ setCurrent(next);
143
+ onSlidingComplete?.(next);
144
+ };
145
+
146
+ const percent = sliderPercent(current, min, max);
147
+ const travel = Math.max(0, length - THUMB_LONG);
148
+ const thumbOffset = (percent / 100) * travel;
149
+ const thumbW = vertical ? THUMB_SHORT : THUMB_LONG;
150
+ const thumbH = vertical ? THUMB_LONG : THUMB_SHORT;
151
+
152
+ const thumbStyle = useAnimatedStyle(() => ({
153
+ transform: [{ scale: scale.get() }],
154
+ opacity: 1 - ((scale.get() - 1) / (DRAG_SCALE - 1)) * 0.15,
155
+ }));
156
+
157
+ const trackColor = palette.default[scheme];
158
+ const thumbColor = scheme === "dark" ? tokens.color.foreground.dark : tokens.color.surface.light;
159
+
160
+ return (
161
+ <View
162
+ accessibilityRole="adjustable"
163
+ accessibilityState={{ disabled }}
164
+ accessibilityValue={{ min, max, now: current }}
165
+ accessibilityActions={[{ name: "increment" }, { name: "decrement" }]}
166
+ onAccessibilityAction={onAccessibilityAction}
167
+ onLayout={onLayout}
168
+ className={cn(vertical ? "h-full min-h-40 w-8 items-center" : "h-8 w-full justify-center", disabled && "opacity-50", className)}
169
+ style={style}
170
+ {...responder.panHandlers}
171
+ {...props}
172
+ >
173
+ <View
174
+ pointerEvents="none"
175
+ className="overflow-hidden rounded-full"
176
+ style={{
177
+ backgroundColor: trackColor,
178
+ ...(vertical ? { width: 6, height: "100%" } : { height: 6, width: "100%" }),
179
+ }}
180
+ >
181
+ <View
182
+ style={{
183
+ position: "absolute",
184
+ backgroundColor: accent,
185
+ ...(vertical
186
+ ? { bottom: 0, left: 0, right: 0, height: `${percent}%` }
187
+ : { left: 0, top: 0, bottom: 0, width: `${percent}%` }),
188
+ }}
189
+ />
190
+ </View>
191
+ <Animated.View
192
+ pointerEvents="none"
193
+ style={[
194
+ {
195
+ position: "absolute",
196
+ width: thumbW,
197
+ height: thumbH,
198
+ borderRadius: 999,
199
+ backgroundColor: thumbColor,
200
+ ...(vertical ? { bottom: thumbOffset, alignSelf: "center" as const } : { left: thumbOffset }),
201
+ },
202
+ glass,
203
+ thumbStyle,
204
+ ]}
205
+ />
206
+ </View>
207
+ );
208
+ }
@@ -8,7 +8,7 @@ import { tokens } from "../tokens";
8
8
  import { useScheme } from "../useScheme";
9
9
 
10
10
  /** The phone chat icon button: 40 square with an 18 icon (hit slop reaches 44). */
11
- export const CHAT_ICON_BUTTON_SIZE = 40;
11
+ const CHAT_ICON_BUTTON_SIZE = 40;
12
12
 
13
13
  const CHAT_ICON_BUTTON = {
14
14
  xs: { side: 36, icon: 16, className: "h-9 w-9" },
package/src/index.ts CHANGED
@@ -110,6 +110,8 @@ export { Separator } from "./Separator";
110
110
  export type { SeparatorProps } from "./Separator";
111
111
  export { Progress } from "./Progress";
112
112
  export type { ProgressProps } from "./Progress";
113
+ export { Slider } from "./Slider";
114
+ export type { SliderProps } from "./Slider";
113
115
  export { Spinner, CenteredSpinner } from "./Spinner";
114
116
  export { KeyboardAvoidingView } from "./primitives/keyboard-parts";
115
117
  export type { KeyboardAvoidingViewProps } from "./primitives/keyboard-parts";
@@ -0,0 +1,18 @@
1
+ export function snapSliderValue(raw: number, min: number, max: number, step: number) {
2
+ const clamped = Math.min(max, Math.max(min, raw));
3
+ if (step <= 0) return clamped;
4
+ const snapped = min + Math.round((clamped - min) / step) * step;
5
+ const decimals = (String(step).split(".")[1] ?? "").length;
6
+ return Math.min(max, Number(snapped.toFixed(decimals)));
7
+ }
8
+
9
+ export function sliderValueFromOffset(offset: number, length: number, min: number, max: number, vertical: boolean) {
10
+ if (length <= 0) return min;
11
+ const ratio = Math.min(1, Math.max(0, offset / length));
12
+ return min + (vertical ? 1 - ratio : ratio) * (max - min);
13
+ }
14
+
15
+ export function sliderPercent(value: number, min: number, max: number) {
16
+ if (max <= min) return 0;
17
+ return Math.min(100, Math.max(0, ((value - min) / (max - min)) * 100));
18
+ }