MCPcopy Create free account
hub / github.com/adobe/react-spectrum / useSearchMenuState

Function useSearchMenuState

packages/dev/s2-docs/src/searchUtils.tsx:824–966  ·  view source on GitHub ↗
(options: SearchMenuStateOptions)

Source from the content-addressed store, hash-verified

822}
823
824export function useSearchMenuState(options: SearchMenuStateOptions): SearchMenuState {
825 const {pages, currentPage, initialSearchValue = '', initialTag} = options;
826
827 // Library state
828 const currentLibrary = getLibraryFromPage(currentPage);
829 const [selectedLibrary, setSelectedLibrary] = useState<Library>(currentLibrary);
830 const orderedLibraries = useMemo(() => getOrderedLibraries(currentPage), [currentPage]);
831
832 // Search state
833 const [searchValue, setSearchValue] = useState(initialSearchValue);
834
835 // Build sections for the selected library
836 const {sections, filteredSections} = useLibrarySections(
837 pages || [],
838 selectedLibrary,
839 searchValue
840 );
841
842 // Section and resource tags
843 const sectionTags = useMemo(() => sections.map(s => ({id: s.id, name: s.name})), [sections]);
844 const resourceTags = useMemo(() => getResourceTags(selectedLibrary), [selectedLibrary]);
845 const resourceTagIds = useMemo(() => resourceTags.map(t => t.id), [resourceTags]);
846
847 // Compute initial selected section
848 const initialSelectedSection = useMemo(() => {
849 const currentSection = sections.find(s => s.children.some(c => c.href === currentPage.url));
850 return (
851 initialTag ||
852 currentSection?.id ||
853 currentPage.exports?.section?.toLowerCase() ||
854 'components'
855 );
856 }, [initialTag, currentPage, sections]);
857
858 // Tag selection
859 const [selectedTagId, setSelectedTagId] = useSearchTagSelection(
860 searchValue,
861 sectionTags,
862 resourceTags,
863 initialSelectedSection,
864 options.isOpen
865 );
866
867 // Section tags for display (includes "All" when searching)
868 const sectionTagsForDisplay = useSectionTagsForDisplay(
869 sections,
870 searchValue,
871 selectedTagId,
872 resourceTagIds,
873 options.isOpen
874 );
875
876 // Icons
877 const filteredIcons = useFilteredIcons(searchValue);
878 const iconFilter = useIconFilter();
879 const isIconsSelected = selectedTagId === 'icons';
880
881 // Handler for tag selection change (works with TagGroup's onSelectionChange)

Callers 2

SearchMenuFunction · 0.90
MobileNavFunction · 0.90

Calls 9

getLibraryFromPageFunction · 0.90
useIconFilterFunction · 0.90
getOrderedLibrariesFunction · 0.85
useLibrarySectionsFunction · 0.85
getResourceTagsFunction · 0.85
useSearchTagSelectionFunction · 0.85
useSectionTagsForDisplayFunction · 0.85
useFilteredIconsFunction · 0.85
getItemsForSectionFunction · 0.85

Tested by

no test coverage detected