import React from "react"; import gql from "graphql-tag"; import { Box, Text, VisuallyHidden } from "@chakra-ui/core"; import { useQuery } from "@apollo/client"; import { Delay, useDebounce } from "../util"; import Item, { ItemListContainer, ItemListSkeleton } from "./Item"; import { itemAppearanceFragment } from "../components/useOutfitAppearance"; /** * SearchPanel shows item search results to the user, so they can preview them * and add them to their outfit! * * It's tightly coordinated with SearchToolbar, using refs to control special * keyboard and focus interactions. */ function SearchPanel({ query, outfitState, dispatchToOutfit, scrollContainerRef, searchQueryRef, firstSearchResultRef, }) { // Whenever the search query changes, scroll back up to the top! React.useEffect(() => { if (scrollContainerRef.current) { scrollContainerRef.current.scrollTop = 0; } }, [query, scrollContainerRef]); // Sometimes we want to give focus back to the search field! const onMoveFocusUpToQuery = (e) => { if (searchQueryRef.current) { searchQueryRef.current.focus(); e.preventDefault(); } }; return ( { // This will catch any Escape presses when the user's focus is inside // the SearchPanel. if (e.key === "Escape") { onMoveFocusUpToQuery(e); } }} > ); } /** * SearchResults loads the search results from the user's query, renders them, * and tracks the scroll container for infinite scrolling. * * For each item, we render a