mirror of
https://github.com/movie-web/movie-web.git
synced 2025-01-12 12:39:10 +01:00
shortcut for enter to unfocus + slash to focus searchbar
This commit is contained in:
parent
08cc5260bd
commit
8bf6510eaf
@ -1,5 +1,5 @@
|
|||||||
import c from "classnames";
|
import c from "classnames";
|
||||||
import { useState } from "react";
|
import { forwardRef, useState } from "react";
|
||||||
|
|
||||||
import { Flare } from "@/components/utils/Flare";
|
import { Flare } from "@/components/utils/Flare";
|
||||||
|
|
||||||
@ -13,50 +13,53 @@ export interface SearchBarProps {
|
|||||||
value: string;
|
value: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function SearchBarInput(props: SearchBarProps) {
|
export const SearchBarInput = forwardRef<HTMLInputElement, SearchBarProps>(
|
||||||
const [focused, setFocused] = useState(false);
|
(props, ref) => {
|
||||||
|
const [focused, setFocused] = useState(false);
|
||||||
|
|
||||||
function setSearch(value: string) {
|
function setSearch(value: string) {
|
||||||
props.onChange(value, false);
|
props.onChange(value, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Flare.Base
|
<Flare.Base
|
||||||
className={c({
|
className={c({
|
||||||
"hover:flare-enabled group relative flex flex-col rounded-[28px] transition-colors sm:flex-row sm:items-center":
|
"hover:flare-enabled group relative flex flex-col rounded-[28px] transition-colors sm:flex-row sm:items-center":
|
||||||
true,
|
true,
|
||||||
"bg-search-background": !focused,
|
|
||||||
"bg-search-focused": focused,
|
|
||||||
})}
|
|
||||||
>
|
|
||||||
<Flare.Light
|
|
||||||
flareSize={400}
|
|
||||||
enabled={focused}
|
|
||||||
className="rounded-[28px]"
|
|
||||||
backgroundClass={c({
|
|
||||||
"transition-colors": true,
|
|
||||||
"bg-search-background": !focused,
|
"bg-search-background": !focused,
|
||||||
"bg-search-focused": focused,
|
"bg-search-focused": focused,
|
||||||
})}
|
})}
|
||||||
/>
|
>
|
||||||
|
<Flare.Light
|
||||||
<Flare.Child className="flex flex-1 flex-col">
|
flareSize={400}
|
||||||
<div className="pointer-events-none absolute bottom-0 left-5 top-0 flex max-h-14 items-center text-search-icon">
|
enabled={focused}
|
||||||
<Icon icon={Icons.SEARCH} />
|
className="rounded-[28px]"
|
||||||
</div>
|
backgroundClass={c({
|
||||||
|
"transition-colors": true,
|
||||||
<TextInputControl
|
"bg-search-background": !focused,
|
||||||
onUnFocus={() => {
|
"bg-search-focused": focused,
|
||||||
setFocused(false);
|
})}
|
||||||
props.onUnFocus();
|
|
||||||
}}
|
|
||||||
onFocus={() => setFocused(true)}
|
|
||||||
onChange={(val) => setSearch(val)}
|
|
||||||
value={props.value}
|
|
||||||
className="w-full flex-1 bg-transparent px-4 py-4 pl-12 text-search-text placeholder-search-placeholder focus:outline-none sm:py-4 sm:pr-2"
|
|
||||||
placeholder={props.placeholder}
|
|
||||||
/>
|
/>
|
||||||
</Flare.Child>
|
|
||||||
</Flare.Base>
|
<Flare.Child className="flex flex-1 flex-col">
|
||||||
);
|
<div className="pointer-events-none absolute bottom-0 left-5 top-0 flex max-h-14 items-center text-search-icon">
|
||||||
}
|
<Icon icon={Icons.SEARCH} />
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<TextInputControl
|
||||||
|
ref={ref}
|
||||||
|
onUnFocus={() => {
|
||||||
|
setFocused(false);
|
||||||
|
props.onUnFocus();
|
||||||
|
}}
|
||||||
|
onFocus={() => setFocused(true)}
|
||||||
|
onChange={(val) => setSearch(val)}
|
||||||
|
value={props.value}
|
||||||
|
className="w-full flex-1 bg-transparent px-4 py-4 pl-12 text-search-text placeholder-search-placeholder focus:outline-none sm:py-4 sm:pr-2"
|
||||||
|
placeholder={props.placeholder}
|
||||||
|
/>
|
||||||
|
</Flare.Child>
|
||||||
|
</Flare.Base>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
22
src/components/player/hooks/useSlashFocus.ts
Normal file
22
src/components/player/hooks/useSlashFocus.ts
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
import { useEffect } from "react";
|
||||||
|
|
||||||
|
export function useSlashFocus(ref: React.RefObject<HTMLInputElement>) {
|
||||||
|
useEffect(() => {
|
||||||
|
const listener = (e: KeyboardEvent) => {
|
||||||
|
if (e.key === "/") {
|
||||||
|
if (
|
||||||
|
document.activeElement &&
|
||||||
|
document.activeElement.tagName.toLowerCase() === "input"
|
||||||
|
)
|
||||||
|
return;
|
||||||
|
e.preventDefault();
|
||||||
|
ref.current?.focus();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
window.addEventListener("keydown", listener);
|
||||||
|
return () => {
|
||||||
|
window.removeEventListener("keydown", listener);
|
||||||
|
};
|
||||||
|
}, [ref]);
|
||||||
|
}
|
@ -1,3 +1,5 @@
|
|||||||
|
import { forwardRef } from "react";
|
||||||
|
|
||||||
export interface TextInputControlPropsNoLabel {
|
export interface TextInputControlPropsNoLabel {
|
||||||
onChange?: (data: string) => void;
|
onChange?: (data: string) => void;
|
||||||
onUnFocus?: () => void;
|
onUnFocus?: () => void;
|
||||||
@ -13,39 +15,51 @@ export interface TextInputControlProps extends TextInputControlPropsNoLabel {
|
|||||||
label?: string;
|
label?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function TextInputControl({
|
export const TextInputControl = forwardRef<
|
||||||
onChange,
|
HTMLInputElement,
|
||||||
onUnFocus,
|
TextInputControlProps
|
||||||
value,
|
>(
|
||||||
label,
|
(
|
||||||
name,
|
{
|
||||||
autoComplete,
|
onChange,
|
||||||
className,
|
onUnFocus,
|
||||||
placeholder,
|
value,
|
||||||
onFocus,
|
label,
|
||||||
}: TextInputControlProps) {
|
name,
|
||||||
const input = (
|
autoComplete,
|
||||||
<input
|
className,
|
||||||
type="text"
|
placeholder,
|
||||||
className={className}
|
onFocus,
|
||||||
placeholder={placeholder}
|
},
|
||||||
onChange={(e) => onChange && onChange(e.target.value)}
|
ref
|
||||||
value={value}
|
) => {
|
||||||
name={name}
|
const input = (
|
||||||
autoComplete={autoComplete}
|
<input
|
||||||
onBlur={() => onUnFocus && onUnFocus()}
|
type="text"
|
||||||
onFocus={() => onFocus?.()}
|
ref={ref}
|
||||||
/>
|
className={className}
|
||||||
);
|
placeholder={placeholder}
|
||||||
|
onChange={(e) => onChange && onChange(e.target.value)}
|
||||||
if (label) {
|
value={value}
|
||||||
return (
|
name={name}
|
||||||
<label>
|
autoComplete={autoComplete}
|
||||||
<span>{label}</span>
|
onBlur={() => onUnFocus && onUnFocus()}
|
||||||
{input}
|
onFocus={() => onFocus?.()}
|
||||||
</label>
|
onKeyDown={(e) =>
|
||||||
|
e.key === "Enter" ? (e.target as HTMLInputElement).blur() : null
|
||||||
|
}
|
||||||
|
/>
|
||||||
);
|
);
|
||||||
}
|
|
||||||
|
|
||||||
return input;
|
if (label) {
|
||||||
}
|
return (
|
||||||
|
<label>
|
||||||
|
<span>{label}</span>
|
||||||
|
{input}
|
||||||
|
</label>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return input;
|
||||||
|
}
|
||||||
|
);
|
||||||
|
@ -1,8 +1,9 @@
|
|||||||
import { useCallback, useState } from "react";
|
import { useCallback, useRef, useState } from "react";
|
||||||
import Sticky from "react-sticky-el";
|
import Sticky from "react-sticky-el";
|
||||||
|
|
||||||
import { SearchBarInput } from "@/components/form/SearchBar";
|
import { SearchBarInput } from "@/components/form/SearchBar";
|
||||||
import { ThinContainer } from "@/components/layout/ThinContainer";
|
import { ThinContainer } from "@/components/layout/ThinContainer";
|
||||||
|
import { useSlashFocus } from "@/components/player/hooks/useSlashFocus";
|
||||||
import { HeroTitle } from "@/components/text/HeroTitle";
|
import { HeroTitle } from "@/components/text/HeroTitle";
|
||||||
import { useRandomTranslation } from "@/hooks/useRandomTranslation";
|
import { useRandomTranslation } from "@/hooks/useRandomTranslation";
|
||||||
import { useSearchQuery } from "@/hooks/useSearchQuery";
|
import { useSearchQuery } from "@/hooks/useSearchQuery";
|
||||||
@ -33,6 +34,9 @@ export function HeroPart({ setIsSticky, searchParams }: HeroPartProps) {
|
|||||||
|
|
||||||
const title = t(`home.titles.${time}`);
|
const title = t(`home.titles.${time}`);
|
||||||
|
|
||||||
|
const inputRef = useRef<HTMLInputElement>(null);
|
||||||
|
useSlashFocus(inputRef);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<ThinContainer>
|
<ThinContainer>
|
||||||
<div className="mt-44 space-y-16 text-center">
|
<div className="mt-44 space-y-16 text-center">
|
||||||
@ -48,6 +52,7 @@ export function HeroPart({ setIsSticky, searchParams }: HeroPartProps) {
|
|||||||
onFixedToggle={stickStateChanged}
|
onFixedToggle={stickStateChanged}
|
||||||
>
|
>
|
||||||
<SearchBarInput
|
<SearchBarInput
|
||||||
|
ref={inputRef}
|
||||||
onChange={setSearch}
|
onChange={setSearch}
|
||||||
value={search}
|
value={search}
|
||||||
onUnFocus={setSearchUnFocus}
|
onUnFocus={setSearchUnFocus}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user