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

GlassSurface.tsx

110 lines3.1 KB

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