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