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.
package/src/Forms2.tsx ADDED
@@ -0,0 +1,608 @@
1
+ import { forwardRef, useRef, useState, type ReactNode } from "react";
2
+ import type { StyleProp, ViewStyle } from "react-native";
3
+
4
+ import { Glass } from "./Glass";
5
+ import { Text } from "./Text";
6
+ import { colors, radii } from "./internal/tokens";
7
+ import { primitives } from "./internal/primitives";
8
+ import { useIsDark } from "./internal/motion";
9
+
10
+ // ---------------------------------------------------------------------------
11
+ // Stepper
12
+
13
+ export interface StepperProps {
14
+ /** Controlled value. Omit and the stepper keeps its own. */
15
+ value?: number;
16
+ defaultValue?: number;
17
+ onValueChange?: (value: number) => void;
18
+ min?: number;
19
+ max?: number;
20
+ step?: number;
21
+ disabled?: boolean;
22
+ /** Announced by screen readers, e.g. "Guests". */
23
+ accessibilityLabel: string;
24
+ /** Formats the middle readout; defaults to the plain number. */
25
+ formatValue?: (value: number) => string;
26
+ style?: StyleProp<ViewStyle>;
27
+ isDark?: boolean;
28
+ testID?: string;
29
+ }
30
+
31
+ /**
32
+ * Minus / value / plus.
33
+ *
34
+ * The same interaction contract Slider established: `adjustable` with
35
+ * increment and decrement accessibility actions, and clamping at min and max
36
+ * rather than wrapping or drifting past the range.
37
+ */
38
+ export const Stepper = forwardRef<any, StepperProps>(function Stepper(
39
+ {
40
+ value: valueProp,
41
+ defaultValue = 0,
42
+ onValueChange,
43
+ min = 0,
44
+ max = 100,
45
+ step = 1,
46
+ disabled = false,
47
+ accessibilityLabel,
48
+ formatValue,
49
+ style,
50
+ isDark: isDarkProp,
51
+ testID,
52
+ ...props
53
+ },
54
+ ref,
55
+ ) {
56
+ const isDark = useIsDark(isDarkProp);
57
+ const theme = isDark ? colors.dark : colors.light;
58
+ const { View, Text: RNText, Pressable } = primitives();
59
+
60
+ const [internal, setInternal] = useState(defaultValue);
61
+ const value = valueProp !== undefined ? valueProp : internal;
62
+
63
+ const apply = (next: number) => {
64
+ const clamped = Math.min(Math.max(next, min), max);
65
+ if (clamped === value) return;
66
+ if (valueProp === undefined) setInternal(clamped);
67
+ onValueChange?.(clamped);
68
+ };
69
+
70
+ const atMin = value <= min;
71
+ const atMax = value >= max;
72
+
73
+ const stepButton = (
74
+ label: string,
75
+ sign: "\u2212" | "+",
76
+ action: () => void,
77
+ blocked: boolean,
78
+ ) => (
79
+ <Pressable
80
+ accessibilityRole="button"
81
+ accessibilityLabel={label}
82
+ accessibilityState={{ disabled: disabled || blocked }}
83
+ disabled={disabled || blocked}
84
+ onPress={action}
85
+ style={{ width: 36, height: 36, alignItems: "center", justifyContent: "center", opacity: disabled || blocked ? 0.38 : 1 }}
86
+ >
87
+ <RNText style={{ fontSize: 20, fontWeight: "500", color: theme.foreground, lineHeight: 22 }}>
88
+ {sign}
89
+ </RNText>
90
+ </Pressable>
91
+ );
92
+
93
+ return (
94
+ <Glass
95
+ ref={ref}
96
+ effect="regular"
97
+ isDark={isDark}
98
+ testID={testID}
99
+ accessibilityRole="adjustable"
100
+ accessibilityLabel={accessibilityLabel}
101
+ accessibilityValue={{ min, max, now: value }}
102
+ accessibilityActions={[{ name: "increment" }, { name: "decrement" }]}
103
+ onAccessibilityAction={(event: any) => {
104
+ const delta = event?.nativeEvent?.actionName === "increment" ? step : -step;
105
+ apply(value + delta);
106
+ }}
107
+ style={{
108
+ flexDirection: "row",
109
+ alignItems: "center",
110
+ alignSelf: "flex-start",
111
+ borderRadius: radii.pill,
112
+ paddingHorizontal: 2,
113
+ opacity: disabled ? 0.38 : 1,
114
+ ...(style as ViewStyle),
115
+ }}
116
+ {...props}
117
+ >
118
+ {stepButton(`Decrease ${accessibilityLabel}`, "\u2212", () => apply(value - step), atMin)}
119
+ <View style={{ minWidth: 44, alignItems: "center", justifyContent: "center" }}>
120
+ <RNText style={{ fontSize: 16, fontWeight: "600", color: theme.foreground }} numberOfLines={1}>
121
+ {formatValue ? formatValue(value) : String(value)}
122
+ </RNText>
123
+ </View>
124
+ {stepButton(`Increase ${accessibilityLabel}`, "+", () => apply(value + step), atMax)}
125
+ </Glass>
126
+ );
127
+ });
128
+
129
+ // ---------------------------------------------------------------------------
130
+ // Toggle
131
+
132
+ export interface ToggleProps {
133
+ /** Controlled pressed state. Omit and the toggle keeps its own. */
134
+ pressed?: boolean;
135
+ defaultPressed?: boolean;
136
+ onPressedChange?: (pressed: boolean) => void;
137
+ /** Equivalent to `pressed`, matching web's Toggle naming. */
138
+ selected?: boolean;
139
+ icon?: ReactNode;
140
+ children?: ReactNode;
141
+ variant?: "glass" | "ghost";
142
+ size?: "sm" | "md" | "lg";
143
+ disabled?: boolean;
144
+ /** Required when the toggle has no visible text. */
145
+ accessibilityLabel?: string;
146
+ style?: StyleProp<ViewStyle>;
147
+ isDark?: boolean;
148
+ testID?: string;
149
+ }
150
+
151
+ const toggleSizes = { sm: 30, md: 36, lg: 44 };
152
+
153
+ /**
154
+ * A button that stays pressed.
155
+ *
156
+ * Distinct from Switch: this is a button with an on-state (bold, favourite,
157
+ * pinned), not a setting. It reports `selected`, which is what web's Toggle
158
+ * announces.
159
+ */
160
+ export const Toggle = forwardRef<any, ToggleProps>(function Toggle(
161
+ {
162
+ pressed: pressedProp,
163
+ defaultPressed = false,
164
+ onPressedChange,
165
+ selected,
166
+ icon,
167
+ children,
168
+ variant = "glass",
169
+ size = "md",
170
+ disabled = false,
171
+ accessibilityLabel,
172
+ style,
173
+ isDark: isDarkProp,
174
+ testID,
175
+ ...props
176
+ },
177
+ ref,
178
+ ) {
179
+ const isDark = useIsDark(isDarkProp);
180
+ const theme = isDark ? colors.dark : colors.light;
181
+ const { View, Text: RNText, Pressable } = primitives();
182
+
183
+ // `selected` is accepted as an alias so the controlled prop matches web.
184
+ const controlled = pressedProp !== undefined ? pressedProp : selected;
185
+ const [internal, setInternal] = useState(defaultPressed);
186
+ const isPressed = controlled !== undefined ? controlled : internal;
187
+
188
+ const dimension = toggleSizes[size];
189
+
190
+ const handlePress = () => {
191
+ const next = !isPressed;
192
+ if (controlled === undefined) setInternal(next);
193
+ onPressedChange?.(next);
194
+ };
195
+
196
+ return (
197
+ <Pressable
198
+ ref={ref}
199
+ testID={testID}
200
+ accessibilityRole="button"
201
+ accessibilityLabel={accessibilityLabel ?? (typeof children === "string" ? children : undefined)}
202
+ accessibilityState={{ selected: isPressed, disabled }}
203
+ disabled={disabled}
204
+ onPress={handlePress}
205
+ style={({ pressed }: { pressed: boolean }) => ({ alignSelf: "flex-start", opacity: disabled ? 0.38 : pressed ? 0.8 : 1 })}
206
+ {...props}
207
+ >
208
+ <Glass
209
+ effect={variant === "ghost" ? "none" : "regular"}
210
+ interactive={!disabled && variant !== "ghost"}
211
+ isDark={isDark}
212
+ tintColor={isPressed && variant !== "ghost" ? theme.glassActive : undefined}
213
+ style={{
214
+ height: dimension,
215
+ borderRadius: radii.pill,
216
+ paddingHorizontal: icon && !children ? 0 : 12,
217
+ width: icon && !children ? dimension : undefined,
218
+ flexDirection: "row",
219
+ alignItems: "center",
220
+ justifyContent: "center",
221
+ gap: 6,
222
+ backgroundColor: variant === "ghost" ? "transparent" : undefined,
223
+ ...(style as ViewStyle),
224
+ }}
225
+ >
226
+ {icon ? <View>{icon}</View> : null}
227
+ {typeof children === "string" ? (
228
+ <RNText
229
+ numberOfLines={1}
230
+ style={{
231
+ fontSize: size === "sm" ? 12 : size === "lg" ? 15 : 13,
232
+ fontWeight: isPressed ? "600" : "500",
233
+ color: isPressed ? theme.foreground : theme.muted,
234
+ }}
235
+ >
236
+ {children}
237
+ </RNText>
238
+ ) : (
239
+ children
240
+ )}
241
+ </Glass>
242
+ </Pressable>
243
+ );
244
+ });
245
+
246
+ // ---------------------------------------------------------------------------
247
+ // ToggleGroup
248
+
249
+ export interface ToggleGroupItem {
250
+ value: string;
251
+ label: string;
252
+ icon?: ReactNode;
253
+ }
254
+
255
+ export interface ToggleGroupProps {
256
+ items: ToggleGroupItem[];
257
+ /** Allow several items on at once. */
258
+ multiple?: boolean;
259
+ /** Controlled selection. A string for single mode, an array for multiple. */
260
+ value?: string | string[];
261
+ defaultValue?: string | string[];
262
+ onValueChange?: (value: string | string[]) => void;
263
+ size?: "sm" | "md" | "lg";
264
+ disabled?: boolean;
265
+ accessibilityLabel?: string;
266
+ style?: StyleProp<ViewStyle>;
267
+ isDark?: boolean;
268
+ testID?: string;
269
+ }
270
+
271
+ /**
272
+ * A row of toggles acting as one choice.
273
+ *
274
+ * Built from the same pieces as SegmentedControl — one glass container, toggles
275
+ * inside it, `glassActive` marking the active ones — but each item carries the
276
+ * button styling of Toggle.
277
+ */
278
+ export const ToggleGroup = forwardRef<any, ToggleGroupProps>(function ToggleGroup(
279
+ {
280
+ items,
281
+ multiple = false,
282
+ value: valueProp,
283
+ defaultValue,
284
+ onValueChange,
285
+ size = "md",
286
+ disabled = false,
287
+ accessibilityLabel = "Toggle group",
288
+ style,
289
+ isDark: isDarkProp,
290
+ testID,
291
+ ...props
292
+ },
293
+ ref,
294
+ ) {
295
+ const isDark = useIsDark(isDarkProp);
296
+ const theme = isDark ? colors.dark : colors.light;
297
+ const { View, Text: RNText, Pressable } = primitives();
298
+
299
+ const initial = defaultValue ?? (multiple ? [] : "");
300
+ const [internal, setInternal] = useState<string | string[]>(initial);
301
+ const selection = valueProp !== undefined ? valueProp : internal;
302
+
303
+ const isOn = (value: string) =>
304
+ Array.isArray(selection) ? selection.includes(value) : selection === value;
305
+
306
+ const toggle = (value: string) => {
307
+ let next: string | string[];
308
+ if (multiple) {
309
+ const current = Array.isArray(selection) ? selection : [];
310
+ next = current.includes(value) ? current.filter((v) => v !== value) : [...current, value];
311
+ } else {
312
+ // Single mode: the same item pressed again clears it, as Toggle does.
313
+ next = selection === value ? "" : value;
314
+ }
315
+ if (valueProp === undefined) setInternal(next);
316
+ onValueChange?.(next);
317
+ };
318
+
319
+ const height = size === "sm" ? 30 : size === "lg" ? 42 : 36;
320
+
321
+ return (
322
+ <Glass
323
+ ref={ref}
324
+ effect="regular"
325
+ isDark={isDark}
326
+ testID={testID}
327
+ accessibilityRole="radiogroup"
328
+ accessibilityLabel={accessibilityLabel}
329
+ style={{
330
+ flexDirection: "row",
331
+ alignItems: "center",
332
+ alignSelf: "flex-start",
333
+ padding: 3,
334
+ gap: 3,
335
+ borderRadius: radii.pill,
336
+ ...(style as ViewStyle),
337
+ }}
338
+ {...props}
339
+ >
340
+ {items.map((item) => {
341
+ const on = isOn(item.value);
342
+ return (
343
+ <Pressable
344
+ key={item.value}
345
+ accessibilityRole="button"
346
+ accessibilityLabel={item.label}
347
+ accessibilityState={{ selected: on, disabled }}
348
+ disabled={disabled}
349
+ onPress={() => toggle(item.value)}
350
+ style={{ opacity: disabled ? 0.38 : 1 }}
351
+ >
352
+ <View
353
+ style={{
354
+ height: height - 6,
355
+ paddingHorizontal: 14,
356
+ borderRadius: radii.pill,
357
+ flexDirection: "row",
358
+ alignItems: "center",
359
+ justifyContent: "center",
360
+ gap: 6,
361
+ backgroundColor: on ? theme.glassActive : "transparent",
362
+ borderWidth: on ? 0.5 : 0,
363
+ borderColor: theme.edge,
364
+ }}
365
+ >
366
+ {item.icon ? <View>{item.icon}</View> : null}
367
+ <RNText
368
+ numberOfLines={1}
369
+ style={{ fontSize: 13, fontWeight: on ? "600" : "500", color: on ? theme.foreground : theme.muted }}
370
+ >
371
+ {item.label}
372
+ </RNText>
373
+ </View>
374
+ </Pressable>
375
+ );
376
+ })}
377
+ </Glass>
378
+ );
379
+ });
380
+
381
+ // ---------------------------------------------------------------------------
382
+ // InputOTP
383
+
384
+ export interface InputOTPProps {
385
+ /** Controlled code. Omit and the field keeps its own. */
386
+ value?: string;
387
+ defaultValue?: string;
388
+ onChange?: (value: string) => void;
389
+ /** Fires once the code reaches `length` characters. */
390
+ onComplete?: (value: string) => void;
391
+ /** How many digits. */
392
+ length?: number;
393
+ disabled?: boolean;
394
+ accessibilityLabel?: string;
395
+ style?: StyleProp<ViewStyle>;
396
+ isDark?: boolean;
397
+ testID?: string;
398
+ }
399
+
400
+ /**
401
+ * A verification-code row.
402
+ *
403
+ * One real text field drives the boxes. Keeping a single field — rather than N
404
+ * inputs — is what makes paste and autofill fill every box in one go, and it is
405
+ * what keeps the caret and keyboard behaviour sane across platforms.
406
+ */
407
+ export const InputOTP = forwardRef<any, InputOTPProps>(function InputOTP(
408
+ {
409
+ value: valueProp,
410
+ defaultValue = "",
411
+ onChange,
412
+ onComplete,
413
+ length = 6,
414
+ disabled = false,
415
+ accessibilityLabel = "Verification code",
416
+ style,
417
+ isDark: isDarkProp,
418
+ testID,
419
+ ...props
420
+ },
421
+ ref,
422
+ ) {
423
+ const isDark = useIsDark(isDarkProp);
424
+ const theme = isDark ? colors.dark : colors.light;
425
+ const { View, Text: RNText, TextInput } = primitives();
426
+
427
+ const [internal, setInternal] = useState(defaultValue);
428
+ const value = valueProp !== undefined ? valueProp : internal;
429
+
430
+ // The code is digits only, and never longer than the boxes available.
431
+ const normalise = (raw: string) => raw.replace(/[^0-9]/g, "").slice(0, length);
432
+
433
+ const handleChange = (raw: string) => {
434
+ const next = normalise(raw);
435
+ if (valueProp === undefined) setInternal(next);
436
+ onChange?.(next);
437
+ if (next.length === length) onComplete?.(next);
438
+ };
439
+
440
+ const focusedIndex = Math.min(value.length, length - 1);
441
+
442
+ return (
443
+ <View
444
+ ref={ref}
445
+ testID={testID}
446
+ style={[{ flexDirection: "row", alignItems: "center", gap: 8, alignSelf: "flex-start" }, style as ViewStyle]}
447
+ {...props}
448
+ >
449
+ {Array.from({ length }, (_, index) => {
450
+ const char = value[index] ?? "";
451
+ const isActive = index === focusedIndex && !disabled;
452
+ return (
453
+ <Glass
454
+ key={index}
455
+ effect="regular"
456
+ isDark={isDark}
457
+ tintColor={isActive ? theme.glassActive : undefined}
458
+ style={{
459
+ width: 44,
460
+ height: 52,
461
+ borderRadius: radii.md,
462
+ alignItems: "center",
463
+ justifyContent: "center",
464
+ borderColor: isActive ? theme.fill : theme.edge,
465
+ opacity: disabled ? 0.38 : 1,
466
+ }}
467
+ >
468
+ <RNText style={{ fontSize: 22, fontWeight: "600", color: theme.foreground }}>{char}</RNText>
469
+ </Glass>
470
+ );
471
+ })}
472
+ {/* The single real field. It is transparent and spans the row so a tap
473
+ anywhere lands in the code, which is how the boxes stay in step. */}
474
+ <TextInput
475
+ accessibilityRole="text"
476
+ accessibilityLabel={accessibilityLabel}
477
+ accessibilityValue={{ text: value }}
478
+ value={value}
479
+ onChangeText={handleChange}
480
+ editable={!disabled}
481
+ keyboardType="number-pad"
482
+ maxLength={length}
483
+ textContentType="oneTimeCode"
484
+ autoComplete="one-time-code"
485
+ style={{
486
+ position: "absolute",
487
+ left: 0,
488
+ right: 0,
489
+ top: 0,
490
+ bottom: 0,
491
+ // Invisible but present: the boxes are the visible representation.
492
+ opacity: 0.02,
493
+ color: "transparent",
494
+ }}
495
+ />
496
+ </View>
497
+ );
498
+ });
499
+
500
+ // ---------------------------------------------------------------------------
501
+ // ButtonGroup
502
+
503
+ export interface ButtonGroupItem {
504
+ label: string;
505
+ onPress: () => void;
506
+ disabled?: boolean;
507
+ }
508
+
509
+ export interface ButtonGroupProps {
510
+ items: ButtonGroupItem[];
511
+ size?: "sm" | "md" | "lg";
512
+ /** `attached` shares edges; `segmented` leaves a gap between buttons. */
513
+ variant?: "attached" | "segmented";
514
+ /** A single handler instead of per-item `onPress`. */
515
+ onItemPress?: (item: ButtonGroupItem, index: number) => void;
516
+ accessibilityLabel?: string;
517
+ style?: StyleProp<ViewStyle>;
518
+ isDark?: boolean;
519
+ testID?: string;
520
+ }
521
+
522
+ /**
523
+ * A connected row of buttons.
524
+ *
525
+ * Stateless by design: it reports presses and holds no selection, which is what
526
+ * separates it from SegmentedControl (a stateful choice) and ToggleGroup.
527
+ */
528
+ export const ButtonGroup = forwardRef<any, ButtonGroupProps>(function ButtonGroup(
529
+ {
530
+ items,
531
+ size = "md",
532
+ variant = "attached",
533
+ onItemPress,
534
+ accessibilityLabel,
535
+ style,
536
+ isDark: isDarkProp,
537
+ testID,
538
+ ...props
539
+ },
540
+ ref,
541
+ ) {
542
+ const isDark = useIsDark(isDarkProp);
543
+ const theme = isDark ? colors.dark : colors.light;
544
+ const { View, Text: RNText, Pressable } = primitives();
545
+
546
+ const height = size === "sm" ? 32 : size === "lg" ? 46 : 40;
547
+
548
+ return (
549
+ <View
550
+ ref={ref}
551
+ testID={testID}
552
+ accessibilityRole="group"
553
+ accessibilityLabel={accessibilityLabel}
554
+ style={{
555
+ flexDirection: "row",
556
+ alignSelf: "flex-start",
557
+ gap: variant === "segmented" ? 6 : 0,
558
+ ...(style as ViewStyle),
559
+ }}
560
+ {...props}
561
+ >
562
+ {items.map((item, index) => {
563
+ const first = index === 0;
564
+ const last = index === items.length - 1;
565
+ // Attached buttons share edges: only the outer corners are rounded,
566
+ // and the inner edges stay square so the group reads as one control.
567
+ const pill = variant === "segmented" ? radii.pill : 0;
568
+ return (
569
+ <Pressable
570
+ key={`${item.label}-${index}`}
571
+ accessibilityRole="button"
572
+ accessibilityLabel={item.label}
573
+ accessibilityState={{ disabled: item.disabled }}
574
+ disabled={item.disabled}
575
+ onPress={() => {
576
+ item.onPress();
577
+ onItemPress?.(item, index);
578
+ }}
579
+ style={({ pressed }: { pressed: boolean }) => ({
580
+ opacity: item.disabled ? 0.38 : pressed ? 0.85 : 1,
581
+ })}
582
+ >
583
+ <Glass
584
+ effect="regular"
585
+ interactive={!item.disabled}
586
+ isDark={isDark}
587
+ style={{
588
+ height,
589
+ paddingHorizontal: 18,
590
+ justifyContent: "center",
591
+ alignItems: "center",
592
+ borderRadius: variant === "segmented" ? pill : undefined,
593
+ borderTopLeftRadius: variant === "segmented" ? pill : first ? radii.md : 0,
594
+ borderBottomLeftRadius: variant === "segmented" ? pill : first ? radii.md : 0,
595
+ borderTopRightRadius: variant === "segmented" ? pill : last ? radii.md : 0,
596
+ borderBottomRightRadius: variant === "segmented" ? pill : last ? radii.md : 0,
597
+ }}
598
+ >
599
+ <RNText numberOfLines={1} style={{ fontSize: size === "sm" ? 13 : 14, fontWeight: "500", color: theme.foreground }}>
600
+ {item.label}
601
+ </RNText>
602
+ </Glass>
603
+ </Pressable>
604
+ );
605
+ })}
606
+ </View>
607
+ );
608
+ });