glacia-native 0.5.1 → 0.5.2

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 (75) hide show
  1. package/LICENSE +29 -4
  2. package/README.md +27 -0
  3. package/lib/module/Bars.js +10 -9
  4. package/lib/module/Button.js +11 -8
  5. package/lib/module/Card.js +2 -2
  6. package/lib/module/Dialog.js +16 -17
  7. package/lib/module/Display.js +8 -7
  8. package/lib/module/Feedback.js +24 -15
  9. package/lib/module/Form.js +29 -33
  10. package/lib/module/Forms2.js +38 -21
  11. package/lib/module/Glass.js +2 -2
  12. package/lib/module/Input.js +12 -10
  13. package/lib/module/Message.js +11 -15
  14. package/lib/module/Overlays.js +22 -6
  15. package/lib/module/Pickers.js +16 -16
  16. package/lib/module/SegmentedControl.js +19 -10
  17. package/lib/module/Slider.js +18 -9
  18. package/lib/module/Switch.js +21 -9
  19. package/lib/module/TabBar.js +18 -12
  20. package/lib/module/Table.js +4 -3
  21. package/lib/module/Tabs.js +3 -2
  22. package/lib/module/ai.js +18 -15
  23. package/lib/module/charts/BarChart.js +11 -3
  24. package/lib/module/internal/GlassView.js +2 -2
  25. package/lib/module/internal/blur.js +78 -0
  26. package/lib/module/internal/fallback.js +110 -20
  27. package/lib/module/internal/glow.js +36 -0
  28. package/lib/module/internal/lens.js +70 -0
  29. package/lib/module/internal/material.js +64 -0
  30. package/lib/module/internal/primitives.js +1 -0
  31. package/lib/module/internal/rim.js +35 -0
  32. package/lib/module/internal/tokens.js +34 -2
  33. package/lib/typescript/Message.d.ts +5 -9
  34. package/lib/typescript/index.d.ts +1 -0
  35. package/lib/typescript/internal/GlassView.d.ts +13 -0
  36. package/lib/typescript/internal/blur.d.ts +24 -0
  37. package/lib/typescript/internal/fallback.d.ts +27 -2
  38. package/lib/typescript/internal/glow.d.ts +11 -0
  39. package/lib/typescript/internal/lens.d.ts +47 -0
  40. package/lib/typescript/internal/material.d.ts +27 -0
  41. package/lib/typescript/internal/primitives.d.ts +2 -0
  42. package/lib/typescript/internal/rim.d.ts +16 -0
  43. package/lib/typescript/internal/tokens.d.ts +36 -0
  44. package/package.json +12 -3
  45. package/src/Bars.tsx +12 -2
  46. package/src/Button.tsx +16 -9
  47. package/src/Card.tsx +2 -2
  48. package/src/Dialog.tsx +37 -26
  49. package/src/Display.tsx +8 -5
  50. package/src/Feedback.tsx +36 -13
  51. package/src/Form.tsx +60 -40
  52. package/src/Forms2.tsx +65 -24
  53. package/src/Glass.tsx +4 -0
  54. package/src/Input.tsx +15 -11
  55. package/src/Message.tsx +15 -16
  56. package/src/Overlays.tsx +25 -6
  57. package/src/Pickers.tsx +11 -6
  58. package/src/SegmentedControl.tsx +33 -9
  59. package/src/Slider.tsx +19 -10
  60. package/src/Switch.tsx +24 -10
  61. package/src/TabBar.tsx +33 -12
  62. package/src/Table.tsx +4 -3
  63. package/src/Tabs.tsx +5 -0
  64. package/src/ai.tsx +28 -20
  65. package/src/charts/BarChart.tsx +41 -3
  66. package/src/index.ts +1 -0
  67. package/src/internal/GlassView.tsx +17 -0
  68. package/src/internal/blur.ts +90 -0
  69. package/src/internal/fallback.tsx +158 -20
  70. package/src/internal/glow.tsx +62 -0
  71. package/src/internal/lens.tsx +105 -0
  72. package/src/internal/material.ts +92 -0
  73. package/src/internal/primitives.ts +3 -0
  74. package/src/internal/rim.tsx +79 -0
  75. package/src/internal/tokens.ts +34 -1
@@ -1,4 +1,4 @@
1
- import { useMemo, useState } from "react";
1
+ import { useId, useMemo, useState } from "react";
2
2
 
3
3
  import { colors } from "../internal/tokens";
4
4
  import { useIsDark } from "../internal/motion";
@@ -42,7 +42,19 @@ export function NativeBarChart({
42
42
  const [active, setActive] = useState<number | null>(null);
43
43
  const isDark = useIsDark(isDarkProp);
44
44
  const theme = isDark ? colors.dark : colors.light;
45
- const { View, Text, Pressable, Svg, G, Line, Rect, SvgText } = primitives();
45
+ const {
46
+ View,
47
+ Text,
48
+ Pressable,
49
+ Svg,
50
+ G,
51
+ Line,
52
+ Rect,
53
+ SvgText,
54
+ Defs,
55
+ LinearGradient,
56
+ Stop,
57
+ } = primitives();
46
58
 
47
59
  const plot = {
48
60
  x: PADDING.left,
@@ -64,6 +76,9 @@ export function NativeBarChart({
64
76
 
65
77
  const y = linearScale(domain, [bottom, plot.y], { nice: false });
66
78
  const bands = bandScale(labels.length, [plot.x, right], 0.32);
79
+ const seriesColors = series.map((s, si) => s.color ?? seriesColor(si, palette));
80
+ // One gradient per series, so the SVG can reference them by id.
81
+ const gradientKey = useId().replace(/[^a-zA-Z0-9_-]/g, "");
67
82
  const barWidth = stacked ? bands.width : bands.width / Math.max(1, series.length);
68
83
  const yTicks = ticks(domain, 4);
69
84
 
@@ -78,6 +93,25 @@ export function NativeBarChart({
78
93
  return (
79
94
  <View style={style}>
80
95
  <Svg width={width} height={height}>
96
+ <Defs>
97
+ {seriesColors.map((color, si) => (
98
+ // The web's bar fill runs from the series colour at the top to
99
+ // transparent at the base, so a bar reads as lit from above.
100
+ <LinearGradient
101
+ key={`grad-${si}`}
102
+ id={`bar-${gradientKey}-${si}`}
103
+ x1="0"
104
+ y1="0"
105
+ x2="0"
106
+ y2="1"
107
+ >
108
+ <Stop offset="0" stopColor={color} stopOpacity={1} />
109
+ <Stop offset="0.55" stopColor={color} stopOpacity={0.62} />
110
+ <Stop offset="1" stopColor={color} stopOpacity={0.04} />
111
+ </LinearGradient>
112
+ ))}
113
+ </Defs>
114
+
81
115
  {yTicks.map((tick, i) => (
82
116
  <Line
83
117
  key={`grid-${i}`}
@@ -117,7 +151,11 @@ export function NativeBarChart({
117
151
  width={w}
118
152
  height={barHeight}
119
153
  rx={Math.min(radius, w / 2)}
120
- fill={color}
154
+ fill={`url(#bar-${gradientKey}-${si})`}
155
+ // The inset edge the web draws inside every bar.
156
+ stroke={color}
157
+ strokeOpacity={0.35}
158
+ strokeWidth={0.5}
121
159
  opacity={active === null || active === i ? 1 : 0.45}
122
160
  />
123
161
  );
package/src/index.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  export { Glass, type GlassProps } from "./Glass";
2
+ export type { SurfaceProfile } from "./internal/material";
2
3
  export { Button, type ButtonProps } from "./Button";
3
4
  export { Switch, type SwitchProps } from "./Switch";
4
5
  export { Slider, type SliderProps } from "./Slider";
@@ -3,12 +3,25 @@ import type { StyleProp, ViewStyle } from "react-native";
3
3
 
4
4
  import { FallbackGlassView } from "./fallback";
5
5
  import { defaultRouter } from "./glass-routing";
6
+ import type { SurfaceProfile } from "./material";
6
7
  import { useIsDark } from "./motion";
7
8
 
8
9
  export interface NativeGlassProps {
9
10
  effect?: "clear" | "regular" | "none";
10
11
  tintColor?: string;
11
12
  interactive?: boolean;
13
+ /**
14
+ * Which web surface profile this is (panel, control, field, overlay, hint,
15
+ * rail). The fallback uses it to pick blur, rim brightness and highlight —
16
+ * read by the fallback branch only; Apple's material replaces the stack on
17
+ * iOS 26, so the prop is not forwarded there.
18
+ */
19
+ surface?: SurfaceProfile;
20
+ /**
21
+ * True while the surface is held, so the fallback can fade its specular glow
22
+ * in. Not forwarded to Apple's view, which draws its own press effect.
23
+ */
24
+ pressed?: boolean;
12
25
  /** Passed to whichever surface renders, so tests can find the glass itself. */
13
26
  testID?: string;
14
27
  style?: StyleProp<ViewStyle>;
@@ -68,6 +81,8 @@ export const GlassView = forwardRef<any, NativeGlassProps>(function GlassView(
68
81
  effect = "regular",
69
82
  tintColor,
70
83
  interactive = false,
84
+ surface = "panel",
85
+ pressed,
71
86
  testID,
72
87
  style,
73
88
  children,
@@ -100,6 +115,8 @@ export const GlassView = forwardRef<any, NativeGlassProps>(function GlassView(
100
115
  ref={ref}
101
116
  effect={effect}
102
117
  tintColor={tintColor}
118
+ surface={surface}
119
+ pressed={pressed}
103
120
  testID={testID}
104
121
  isDark={isDark}
105
122
  style={style}
@@ -0,0 +1,90 @@
1
+ // Backdrop blur for the fallback material.
2
+ //
3
+ // React Native has no backdrop blur of its own. The fallback's biggest missing
4
+ // layer is exactly that: without it a surface is a translucent rectangle, not
5
+ // glass. Two community backends provide a real native blur view, and the
6
+ // package accepts either:
7
+ //
8
+ // - `expo-blur` (Expo apps; also works on web through backdrop-filter)
9
+ // - `@react-native-community/blur` (bare React Native)
10
+ //
11
+ // They are optional peer dependencies, resolved at runtime like every other
12
+ // native module in this package: a project that installs neither still gets a
13
+ // working (unblurred) fallback instead of a crash, and the jsdom test run never
14
+ // touches a native module.
15
+
16
+ declare const require: any;
17
+
18
+ export type BlurKind = "expo" | "community";
19
+
20
+ interface ResolvedBlur {
21
+ kind: BlurKind;
22
+ View: any;
23
+ }
24
+
25
+ let resolved: ResolvedBlur | null | undefined;
26
+
27
+ /**
28
+ * The installed blur backend, or null when neither is available.
29
+ *
30
+ * Memoised: the answer is a property of the installed modules, not of the
31
+ * render, so resolving once keeps every surface on the same backend.
32
+ */
33
+ export function resolveBlurView(): ResolvedBlur | null {
34
+ if (resolved !== undefined) return resolved;
35
+
36
+ try {
37
+ const expo = require("expo-blur");
38
+ if (expo?.BlurView) {
39
+ resolved = { kind: "expo", View: expo.BlurView };
40
+ return resolved;
41
+ }
42
+ } catch {
43
+ // Not installed: try the bare-React-Native backend below.
44
+ }
45
+
46
+ try {
47
+ const community = require("@react-native-community/blur");
48
+ if (community?.BlurView) {
49
+ resolved = { kind: "community", View: community.BlurView };
50
+ return resolved;
51
+ }
52
+ } catch {
53
+ // Neither backend is installed; the fallback renders tint, rim and shadow.
54
+ }
55
+
56
+ resolved = null;
57
+ return resolved;
58
+ }
59
+
60
+ /** Clears the memoised backend. Only tests call this. */
61
+ export function resetBlurResolver() {
62
+ resolved = undefined;
63
+ }
64
+
65
+ /**
66
+ * The props one backend needs to blur a surface.
67
+ *
68
+ * The two libraries name the same idea differently — expo-blur takes a 0–100
69
+ * intensity and a tint, community-blur a 0–100 amount and a blur type — so the
70
+ * mapping lives here rather than in the fallback's render. Android needs an
71
+ * explicit method on expo-blur; Dimezis is its own real-time backdrop blur.
72
+ */
73
+ export function blurProps(
74
+ kind: BlurKind,
75
+ intensity: number,
76
+ isDark: boolean,
77
+ ): Record<string, any> {
78
+ if (kind === "expo") {
79
+ return {
80
+ intensity,
81
+ tint: isDark ? "dark" : "light",
82
+ experimentalBlurMethod: "dimezisBlurView",
83
+ };
84
+ }
85
+ return {
86
+ // Community blur amounts are coarser; the tint carries most of the read.
87
+ blurAmount: Math.max(1, Math.round(intensity / 1.6)),
88
+ blurType: isDark ? "dark" : "light",
89
+ };
90
+ }
@@ -1,13 +1,25 @@
1
1
  import { forwardRef, type ReactNode } from "react";
2
2
  import type { StyleProp, ViewStyle } from "react-native";
3
3
 
4
- import { colors, radii } from "./tokens";
4
+ import { colors, material, radii } from "./tokens";
5
5
  import { primitives } from "./primitives";
6
6
  import { useReducedTransparency } from "./a11y";
7
+ import { blurProps, resolveBlurView } from "./blur";
8
+ import { SurfaceGlow } from "./glow";
9
+ import { isFlatProfile, materialFor, type SurfaceProfile } from "./material";
10
+ import { GlassRim } from "./rim";
7
11
 
8
12
  export interface FallbackGlassProps {
9
13
  effect?: "clear" | "regular" | "none";
14
+ /**
15
+ * Which of the web's six lenses this surface is. The profile decides the
16
+ * blur strength, rim brightness and top highlight, so a dialog and a badge
17
+ * no longer share one flat recipe.
18
+ */
19
+ surface?: SurfaceProfile;
10
20
  tintColor?: string;
21
+ /** True while the surface is held; fades the specular glow in. */
22
+ pressed?: boolean;
11
23
  testID?: string;
12
24
  style?: StyleProp<ViewStyle>;
13
25
  children?: ReactNode;
@@ -19,17 +31,57 @@ export interface FallbackGlassProps {
19
31
  reducedTransparency?: boolean;
20
32
  }
21
33
 
34
+ const fill = { position: "absolute", top: 0, left: 0, right: 0, bottom: 0 } as const;
35
+
36
+ /** Layout and paint properties that belong to the surface itself. */
37
+ const HOISTED = new Set([
38
+ "backgroundColor",
39
+ "shadowColor",
40
+ "shadowOpacity",
41
+ "shadowRadius",
42
+ "shadowOffset",
43
+ "elevation",
44
+ ]);
45
+
46
+ /** Merges a style prop into one object; a function style is not resolvable here. */
47
+ function flattenStyle(style: any): ViewStyle {
48
+ if (!style) return {};
49
+ if (Array.isArray(style)) {
50
+ return style.reduce<ViewStyle>((acc, entry) => ({ ...acc, ...flattenStyle(entry) }), {});
51
+ }
52
+ if (typeof style === "function") return {};
53
+ return style as ViewStyle;
54
+ }
55
+
22
56
  /**
23
57
  * Glacia's own glass surface for every platform where Apple's material is not
24
58
  * available: Android, iOS before 26, and Expo Go.
25
59
  *
26
- * It renders the resolver's real React Native `View` with the same shape and
27
- * layout as the native glass, so switching branches never shifts the layout.
60
+ * The material is the web one, rebuilt in layers, because a single flat View
61
+ * could never read as glass:
62
+ *
63
+ * 1. backdrop blur (`expo-blur` / `@react-native-community/blur` when either
64
+ * is installed — a real `UIVisualEffectView` on iOS 17),
65
+ * 2. the tint token on top of the blurred backdrop,
66
+ * 3. a painted rim light, brightest on the top edge, so corners read as curved,
67
+ * 4. a soft highlight just inside the top edge,
68
+ * 5. the 0.5px edge line, and
69
+ * 6. a drop shadow on the surface itself (outside the clipped layer).
70
+ *
71
+ * All of that lives in an absolutely-positioned, clipped layer behind the
72
+ * children. The caller's style stays on the outer View, so heights, padding and
73
+ * flex behaviour are byte-identical to the old flat implementation and
74
+ * switching branches never shifts the layout.
75
+ *
76
+ * Reduce Transparency still replaces the whole thing with a solid colour: the
77
+ * preference exists precisely to remove see-through surfaces.
28
78
  */
29
79
  export const FallbackGlassView = forwardRef<any, FallbackGlassProps>(function FallbackGlassView(
30
80
  {
31
81
  effect = "regular",
82
+ surface = "panel",
32
83
  tintColor,
84
+ pressed,
33
85
  testID,
34
86
  style,
35
87
  children,
@@ -47,31 +99,117 @@ export const FallbackGlassView = forwardRef<any, FallbackGlassProps>(function Fa
47
99
  const systemReduced = useReducedTransparency();
48
100
  const solid = reducedTransparency ?? systemReduced;
49
101
 
50
- const bg = solid
51
- ? effect === "none"
52
- ? "transparent"
53
- : tintColor && effect === "clear"
54
- ? tintColor
55
- : themeColors.glassSolid
56
- : effect === "none"
57
- ? "transparent"
102
+ const flat = flattenStyle(style);
103
+ const radius = typeof flat.borderRadius === "number" ? flat.borderRadius : radii.md;
104
+ const explicitBackground =
105
+ typeof flat.backgroundColor === "string" ? flat.backgroundColor : undefined;
106
+
107
+ // `effect="none"` is a deliberate "no surface here" (a ghost button); it
108
+ // paints nothing at all, exactly as before.
109
+ const bare = effect === "none";
110
+ const layered = !solid && !bare;
111
+
112
+ const profile = materialFor(surface, bare ? "regular" : effect, isDark);
113
+ const blur = layered ? resolveBlurView() : null;
114
+
115
+ const surfaceStyle: ViewStyle = { ...flat };
116
+ for (const key of HOISTED) delete (surfaceStyle as any)[key];
117
+ // The edge is the web's 0.5px inset line. A component that draws only one
118
+ // edge (AppBar's scroll edge) keeps its own widths; a component that asked
119
+ // for a 1px box border is brought back to the hairline.
120
+ const perEdge = ["borderTopWidth", "borderRightWidth", "borderBottomWidth", "borderLeftWidth"].some(
121
+ (key) => typeof (flat as any)[key] === "number",
122
+ );
123
+ if (typeof flat.borderWidth === "number") {
124
+ surfaceStyle.borderWidth = Math.min(flat.borderWidth, 0.5);
125
+ } else if (!perEdge) {
126
+ surfaceStyle.borderWidth = 0.5;
127
+ }
128
+ surfaceStyle.borderColor = flat.borderColor ?? themeColors.edge;
129
+ surfaceStyle.borderRadius = radius;
130
+
131
+ if (solid) {
132
+ // The solid path is the old single flat view: opaque, no layers, and the
133
+ // caller's own background (a filled button) still wins over the token.
134
+ surfaceStyle.backgroundColor =
135
+ flat.backgroundColor ??
136
+ (effect === "clear" && tintColor ? tintColor : themeColors.glassSolid);
137
+ surfaceStyle.overflow = "hidden";
138
+ } else if (bare) {
139
+ surfaceStyle.backgroundColor = flat.backgroundColor ?? "transparent";
140
+ surfaceStyle.overflow = "hidden";
141
+ } else {
142
+ // Layered material. An opaque background is a filled control, not glass:
143
+ // it still gets a rim so it does not read as a sticker, but no blur tint.
144
+ const opaque = Boolean(explicitBackground);
145
+ const tint = opaque
146
+ ? explicitBackground
58
147
  : effect === "clear"
59
148
  ? isDark
60
149
  ? "rgba(28, 28, 30, 0.35)"
61
150
  : "rgba(255, 255, 255, 0.35)"
62
151
  : tintColor || themeColors.glass;
63
152
 
64
- const containerStyle: ViewStyle = {
65
- backgroundColor: bg,
66
- borderColor: themeColors.edge,
67
- borderWidth: 0.5,
68
- borderRadius: radii.md,
69
- overflow: "hidden",
70
- ...(style as ViewStyle),
71
- };
153
+ const rim = opaque ? profile.rim * 0.5 : profile.rim;
154
+ const highlight = opaque ? profile.highlight * 0.5 : profile.highlight;
155
+ const blurrable = Boolean(blur) && !opaque;
156
+
157
+ const shadowOpacity =
158
+ typeof flat.shadowOpacity === "number" ? flat.shadowOpacity : profile.shadowOpacity;
159
+ const shadowRadius =
160
+ typeof flat.shadowRadius === "number" ? flat.shadowRadius : profile.shadowRadius;
161
+ const shadowHeight =
162
+ typeof flat.shadowOffset?.height === "number"
163
+ ? flat.shadowOffset.height
164
+ : profile.shadowOffsetY;
165
+ // The hint family (badges, tooltips) sits flush with the page on the web.
166
+ const lifts = !isFlatProfile(surface) && shadowOpacity > 0;
167
+
168
+ return (
169
+ <View
170
+ ref={ref}
171
+ testID={testID}
172
+ style={[
173
+ surfaceStyle,
174
+ lifts
175
+ ? {
176
+ shadowColor: material.shadowColor,
177
+ shadowOpacity,
178
+ shadowRadius,
179
+ shadowOffset: { width: 0, height: shadowHeight },
180
+ elevation: typeof flat.elevation === "number" ? flat.elevation : profile.elevation,
181
+ }
182
+ : null,
183
+ ]}
184
+ {...props}
185
+ >
186
+ {/* The material layer carries a derived testID so a test can assert on
187
+ the blur/tint/rim stack rather than on the outer layout box. */}
188
+ <View
189
+ testID={testID ? `${testID}-material` : undefined}
190
+ style={[fill, { borderRadius: radius, overflow: "hidden" }]}
191
+ pointerEvents="none"
192
+ >
193
+ {blurrable && blur ? (
194
+ <blur.View
195
+ {...blurProps(blur.kind, profile.blur, isDark)}
196
+ style={[fill, { borderRadius: radius }]}
197
+ />
198
+ ) : null}
199
+ <View
200
+ testID={testID ? `${testID}-tint` : undefined}
201
+ style={[fill, { backgroundColor: tint }]}
202
+ />
203
+ <GlassRim radius={radius} rim={rim} highlight={highlight} />
204
+ {pressed !== undefined ? <SurfaceGlow active={pressed} /> : null}
205
+ </View>
206
+ {children}
207
+ </View>
208
+ );
209
+ }
72
210
 
73
211
  return (
74
- <View ref={ref} testID={testID} style={containerStyle} {...props}>
212
+ <View ref={ref} testID={testID} style={surfaceStyle} {...props}>
75
213
  {children}
76
214
  </View>
77
215
  );
@@ -0,0 +1,62 @@
1
+ // The specular press highlight, painted as a radial glow.
2
+ //
3
+ // The web material fades a 240px radial white highlight in under the finger
4
+ // for 220ms whenever a glass control is held. That is what makes a press feel
5
+ // like touching a lens rather than a flat sticker. The fallback had only an
6
+ // opacity dip, so controls never looked held.
7
+ //
8
+ // The glow lives inside the clipped material layer, so it follows the same
9
+ // radius as the surface and can never spill outside it.
10
+
11
+ import { useId } from "react";
12
+
13
+ import { useSpring } from "./motion";
14
+ import { primitives } from "./primitives";
15
+ import { material } from "./tokens";
16
+
17
+ const fill = { position: "absolute", top: 0, left: 0, right: 0, bottom: 0 } as const;
18
+
19
+ export interface SurfaceGlowProps {
20
+ /** True while the surface is held. */
21
+ active: boolean;
22
+ }
23
+
24
+ /**
25
+ * A radial highlight that fades in while `active`.
26
+ *
27
+ * Motion uses the package's spring tokens and respects Reduce Motion: with the
28
+ * preference on, the glow appears at its end state rather than fading.
29
+ */
30
+ export function SurfaceGlow({ active }: SurfaceGlowProps) {
31
+ const { Svg, Defs, RadialGradient, Stop, Rect, Animated } = primitives();
32
+ const { value, animated } = useSpring(active ? 1 : 0, "snappy");
33
+ const gradientId = `glow-${useId().replace(/[^a-zA-Z0-9_-]/g, "")}`;
34
+
35
+ const painting = (
36
+ <Svg width="100%" height="100%" style={fill} pointerEvents="none">
37
+ <Defs>
38
+ <RadialGradient id={gradientId} cx="50%" cy="50%" r="50%">
39
+ <Stop offset="0" stopColor={material.rimColor} stopOpacity={material.glow} />
40
+ <Stop offset="1" stopColor={material.rimColor} stopOpacity={0} />
41
+ </RadialGradient>
42
+ </Defs>
43
+ <Rect x="0" y="0" width="100%" height="100%" fill={`url(#${gradientId})`} />
44
+ </Svg>
45
+ );
46
+
47
+ // Off-device there is no Animated API; the settled state is what a preview
48
+ // should show, exactly as `useSpring` documents.
49
+ if (!animated) {
50
+ return (
51
+ <Animated.View style={[fill, { opacity: active ? 1 : 0 }]} pointerEvents="none">
52
+ {painting}
53
+ </Animated.View>
54
+ );
55
+ }
56
+
57
+ return (
58
+ <Animated.View style={[fill, { opacity: value }]} pointerEvents="none">
59
+ {painting}
60
+ </Animated.View>
61
+ );
62
+ }
@@ -0,0 +1,105 @@
1
+ // The selected lens: one raised surface, without a second glass material.
2
+ //
3
+ // Every selected thing in the fallback — a list row, a menu option, a chosen
4
+ // weekday, the tab bar's indicator — is the web's "lens": the selected tint,
5
+ // lifted by a shadow, with the same painted rim as the surfaces around it.
6
+ //
7
+ // It cannot be a `Glass`. Glass inside a glass panel is glass-on-glass, which
8
+ // the material rules forbid, and the surfaces these sit in are already glass.
9
+ // So the lens is painted directly: tint, shadow, rim. Same look, no stacking.
10
+
11
+ import type { ReactNode } from "react";
12
+ import type { StyleProp, ViewStyle } from "react-native";
13
+
14
+ import { colors, material, radii } from "./tokens";
15
+ import { primitives } from "./primitives";
16
+ import { GlassRim } from "./rim";
17
+
18
+ const fill = { position: "absolute", top: 0, left: 0, right: 0, bottom: 0 } as const;
19
+
20
+ /**
21
+ * How far a lens sits above its row.
22
+ *
23
+ * Shallower than a panel's shadow: a lens is inside a surface that already
24
+ * floats, so it needs contact, not altitude.
25
+ */
26
+ export function lensShadow(isDark?: boolean): ViewStyle {
27
+ return {
28
+ shadowColor: material.shadowColor,
29
+ shadowOpacity: isDark ? 0 : 0.1,
30
+ shadowRadius: 8,
31
+ shadowOffset: { width: 0, height: 2 },
32
+ } as ViewStyle;
33
+ }
34
+
35
+ /**
36
+ * The rim of a lens, on its own.
37
+ *
38
+ * For indicators that are already animated views and cannot become a
39
+ * `SelectedLens`: they keep their own transform, and this paints the light on
40
+ * top of whatever tint they already have.
41
+ */
42
+ export function LensRim({ radius, isDark }: { radius: number; isDark?: boolean }) {
43
+ const { View } = primitives();
44
+ const rim = (isDark ? material.rim.dark : material.rim.light) * 0.66;
45
+ return (
46
+ <View style={{ ...fill, overflow: "hidden", borderRadius: radius }} pointerEvents="none">
47
+ <GlassRim radius={radius} rim={rim} highlight={material.highlight * 0.7} />
48
+ </View>
49
+ );
50
+ }
51
+
52
+ export interface SelectedLensProps {
53
+ /** The lens shape. A circle is simply a radius of half the side. */
54
+ radius: number;
55
+ isDark?: boolean;
56
+ style?: StyleProp<ViewStyle>;
57
+ children?: ReactNode;
58
+ testID?: string;
59
+ }
60
+
61
+ /**
62
+ * A selected state, drawn as the web's lens.
63
+ *
64
+ * Rendered as an absolutely positioned overlay so it never changes the layout
65
+ * of the row it marks: parents keep their padding and press handling, and the
66
+ * content paints above it by sibling order.
67
+ */
68
+ export function SelectedLens({ radius, isDark, style, children, testID }: SelectedLensProps) {
69
+ const { View } = primitives();
70
+ const theme = isDark ? colors.dark : colors.light;
71
+
72
+ return (
73
+ <View
74
+ testID={testID}
75
+ pointerEvents="none"
76
+ style={[
77
+ {
78
+ borderRadius: radius,
79
+ backgroundColor: theme.glassActive,
80
+ ...lensShadow(isDark),
81
+ },
82
+ style as ViewStyle,
83
+ ]}
84
+ >
85
+ {/* A tighter rim than a panel's: small surfaces catch less light. */}
86
+ <LensRim radius={radius} isDark={isDark} />
87
+ {children}
88
+ </View>
89
+ );
90
+ }
91
+
92
+ /** The lens overlay for a row: inset from the panel's edges, full width inside. */
93
+ export function rowLensStyle(radius: number, inset = 6): ViewStyle {
94
+ return {
95
+ position: "absolute",
96
+ top: 2,
97
+ bottom: 2,
98
+ left: inset,
99
+ right: inset,
100
+ borderRadius: radius,
101
+ };
102
+ }
103
+
104
+ /** A pill lens filling its own box, for indicators pinned to a control. */
105
+ export const lensFill = { ...fill, borderRadius: radii.pill } as const;