feat: add possibility to access search client via provider
Provide SearchClientContext for type-safe, flexible search client access
Background / Motivation
Currently, SearchkitWrapper initializes the Algolia search client internally and passes it directly to <InstantSearch>. While this works for basic search functionality, it limits flexibility for advanced use cases, namely that components deeper in the tree cannot easily access the underlying search client (ie https://support.algolia.com/hc/en-us/articles/4415033762449-How-to-show-all-facet-values-including-the-ones-with-no-hits)
To address these limitations, we introduce a context-based approach.
Changes
-
Add
SearchClientContext- Created a React context (
SearchClientContext) to provide the search client throughout the component tree. - Introduced a
useSearchClienthook to safely access the client in child components.
- Created a React context (
-
Update
SearchkitWrapper- Wrap
<InstantSearch>withSearchClientContext.Provider. - Typed
searchClientasSearchClientfor full TypeScript support. - Ensures
onlyFetchWithQuerylogic remains functional and type-safe.
- Wrap
-
Type improvements
- Added
SearchClienttype intypes/General.tswith full typing forsearchForFacetValues. - Stronger typing improves DX and reduces potential runtime errors when using the search client in custom components.
- Added
-
Library exports
- Exported
useSearchClientfrom the library entry point (src/index.ts) for external usage.
- Exported
Impact
- Components can consume the search client via
useSearchClient. - Improves TypeScript safety for advanced search operations.
- Lays the foundation for more flexible, optimized search interactions (e.g., conditional fetching, custom facet queries).
Edited by Pandu Supriyono