Make search bar collapse on small screen

This commit is contained in:
Zoe Roux 2023-01-03 22:34:17 +09:00
parent 942f4f1c75
commit 95eb703788
3 changed files with 72 additions and 36 deletions

View File

@ -18,30 +18,31 @@
* along with Kyoo. If not, see <https://www.gnu.org/licenses/>. * along with Kyoo. If not, see <https://www.gnu.org/licenses/>.
*/ */
import { forwardRef } from "react";
import { TextInput } from "react-native"; import { TextInput } from "react-native";
import { px, Stylable, useYoshiki } from "yoshiki/native"; import { px, Stylable, useYoshiki } from "yoshiki/native";
import { ts } from "./utils"; import { ts } from "./utils";
export const Input = ({ export const Input = forwardRef<
onChange, TextInput,
value, {
placeholder,
placeholderTextColor,
...props
}: {
onChange: (value: string) => void; onChange: (value: string) => void;
value?: string; value?: string;
placeholder?: string; placeholder?: string;
placeholderTextColor?: string; placeholderTextColor?: string;
} & Stylable) => { onBlur?: (value: string | undefined) => void;
} & Stylable
>(function _Input({ onChange, value, placeholder, placeholderTextColor, onBlur, ...props }, ref) {
const { css, theme } = useYoshiki(); const { css, theme } = useYoshiki();
return ( return (
<TextInput <TextInput
ref={ref}
value={value ?? ""} value={value ?? ""}
onChangeText={onChange} onChangeText={onChange}
placeholder={placeholder} placeholder={placeholder}
placeholderTextColor={placeholderTextColor ?? theme.overlay1} placeholderTextColor={placeholderTextColor ?? theme.overlay1}
onBlur={() => onBlur?.call(null, value)}
{...css( {...css(
{ {
borderColor: (theme) => theme.accent, borderColor: (theme) => theme.accent,
@ -53,4 +54,4 @@ export const Input = ({
)} )}
/> />
); );
}; });

View File

@ -75,7 +75,8 @@ export const PressableFeedback = forwardRef<
const pressProps = { onBlur, onFocus, onPressIn, onPressOut, onPress }; const pressProps = { onBlur, onFocus, onPressIn, onPressOut, onPress };
const wrapperProps = Platform.select<ViewProps & { ref?: any }>({ const wrapperProps = Platform.select<ViewProps & { ref?: any }>({
android: { android: {
style: { borderRadius: StyleSheet.flatten(props?.style)?.borderRadius, overflow: "hidden" }, ...props,
style: { ...StyleSheet.flatten(props?.style), overflow: "hidden" },
ref, ref,
}, },
default: {}, default: {},

View File

@ -30,7 +30,7 @@ import {
ts, ts,
Link, Link,
} from "@kyoo/primitives"; } from "@kyoo/primitives";
import { Platform, View } from "react-native"; import { Platform, TextInput, View } from "react-native";
import { useTranslation } from "react-i18next"; import { useTranslation } from "react-i18next";
import { createParam } from "solito"; import { createParam } from "solito";
import { useRouter } from "solito/router"; import { useRouter } from "solito/router";
@ -39,7 +39,7 @@ import Menu from "@material-symbols/svg-400/rounded/menu-fill.svg";
import Search from "@material-symbols/svg-400/rounded/search-fill.svg"; import Search from "@material-symbols/svg-400/rounded/search-fill.svg";
import { Fetch } from "../fetch"; import { Fetch } from "../fetch";
import { KyooLongLogo } from "./icon"; import { KyooLongLogo } from "./icon";
import { useState } from "react"; import { forwardRef, useRef, useState } from "react";
export const NavbarTitle = (props: Stylable) => { export const NavbarTitle = (props: Stylable) => {
const { t } = useTranslation(); const { t } = useTranslation();
@ -53,19 +53,21 @@ export const NavbarTitle = (props: Stylable) => {
const { useParam } = createParam<{ q?: string }>(); const { useParam } = createParam<{ q?: string }>();
const SearchBar = () => { const SearchBar = forwardRef<
TextInput,
{ onBlur?: (value: string | undefined) => void } & Stylable
>(function _SearchBar({ onBlur, ...props }, ref) {
const { css, theme } = useYoshiki(); const { css, theme } = useYoshiki();
const { t } = useTranslation(); const { t } = useTranslation();
const { push, replace, back } = useRouter(); const { push, replace, back } = useRouter();
// eslint-disable-next-line react-hooks/rules-of-hooks const [query] = useParam("q");
// const [query, setQuery] = Platform.OS === "web" ? useState("") : useParam("q");
const [query, setQuery] = useParam("q");
return ( return (
<Input <Input
ref={ref}
value={query} value={query}
onBlur={onBlur}
onChange={(q) => { onChange={(q) => {
setQuery(q);
if (Platform.OS === "web") { if (Platform.OS === "web") {
const action = window.location.pathname.startsWith("/search") ? replace : push; const action = window.location.pathname.startsWith("/search") ? replace : push;
if (q) action(`/search?q=${q}`, undefined, { shallow: true }); if (q) action(`/search?q=${q}`, undefined, { shallow: true });
@ -75,27 +77,59 @@ const SearchBar = () => {
placeholder={t("navbar.search")} placeholder={t("navbar.search")}
placeholderTextColor={theme.light.overlay0} placeholderTextColor={theme.light.overlay0}
{...tooltip(t("navbar.search"))} {...tooltip(t("navbar.search"))}
{...css({ borderColor: (theme) => theme.colors.white })} {...css({ borderColor: (theme) => theme.colors.white }, props)}
/> />
); );
}; });
const Right = () => { export const NavbarProfile = () => {
const theme = useTheme(); const { css, theme } = useYoshiki();
const { css } = useYoshiki();
const { t } = useTranslation(); const { t } = useTranslation();
return ( return (
<> <Link
{Platform.OS === "web" ? ( href="/auth/login"
<SearchBar /> {...tooltip(t("navbar.login"))}
) : ( {...css({ marginLeft: ts(1), justifyContent: "center" })}
<IconButton icon={Search} as={Link} href="/search" {...tooltip("navbar.search")} /> >
)}
<A href="/auth/login" {...tooltip(t("navbar.login"))} {...css({ marginLeft: ts(1) })}>
<Avatar alt={t("navbar.login")} size={30} color={theme.colors.white} /> <Avatar alt={t("navbar.login")} size={30} color={theme.colors.white} />
</A> </Link>
</> );
};
export const NavbarRight = () => {
const { css } = useYoshiki();
const { t } = useTranslation();
const [isSearching, setSearch] = useState(false);
const ref = useRef<TextInput | null>(null);
const [query] = useParam("q");
const searchExpanded = isSearching || query;
return (
<View {...css({ flexDirection: "row" })}>
<SearchBar
ref={ref}
onBlur={(q) => {
if (!q) setSearch(false);
}}
{...css(
Platform.OS === "web" && {
display: { xs: searchExpanded ? "flex" : "none", md: "flex" },
},
)}
/>
{!searchExpanded && (
<IconButton
icon={Search}
onPress={() => {
setSearch(true);
setTimeout(() => ref.current?.focus(), 0);
}}
{...tooltip(t("navbar.search"))}
{...css(Platform.OS === "web" && { display: { xs: "flex", md: "none" } })}
/>
)}
<NavbarProfile />
</View>
); );
}; };
@ -163,7 +197,7 @@ export const Navbar = (props: Stylable) => {
} }
</Fetch> </Fetch>
</View> </View>
<Right /> <NavbarRight />
</Header> </Header>
); );
}; };