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

AndroidScreenHeader.tsx

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