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

GlassSurface.tsx

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