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