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

SheetHeader.tsx

165 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 visibilityPriority="high"
79 tintColor={colors["--color-prominent-action"]}
80 accessibilityLabel={confirmation.label}
81 disabled={confirmation.disabled}
82 onPress={confirmation.onPress}
83 />
84 ) : null}
85 </Toolbar>
86 ) : null}
87 </>
88 );
90​
91function InlineConfirmation({
92 confirmation,
93}: {
94 readonly confirmation: NonNullable<SheetHeaderProps["confirm"]>;
95}) {
96 const confirmDisabled = confirmation.disabled || confirmation.loading;
97 return (
98 <Pressable
99 accessibilityRole="button"
100 accessibilityLabel={confirmation.label}
101 accessibilityState={{
102 disabled: Boolean(confirmDisabled),
103 busy: Boolean(confirmation.loading),
104 }}
105 disabled={confirmDisabled}
106 onPress={confirmation.onPress}
107 className={
108 confirmDisabled
109 ? "size-11 items-center justify-center rounded-full bg-subtle-strong"
110 : "size-11 items-center justify-center rounded-full bg-prominent-action"
111 }
112 >
113 {confirmation.loading ? (
114 <AppActivityIndicator colorClassName="accent-icon-subtle" />
115 ) : (
116 <SymbolView
117 name="checkmark"
118 size={20}
119 tintColorClassName={
120 confirmDisabled ? "accent-icon-subtle" : "accent-prominent-action-foreground"
121 }
122 />
123 )}
124 </Pressable>
125 );
127​
128function InlineSheetHeader(props: SheetHeaderProps) {
129 const close = (
130 <SheetCloseButton
131 {...(props.onCancel ? { onCancel: props.onCancel } : { onClose: props.onClose })}
132 accessibilityLabel={props.accessibilityLabel}
133 disabled={props.dismissDisabled}
134 />
135 );
136 const confirm = props.confirm ? <InlineConfirmation confirmation={props.confirm} /> : null;
137 const trailing =
138 props.trailing || confirm ? (
139 <View className="flex-row items-center gap-2">
140 {props.trailing}
141 {confirm}
142 </View>
143 ) : undefined;
144​
145 return (
146 <View className="flex-row items-center gap-3 px-4 pt-3 pb-3">
147 {close}
148 <View className="min-w-0 flex-1 gap-1">
149 <AppText numberOfLines={1} className="text-base font-t3-bold text-foreground">
150 {props.title}
151 </AppText>
152 {props.subtitle ? (
153 <AppText
154 numberOfLines={1}
155 ellipsizeMode="middle"
156 className="text-xs text-foreground-muted"
157 >
158 {props.subtitle}
159 </AppText>
160 ) : null}
161 </View>
162 {trailing}
163 </View>
164 );