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

GlassSurface.tsx

78 lines2.2 KB

1import { GlassView, isGlassEffectAPIAvailable } from "expo-glass-effect";
2import type { ReactNode } from "react";
3import {
4 Platform,
5 useColorScheme,
6 View,
7 type ColorValue,
8 type StyleProp,
9 type ViewProps,
10 type ViewStyle,
11} from "react-native";
12import { useThemeColor } from "../lib/useThemeColor";
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 isDarkMode = useColorScheme() === "dark";
33 const borderColor = useThemeColor("--color-border");
34 const glassSurface = useThemeColor("--color-glass-surface");
35 const glassTint = useThemeColor("--color-glass-tint");
36 const supportsGlass = Platform.OS === "ios" && isGlassEffectAPIAvailable();
37 const surfaceStyle: ViewStyle = {
38 borderRadius: 32,
39 overflow: "hidden",
40 borderWidth: chrome === "none" ? 0 : 1,
41 borderColor: chrome === "none" ? "transparent" : borderColor,
42 backgroundColor: chrome === "none" ? "transparent" : glassSurface,
43 shadowColor: chrome === "none" ? "transparent" : "#000000",
44 shadowOpacity: chrome === "none" ? 0 : isDarkMode ? 0.22 : 0.08,
45 shadowRadius: chrome === "none" ? 0 : 28,
46 shadowOffset:
47 chrome === "none"
48 ? {
49 width: 0,
50 height: 0,
51 }
52 : {
53 width: 0,
54 height: 14,
55 },
56 elevation: chrome === "none" ? 0 : 12,
57 };
58​
59 if (supportsGlass) {
60 return (
61 <GlassView
62 {...props}
63 glassEffectStyle={glassEffectStyle}
64 tintColor={String(tintColor ?? glassTint)}
65 colorScheme={isDarkMode ? "dark" : "light"}
66 style={[surfaceStyle, style]}
67 >
68 {children}
69 </GlassView>
70 );
71 }
72​
73 return (
74 <View {...props} style={[surfaceStyle, fallbackStyle, style]}>
75 {children}
76 </View>
77 );