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

AndroidScreenHeader.tsx

117 lines3.4 KB

1import type { ReactNode } from "react";
2import { Pressable, View } from "react-native";
3import { useSafeAreaInsets } from "react-native-safe-area-context";
4​
5import { SymbolView, type AppSymbolName } from "./AppSymbol";
6import { AppText as Text } from "./AppText";
7import { cn } from "../lib/cn";
8import { useThemeColor } from "../lib/useThemeColor";
9​
10export interface AndroidHeaderAction {
11 readonly accessibilityLabel: string;
12 readonly icon: AppSymbolName;
13 readonly onPress: () => void;
14 readonly disabled?: boolean;
16​
17export function AndroidHeaderIconButton(props: {
18 readonly accessibilityLabel: string;
19 readonly icon: AppSymbolName;
20 readonly onPress?: () => void;
21 readonly disabled?: boolean;
22}) {
23 const foregroundColor = useThemeColor("--color-foreground");
24 const disabledColor = useThemeColor("--color-icon-subtle");
25​
26 return (
27 <Pressable
28 accessibilityLabel={props.accessibilityLabel}
29 accessibilityRole="button"
30 disabled={props.disabled}
31 hitSlop={8}
32 onPress={props.onPress}
33 className={cn(
34 "size-11 items-center justify-center rounded-full bg-subtle",
35 props.disabled && "opacity-55",
36 )}
37 >
38 <SymbolView
39 name={props.icon}
40 size={20}
41 tintColor={props.disabled ? disabledColor : foregroundColor}
42 type="monochrome"
43 />
44 </Pressable>
45 );
47​
48export function AndroidScreenHeader(props: {
49 readonly title: string;
50 readonly subtitle?: string | null;
51 readonly actions?: ReadonlyArray<AndroidHeaderAction>;
52 readonly trailing?: ReactNode;
53 readonly onBack?: () => void;
54 readonly embedded?: boolean;
55}) {
56 const insets = useSafeAreaInsets();
57 const foregroundColor = useThemeColor("--color-foreground");
58​
59 return (
60 <View
61 className="border-b border-header-border bg-header px-3 pb-2.5"
62 style={{
63 paddingTop: props.embedded ? 8 : Math.max(insets.top, 12),
64 }}
65 >
66 <View className="min-h-12 flex-row items-center gap-2">
67 {props.onBack ? (
68 <Pressable
69 accessibilityLabel="Navigate up"
70 accessibilityRole="button"
71 hitSlop={8}
72 onPress={props.onBack}
73 className="size-11 items-center justify-center"
74 >
75 <SymbolView
76 name="chevron.left"
77 size={24}
78 tintColor={foregroundColor}
79 type="monochrome"
80 />
81 </Pressable>
82 ) : null}
83​
84 <View className={cn("min-w-0 flex-1", !props.onBack && "pl-1")}>
85 <Text numberOfLines={1} className="text-lg font-t3-bold text-foreground">
86 {props.title}
87 </Text>
88 {props.subtitle ? (
89 <Text
90 numberOfLines={1}
91 className="mt-px text-[13px] font-t3-medium text-foreground-muted"
92 >
93 {props.subtitle}
94 </Text>
95 ) : null}
96 </View>
97​
98 {props.actions?.map((action) => (
99 <AndroidHeaderIconButton
100 key={action.accessibilityLabel}
101 accessibilityLabel={action.accessibilityLabel}
102 disabled={action.disabled}
103 icon={action.icon}
104 onPress={action.onPress}
105 />
106 ))}
107 {props.trailing}
108 </View>
109 </View>
110 );
112​
113export function AndroidSheetHeader(
114 props: Omit<Parameters<typeof AndroidScreenHeader>[0], "embedded">,
115) {
116 return <AndroidScreenHeader {...props} embedded />;