glacia-native 0.4.0 → 0.5.1

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 (73) hide show
  1. package/lib/module/Bars.js +269 -0
  2. package/lib/module/Carousel.js +103 -0
  3. package/lib/module/Display.js +317 -0
  4. package/lib/module/Feedback.js +386 -0
  5. package/lib/module/Forms2.js +454 -0
  6. package/lib/module/Message.js +88 -0
  7. package/lib/module/Overlays.js +328 -0
  8. package/lib/module/Pickers.js +243 -0
  9. package/lib/module/SegmentedControl.js +5 -1
  10. package/lib/module/Slider.js +6 -1
  11. package/lib/module/Switch.js +4 -1
  12. package/lib/module/TabBar.js +5 -1
  13. package/lib/module/Text.js +37 -0
  14. package/lib/module/ai.js +8 -3
  15. package/lib/module/index.js +79 -1
  16. package/lib/module/internal/GlassContainer.js +54 -0
  17. package/lib/module/internal/GlassView.js +31 -7
  18. package/lib/module/internal/a11y.js +94 -0
  19. package/lib/module/internal/fallback.js +19 -8
  20. package/lib/module/internal/glass-routing.js +7 -0
  21. package/lib/module/internal/haptics.js +54 -0
  22. package/lib/module/internal/motion.js +10 -1
  23. package/lib/module/internal/primitives.js +73 -3
  24. package/lib/module/internal/tokens.js +101 -1
  25. package/lib/typescript/Bars.d.ts +188 -0
  26. package/lib/typescript/Carousel.d.ts +47 -0
  27. package/lib/typescript/Display.d.ts +178 -0
  28. package/lib/typescript/Feedback.d.ts +196 -0
  29. package/lib/typescript/Forms2.d.ts +209 -0
  30. package/lib/typescript/Glass.d.ts +1 -1
  31. package/lib/typescript/Message.d.ts +71 -0
  32. package/lib/typescript/Overlays.d.ts +146 -0
  33. package/lib/typescript/Pickers.d.ts +105 -0
  34. package/lib/typescript/SegmentedControl.d.ts +2 -0
  35. package/lib/typescript/Slider.d.ts +2 -0
  36. package/lib/typescript/Switch.d.ts +2 -0
  37. package/lib/typescript/TabBar.d.ts +2 -0
  38. package/lib/typescript/Text.d.ts +28 -0
  39. package/lib/typescript/index.d.ts +11 -1
  40. package/lib/typescript/internal/GlassContainer.d.ts +37 -0
  41. package/lib/typescript/internal/GlassView.d.ts +18 -1
  42. package/lib/typescript/internal/a11y.d.ts +19 -0
  43. package/lib/typescript/internal/fallback.d.ts +5 -0
  44. package/lib/typescript/internal/glass-routing.d.ts +10 -0
  45. package/lib/typescript/internal/haptics.d.ts +7 -0
  46. package/lib/typescript/internal/motion.d.ts +4 -0
  47. package/lib/typescript/internal/primitives.d.ts +21 -0
  48. package/lib/typescript/internal/tokens.d.ts +114 -0
  49. package/package.json +2 -2
  50. package/src/Bars.tsx +797 -0
  51. package/src/Carousel.tsx +233 -0
  52. package/src/Display.tsx +778 -0
  53. package/src/Feedback.tsx +969 -0
  54. package/src/Forms2.tsx +1014 -0
  55. package/src/Message.tsx +219 -0
  56. package/src/Overlays.tsx +685 -0
  57. package/src/Pickers.tsx +590 -0
  58. package/src/SegmentedControl.tsx +6 -0
  59. package/src/Slider.tsx +7 -0
  60. package/src/Switch.tsx +5 -0
  61. package/src/TabBar.tsx +6 -0
  62. package/src/Text.tsx +83 -0
  63. package/src/ai.tsx +9 -3
  64. package/src/index.ts +142 -1
  65. package/src/internal/GlassContainer.tsx +82 -0
  66. package/src/internal/GlassView.tsx +37 -7
  67. package/src/internal/a11y.ts +105 -0
  68. package/src/internal/fallback.tsx +19 -2
  69. package/src/internal/glass-routing.ts +17 -0
  70. package/src/internal/haptics.ts +59 -0
  71. package/src/internal/motion.ts +10 -1
  72. package/src/internal/primitives.ts +84 -3
  73. package/src/internal/tokens.ts +110 -0
@@ -0,0 +1,59 @@
1
+ // Haptic feedback for controls.
2
+ //
3
+ // Native iOS controls tick as you move them — a switch toggling, a segment
4
+ // being chosen. Glacia's controls are drawn by the library, so they have to
5
+ // ask for that tick themselves.
6
+ //
7
+ // There is no single React Native API for it, and Glacia does not take a
8
+ // dependency for one. Instead this tries, in order, the APIs that ship with
9
+ // common Expo and React Native projects, and does nothing where none is
10
+ // present. A missing tick must never break a control, so every call is
11
+ // wrapped: these helpers cannot throw.
12
+
13
+ import { primitives } from "./primitives";
14
+
15
+ declare const require: any;
16
+
17
+ /** The selection-tick implementation, resolved once. */
18
+ type Tick = () => void;
19
+
20
+ function resolveTick(): Tick | null {
21
+ // Expo's haptics module, when the app has it installed.
22
+ try {
23
+ const expo = require("expo-haptics");
24
+ if (expo?.selectionAsync) {
25
+ return () => {
26
+ expo.selectionAsync().catch?.(() => {});
27
+ };
28
+ }
29
+ } catch {
30
+ // Not an Expo project, or expo-haptics is not installed: try the next one.
31
+ }
32
+
33
+ // React Native's own Vibration API: a 10 ms buzz is the closest thing to a
34
+ // selection tick that ships with core. It is a coarser feel, but it is real
35
+ // feedback on a device that has no better module.
36
+ const { Vibration } = primitives();
37
+ if (Vibration?.vibrate) {
38
+ return () => {
39
+ Vibration.vibrate(10);
40
+ };
41
+ }
42
+
43
+ return null;
44
+ }
45
+
46
+ /**
47
+ * One selection tick, for a control whose value just changed by one step.
48
+ *
49
+ * Silent where no haptics API is available (the DOM preview, a test run, a
50
+ * device without a vibration motor). Never throws.
51
+ */
52
+ export function hapticSelection(): void {
53
+ try {
54
+ const tick = resolveTick();
55
+ tick?.();
56
+ } catch {
57
+ // Haptics are a nicety; a failure here must not surface to the user.
58
+ }
59
+ }
@@ -6,9 +6,13 @@
6
6
  // does not pass `isDark`, so a theme-aware app needs no extra prop threading.
7
7
  // - `useSpring` animates a number towards its target with the package's spring
8
8
  // tokens through React Native's Animated API. No CSS, no timers of our own.
9
+ // - Motion respects the system's Reduce Motion preference: when it is on, the
10
+ // value settles at its target instead of animating. Doing it here means every
11
+ // animation built on `useSpring` complies without repeating the check.
9
12
 
10
13
  import { useEffect, useRef } from "react";
11
14
 
15
+ import { useReducedMotion } from "./a11y";
12
16
  import { primitives } from "./primitives";
13
17
  import { springs } from "./tokens";
14
18
 
@@ -31,10 +35,15 @@ export type SpringName = keyof typeof springs;
31
35
  * on this platform. A caller that gets `animated === false` renders the target
32
36
  * directly, so the DOM preview and tests show the settled state rather than an
33
37
  * animation the environment cannot advance.
38
+ *
39
+ * Reduce Motion turns this into the same case: `animated` is false and the
40
+ * value is already at its target, so the component jumps to its end state
41
+ * rather than playing a transition the user asked not to see.
34
42
  */
35
43
  export function useSpring(target: number, spring: SpringName = "snappy") {
36
44
  const { Animated, isNative } = primitives();
37
- const animated = Boolean(isNative && Animated?.Value && Animated?.spring);
45
+ const reducedMotion = useReducedMotion();
46
+ const animated = Boolean(isNative && Animated?.Value && Animated?.spring) && !reducedMotion;
38
47
 
39
48
  // Off-device there is no Animated; a plain ref keeps the call sites identical.
40
49
  const value = useRef<any>(null);
@@ -28,6 +28,11 @@ export interface RNPrimitives {
28
28
  Circle: any;
29
29
  Rect: any;
30
30
  Polygon: any;
31
+ /** Gradient and clipping nodes, for the colour picker's hue track. */
32
+ Defs: any;
33
+ LinearGradient: any;
34
+ Stop: any;
35
+ ClipPath: any;
31
36
  SvgText: any;
32
37
  TextInput: any;
33
38
  Modal: any;
@@ -46,6 +51,19 @@ export interface RNPrimitives {
46
51
  PanResponder: any;
47
52
  /** React Native's useColorScheme, or null outside a native runtime. */
48
53
  useColorScheme: (() => "light" | "dark" | null | undefined) | null;
54
+ /**
55
+ * React Native's AccessibilityInfo, or null outside a native runtime. Read
56
+ * for the Reduce Transparency and Reduce Motion preferences.
57
+ */
58
+ AccessibilityInfo: any;
59
+ /** React Native's Vibration, or null outside a native runtime. */
60
+ Vibration: any;
61
+ /**
62
+ * React Native's useWindowDimensions, or a fixed reader off-device. Snap
63
+ * points are fractions of the window, so the sheet needs the height before
64
+ * it can resolve them.
65
+ */
66
+ useWindowDimensions: () => { width: number; height: number };
49
67
  }
50
68
 
51
69
  /**
@@ -202,8 +220,10 @@ function resolve(): RNPrimitives {
202
220
  if (onMouseUp) domProps.onMouseUp = onMouseUp;
203
221
  if (onMouseLeave) domProps.onMouseLeave = onMouseLeave;
204
222
 
205
- if (onPress) {
223
+ if (onPress && !disabled) {
206
224
  // Any element hosting a press handler needs a DOM click to fire it.
225
+ // A disabled control gets no handler at all, mirroring React Native's
226
+ // Pressable, which swallows the press rather than calling it through.
207
227
  domProps.onClick = onPress;
208
228
  if (
209
229
  domProps.role === "button" ||
@@ -240,12 +260,23 @@ function resolve(): RNPrimitives {
240
260
 
241
261
  /** An image that reads its remote source in the DOM preview. */
242
262
  function imageFallback(props: any) {
243
- const { source, accessibilityLabel, style } = props ?? {};
263
+ const { source, accessibilityLabel, accessibilityRole, style, onError, onLoad, testID, ...rest } = props ?? {};
244
264
  const { createElement } = require("react");
265
+ const source_ = typeof source === "number" ? undefined : source?.uri;
245
266
  return createElement("img", {
246
- src: source?.uri,
267
+ src: source_,
268
+ // `alt` covers the visible-description case; `aria-label` is what queries
269
+ // and screen readers in the preview read, so both are set.
247
270
  alt: accessibilityLabel ?? "",
271
+ "aria-label": accessibilityLabel,
272
+ "aria-role": accessibilityRole,
273
+ "data-testid": testID,
248
274
  style: toDomStyle(style),
275
+ // Load and error must reach the component the way they do on a device,
276
+ // or the placeholder and the fallback can never render in the preview.
277
+ onError,
278
+ onLoad,
279
+ ...rest,
249
280
  });
250
281
  }
251
282
 
@@ -263,6 +294,10 @@ function resolve(): RNPrimitives {
263
294
  Circle: svg?.Circle ?? fallback("circle"),
264
295
  Rect: svg?.Rect ?? fallback("rect"),
265
296
  Polygon: svg?.Polygon ?? fallback("polygon"),
297
+ Defs: svg?.Defs ?? fallback("defs"),
298
+ LinearGradient: svg?.LinearGradient ?? fallback("linearGradient"),
299
+ Stop: svg?.Stop ?? fallback("stop"),
300
+ ClipPath: svg?.ClipPath ?? fallback("clipPath"),
266
301
  // TextInput is not an SVG primitive, but resolving it here keeps every
267
302
  // component on one runtime-resolution path instead of two.
268
303
  TextInput: rn?.TextInput ?? fallback("input"),
@@ -280,6 +315,19 @@ function resolve(): RNPrimitives {
280
315
  isNative: Boolean(rn?.View),
281
316
  PanResponder: rn?.PanResponder ?? panResponderShim(),
282
317
  useColorScheme: rn?.useColorScheme ?? null,
318
+ // AccessibilityInfo and Vibration are only meaningful on a device; both
319
+ // are null off it, and their callers already treat absence as "off".
320
+ AccessibilityInfo: rn?.AccessibilityInfo ?? null,
321
+ Vibration: rn?.Vibration ?? null,
322
+ // Off-device there is no window; the DOM preview reports the viewport when
323
+ // it can and a conventional phone size otherwise. Snap points are
324
+ // fractions, so any consistent height yields the same proportions.
325
+ useWindowDimensions:
326
+ rn?.useWindowDimensions ??
327
+ (() => {
328
+ const view: any = typeof globalThis !== "undefined" ? (globalThis as any).window : undefined;
329
+ return { width: view?.innerWidth ?? 390, height: view?.innerHeight ?? 800 };
330
+ }),
283
331
  };
284
332
  }
285
333
 
@@ -438,6 +486,32 @@ const LENGTH_PROPS = new Set([
438
486
  ]);
439
487
 
440
488
  /** Converts an RN style object to DOM-compatible CSS. */
489
+ /**
490
+ * Rewrites React Native's `transform` array into a CSS transform string.
491
+ *
492
+ * The preview is a browser, and a browser does not understand
493
+ * `[{ translateX: 12 }, { scale: 2 }]`; React Native's Shape controls and
494
+ * glass offsets would silently not move. Translating the entries here keeps the
495
+ * preview faithful to what a device shows.
496
+ */
497
+ function transformToCss(entries: any): string | undefined {
498
+ if (!Array.isArray(entries)) return typeof entries === "string" ? entries : undefined;
499
+ const parts: string[] = [];
500
+ for (const entry of entries) {
501
+ if (!entry || typeof entry !== "object") continue;
502
+ for (const [key, raw] of Object.entries(entry)) {
503
+ const value = typeof (raw as any)?.__getValue === "function" ? (raw as any).__getValue() : raw;
504
+ if (typeof value !== "number") continue;
505
+ if (key === "translateX") parts.push(`translateX(${value}px)`);
506
+ else if (key === "translateY") parts.push(`translateY(${value}px)`);
507
+ else if (key === "scale") parts.push(`scale(${value})`);
508
+ else if (key === "rotate") parts.push(`rotate(${value}deg)`);
509
+ else if (key === "rotateZ") parts.push(`rotateZ(${value}deg)`);
510
+ }
511
+ }
512
+ return parts.length > 0 ? parts.join(" ") : undefined;
513
+ }
514
+
441
515
  function toDomStyle(style: any): any {
442
516
  if (!style) return style;
443
517
  const list = Array.isArray(style) ? style : [style];
@@ -450,6 +524,13 @@ function toDomStyle(style: any): any {
450
524
  // Animated values resolve to plain numbers off-device.
451
525
  const resolved =
452
526
  typeof (value as any)?.interpolate === "function" ? (value as any).__getValue?.() : value;
527
+ if (key === "transform") {
528
+ const css = transformToCss(resolved);
529
+ // Only set it when there is something to say; an empty transform would
530
+ // override an earlier entry in a style array.
531
+ if (css) merged[key] = css;
532
+ continue;
533
+ }
453
534
  merged[key] =
454
535
  typeof resolved === "number" && LENGTH_PROPS.has(key) ? `${resolved}px` : resolved;
455
536
  }
@@ -14,6 +14,57 @@ export const springs = {
14
14
  bouncy: tokenSprings.bouncy,
15
15
  };
16
16
 
17
+ /**
18
+ * The package's type scale, in React Native points.
19
+ *
20
+ * Text maps its variants onto these names so every component in the library —
21
+ * and every app built on it — sizes type the same way.
22
+ */
23
+ export const typeScale = {
24
+ title: { fontSize: 28, lineHeight: 34, fontWeight: "700" as const },
25
+ heading: { fontSize: 20, lineHeight: 26, fontWeight: "600" as const },
26
+ body: { fontSize: 16, lineHeight: 22, fontWeight: "400" as const },
27
+ label: { fontSize: 13, lineHeight: 18, fontWeight: "600" as const },
28
+ caption: { fontSize: 12, lineHeight: 16, fontWeight: "400" as const },
29
+ };
30
+
31
+ /**
32
+ * The default colour palette for ColorPicker.
33
+ *
34
+ * A palette is data, not a theme colour — it is the same twelve hues in light
35
+ * and dark — so it lives here rather than in either theme. Keeping it in the
36
+ * token module is also what lets the conformance guard require that component
37
+ * code contains no colour literal at all.
38
+ */
39
+ export const palette = {
40
+ default: [
41
+ "#FF3B30", "#FF9500", "#FFCC00", "#34C759", "#32ADE6", "#007AFF",
42
+ "#5856D6", "#AF52DE", "#FF2D55", "#A2845E", "#8E8E93", "#1C1C1E",
43
+ ],
44
+ /** The colour a picker starts on when the caller gives none. */
45
+ fallback: "#007AFF",
46
+ };
47
+
48
+ /**
49
+ * Control sizes in points, for components whose dimension is not a radius.
50
+ *
51
+ * Rating's stars are the reason this exists: a star has a size and the size
52
+ * must come from the scale rather than from a literal in the component.
53
+ */
54
+ export const controlSizes = {
55
+ star: { sm: 14, md: 20, lg: 28 },
56
+ swatch: { sm: 28, md: 36, lg: 44 },
57
+ picker: { track: 28 },
58
+ };
59
+
60
+ /**
61
+ * Theme colours in the form React Native accepts.
62
+ *
63
+ * The shared tokens express colours as CSS colour strings (`rgb(r g b / a)`),
64
+ * which is right for the web package and rejected by React Native. The values
65
+ * below are the same colours written as `rgba(...)`/hex, sourced from the same
66
+ * token file, so the two platforms stay in step.
67
+ */
17
68
  export const colors = {
18
69
  light: {
19
70
  foreground: "#000000E0",
@@ -21,8 +72,31 @@ export const colors = {
21
72
  glass: "rgba(255, 255, 255, 0.6)",
22
73
  glassActive: "rgba(236, 236, 240, 0.85)",
23
74
  edge: "rgba(0, 0, 0, 0.08)",
75
+ separator: "rgba(60, 60, 67, 0.18)",
24
76
  fill: "#0088FF",
25
77
  destructive: "#FF3B30",
78
+ on: "#34C759",
79
+ warning: "#FF9500",
80
+ info: "#007AFF",
81
+ // The dimmed layer behind a sheet, drawer or dialog.
82
+ scrim: "rgba(0, 0, 0, 0.45)",
83
+ // Text and icons drawn on top of a filled (tinted) surface.
84
+ onTint: "#FFFFFF",
85
+ skeleton: "rgba(60, 60, 67, 0.12)",
86
+ /**
87
+ * The solid colour glass becomes when the user has asked the system to
88
+ * reduce transparency. Apple's rule is that a translucent surface is
89
+ * replaced by an opaque one, not merely tinted differently.
90
+ */
91
+ glassSolid: "#F2F2F7",
92
+ /**
93
+ * Neutral colours used by pickers and fields: the field's own wash, and
94
+ * the far end of a saturation gradient (the point of no saturation).
95
+ */
96
+ fieldWash: "rgba(120, 120, 128, 0.12)",
97
+ /** A flat neutral surface: incoming message bubbles, file chips. */
98
+ wash: "rgba(120, 120, 128, 0.12)",
99
+ neutralTop: "#FFFFFF",
26
100
  // Chart axis and grid lines: low-contrast, so they never compete with data.
27
101
  axis: "#3C3C43",
28
102
  },
@@ -32,8 +106,44 @@ export const colors = {
32
106
  glass: "rgba(40, 40, 40, 0.62)",
33
107
  glassActive: "rgba(70, 70, 70, 0.62)",
34
108
  edge: "rgba(255, 255, 255, 0.08)",
109
+ separator: "rgba(235, 235, 245, 0.16)",
35
110
  fill: "#0091FF",
36
111
  destructive: "#FF4245",
112
+ on: "#30D158",
113
+ warning: "#FF9F0A",
114
+ info: "#0A84FF",
115
+ scrim: "rgba(0, 0, 0, 0.6)",
116
+ onTint: "#FFFFFF",
117
+ skeleton: "rgba(235, 235, 245, 0.14)",
118
+ glassSolid: "#1C1C1E",
119
+ fieldWash: "rgba(120, 120, 128, 0.12)",
120
+ /** A flat neutral surface: incoming message bubbles, file chips. */
121
+ wash: "rgba(120, 120, 128, 0.12)",
122
+ neutralTop: "#FFFFFF",
37
123
  axis: "#EBEBF5",
38
124
  },
39
125
  };
126
+
127
+ /** Keeps the shared token file honest if a colour is added there first. */
128
+ export type ThemeColors = typeof colors.light;
129
+
130
+ /** The semantic tone of a feedback component (Banner, Toast). */
131
+ export type Tone = "info" | "success" | "warning" | "error";
132
+
133
+ /** Resolves a tone to its theme colour. */
134
+ export function toneColor(theme: ThemeColors, tone: Tone): string {
135
+ switch (tone) {
136
+ case "success":
137
+ return theme.on;
138
+ case "warning":
139
+ return theme.warning;
140
+ case "error":
141
+ return theme.destructive;
142
+ default:
143
+ return theme.info;
144
+ }
145
+ }
146
+
147
+ // Referenced so the shared tokens stay a live dependency: a colour removed
148
+ // from glacia-tokens should not silently linger here forever.
149
+ void tokenColors;