77 useState ,
88} from "react" ;
99import { createPortal } from "react-dom" ;
10- import { unstable_useRoute as useRoute } from "react-router" ;
10+ import { useMatches } from "react-router" ;
1111import type { DocSearchProps } from "@docsearch/react" ;
1212import {
1313 DocSearchModal as OriginalDocSearchModal ,
@@ -56,9 +56,7 @@ export function DocSearch({ children }: { children: React.ReactNode }) {
5656 onOpen,
5757 onClose,
5858 onInput,
59- // React 19 types this ref as HTMLButtonElement | null, but DocSearch's
60- // hook still expects the pre-React-19 RefObject<HTMLButtonElement>.
61- searchButtonRef : searchButtonRef as React . RefObject < HTMLButtonElement > ,
59+ searchButtonRef,
6260 } ) ;
6361
6462 const contextValue = useMemo (
@@ -69,12 +67,15 @@ export function DocSearch({ children }: { children: React.ReactNode }) {
6967 [ onOpen , searchButtonRef ] ,
7068 ) ;
7169
72- let docsRoute = useRoute ( "docs" ) ;
73- let v6IndexRoute = useRoute ( "v6-index-layout" ) ;
74- let header =
75- docsRoute ?. loaderData ?. header ?? v6IndexRoute ?. loaderData ?. header ;
76-
77- let version = header ?. docSearchVersion ;
70+ let docSearchVersion = useMatches ( )
71+ . map ( ( match ) => match . loaderData )
72+ . find (
73+ ( loaderData ) : loaderData is { docSearchVersion : string } =>
74+ typeof loaderData === "object" &&
75+ loaderData !== null &&
76+ "docSearchVersion" in loaderData &&
77+ typeof loaderData . docSearchVersion === "string" ,
78+ ) ?. docSearchVersion ;
7879
7980 return (
8081 < DocSearchContext value = { contextValue } >
@@ -87,9 +88,11 @@ export function DocSearch({ children }: { children: React.ReactNode }) {
8788 onClose = { onClose }
8889 // NOTE: to use the facet for search, it has to be set in the algolia dashboard:
8990 // "Configuration" > "Filtering and Faceting" > "Facets"
90- searchParameters = { {
91- ...( version ? { facetFilters : [ `version:${ version } ` ] } : { } ) ,
92- } }
91+ searchParameters = {
92+ docSearchVersion
93+ ? { facetFilters : [ `version:${ docSearchVersion } ` ] }
94+ : undefined
95+ }
9396 { ...docSearchProps }
9497 /> ,
9598 document . body ,
0 commit comments