shortcut for enter to unfocus + slash to focus searchbar

This commit is contained in:
mrjvs 2023-11-29 18:20:31 +01:00
parent 08cc5260bd
commit 8bf6510eaf
4 changed files with 121 additions and 77 deletions

View File

@ -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,7 +13,8 @@ export interface SearchBarProps {
value: string; value: string;
} }
export function SearchBarInput(props: SearchBarProps) { export const SearchBarInput = forwardRef<HTMLInputElement, SearchBarProps>(
(props, ref) => {
const [focused, setFocused] = useState(false); const [focused, setFocused] = useState(false);
function setSearch(value: string) { function setSearch(value: string) {
@ -46,6 +47,7 @@ export function SearchBarInput(props: SearchBarProps) {
</div> </div>
<TextInputControl <TextInputControl
ref={ref}
onUnFocus={() => { onUnFocus={() => {
setFocused(false); setFocused(false);
props.onUnFocus(); props.onUnFocus();
@ -59,4 +61,5 @@ export function SearchBarInput(props: SearchBarProps) {
</Flare.Child> </Flare.Child>
</Flare.Base> </Flare.Base>
); );
} }
);

View 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]);
}

View File

@ -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,7 +15,12 @@ export interface TextInputControlProps extends TextInputControlPropsNoLabel {
label?: string; label?: string;
} }
export function TextInputControl({ export const TextInputControl = forwardRef<
HTMLInputElement,
TextInputControlProps
>(
(
{
onChange, onChange,
onUnFocus, onUnFocus,
value, value,
@ -23,10 +30,13 @@ export function TextInputControl({
className, className,
placeholder, placeholder,
onFocus, onFocus,
}: TextInputControlProps) { },
ref
) => {
const input = ( const input = (
<input <input
type="text" type="text"
ref={ref}
className={className} className={className}
placeholder={placeholder} placeholder={placeholder}
onChange={(e) => onChange && onChange(e.target.value)} onChange={(e) => onChange && onChange(e.target.value)}
@ -35,6 +45,9 @@ export function TextInputControl({
autoComplete={autoComplete} autoComplete={autoComplete}
onBlur={() => onUnFocus && onUnFocus()} onBlur={() => onUnFocus && onUnFocus()}
onFocus={() => onFocus?.()} onFocus={() => onFocus?.()}
onKeyDown={(e) =>
e.key === "Enter" ? (e.target as HTMLInputElement).blur() : null
}
/> />
); );
@ -48,4 +61,5 @@ export function TextInputControl({
} }
return input; return input;
} }
);

View File

@ -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}