diff --git a/app/components/Search.tsx b/app/components/Search.tsx index 9b5f96d..cf3b559 100644 --- a/app/components/Search.tsx +++ b/app/components/Search.tsx @@ -31,10 +31,10 @@ const SearchContext = createContext<{ results: Accessor; isLoading: Accessor; isOpened: Accessor; - setQuery: (query: string) => void; - setIsOpened: (isOpened: boolean) => void; - setIsLoading: (isLoading: boolean) => void; - setResults: (results: SearchResult[]) => void; + setQuery: Setter; + setIsOpened: Setter; + setIsLoading: Setter; + setResults: Setter; }>({ query: () => "", close: () => {}, @@ -89,11 +89,11 @@ function SearchInput() { return (
- + { @@ -108,11 +108,14 @@ function SearchInput() { } }} value={query()} - onChange={(event) => setQuery(event.currentTarget.value)} + onInput={(event) => { + const { value } = event.currentTarget; + setQuery(value); + }} /> {isLoading() && (
- +
)}
@@ -146,7 +149,7 @@ function SearchResultItem(props: { result: SearchResult; query: string }) { return (
  • { @@ -163,7 +166,7 @@ function SearchResultItem(props: { result: SearchResult; query: string }) { @@ -196,13 +199,11 @@ function SearchResultItem(props: { result: SearchResult; query: string }) { function SearchResults() { const { results, query } = useContext(SearchContext); - if (results.length === 0) { + if (results().length === 0) { return ( -

    +

    Aucun résultat pour “ - - {query()} - + {query()}

    ); @@ -241,6 +242,14 @@ function SearchDialog(props: { class?: string }) { }; }, [isOpened, setIsOpened]); + const handleClickOutside = (event: MouseEvent) => { + const { target, currentTarget } = event; + + if (target instanceof Node && currentTarget instanceof Node) { + if (target === currentTarget) close(); + } + }; + return ( <>
    -
    - +
    { + if (event.key === "Escape") close(); + }} + > +
    event.preventDefault()}> -
    - {results.length > 0 && } +
    + {results().length > 0 && }
    @@ -267,23 +282,22 @@ function SearchDialog(props: { class?: string }) { export function Search() { const [results, setResults] = createSignal([]); - const [debouncedQuery, setDebouncedQuery] = useDebounce(""); const [modifierKey, setModifierKey] = createSignal(); const [isLoading, setIsLoading] = createSignal(false); const [isOpened, setIsOpened] = createSignal(false); const [query, setQuery] = createSignal(""); + const debouncedQuery = useDebounce(query, 300); + createEffect(() => { const platform = navigator.userAgentData?.platform || navigator.platform; setModifierKey(/(Mac|iPhone|iPod|iPad)/i.test(platform) ? "⌘" : "Ctrl "); }, []); createEffect(() => { - setDebouncedQuery(query()); - }, [query()]); + const query = debouncedQuery(); - createEffect(() => { - if (debouncedQuery.length === 0) { + if (query.length === 0) { setIsLoading(false); setResults([]); return; @@ -291,12 +305,12 @@ export function Search() { setIsLoading(true); - onSearch(debouncedQuery()) + onSearch(query) .then(setResults) .finally(() => { setIsLoading(false); }); - }, [debouncedQuery()]); + }); return (