glacia-native 0.4.0 → 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.
@@ -0,0 +1,233 @@
1
+ import { forwardRef, useRef, useState, type ReactNode } from "react";
2
+ import type { StyleProp, ViewStyle } from "react-native";
3
+
4
+ import { colors } from "./internal/tokens";
5
+ import { primitives } from "./internal/primitives";
6
+ import { useIsDark, useSpring } from "./internal/motion";
7
+
8
+ // ---------------------------------------------------------------------------
9
+ // Carousel
10
+
11
+ export interface CarouselProps {
12
+ /** One node per page. */
13
+ slides: ReactNode[];
14
+ /** Controlled page. Omit and the carousel tracks itself. */
15
+ index?: number;
16
+ defaultIndex?: number;
17
+ /** Fires when a swipe settles on a new page. */
18
+ onIndexChange?: (index: number) => void;
19
+ /** Gap between pages, so neighbours peek at the edges. */
20
+ gap?: number;
21
+ style?: StyleProp<ViewStyle>;
22
+ isDark?: boolean;
23
+ testID?: string;
24
+ }
25
+
26
+ /**
27
+ * A horizontally paging swiper.
28
+ *
29
+ * It rides the resolver's ScrollView with `pagingEnabled`, so the paging feel is
30
+ * the platform's own and the pages are plain content the caller supplies. The
31
+ * active page is derived from the scroll offset, which is also what reports
32
+ * `onIndexChange`.
33
+ */
34
+ export const Carousel = forwardRef<any, CarouselProps>(function Carousel(
35
+ {
36
+ slides,
37
+ index: indexProp,
38
+ defaultIndex = 0,
39
+ onIndexChange,
40
+ gap = 0,
41
+ style,
42
+ isDark: isDarkProp,
43
+ testID,
44
+ ...props
45
+ },
46
+ ref,
47
+ ) {
48
+ const isDark = useIsDark(isDarkProp);
49
+ const { getScrollView } = primitives();
50
+ const ScrollView = getScrollView();
51
+ const { View } = primitives();
52
+
53
+ const [internalIndex, setInternalIndex] = useState(defaultIndex);
54
+ const index = indexProp !== undefined ? indexProp : internalIndex;
55
+
56
+ const viewportWidth = useRef(0);
57
+ const scrollRef = useRef<any>(null);
58
+
59
+ // Derives the page from the offset rather than trusting a counter, so a
60
+ // fast swipe that skips pages still reports the page it landed on.
61
+ const handleScroll = (event: any) => {
62
+ const { contentOffset, layoutMeasurement } = event?.nativeEvent ?? {};
63
+ const width = layoutMeasurement?.width || viewportWidth.current;
64
+ if (!width || !contentOffset) return;
65
+ const next = Math.round(contentOffset.x / width);
66
+ const clamped = Math.min(Math.max(next, 0), Math.max(slides.length - 1, 0));
67
+ if (clamped !== index) {
68
+ if (indexProp === undefined) setInternalIndex(clamped);
69
+ onIndexChange?.(clamped);
70
+ }
71
+ };
72
+
73
+ const pages = slides.map((slide, i) => (
74
+ <View key={i} style={{ width: viewportWidth.current || "100%", paddingRight: gap }}>
75
+ {slide}
76
+ </View>
77
+ ));
78
+
79
+ // Off-device there is no ScrollView; the pages still render in a row so the
80
+ // component mounts, and the tests drive `handleScroll` through onScroll.
81
+ if (!ScrollView) {
82
+ return (
83
+ <View ref={ref} testID={testID} style={[{ flexDirection: "row", overflow: "hidden" }, style as ViewStyle]} {...props}>
84
+ {pages}
85
+ </View>
86
+ );
87
+ }
88
+
89
+ return (
90
+ <View
91
+ ref={ref}
92
+ testID={testID}
93
+ onLayout={(event: any) => {
94
+ viewportWidth.current = event?.nativeEvent?.layout?.width ?? 0;
95
+ }}
96
+ style={style as ViewStyle}
97
+ {...props}
98
+ >
99
+ <ScrollView
100
+ ref={scrollRef}
101
+ horizontal
102
+ pagingEnabled={gap === 0}
103
+ showsHorizontalScrollIndicator={false}
104
+ onScroll={handleScroll}
105
+ scrollEventThrottle={16}
106
+ accessibilityRole="adjustable"
107
+ accessibilityLabel="Carousel"
108
+ accessibilityValue={{ min: 0, max: Math.max(slides.length - 1, 0), now: index }}
109
+ accessibilityActions={[{ name: "increment" }, { name: "decrement" }]}
110
+ onAccessibilityAction={(event: any) => {
111
+ const step = event?.nativeEvent?.actionName === "increment" ? 1 : -1;
112
+ const next = Math.min(Math.max(index + step, 0), Math.max(slides.length - 1, 0));
113
+ if (next === index) return;
114
+ if (indexProp === undefined) setInternalIndex(next);
115
+ onIndexChange?.(next);
116
+ }}
117
+ >
118
+ {pages}
119
+ </ScrollView>
120
+ </View>
121
+ );
122
+ });
123
+
124
+ // ---------------------------------------------------------------------------
125
+ // PageControl
126
+
127
+ export interface PageControlProps {
128
+ count: number;
129
+ /** Controlled active page. Omit and the control keeps its own. */
130
+ activeIndex?: number;
131
+ defaultActiveIndex?: number;
132
+ /** Called when a dot is tapped. */
133
+ onChange?: (index: number) => void;
134
+ /** Tint the active dot; defaults to the theme accent. */
135
+ tint?: string;
136
+ /** Hide the dots when there is only one page. */
137
+ hidesForSinglePage?: boolean;
138
+ style?: StyleProp<ViewStyle>;
139
+ isDark?: boolean;
140
+ testID?: string;
141
+ }
142
+
143
+ const DOT = 7;
144
+ const ACTIVE_DOT = 18;
145
+
146
+ /**
147
+ * The page dots.
148
+ *
149
+ * The active dot elongates and the others stay narrow, which is how iOS reads
150
+ * "you are here". The width change runs on the springs token through Animated.
151
+ */
152
+ export const PageControl = forwardRef<any, PageControlProps>(function PageControl(
153
+ {
154
+ count,
155
+ activeIndex: activeProp,
156
+ defaultActiveIndex = 0,
157
+ onChange,
158
+ tint,
159
+ hidesForSinglePage = false,
160
+ style,
161
+ isDark: isDarkProp,
162
+ testID,
163
+ ...props
164
+ },
165
+ ref,
166
+ ) {
167
+ const isDark = useIsDark(isDarkProp);
168
+ const theme = isDark ? colors.dark : colors.light;
169
+ const { View, Pressable } = primitives();
170
+
171
+ const [internalIndex, setInternalIndex] = useState(defaultActiveIndex);
172
+ const active = activeProp !== undefined ? activeProp : internalIndex;
173
+
174
+ if (hidesForSinglePage && count <= 1) return null;
175
+
176
+ // The accent tints the active dot; the resting dots sit on `glassActive` so
177
+ // selection reads the same way it does in SegmentedControl and TabBar.
178
+ const accent = tint ?? theme.fill;
179
+ const resting = theme.glassActive;
180
+
181
+ return (
182
+ <View
183
+ ref={ref}
184
+ testID={testID}
185
+ accessibilityRole="tablist"
186
+ style={[{ flexDirection: "row", alignItems: "center", gap: 6, alignSelf: "center" }, style as ViewStyle]}
187
+ {...props}
188
+ >
189
+ {Array.from({ length: Math.max(count, 0) }, (_, index) => {
190
+ const isActive = index === active;
191
+ return (
192
+ <Pressable
193
+ key={index}
194
+ accessibilityRole="tab"
195
+ accessibilityLabel={`Page ${index + 1} of ${count}`}
196
+ accessibilityState={{ selected: isActive }}
197
+ onPress={() => {
198
+ if (activeProp === undefined) setInternalIndex(index);
199
+ onChange?.(index);
200
+ }}
201
+ hitSlop={8}
202
+ >
203
+ <Dot active={isActive} accent={accent} muted={resting} />
204
+ </Pressable>
205
+ );
206
+ })}
207
+ </View>
208
+ );
209
+ });
210
+
211
+ /** One dot, whose width springs between the resting and active lengths. */
212
+ function Dot({ active, accent, muted }: { active: boolean; accent: string; muted: string }) {
213
+ const { Animated, View } = primitives();
214
+ const { value: progress, animated } = useSpring(active ? 1 : 0, "snappy");
215
+
216
+ const width = animated
217
+ ? progress.interpolate({ inputRange: [0, 1], outputRange: [DOT, ACTIVE_DOT] })
218
+ : active
219
+ ? ACTIVE_DOT
220
+ : DOT;
221
+
222
+ const style: ViewStyle = {
223
+ height: DOT,
224
+ borderRadius: DOT / 2,
225
+ backgroundColor: active ? accent : muted,
226
+ };
227
+
228
+ return animated ? (
229
+ <Animated.View style={{ ...style, width }} />
230
+ ) : (
231
+ <View style={{ ...style, width: width as number }} />
232
+ );
233
+ }