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

SheetHeader.tsx

164 lines5.0 KB

1import { Pressable, View } from "react-native";
2​
3import { NativeStackScreenOptions } from "../../native/StackHeader";
4import { AppActivityIndicator } from "./AppActivityIndicator";
5import { AppText } from "./AppText";
6import { SymbolView } from "./AppSymbol";
7import { IconButton } from "./IconButton";
8import { Toolbar } from "./Toolbar";
9import { useControlTheme } from "./useControlTheme";
10import type { SheetCloseButtonProps, SheetHeaderProps } from "./SheetHeader.types";
11​
12export function SheetCloseButton(props: SheetCloseButtonProps) {
13 return (
14 <IconButton
15 icon="xmark"
16 accessibilityLabel={props.accessibilityLabel ?? (props.onCancel ? "Cancel" : "Close")}
17 disabled={props.disabled}
18 onPress={props.onCancel ?? props.onClose}
19 />
20 );
22​
23export function SheetHeader(props: SheetHeaderProps) {
24 return props.presentation !== "inline" ? (
25 <NativeSheetHeader {...props} />
26 ) : (
27 <InlineSheetHeader {...props} />
28 );
30​
31function NativeSheetHeader(props: SheetHeaderProps) {
32 const { colors } = useControlTheme();
33 const onDismiss = props.onCancel ?? props.onClose;
34 const dismissLabel = props.accessibilityLabel ?? (props.onCancel ? "Cancel" : "Close");
35 const confirmation = props.confirm;
36​
37 // Toolbar children are parsed as native items, so keep the actual Button
38 // elements here instead of hiding them behind another component.
39 return (
40 <>
41 <NativeStackScreenOptions
42 options={{
43 title: props.title,
44 headerBackVisible: false,
45 unstable_headerSubtitle: props.subtitle,
46 }}
47 />
48 <Toolbar placement="left">
49 <Toolbar.Button
50 icon="xmark"
51 accessibilityLabel={dismissLabel}
52 onPress={onDismiss}
53 disabled={props.dismissDisabled}
54 />
55 </Toolbar>
56 {confirmation || props.trailing ? (
57 <Toolbar placement="right">
58 {props.trailing ? (
59 <Toolbar.Custom>
60 <View>{props.trailing}</View>
61 </Toolbar.Custom>
62 ) : null}
63 {confirmation?.loading ? (
64 <Toolbar.Custom>
65 <View
66 className="size-11 items-center justify-center"
67 accessible
68 accessibilityLabel={confirmation.label}
69 accessibilityState={{ busy: true }}
70 >
71 <AppActivityIndicator colorClassName="accent-prominent-action" />
72 </View>
73 </Toolbar.Custom>
74 ) : confirmation ? (
75 <Toolbar.Button
76 icon="checkmark"
77 variant="prominent"
78 tintColor={colors["--color-prominent-action"]}
79 accessibilityLabel={confirmation.label}
80 disabled={confirmation.disabled}
81 onPress={confirmation.onPress}
82 />
83 ) : null}
84 </Toolbar>
85 ) : null}
86 </>
87 );
89​
90function InlineConfirmation({
91 confirmation,
92}: {
93 readonly confirmation: NonNullable<SheetHeaderProps["confirm"]>;
94}) {
95 const confirmDisabled = confirmation.disabled || confirmation.loading;
96 return (
97 <Pressable
98 accessibilityRole="button"
99 accessibilityLabel={confirmation.label}
100 accessibilityState={{
101 disabled: Boolean(confirmDisabled),
102 busy: Boolean(confirmation.loading),
103 }}
104 disabled={confirmDisabled}
105 onPress={confirmation.onPress}
106 className={
107 confirmDisabled
108 ? "size-11 items-center justify-center rounded-full bg-subtle-strong"
109 : "size-11 items-center justify-center rounded-full bg-prominent-action"
110 }
111 >
112 {confirmation.loading ? (
113 <AppActivityIndicator colorClassName="accent-icon-subtle" />
114 ) : (
115 <SymbolView
116 name="checkmark"
117 size={20}
118 tintColorClassName={
119 confirmDisabled ? "accent-icon-subtle" : "accent-prominent-action-foreground"
120 }
121 />
122 )}
123 </Pressable>
124 );
126​
127function InlineSheetHeader(props: SheetHeaderProps) {
128 const close = (
129 <SheetCloseButton
130 {...(props.onCancel ? { onCancel: props.onCancel } : { onClose: props.onClose })}
131 accessibilityLabel={props.accessibilityLabel}
132 disabled={props.dismissDisabled}
133 />
134 );
135 const confirm = props.confirm ? <InlineConfirmation confirmation={props.confirm} /> : null;
136 const trailing =
137 props.trailing || confirm ? (
138 <View className="flex-row items-center gap-2">
139 {props.trailing}
140 {confirm}
141 </View>
142 ) : undefined;
143​
144 return (
145 <View className="flex-row items-center gap-3 px-4 pt-3 pb-3">
146 {close}
147 <View className="min-w-0 flex-1 gap-1">
148 <AppText numberOfLines={1} className="text-base font-t3-bold text-foreground">
149 {props.title}
150 </AppText>
151 {props.subtitle ? (
152 <AppText
153 numberOfLines={1}
154 ellipsizeMode="middle"
155 className="text-xs text-foreground-muted"
156 >
157 {props.subtitle}
158 </AppText>
159 ) : null}
160 </View>
161 {trailing}
162 </View>
163 );