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

GlassSurface.tsx

103 lines3.1 KB

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