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

ComponentCatalogScreen.tsx

301 lines11.4 KB

1import { NativeAppBarExample } from "./NativeAppBarExample";
2import { Toast } from "../../components/ui/Toast";
3import { Progress } from "../../components/ui/Progress";
4import { useNavigation } from "@react-navigation/native";
5import { useRef, useState } from "react";
6import { View, type TextInputInstance } from "react-native";
7​
8import { AppText, AppTextInput } from "../../components/ui/AppText";
9import { Sheet } from "../../components/ui/Sheet";
10import { Chip } from "../../components/ui/Chip";
11import { Menu } from "../../components/ui/Menu";
12import { ListActionRow } from "../../components/ui/ListActionRow";
13import { ListChoiceRow } from "../../components/ui/ListChoiceRow";
14import { ListRow } from "../../components/ui/ListRow";
15import { ListSection } from "../../components/ui/ListSection";
16import { ListToggleRow } from "../../components/ui/ListToggleRow";
17import { Button } from "../../components/ui/Button";
18import { IconButton } from "../../components/ui/IconButton";
19import { FloatingActionButton } from "../../components/ui/FloatingActionButton";
20import { SearchField } from "../../components/ui/SearchField";
21import { Notice } from "../../components/ui/Notice";
22import { NumberField } from "../../components/ui/NumberField";
23import { AppBar } from "../../components/ui/AppBar";
24import { ScreenScrollView } from "../../components/ui/ScreenScrollView";
25import { SegmentedControl } from "../../components/ui/SegmentedControl";
26import { SliderRow } from "../../components/ui/SliderRow";
27import * as Dialog from "../../components/ui/Dialog";
28import { useAppearancePreferences } from "../settings/appearance/AppearancePreferencesProvider";
29import { MOBILE_THEME_IDS } from "../../lib/mobileTheme";
30​
31const CHOICES = [
32 { value: "overview", label: "Overview" },
33 { value: "activity", label: "Activity" },
34];
35​
36export function ComponentCatalogScreen() {
37 const navigation = useNavigation();
38 const appearance = useAppearancePreferences();
39 const [page, setPage] = useState("controls");
40 const [tab, setTab] = useState("overview");
41 const [enabled, setEnabled] = useState(true);
42 const [value, setValue] = useState(3);
43 const [text, setText] = useState("");
44 const [search, setSearch] = useState("");
45 const [sheet, setSheet] = useState(false);
46 const [notice, setNotice] = useState(true);
47 const searchRef = useRef<TextInputInstance>(null);
48 const announceAction = () => setNotice(true);
49 const confirm = () => {
50 Dialog.confirm({
51 title: "Discard changes?",
52 message: "This example only changes local catalog state.",
53 confirmText: "Discard",
54 destructive: true,
55 onConfirm: () => setText(""),
56 });
57 };
58 return (
59 <View className="flex-1 bg-screen">
60 <AppBar title="UI components" onBack={() => navigation.goBack()} sidebar={false} />
61 <ScreenScrollView
62 contentInsetAdjustmentBehavior="automatic"
63 contentContainerStyle={{ padding: 16, gap: 20, paddingBottom: 40 }}
64 keyboardShouldPersistTaps="handled"
65 >
66 <View className="gap-3">
67 <AppText className="text-sm text-foreground-muted">Theme preview</AppText>
68 <View className="flex-row flex-wrap gap-2">
69 {MOBILE_THEME_IDS.filter(
70 (id) => id !== "material-you" || appearance.systemColorsAvailable,
71 ).map((id) => (
72 <Chip
73 key={id}
74 label={id}
75 selected={appearance.themeId === id}
76 onPress={() => appearance.setThemeIdForBothAppearances(id)}
77 />
78 ))}
79 </View>
80 <SegmentedControl
81 options={[
82 { value: "light", label: "Light" },
83 { value: "dark", label: "Dark" },
84 ]}
85 selected={appearance.themeAppearance}
86 onSelect={appearance.setThemeMode}
87 size="compact"
88 />
89 </View>
90 <SegmentedControl
91 options={[
92 { value: "controls", label: "Controls" },
93 { value: "forms", label: "Forms" },
94 { value: "feedback", label: "Feedback" },
95 ]}
96 selected={page}
97 onSelect={setPage}
98 size="compact"
99 />
100 {page === "controls" ? (
101 <>
102 <View className="gap-3">
103 <AppText className="text-lg font-t3-semibold">Lists and selection</AppText>
104 <ListSection>
105 <ListRow icon="folder" label="Projects" detail="4" onPress={() => setSheet(true)} />
106 <ListToggleRow
107 icon="bell.badge"
108 label="Notifications"
109 description="Updates from your active threads"
110 value={enabled}
111 onValueChange={setEnabled}
112 />
113 <ListChoiceRow label="Selected option" selected onPress={announceAction} />
114 <ListActionRow
115 icon="trash"
116 label="Discard changes"
117 tone="danger"
118 onPress={confirm}
119 />
120 </ListSection>
121 <SegmentedControl
122 options={CHOICES}
123 selected={tab}
124 onSelect={setTab}
125 size="compact"
126 role="tab"
127 />
128 <View className="flex-row flex-wrap gap-2">
129 <Chip
130 label="Selected filter"
131 selected={enabled}
132 onPress={() => setEnabled(!enabled)}
133 />
134 <Chip label="Label" size="small" />
135 <Chip label="Disabled" disabled onPress={announceAction} />
136 </View>
137 </View>
138 <View className="gap-3">
139 <NativeAppBarExample />
140 <AppText className="text-lg font-t3-semibold">Buttons</AppText>
141 <View className="flex-row flex-wrap gap-2">
142 <Button label="Primary" tone="primary" onPress={announceAction} />
143 <Button label="Secondary" onPress={announceAction} />
144 <Button label="Contrast" tone="contrast" onPress={announceAction} />
145 </View>
146 <View className="flex-row flex-wrap gap-2">
147 <Button label="Disabled" disabled onPress={announceAction} />
148 <Button label="Loading" loading onPress={announceAction} />
149 <IconButton
150 accessibilityLabel="Open example sheet"
151 icon="plus"
152 variant="tonal"
153 onPress={() => setSheet(true)}
154 />
155 </View>
156 </View>
157 </>
158 ) : null}
159 {page === "forms" ? (
160 <View className="gap-3">
161 <AppText className="text-lg font-t3-semibold">Forms</AppText>
162 <AppTextInput
163 accessibilityLabel="Example name"
164 placeholder="Name"
165 value={text}
166 onChangeText={setText}
167 />
168 <View className="flex-row">
169 <SearchField
170 inputRef={searchRef}
171 accessibilityLabel="Search examples"
172 clearAccessibilityLabel="Clear examples search"
173 placeholder="Search examples"
174 value={search}
175 onChangeText={setSearch}
176 autoFocus={false}
177 />
178 </View>
179 <View className="flex-row items-center justify-between">
180 <AppText>Count</AppText>
181 <NumberField
182 label="Example count"
183 min={1}
184 max={10}
185 value={value}
186 onValueChange={setValue}
187 />
188 </View>
189 <ListSection>
190 <SliderRow
191 icon="textformat.size"
192 label="Example slider"
193 valueLabel={String(value)}
194 min={1}
195 max={10}
196 step={1}
197 value={value}
198 onChange={setValue}
199 />
200 </ListSection>
201 </View>
202 ) : null}
203 {page === "feedback" ? (
204 <View className="gap-3">
205 <AppText className="text-lg font-t3-semibold">Feedback and presentation</AppText>
206 <Notice message="This action could not finish. Try again." tone="danger" />
207 {notice ? (
208 <Toast
209 message="Changes are ready to review."
210 tone="success"
211 action={{
212 label: "Undo",
213 onPress: () => {
214 setNotice(false);
215 setText("Restored example");
216 },
217 }}
218 onDismiss={() => setNotice(false)}
219 />
220 ) : (
221 <Button label="Show notice" onPress={announceAction} />
222 )}
223 <View className="flex-row flex-wrap gap-2">
224 <Button label="Sheet" onPress={() => setSheet(true)} />
225 <Button label="Dialog" onPress={confirm} />
226 <Button
227 label="Alert"
228 onPress={() =>
229 Dialog.alert("Example alert", "Choose an action.", [
230 {
231 text: "More info",
232 onPress: () =>
233 Dialog.alert("More information", "This second alert stays open."),
234 },
235 { text: "Cancel", style: "cancel" },
236 { text: "Continue", onPress: announceAction },
237 ])
238 }
239 />
240 <Button
241 label="Prompt"
242 onPress={() =>
243 Dialog.prompt({
244 title: "Rename example",
245 initialValue: text,
246 confirmText: "Rename",
247 onConfirm: setText,
248 })
249 }
250 />
251 <Menu
252 title="Example menu"
253 actions={[
254 { id: "copy", title: "Copy", image: "doc.on.doc" },
255 { id: "discard", title: "Discard", attributes: { destructive: true } },
256 ]}
257 onPressAction={({ nativeEvent }) =>
258 nativeEvent.event === "discard" ? confirm() : announceAction()
259 }
260 >
261 <Chip label="Menu" trailingIcon="chevron.down" />
262 </Menu>
263 </View>
264 <Progress value={0.6} label="Example upload" />
265 <Progress label="Connecting" />
266 <View className="items-start">
267 <FloatingActionButton
268 label="New thread"
269 icon="plus"
270 variant="extended"
271 tone="primary"
272 onPress={() => setSheet(true)}
273 />
274 </View>
275 </View>
276 ) : null}
277 </ScreenScrollView>
278 {sheet ? (
279 <Sheet title="Example sheet" onClose={() => setSheet(false)}>
280 <ScreenScrollView
281 contentInsetAdjustmentBehavior="automatic"
282 contentContainerClassName="p-4"
283 >
284 <ListSection>
285 <ListChoiceRow
286 label="Overview"
287 selected={tab === "overview"}
288 onPress={() => setTab("overview")}
289 />
290 <ListChoiceRow
291 label="Activity"
292 selected={tab === "activity"}
293 onPress={() => setTab("activity")}
294 />
295 </ListSection>
296 </ScreenScrollView>
297 </Sheet>
298 ) : null}
299 </View>
300 );