glacia-native 0.3.5 → 0.5.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.
Files changed (121) hide show
  1. package/lib/module/Bars.js +176 -0
  2. package/lib/module/Button.js +15 -2
  3. package/lib/module/Card.js +29 -14
  4. package/lib/module/Carousel.js +103 -0
  5. package/lib/module/Dialog.js +43 -32
  6. package/lib/module/Display.js +240 -0
  7. package/lib/module/Feedback.js +172 -0
  8. package/lib/module/Form.js +51 -12
  9. package/lib/module/Forms2.js +227 -0
  10. package/lib/module/Glass.js +11 -2
  11. package/lib/module/Input.js +19 -7
  12. package/lib/module/Overlays.js +166 -0
  13. package/lib/module/Pickers.js +243 -0
  14. package/lib/module/SegmentedControl.js +65 -21
  15. package/lib/module/Slider.js +121 -25
  16. package/lib/module/Switch.js +33 -15
  17. package/lib/module/TabBar.js +63 -25
  18. package/lib/module/Table.js +24 -6
  19. package/lib/module/Tabs.js +55 -33
  20. package/lib/module/Text.js +37 -0
  21. package/lib/module/ai.js +49 -20
  22. package/lib/module/charts/BarChart.js +4 -2
  23. package/lib/module/charts/FunnelChart.js +4 -2
  24. package/lib/module/charts/HeatmapChart.js +4 -2
  25. package/lib/module/charts/LineChart.js +4 -2
  26. package/lib/module/charts/PlotChart.js +11 -3
  27. package/lib/module/charts/RadarChart.js +4 -2
  28. package/lib/module/charts/RingChart.js +4 -2
  29. package/lib/module/charts/ScatterChart.js +20 -3
  30. package/lib/module/charts/WaterfallChart.js +12 -3
  31. package/lib/module/charts/advanced.js +12 -6
  32. package/lib/module/charts/extra.js +6 -3
  33. package/lib/module/index.js +58 -1
  34. package/lib/module/internal/GlassView.js +31 -17
  35. package/lib/module/internal/fallback.js +14 -3
  36. package/lib/module/internal/glass-routing.js +56 -0
  37. package/lib/module/internal/motion.js +55 -0
  38. package/lib/module/internal/primitives.js +413 -0
  39. package/lib/module/internal/tokens.js +55 -1
  40. package/lib/typescript/Bars.d.ts +121 -0
  41. package/lib/typescript/Button.d.ts +11 -1
  42. package/lib/typescript/Card.d.ts +20 -8
  43. package/lib/typescript/Carousel.d.ts +47 -0
  44. package/lib/typescript/Dialog.d.ts +19 -6
  45. package/lib/typescript/Display.d.ts +119 -0
  46. package/lib/typescript/Feedback.d.ts +138 -0
  47. package/lib/typescript/Form.d.ts +27 -11
  48. package/lib/typescript/Forms2.d.ts +128 -0
  49. package/lib/typescript/Glass.d.ts +10 -1
  50. package/lib/typescript/Input.d.ts +11 -1
  51. package/lib/typescript/Overlays.d.ts +105 -0
  52. package/lib/typescript/Pickers.d.ts +105 -0
  53. package/lib/typescript/SegmentedControl.d.ts +11 -1
  54. package/lib/typescript/Slider.d.ts +13 -1
  55. package/lib/typescript/Switch.d.ts +12 -1
  56. package/lib/typescript/TabBar.d.ts +13 -1
  57. package/lib/typescript/Table.d.ts +14 -5
  58. package/lib/typescript/Tabs.d.ts +8 -5
  59. package/lib/typescript/Text.d.ts +28 -0
  60. package/lib/typescript/ai.d.ts +12 -9
  61. package/lib/typescript/charts/BarChart.d.ts +1 -1
  62. package/lib/typescript/charts/FunnelChart.d.ts +1 -1
  63. package/lib/typescript/charts/HeatmapChart.d.ts +1 -1
  64. package/lib/typescript/charts/LineChart.d.ts +1 -1
  65. package/lib/typescript/charts/PlotChart.d.ts +1 -1
  66. package/lib/typescript/charts/RadarChart.d.ts +1 -1
  67. package/lib/typescript/charts/RingChart.d.ts +1 -1
  68. package/lib/typescript/charts/ScatterChart.d.ts +1 -1
  69. package/lib/typescript/charts/WaterfallChart.d.ts +1 -1
  70. package/lib/typescript/charts/advanced.d.ts +5 -5
  71. package/lib/typescript/charts/extra.d.ts +2 -2
  72. package/lib/typescript/index.d.ts +9 -1
  73. package/lib/typescript/internal/GlassView.d.ts +21 -4
  74. package/lib/typescript/internal/fallback.d.ts +10 -1
  75. package/lib/typescript/internal/glass-routing.d.ts +30 -0
  76. package/lib/typescript/internal/motion.d.ts +19 -0
  77. package/lib/typescript/internal/primitives.d.ts +41 -0
  78. package/lib/typescript/internal/tokens.d.ts +61 -0
  79. package/package.json +2 -2
  80. package/src/Bars.tsx +570 -0
  81. package/src/Button.tsx +47 -22
  82. package/src/Card.tsx +52 -31
  83. package/src/Carousel.tsx +233 -0
  84. package/src/Dialog.tsx +99 -64
  85. package/src/Display.tsx +550 -0
  86. package/src/Feedback.tsx +579 -0
  87. package/src/Form.tsx +90 -25
  88. package/src/Forms2.tsx +608 -0
  89. package/src/Glass.tsx +24 -1
  90. package/src/Input.tsx +33 -12
  91. package/src/Overlays.tsx +474 -0
  92. package/src/Pickers.tsx +590 -0
  93. package/src/SegmentedControl.tsx +104 -32
  94. package/src/Slider.tsx +151 -47
  95. package/src/Switch.tsx +56 -32
  96. package/src/TabBar.tsx +100 -30
  97. package/src/Table.tsx +38 -14
  98. package/src/Tabs.tsx +85 -32
  99. package/src/Text.tsx +83 -0
  100. package/src/ai.tsx +77 -31
  101. package/src/charts/BarChart.tsx +4 -2
  102. package/src/charts/FunnelChart.tsx +4 -2
  103. package/src/charts/HeatmapChart.tsx +4 -2
  104. package/src/charts/LineChart.tsx +4 -2
  105. package/src/charts/PlotChart.tsx +23 -3
  106. package/src/charts/RadarChart.tsx +4 -2
  107. package/src/charts/RingChart.tsx +4 -2
  108. package/src/charts/ScatterChart.tsx +24 -3
  109. package/src/charts/WaterfallChart.tsx +21 -3
  110. package/src/charts/advanced.tsx +12 -6
  111. package/src/charts/extra.tsx +6 -3
  112. package/src/index.ts +101 -1
  113. package/src/internal/GlassView.tsx +51 -19
  114. package/src/internal/fallback.tsx +23 -8
  115. package/src/internal/glass-routing.ts +68 -0
  116. package/src/internal/motion.ts +60 -0
  117. package/src/internal/primitives.ts +482 -0
  118. package/src/internal/tokens.ts +62 -0
  119. package/lib/module/charts/primitives.js +0 -212
  120. package/lib/typescript/charts/primitives.d.ts +0 -23
  121. package/src/charts/primitives.ts +0 -262
@@ -1,16 +1,31 @@
1
- import { useState, forwardRef } from "react";
1
+ import { forwardRef, useState } from "react";
2
+ import type { StyleProp, ViewStyle } from "react-native";
3
+
2
4
  import { Glass } from "./Glass";
3
5
  import { colors, radii } from "./internal/tokens";
6
+ import { primitives } from "./internal/primitives";
7
+ import { useIsDark, useSpring } from "./internal/motion";
4
8
 
5
9
  export interface SegmentedControlProps {
6
10
  values: string[];
7
11
  selectedIndex?: number;
8
12
  defaultIndex?: number;
9
13
  onValueChange?: (value: string, index: number) => void;
10
- style?: any;
14
+ style?: StyleProp<ViewStyle>;
11
15
  isDark?: boolean;
16
+ /** Passed through to the control so it is testable. */
17
+ testID?: string;
12
18
  }
13
19
 
20
+ const SEGMENT_HEIGHT = 30;
21
+
22
+ /**
23
+ * A row of mutually exclusive segments on one glass container.
24
+ *
25
+ * The selected segment is marked by a pill that slides on the springs token.
26
+ * The pill's colours come from the theme tokens, so it reads correctly in both
27
+ * light and dark instead of assuming a white-on-light palette.
28
+ */
14
29
  export const SegmentedControl = forwardRef<any, SegmentedControlProps>(function SegmentedControl(
15
30
  {
16
31
  values,
@@ -18,60 +33,117 @@ export const SegmentedControl = forwardRef<any, SegmentedControlProps>(function
18
33
  defaultIndex = 0,
19
34
  onValueChange,
20
35
  style,
21
- isDark = false,
36
+ isDark: isDarkProp,
37
+ testID,
22
38
  ...props
23
39
  },
24
40
  ref,
25
41
  ) {
42
+ const isDark = useIsDark(isDarkProp);
43
+ const themeColors = isDark ? colors.dark : colors.light;
44
+ const { View, Text, Pressable, Animated } = primitives();
45
+
26
46
  const [internalIndex, setInternalIndex] = useState(defaultIndex);
27
47
  const activeIndex = selectedProp !== undefined ? selectedProp : internalIndex;
28
- const themeColors = isDark ? colors.dark : colors.light;
48
+ const safeIndex = Math.min(Math.max(activeIndex, 0), Math.max(values.length - 1, 0));
49
+
50
+ // The pill slides between equal-width segments, so a ratio is enough: 0 is
51
+ // the first segment, values.length - 1 the last.
52
+ const { value: slide, animated } = useSpring(safeIndex, "snappy");
29
53
 
30
54
  const handleSelect = (index: number) => {
31
- if (selectedProp === undefined) {
32
- setInternalIndex(index);
33
- }
55
+ if (selectedProp === undefined) setInternalIndex(index);
34
56
  const val = values[index];
35
- if (val !== undefined) {
36
- onValueChange?.(val, index);
37
- }
57
+ if (val !== undefined) onValueChange?.(val, index);
38
58
  };
39
59
 
40
- const containerStyle = {
41
- position: "relative" as const,
42
- display: "inline-flex" as const,
43
- flexDirection: "row" as const,
44
- alignItems: "center" as const,
60
+ const count = Math.max(values.length, 1);
61
+ const pillWidth = `${100 / count}%`;
62
+
63
+ const containerStyle: ViewStyle = {
64
+ position: "relative",
65
+ flexDirection: "row",
66
+ alignItems: "center",
67
+ alignSelf: "flex-start",
45
68
  padding: 2,
46
69
  borderRadius: radii.md,
47
70
  backgroundColor: "rgba(118, 118, 128, 0.12)",
48
- ...style,
71
+ ...(style as ViewStyle),
49
72
  };
50
73
 
74
+ const pillTranslate = animated
75
+ ? slide.interpolate({
76
+ inputRange: [0, Math.max(count - 1, 1)],
77
+ // One segment's worth per step, measured against the pill's own width.
78
+ outputRange: ["0%", `${((count - 1) * 100) / count}%`],
79
+ })
80
+ : `${(safeIndex * 100) / count}%`;
81
+
51
82
  return (
52
- <Glass ref={ref} effect="regular" style={containerStyle} {...props}>
83
+ <Glass ref={ref} effect="regular" isDark={isDark} style={containerStyle} testID={testID} {...props}>
84
+ {/* The sliding pill: one View behind the labels, coloured by the theme. */}
85
+ {animated ? (
86
+ <Animated.View
87
+ style={{
88
+ position: "absolute",
89
+ top: 2,
90
+ left: 2,
91
+ width: pillWidth,
92
+ height: SEGMENT_HEIGHT,
93
+ borderRadius: radii.sm,
94
+ backgroundColor: themeColors.glassActive,
95
+ borderWidth: 0.5,
96
+ borderColor: themeColors.edge,
97
+ transform: [{ translateX: pillTranslate }],
98
+ }}
99
+ pointerEvents="none"
100
+ />
101
+ ) : (
102
+ <View
103
+ style={{
104
+ position: "absolute",
105
+ top: 2,
106
+ left: `${2 + (safeIndex * 100) / count}%`,
107
+ width: pillWidth,
108
+ height: SEGMENT_HEIGHT,
109
+ borderRadius: radii.sm,
110
+ backgroundColor: themeColors.glassActive,
111
+ borderWidth: 0.5,
112
+ borderColor: themeColors.edge,
113
+ }}
114
+ pointerEvents="none"
115
+ />
116
+ )}
117
+
53
118
  {values.map((label, idx) => {
54
- const isSelected = idx === activeIndex;
119
+ const isSelected = idx === safeIndex;
55
120
  return (
56
- <button
121
+ <Pressable
57
122
  key={label}
58
- type="button"
59
- onClick={() => handleSelect(idx)}
123
+ accessibilityRole="tab"
124
+ accessibilityLabel={label}
125
+ accessibilityState={{ selected: isSelected }}
126
+ onPress={() => handleSelect(idx)}
60
127
  style={{
61
- padding: "6px 16px",
128
+ flex: 1,
129
+ height: SEGMENT_HEIGHT,
130
+ alignItems: "center",
131
+ justifyContent: "center",
132
+ paddingHorizontal: 16,
62
133
  borderRadius: radii.sm,
63
- border: 0,
64
- backgroundColor: isSelected ? "#FFFFFF" : "transparent",
65
- color: isSelected ? "#000000" : themeColors.muted,
66
- boxShadow: isSelected ? "0 1px 3px rgba(0, 0, 0, 0.12)" : "none",
67
- fontSize: 13,
68
- fontWeight: 500,
69
- cursor: "pointer",
70
- transition: "all 150ms ease",
71
134
  }}
72
135
  >
73
- {label}
74
- </button>
136
+ <Text
137
+ numberOfLines={1}
138
+ style={{
139
+ fontSize: 13,
140
+ fontWeight: isSelected ? "600" : "500",
141
+ color: isSelected ? themeColors.foreground : themeColors.muted,
142
+ }}
143
+ >
144
+ {label}
145
+ </Text>
146
+ </Pressable>
75
147
  );
76
148
  })}
77
149
  </Glass>
package/src/Slider.tsx CHANGED
@@ -1,6 +1,11 @@
1
- import { useState, forwardRef } from "react";
1
+ import { forwardRef, useMemo, useRef, useState } from "react";
2
+ import { measurePreviewWidth } from "./internal/primitives";
3
+ import type { StyleProp, ViewStyle } from "react-native";
4
+
2
5
  import { Glass } from "./Glass";
3
6
  import { colors } from "./internal/tokens";
7
+ import { primitives } from "./internal/primitives";
8
+ import { useIsDark } from "./internal/motion";
4
9
 
5
10
  export interface SliderProps {
6
11
  value?: number;
@@ -11,10 +16,36 @@ export interface SliderProps {
11
16
  onValueChange?: (value: number) => void;
12
17
  disabled?: boolean;
13
18
  accessibilityLabel: string;
14
- style?: any;
19
+ style?: StyleProp<ViewStyle>;
15
20
  isDark?: boolean;
21
+ /** Passed through to the draggable track so it is testable. */
22
+ testID?: string;
23
+ }
24
+
25
+ const TRACK_HEIGHT = 8;
26
+ const THUMB = 22;
27
+ const CONTAINER_HEIGHT = 40;
28
+
29
+ /** Rounds `value` onto the nearest step from `min`, clamped to the range. */
30
+ function quantize(value: number, min: number, max: number, step: number): number {
31
+ if (!Number.isFinite(value)) return min;
32
+ const clamped = Math.min(max, Math.max(min, value));
33
+ if (!(step > 0)) return clamped;
34
+ const stepped = min + Math.round((clamped - min) / step) * step;
35
+ // Round away binary float noise introduced by the division above.
36
+ const decimals = (String(step).split(".")[1] ?? "").length;
37
+ return Number(Math.min(max, Math.max(min, stepped)).toFixed(decimals));
16
38
  }
17
39
 
40
+ /**
41
+ * A continuous value chosen by dragging.
42
+ *
43
+ * The interaction is Glacia's own: the track and thumb are Views and dragging
44
+ * is a PanResponder reading the touch position against the track's measured
45
+ * width. React Native has no DOM `<input type="range">` and the platform
46
+ * sliders are not used, so the control looks and behaves the same on every
47
+ * platform; only the glass material changes.
48
+ */
18
49
  export const Slider = forwardRef<any, SliderProps>(function Slider(
19
50
  {
20
51
  value: valueProp,
@@ -26,72 +57,145 @@ export const Slider = forwardRef<any, SliderProps>(function Slider(
26
57
  disabled = false,
27
58
  accessibilityLabel,
28
59
  style,
29
- isDark = false,
60
+ isDark: isDarkProp,
61
+ testID,
30
62
  ...props
31
63
  },
32
64
  ref,
33
65
  ) {
34
- const [internalValue, setInternalValue] = useState(defaultValue);
35
- const value = valueProp !== undefined ? valueProp : internalValue;
66
+ const isDark = useIsDark(isDarkProp);
36
67
  const themeColors = isDark ? colors.dark : colors.light;
68
+ const { View, Pressable, PanResponder } = primitives();
69
+
70
+ const [internalValue, setInternalValue] = useState(() => quantize(defaultValue, min, max, step));
71
+ const value = valueProp !== undefined ? quantize(valueProp, min, max, step) : internalValue;
72
+
73
+ // Two actions can arrive before React re-renders (increment then decrement,
74
+ // or two drag moves in one frame). The ref holds the newest value so the
75
+ // second one starts from it instead of a stale closure.
76
+ const valueRef = useRef(value);
77
+ valueRef.current = value;
78
+
79
+ // Track width as measured on layout. A drag before layout is a no-op rather
80
+ // than a jump: without a width there is no honest mapping from x to value.
81
+ const widthRef = useRef(0);
37
82
 
38
- const percentage = Math.min(100, Math.max(0, ((value - min) / (max - min)) * 100));
83
+ /** Converts an x position relative to the track into a stepped value. */
84
+ const valueAtX = useMemo(
85
+ () =>
86
+ (x: number): number | null => {
87
+ const width = widthRef.current;
88
+ if (width <= 0 || max === min) return null;
89
+ const ratio = Math.min(1, Math.max(0, x / width));
90
+ return quantize(min + ratio * (max - min), min, max, step);
91
+ },
92
+ [max, min, step],
93
+ );
94
+
95
+ const report = (next: number) => {
96
+ if (next === valueRef.current) return;
97
+ valueRef.current = next;
98
+ if (valueProp === undefined) setInternalValue(next);
99
+ onValueChange?.(next);
100
+ };
101
+
102
+ const panResponder = useMemo(() => {
103
+ if (!PanResponder?.create) return null;
104
+ return PanResponder.create({
105
+ // Claim the gesture only for horizontal movement, so a Slider inside a
106
+ // vertical scroll view does not swallow the scroll.
107
+ onStartShouldSetPanResponder: () => !disabled,
108
+ onMoveShouldSetPanResponder: (_event: any, gesture: any) =>
109
+ !disabled && Math.abs(gesture.dx) > Math.abs(gesture.dy),
110
+ onPanResponderGrant: (event: any) => {
111
+ const next = valueAtX(event.nativeEvent.locationX);
112
+ if (next !== null) report(next);
113
+ },
114
+ onPanResponderMove: (event: any) => {
115
+ const next = valueAtX(event.nativeEvent.locationX);
116
+ if (next !== null) report(next);
117
+ },
118
+ onPanResponderTerminationRequest: () => false,
119
+ });
120
+ // `report` reads the value from a ref, so the responder does not need to be
121
+ // rebuilt on every value change and never acts on a stale render.
122
+ }, [disabled, valueAtX]);
39
123
 
40
- const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
41
- const val = Number(e.target.value);
42
- if (valueProp === undefined) {
43
- setInternalValue(val);
44
- }
45
- onValueChange?.(val);
124
+ /** Steps the value for accessibility actions and hardware keys. */
125
+ const nudge = (direction: 1 | -1) => {
126
+ if (disabled) return;
127
+ const from = valueRef.current;
128
+ report(quantize(from + direction * Math.max(step, (max - min) / 20), min, max, step));
46
129
  };
47
130
 
48
- const containerStyle = {
49
- position: "relative" as const,
50
- height: 40,
51
- justifyContent: "center" as const,
131
+ const percentage = max === min ? 0 : ((value - min) / (max - min)) * 100;
132
+
133
+ const containerStyle: ViewStyle = {
134
+ height: CONTAINER_HEIGHT,
135
+ justifyContent: "center",
52
136
  opacity: disabled ? 0.38 : 1,
53
- ...style,
137
+ ...(style as ViewStyle),
54
138
  };
55
139
 
56
- const trackStyle = {
57
- height: 8,
58
- borderRadius: 4,
140
+ const trackStyle: ViewStyle = {
141
+ height: TRACK_HEIGHT,
142
+ borderRadius: TRACK_HEIGHT / 2,
59
143
  backgroundColor: "rgba(120, 120, 128, 0.2)",
60
- overflow: "hidden" as const,
144
+ overflow: "hidden",
61
145
  width: "100%",
62
146
  };
63
147
 
64
- const fillStyle = {
148
+ const fillStyle: ViewStyle = {
65
149
  height: "100%",
66
- width: `${percentage}%`,
150
+ width: `${Math.min(100, Math.max(0, percentage))}%`,
67
151
  backgroundColor: themeColors.fill,
68
152
  };
69
153
 
154
+ const thumbStyle: ViewStyle = {
155
+ position: "absolute",
156
+ top: (CONTAINER_HEIGHT - THUMB) / 2,
157
+ left: `${Math.min(100, Math.max(0, percentage))}%`,
158
+ width: THUMB,
159
+ height: THUMB,
160
+ marginLeft: -THUMB / 2,
161
+ borderRadius: THUMB / 2,
162
+ backgroundColor: "#FFFFFF",
163
+ borderWidth: 0.5,
164
+ borderColor: "rgba(0, 0, 0, 0.12)",
165
+ };
166
+
70
167
  return (
71
- <div ref={ref} style={containerStyle} {...props}>
72
- <Glass effect="regular" style={trackStyle}>
73
- <div style={fillStyle} />
168
+ <Pressable
169
+ testID={testID}
170
+ accessibilityRole="adjustable"
171
+ accessibilityLabel={accessibilityLabel}
172
+ accessibilityState={{ disabled }}
173
+ accessibilityValue={{ min, max, now: value }}
174
+ accessibilityActions={[{ name: "increment" }, { name: "decrement" }]}
175
+ onAccessibilityAction={(event: any) => {
176
+ if (event?.nativeEvent?.actionName === "increment") nudge(1);
177
+ if (event?.nativeEvent?.actionName === "decrement") nudge(-1);
178
+ }}
179
+ disabled={disabled}
180
+ onLayout={(event: any) => {
181
+ const width = event?.nativeEvent?.layout?.width;
182
+ if (typeof width === "number" && width > 0) widthRef.current = width;
183
+ }}
184
+ // The DOM preview has no layout events, so measure on mount there too;
185
+ // on a device this resolves to nothing and onLayout is the source.
186
+ ref={(node: any) => {
187
+ measurePreviewWidth(node, widthRef);
188
+ if (typeof ref === "function") ref(node);
189
+ else if (ref) ref.current = node;
190
+ }}
191
+ style={containerStyle}
192
+ {...(panResponder ? panResponder.panHandlers : {})}
193
+ {...props}
194
+ >
195
+ <Glass effect="regular" isDark={isDark} style={trackStyle}>
196
+ <View style={fillStyle} />
74
197
  </Glass>
75
- <input
76
- type="range"
77
- min={min}
78
- max={max}
79
- step={step}
80
- value={value}
81
- disabled={disabled}
82
- aria-label={accessibilityLabel}
83
- onChange={handleChange}
84
- style={{
85
- position: "absolute",
86
- top: 0,
87
- left: 0,
88
- width: "100%",
89
- height: "100%",
90
- opacity: 0,
91
- cursor: disabled ? "default" : "pointer",
92
- margin: 0,
93
- }}
94
- />
95
- </div>
198
+ <View style={thumbStyle} pointerEvents="none" />
199
+ </Pressable>
96
200
  );
97
201
  });
package/src/Switch.tsx CHANGED
@@ -1,6 +1,10 @@
1
- import { useState, forwardRef } from "react";
1
+ import { forwardRef, useState } from "react";
2
+ import type { StyleProp, ViewStyle } from "react-native";
3
+
2
4
  import { Glass } from "./Glass";
3
5
  import { colors } from "./internal/tokens";
6
+ import { primitives } from "./internal/primitives";
7
+ import { useIsDark, useSpring } from "./internal/motion";
4
8
 
5
9
  export interface SwitchProps {
6
10
  checked?: boolean;
@@ -8,10 +12,25 @@ export interface SwitchProps {
8
12
  onCheckedChange?: (checked: boolean) => void;
9
13
  disabled?: boolean;
10
14
  accessibilityLabel: string;
11
- style?: any;
15
+ style?: StyleProp<ViewStyle>;
12
16
  isDark?: boolean;
17
+ /** Passed through to the switch track so it is testable. */
18
+ testID?: string;
13
19
  }
14
20
 
21
+ const TRACK_WIDTH = 51;
22
+ const TRACK_HEIGHT = 31;
23
+ const THUMB = 27;
24
+ const TRAVEL = TRACK_WIDTH - THUMB - 4;
25
+
26
+ /**
27
+ * An on/off switch.
28
+ *
29
+ * The track is Glacia's glass; the thumb is one of our own Views, animated on
30
+ * the springs token. The material changes by platform (Apple's glass on
31
+ * iOS 26+, Glacia's tint elsewhere) but the control is always ours — React
32
+ * Native's built-in Switch is never used.
33
+ */
15
34
  export const Switch = forwardRef<any, SwitchProps>(function Switch(
16
35
  {
17
36
  checked: checkedProp,
@@ -20,61 +39,66 @@ export const Switch = forwardRef<any, SwitchProps>(function Switch(
20
39
  disabled = false,
21
40
  accessibilityLabel,
22
41
  style,
23
- isDark = false,
42
+ isDark: isDarkProp,
43
+ testID,
24
44
  ...props
25
45
  },
26
46
  ref,
27
47
  ) {
48
+ const isDark = useIsDark(isDarkProp);
49
+ const themeColors = isDark ? colors.dark : colors.light;
50
+ const { View, Pressable, Animated } = primitives();
28
51
  const [internalChecked, setInternalChecked] = useState(defaultChecked);
29
52
  const isChecked = checkedProp !== undefined ? checkedProp : internalChecked;
30
- const themeColors = isDark ? colors.dark : colors.light;
53
+
54
+ // The thumb's travel is a single number animated on the springs token.
55
+ const { value: offset, animated } = useSpring(isChecked ? TRAVEL : 0, "snappy");
31
56
 
32
57
  const toggle = () => {
33
58
  if (disabled) return;
34
59
  const next = !isChecked;
35
- if (checkedProp === undefined) {
36
- setInternalChecked(next);
37
- }
60
+ if (checkedProp === undefined) setInternalChecked(next);
38
61
  onCheckedChange?.(next);
39
62
  };
40
63
 
41
- const trackStyle = {
42
- width: 51,
43
- height: 31,
44
- borderRadius: 15.5,
64
+ const trackStyle: ViewStyle = {
65
+ width: TRACK_WIDTH,
66
+ height: TRACK_HEIGHT,
67
+ borderRadius: TRACK_HEIGHT / 2,
45
68
  backgroundColor: isChecked ? themeColors.fill : "rgba(120, 120, 128, 0.16)",
46
69
  padding: 2,
47
- justifyContent: "center" as const,
70
+ justifyContent: "center",
48
71
  opacity: disabled ? 0.38 : 1,
49
- transition: "background-color 200ms ease",
50
- ...style,
72
+ ...(style as ViewStyle),
51
73
  };
52
74
 
53
- const thumbStyle = {
54
- width: 27,
55
- height: 27,
56
- borderRadius: 13.5,
75
+ const thumbStyle: ViewStyle = {
76
+ width: THUMB,
77
+ height: THUMB,
78
+ borderRadius: THUMB / 2,
57
79
  backgroundColor: "#FFFFFF",
58
- boxShadow: "0 2px 4px rgba(0, 0, 0, 0.2)",
59
- transform: isChecked ? "translateX(20px)" : "translateX(0px)",
60
- transition: "transform 200ms cubic-bezier(0.25, 0.1, 0.25, 1)",
80
+ // A hairline edge instead of a drop shadow: RN shadows need per-platform
81
+ // props, and the edge reads correctly on a translucent track.
82
+ borderWidth: 0.5,
83
+ borderColor: "rgba(0, 0, 0, 0.12)",
84
+ transform: [{ translateX: animated ? offset : isChecked ? TRAVEL : 0 }],
61
85
  };
62
86
 
63
87
  return (
64
- <div
88
+ <Pressable
65
89
  ref={ref}
66
- role="switch"
67
- aria-checked={isChecked}
68
- aria-label={accessibilityLabel}
69
- aria-disabled={disabled}
70
- tabIndex={disabled ? -1 : 0}
71
- onClick={toggle}
72
- style={{ display: "inline-block", cursor: disabled ? "default" : "pointer" }}
90
+ testID={testID}
91
+ accessibilityRole="switch"
92
+ accessibilityLabel={accessibilityLabel}
93
+ accessibilityState={{ checked: isChecked, disabled }}
94
+ disabled={disabled}
95
+ onPress={toggle}
96
+ style={{ alignSelf: "flex-start" }}
73
97
  {...props}
74
98
  >
75
- <Glass effect="regular" style={trackStyle}>
76
- <div style={thumbStyle} />
99
+ <Glass effect="regular" isDark={isDark} style={trackStyle}>
100
+ {animated ? <Animated.View style={thumbStyle} /> : <View style={thumbStyle} />}
77
101
  </Glass>
78
- </div>
102
+ </Pressable>
79
103
  );
80
104
  });