Open History to see the last commit
1import { GlassView, isGlassEffectAPIAvailable } from "expo-glass-effect";
2import type { ReactNode, Ref } from "react";
3import { Platform, View, type ColorValue, type ViewProps, type ViewStyle } from "react-native";
4import { withUniwind } from "uniwind";
5
6import { cn } from "../lib/cn";
7import { useAppearancePreferences } from "../features/settings/appearance/AppearancePreferencesProvider";
8import { GlassBackdrop } from "./GlassBackdrop";
9
10// Explicit mappings keep the native glassEffectStyle enum out of style-array conversion.
11const ThemedGlassView = withUniwind(GlassView, {
12 style: { fromClassName: "className" },
13 tintColor: { fromClassName: "tintColorClassName", styleProperty: "accentColor" },
14});
15
16interface GlassSurfaceProps extends ViewProps {
17 readonly ref?: Ref<View>;
18 readonly children: ReactNode;
19 readonly glassEffectStyle?: "clear" | "regular" | "none";
20 readonly tintColor?: ColorValue;
21 readonly tintColorClassName?: string;
22 readonly chrome?: "default" | "none";
23 /** Base color for the frosted tint, or solid fill when blur is unavailable. */
24 readonly fallbackColor?: ColorValue;
25 /** Uniwind styling used only when native Liquid Glass is unavailable. */
26 readonly fallbackClassName?: string;
27}
28
29export function GlassSurface({
30 ref,
31 children,
32 glassEffectStyle = "regular",
33 chrome = "default",
34 tintColor,
35 tintColorClassName,
36 fallbackColor,
37 fallbackClassName,
38 className,
39 style,
40 ...props
41}: GlassSurfaceProps) {
42 const { themeAppearance } = useAppearancePreferences();
43 const isDarkMode = themeAppearance === "dark";
44 const supportsGlass = Platform.OS === "ios" && isGlassEffectAPIAvailable();
45 const hasShadow = chrome !== "none" && Platform.OS !== "android";
46 const surfaceStyle: ViewStyle = {
47 borderRadius: 32,
48 overflow: "hidden",
49 shadowColor: hasShadow ? "#000000" : "transparent",
50 shadowOpacity: hasShadow ? (isDarkMode ? 0.22 : 0.08) : 0,
51 shadowRadius: hasShadow ? 28 : 0,
52 shadowOffset: { width: 0, height: hasShadow ? 14 : 0 },
53 elevation: hasShadow ? 12 : 0,
54 };
55
56 if (supportsGlass) {
57 return (
58 <ThemedGlassView
59 {...props}
60 ref={ref}
61 className={cn(
62 chrome === "none"
63 ? "border-0 border-transparent bg-transparent"
64 : "border border-border bg-glass-surface",
65 className,
66 )}
67 glassEffectStyle={glassEffectStyle}
68 tintColor={tintColor === undefined ? undefined : String(tintColor)}
69 tintColorClassName={
70 tintColorClassName ?? (tintColor === undefined ? "accent-glass-tint" : undefined)
71 }
72 colorScheme={isDarkMode ? "dark" : "light"}
73 style={[surfaceStyle, style]}
74 >
75 {children}
76 </ThemedGlassView>
77 );
78 }
79
80 return (
81 <View
82 {...props}
83 ref={ref}
84 className={cn(
85 chrome === "none" ? "border-0 border-transparent" : "border border-border",
86 fallbackClassName,
87 className,
88 )}
89 style={[surfaceStyle, style]}
90 >
91 <GlassBackdrop fallbackColor={fallbackColor} />
92 {children}
93 </View>
94 );
95}