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

GlassSurface.tsx

79 lines2.3 KB

1import { GlassView, isGlassEffectAPIAvailable } from "expo-glass-effect";
2import type { ReactNode } from "react";
3import {
4 Platform,
5 View,
6 type ColorValue,
7 type StyleProp,
8 type ViewProps,
9 type ViewStyle,
10} from "react-native";
11import { useThemeColor } from "../lib/useThemeColor";
12import { useAppearancePreferences } from "../features/settings/appearance/AppearancePreferencesProvider";
13​
14interface GlassSurfaceProps extends Omit<ViewProps, "className"> {
15 readonly children: ReactNode;
16 readonly glassEffectStyle?: "clear" | "regular" | "none";
17 readonly tintColor?: ColorValue;
18 readonly chrome?: "default" | "none";
19 /** Styling used only when native Liquid Glass is unavailable. */
20 readonly fallbackStyle?: StyleProp<ViewStyle>;
22​
23export function GlassSurface({
24 children,
25 glassEffectStyle = "regular",
26 chrome = "default",
27 tintColor,
28 fallbackStyle,
29 style,
30 ...props
31}: GlassSurfaceProps) {
32 const { themeAppearance } = useAppearancePreferences();
33 const isDarkMode = themeAppearance === "dark";
34 const borderColor = useThemeColor("--color-border");
35 const glassSurface = useThemeColor("--color-glass-surface");
36 const glassTint = useThemeColor("--color-glass-tint");
37 const supportsGlass = Platform.OS === "ios" && isGlassEffectAPIAvailable();
38 const surfaceStyle: ViewStyle = {
39 borderRadius: 32,
40 overflow: "hidden",
41 borderWidth: chrome === "none" ? 0 : 1,
42 borderColor: chrome === "none" ? "transparent" : borderColor,
43 backgroundColor: chrome === "none" ? "transparent" : glassSurface,
44 shadowColor: chrome === "none" ? "transparent" : "#000000",
45 shadowOpacity: chrome === "none" ? 0 : isDarkMode ? 0.22 : 0.08,
46 shadowRadius: chrome === "none" ? 0 : 28,
47 shadowOffset:
48 chrome === "none"
49 ? {
50 width: 0,
51 height: 0,
52 }
53 : {
54 width: 0,
55 height: 14,
56 },
57 elevation: chrome === "none" ? 0 : 12,
58 };
59​
60 if (supportsGlass) {
61 return (
62 <GlassView
63 {...props}
64 glassEffectStyle={glassEffectStyle}
65 tintColor={String(tintColor ?? glassTint)}
66 colorScheme={isDarkMode ? "dark" : "light"}
67 style={[surfaceStyle, style]}
68 >
69 {children}
70 </GlassView>
71 );
72 }
73​
74 return (
75 <View {...props} style={[surfaceStyle, fallbackStyle, style]}>
76 {children}
77 </View>
78 );