Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

GlassSurface.tsx

100 lines2.9 KB

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 ViewProps,
9 type ViewStyle,
10} from "react-native";
11import { withUniwind } from "uniwind";
12​
13import { cn } from "../lib/cn";
14import { GlassBackdrop } from "./GlassBackdrop";
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 /** Base color for the frosted tint, or solid fill when blur is unavailable. */
30 readonly fallbackColor?: ColorValue;
31 /** Uniwind styling used only when native Liquid Glass is unavailable. */
32 readonly fallbackClassName?: string;
34​
35export function GlassSurface({
36 ref,
37 children,
38 glassEffectStyle = "regular",
39 chrome = "default",
40 tintColor,
41 tintColorClassName,
42 fallbackColor,
43 fallbackClassName,
44 className,
45 style,
46 ...props
47}: GlassSurfaceProps) {
48 const isDarkMode = useColorScheme() === "dark";
49 const supportsGlass = Platform.OS === "ios" && isGlassEffectAPIAvailable();
50 const hasShadow = chrome !== "none" && Platform.OS !== "android";
51 const surfaceStyle: ViewStyle = {
52 borderRadius: 32,
53 overflow: "hidden",
54 shadowColor: hasShadow ? "#000000" : "transparent",
55 shadowOpacity: hasShadow ? (isDarkMode ? 0.22 : 0.08) : 0,
56 shadowRadius: hasShadow ? 28 : 0,
57 shadowOffset: { width: 0, height: hasShadow ? 14 : 0 },
58 elevation: hasShadow ? 12 : 0,
59 };
60​
61 if (supportsGlass) {
62 return (
63 <ThemedGlassView
64 {...props}
65 ref={ref}
66 className={cn(
67 chrome === "none"
68 ? "border-0 border-transparent bg-transparent"
69 : "border border-border bg-glass-surface",
70 className,
71 )}
72 glassEffectStyle={glassEffectStyle}
73 tintColor={tintColor === undefined ? undefined : String(tintColor)}
74 tintColorClassName={
75 tintColorClassName ?? (tintColor === undefined ? "accent-glass-tint" : undefined)
76 }
77 colorScheme={isDarkMode ? "dark" : "light"}
78 style={[surfaceStyle, style]}
79 >
80 {children}
81 </ThemedGlassView>
82 );
83 }
84​
85 return (
86 <View
87 {...props}
88 ref={ref}
89 className={cn(
90 chrome === "none" ? "border-0 border-transparent" : "border border-border",
91 fallbackClassName,
92 className,
93 )}
94 style={[surfaceStyle, style]}
95 >
96 <GlassBackdrop fallbackColor={fallbackColor} />
97 {children}
98 </View>
99 );