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

GlassSurface.tsx

101 lines3.0 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 readonly fallbackColor?: ColorValue;
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 { themeAppearance } = useAppearancePreferences();
49 const isDarkMode = themeAppearance === "dark";
50 const supportsGlass = Platform.OS === "ios" && isGlassEffectAPIAvailable();
51 const hasShadow = chrome !== "none" && Platform.OS !== "android";
52 const surfaceStyle: ViewStyle = {
53 borderRadius: MOBILE_SHAPE.glass,
54 overflow: "hidden",
55 shadowColor: hasShadow ? "#000000" : "transparent",
56 shadowOpacity: hasShadow ? (isDarkMode ? 0.22 : 0.08) : 0,
57 shadowRadius: hasShadow ? 28 : 0,
58 shadowOffset: { width: 0, height: hasShadow ? 14 : 0 },
59 elevation: hasShadow ? 12 : 0,
60 };
61​
62 if (supportsGlass) {
63 return (
64 <ThemedGlassView
65 {...props}
66 ref={ref}
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 ref={ref}
90 className={cn(
91 chrome === "none" ? "border-0 border-transparent" : "border border-border",
92 fallbackClassName,
93 className,
94 )}
95 style={[surfaceStyle, style]}
96 >
97 <GlassBackdrop fallbackColor={fallbackColor} />
98 {children}
99 </View>
100 );