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