Open History to see the last commit
1import { useCallback, useEffect, useRef, useState, type ReactNode } from "react";
2import { BackHandler, Keyboard, Pressable, TextInput, View } from "react-native";
3import { useSafeAreaInsets } from "react-native-safe-area-context";
4
5import { AndroidHeaderIconButton, AndroidScreenHeader } from "../../components/AndroidScreenHeader";
6import { SymbolView } from "../../components/AppSymbol";
7
8/** Keep Files search in the same header row on compact and expanded layouts. */
9export function MaterialFilesHeader(props: {
10 readonly projectName: string;
11 readonly searchQuery: string;
12 readonly onSearchQueryChange: (query: string) => void;
13 readonly onRefresh: () => void;
14 readonly onBack?: () => void;
15 readonly leading?: ReactNode;
16}) {
17 const insets = useSafeAreaInsets();
18 const searchRef = useRef<TextInput>(null);
19 const [searchOpen, setSearchOpen] = useState(false);
20 const searching = searchOpen || props.searchQuery.length > 0;
21 const { onSearchQueryChange } = props;
22 const closeSearch = useCallback(() => {
23 onSearchQueryChange("");
24 setSearchOpen(false);
25 Keyboard.dismiss();
26 }, [onSearchQueryChange]);
27
28 useEffect(() => {
29 if (!searching) return;
30 const subscription = BackHandler.addEventListener("hardwareBackPress", () => {
31 closeSearch();
32 return true;
33 });
34 return () => subscription.remove();
35 }, [closeSearch, searching]);
36
37 return (
38 <View>
39 {/* Keep the title/subtitle's natural height, including larger text, while searching. */}
40 <View
41 pointerEvents={searching ? "none" : "auto"}
42 accessibilityElementsHidden={searching}
43 importantForAccessibility={searching ? "no-hide-descendants" : "auto"}
44 style={{ opacity: searching ? 0 : 1 }}
45 >
46 <AndroidScreenHeader
47 title="Files"
48 subtitle={props.projectName}
49 hideBottomBorder
50 onBack={props.onBack}
51 leading={props.leading}
52 actions={[
53 {
54 accessibilityLabel: "Search files",
55 icon: "magnifyingglass",
56 onPress: () => setSearchOpen(true),
57 },
58 {
59 accessibilityLabel: "Refresh files",
60 icon: "arrow.clockwise",
61 onPress: props.onRefresh,
62 },
63 ]}
64 />
65 </View>
66 {searching ? (
67 <View
68 className="absolute inset-0 bg-header px-2 pb-2"
69 style={{ paddingTop: Math.max(insets.top, 12) }}
70 >
71 <View className="min-h-14 flex-1 flex-row items-center gap-1">
72 <AndroidHeaderIconButton
73 accessibilityLabel="Close file search"
74 icon="arrow.left"
75 onPress={closeSearch}
76 />
77 <View className="h-12 min-w-0 flex-1 flex-row items-center gap-2 rounded-full border border-input-border bg-input px-3">
78 <SymbolView
79 name="magnifyingglass"
80 size={18}
81 tintColorClassName="accent-foreground-muted"
82 />
83 <TextInput
84 ref={searchRef}
85 accessibilityLabel="Search files"
86 autoFocus
87 autoCapitalize="none"
88 autoCorrect={false}
89 returnKeyType="search"
90 placeholder="Search files"
91 placeholderTextColorClassName="accent-placeholder"
92 selectionColorClassName="accent-primary/32"
93 cursorColorClassName="accent-primary"
94 selectionHandleColorClassName="accent-primary"
95 className="min-w-0 flex-1 py-2 font-sans text-base text-foreground"
96 value={props.searchQuery}
97 onChangeText={onSearchQueryChange}
98 />
99 {props.searchQuery.length > 0 ? (
100 <Pressable
101 accessibilityLabel="Clear file search"
102 accessibilityRole="button"
103 hitSlop={10}
104 onPress={() => {
105 onSearchQueryChange("");
106 searchRef.current?.focus();
107 }}
108 >
109 <SymbolView
110 name="xmark.circle.fill"
111 size={18}
112 tintColorClassName="accent-foreground-muted"
113 />
114 </Pressable>
115 ) : null}
116 </View>
117 </View>
118 </View>
119 ) : null}
120 </View>
121 );
122}