Understanding DocSearch Packages
DocSearch is built as a modular system with several packages:@docsearch/js
Vanilla JavaScript wrapper using Preact
@docsearch/react
React components and hooks
@docsearch/core
Framework-agnostic core logic
@docsearch/css
Styles and CSS variables
Architecture Overview
DocSearch is built on top of Algolia’s Autocomplete library:Building a Framework Integration
Approach 1: Wrap the JavaScript Package
The simplest approach is to wrap@docsearch/js:
Approach 2: Use React Components Directly
Many frameworks support React components. Example for Solid.js:DocSearch.tsx
Approach 3: Build from Core
For complete control, use@docsearch/core directly:
custom-docsearch.ts
Custom Search Client
You can customize the Algolia search client:custom-client.ts
Custom Result Transformations
Transform search results before rendering:Custom Navigation
Control how DocSearch navigates:Custom Hit Component
Customize how individual results are rendered:Custom Results Footer
Add a custom footer to the results:TypeScript Integration
Create type-safe wrappers:docsearch-wrapper.ts
Server-Side Rendering
Handle SSR in your custom integration:docsearch-ssr.ts
Analytics Integration
Track search usage:A/B Testing
Implement A/B testing for search:Testing Your Integration
Write tests for your custom integration:docsearch.test.ts
Best Practices
1. Handle Lifecycle Properly
1. Handle Lifecycle Properly
Always clean up DocSearch instances when components unmount:
2. Optimize for Performance
2. Optimize for Performance
- Lazy load DocSearch only when needed
- Use the
onTouchStart,onFocus, andonMouseOvercallbacks to preload - Implement debouncing for search queries
3. Type Safety
3. Type Safety
Use TypeScript for better developer experience:
4. Error Handling
4. Error Handling
Handle errors gracefully:
5. Accessibility
5. Accessibility
Ensure your custom integration maintains accessibility:
- Keep keyboard navigation working
- Preserve ARIA attributes
- Test with screen readers
Examples by Framework
Vue 3
See the Vue 3 example above for wrapping
@docsearch/jsSvelte
Similar to Vue, use
onMount and onDestroy lifecycle hooksAngular
Create a directive or component that initializes DocSearch in
ngAfterViewInitNext.js
Use dynamic imports to avoid SSR issues
Next Steps
API Reference
Complete API documentation
Styling
Customize appearance
Core Package
Use @docsearch/core directly
Contributing
Contribute your integration
