@headband/react 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +520 -0
- package/dist/index.d.mts +416 -0
- package/dist/index.d.ts +416 -0
- package/dist/index.js +1124 -0
- package/dist/index.mjs +1092 -0
- package/package.json +61 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,416 @@
|
|
|
1
|
+
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
2
|
+
import { ReactNode, ComponentType } from 'react';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Headband search client.
|
|
6
|
+
*
|
|
7
|
+
* Makes requests to the Headband `/v1/search` and `/v1/multi-search` endpoints.
|
|
8
|
+
*/
|
|
9
|
+
interface SearchParams {
|
|
10
|
+
filter?: string;
|
|
11
|
+
facets?: string[];
|
|
12
|
+
sort?: string[];
|
|
13
|
+
limit?: number;
|
|
14
|
+
offset?: number;
|
|
15
|
+
attributesToHighlight?: string[];
|
|
16
|
+
attributesToRetrieve?: string[];
|
|
17
|
+
attributesToCrop?: string[];
|
|
18
|
+
cropLength?: number;
|
|
19
|
+
showMatchesPosition?: boolean;
|
|
20
|
+
matchingStrategy?: "all" | "last" | "frequency";
|
|
21
|
+
}
|
|
22
|
+
interface SearchResponse {
|
|
23
|
+
hits: Record<string, unknown>[];
|
|
24
|
+
estimatedTotalHits?: number;
|
|
25
|
+
totalHits?: number;
|
|
26
|
+
processingTimeMs: number;
|
|
27
|
+
query: string;
|
|
28
|
+
facetDistribution?: Record<string, Record<string, number>>;
|
|
29
|
+
facetStats?: Record<string, {
|
|
30
|
+
min: number;
|
|
31
|
+
max: number;
|
|
32
|
+
}>;
|
|
33
|
+
}
|
|
34
|
+
interface MultiSearchQuery {
|
|
35
|
+
indexUid: string;
|
|
36
|
+
q: string;
|
|
37
|
+
filter?: string;
|
|
38
|
+
facets?: string[];
|
|
39
|
+
sort?: string[];
|
|
40
|
+
limit?: number;
|
|
41
|
+
offset?: number;
|
|
42
|
+
attributesToHighlight?: string[];
|
|
43
|
+
attributesToRetrieve?: string[];
|
|
44
|
+
}
|
|
45
|
+
interface MultiSearchResponse {
|
|
46
|
+
results: Array<SearchResponse & {
|
|
47
|
+
indexUid: string;
|
|
48
|
+
}>;
|
|
49
|
+
}
|
|
50
|
+
interface HeadbandClient {
|
|
51
|
+
search(index: string, query: string, params?: SearchParams): Promise<SearchResponse>;
|
|
52
|
+
multiSearch(queries: MultiSearchQuery[]): Promise<MultiSearchResponse>;
|
|
53
|
+
}
|
|
54
|
+
declare class HeadbandError extends Error {
|
|
55
|
+
readonly status: number;
|
|
56
|
+
readonly code: string | undefined;
|
|
57
|
+
constructor(message: string, status: number, code?: string);
|
|
58
|
+
}
|
|
59
|
+
declare function headband(host: string, apiKey: string): HeadbandClient;
|
|
60
|
+
|
|
61
|
+
/** Map of attribute -> selected values for facet refinements. */
|
|
62
|
+
type RefinementMap = Map<string, Set<string>>;
|
|
63
|
+
/** Numeric range filter: attribute -> { min?, max? }. */
|
|
64
|
+
type RangeMap = Map<string, {
|
|
65
|
+
min?: number;
|
|
66
|
+
max?: number;
|
|
67
|
+
}>;
|
|
68
|
+
interface SearchState {
|
|
69
|
+
query: string;
|
|
70
|
+
results: SearchResponse | null;
|
|
71
|
+
isLoading: boolean;
|
|
72
|
+
error: Error | null;
|
|
73
|
+
refinements: RefinementMap;
|
|
74
|
+
ranges: RangeMap;
|
|
75
|
+
sortBy: string | null;
|
|
76
|
+
page: number;
|
|
77
|
+
hitsPerPage: number;
|
|
78
|
+
facets: Set<string>;
|
|
79
|
+
}
|
|
80
|
+
interface SearchActions {
|
|
81
|
+
setQuery: (query: string) => void;
|
|
82
|
+
setPage: (page: number) => void;
|
|
83
|
+
setSortBy: (sort: string | null) => void;
|
|
84
|
+
setHitsPerPage: (n: number) => void;
|
|
85
|
+
toggleRefinement: (attribute: string, value: string) => void;
|
|
86
|
+
setRange: (attribute: string, range: {
|
|
87
|
+
min?: number;
|
|
88
|
+
max?: number;
|
|
89
|
+
}) => void;
|
|
90
|
+
clearRange: (attribute: string) => void;
|
|
91
|
+
registerFacet: (attribute: string) => void;
|
|
92
|
+
unregisterFacet: (attribute: string) => void;
|
|
93
|
+
refresh: () => void;
|
|
94
|
+
}
|
|
95
|
+
interface HeadbandContextValue {
|
|
96
|
+
client: HeadbandClient;
|
|
97
|
+
index: string;
|
|
98
|
+
state: SearchState;
|
|
99
|
+
actions: SearchActions;
|
|
100
|
+
}
|
|
101
|
+
declare function useHeadbandContext(): HeadbandContextValue;
|
|
102
|
+
interface HeadbandProviderProps {
|
|
103
|
+
client: HeadbandClient;
|
|
104
|
+
index: string;
|
|
105
|
+
initialQuery?: string;
|
|
106
|
+
children: ReactNode;
|
|
107
|
+
}
|
|
108
|
+
declare function HeadbandProvider({ client, index, initialQuery, children, }: HeadbandProviderProps): react_jsx_runtime.JSX.Element;
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Core hook that exposes the full search state and actions.
|
|
112
|
+
*
|
|
113
|
+
* Most consumers should prefer the specialised hooks (useSearchBox, useHits,
|
|
114
|
+
* etc.) — this hook is useful when you need direct access to everything.
|
|
115
|
+
*/
|
|
116
|
+
declare function useSearch(): SearchState & SearchActions;
|
|
117
|
+
|
|
118
|
+
interface UseSearchBoxReturn {
|
|
119
|
+
/** Current query string. */
|
|
120
|
+
query: string;
|
|
121
|
+
/** Update the query — resets pagination and triggers a debounced search. */
|
|
122
|
+
setQuery: (query: string) => void;
|
|
123
|
+
/** Clear the query to an empty string. */
|
|
124
|
+
clear: () => void;
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Binds to the search input. Returns the current query plus setters.
|
|
128
|
+
*/
|
|
129
|
+
declare function useSearchBox(): UseSearchBoxReturn;
|
|
130
|
+
|
|
131
|
+
interface Hit<T = Record<string, unknown>> {
|
|
132
|
+
/** The original document fields. */
|
|
133
|
+
[key: string]: unknown;
|
|
134
|
+
/** Highlighted / formatted version of fields (from attributesToHighlight). */
|
|
135
|
+
_formatted?: Record<string, unknown>;
|
|
136
|
+
/** Typed access helper — same as spreading the hit. */
|
|
137
|
+
__data: T;
|
|
138
|
+
}
|
|
139
|
+
interface UseHitsReturn<T = Record<string, unknown>> {
|
|
140
|
+
hits: Hit<T>[];
|
|
141
|
+
isLoading: boolean;
|
|
142
|
+
error: Error | null;
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* Returns the current search hits with loading / error state.
|
|
146
|
+
*/
|
|
147
|
+
declare function useHits<T extends Record<string, unknown> = Record<string, unknown>>(): UseHitsReturn<T>;
|
|
148
|
+
|
|
149
|
+
interface RefinementListItem {
|
|
150
|
+
value: string;
|
|
151
|
+
count: number;
|
|
152
|
+
isRefined: boolean;
|
|
153
|
+
}
|
|
154
|
+
interface UseRefinementListProps {
|
|
155
|
+
/** The facet attribute to refine on (e.g. "genres"). */
|
|
156
|
+
attribute: string;
|
|
157
|
+
/** Maximum number of facet values to display. Default: 20. */
|
|
158
|
+
limit?: number;
|
|
159
|
+
/** Sort facet values: "count" (desc) or "alpha" (asc). Default: "count". */
|
|
160
|
+
sortBy?: "count" | "alpha";
|
|
161
|
+
}
|
|
162
|
+
interface UseRefinementListReturn {
|
|
163
|
+
items: RefinementListItem[];
|
|
164
|
+
/** Toggle a facet value on/off. */
|
|
165
|
+
refine: (value: string) => void;
|
|
166
|
+
/** Whether there are facet values available to toggle. */
|
|
167
|
+
canToggle: boolean;
|
|
168
|
+
isLoading: boolean;
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* Provides facet filtering for a given attribute.
|
|
172
|
+
*
|
|
173
|
+
* Registers the attribute as a requested facet so the provider includes it in
|
|
174
|
+
* search requests automatically.
|
|
175
|
+
*/
|
|
176
|
+
declare function useRefinementList({ attribute, limit, sortBy: sortOrder, }: UseRefinementListProps): UseRefinementListReturn;
|
|
177
|
+
|
|
178
|
+
interface UsePaginationReturn {
|
|
179
|
+
currentPage: number;
|
|
180
|
+
totalPages: number;
|
|
181
|
+
totalHits: number;
|
|
182
|
+
setPage: (page: number) => void;
|
|
183
|
+
canPrevious: boolean;
|
|
184
|
+
canNext: boolean;
|
|
185
|
+
/** Array of page numbers useful for rendering a page list. */
|
|
186
|
+
pages: number[];
|
|
187
|
+
}
|
|
188
|
+
/**
|
|
189
|
+
* Pagination state derived from the current search results.
|
|
190
|
+
*
|
|
191
|
+
* Pages are 0-indexed internally but the `pages` array is 0-indexed as well
|
|
192
|
+
* for simplicity. The consumer can add 1 for display purposes.
|
|
193
|
+
*/
|
|
194
|
+
declare function usePagination(): UsePaginationReturn;
|
|
195
|
+
|
|
196
|
+
interface UseStatsReturn {
|
|
197
|
+
totalHits: number;
|
|
198
|
+
processingTimeMs: number;
|
|
199
|
+
query: string;
|
|
200
|
+
isLoading: boolean;
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* Returns summary stats about the current search results.
|
|
204
|
+
*/
|
|
205
|
+
declare function useStats(): UseStatsReturn;
|
|
206
|
+
|
|
207
|
+
interface SortByItem {
|
|
208
|
+
/** The sort value sent to the API (e.g. "rating:desc"). */
|
|
209
|
+
value: string;
|
|
210
|
+
/** Human-readable label (e.g. "Rating (high to low)"). */
|
|
211
|
+
label: string;
|
|
212
|
+
}
|
|
213
|
+
interface UseSortByProps {
|
|
214
|
+
items: SortByItem[];
|
|
215
|
+
}
|
|
216
|
+
interface UseSortByReturn {
|
|
217
|
+
/** Currently active sort value, or null for relevance. */
|
|
218
|
+
currentSort: string | null;
|
|
219
|
+
/** Set the active sort. Pass null to reset to default relevance. */
|
|
220
|
+
setSort: (value: string | null) => void;
|
|
221
|
+
/** The items list with an `isSelected` flag added. */
|
|
222
|
+
items: Array<SortByItem & {
|
|
223
|
+
isSelected: boolean;
|
|
224
|
+
}>;
|
|
225
|
+
}
|
|
226
|
+
/**
|
|
227
|
+
* Sort-by dropdown state.
|
|
228
|
+
*/
|
|
229
|
+
declare function useSortBy({ items }: UseSortByProps): UseSortByReturn;
|
|
230
|
+
|
|
231
|
+
interface UseRangeProps {
|
|
232
|
+
/** The numeric attribute to filter on (e.g. "price"). */
|
|
233
|
+
attribute: string;
|
|
234
|
+
}
|
|
235
|
+
interface UseRangeReturn {
|
|
236
|
+
/** Current min bound, or undefined if not set. */
|
|
237
|
+
min: number | undefined;
|
|
238
|
+
/** Current max bound, or undefined if not set. */
|
|
239
|
+
max: number | undefined;
|
|
240
|
+
/** Facet stats from the engine (overall min/max for this attribute). */
|
|
241
|
+
stats: {
|
|
242
|
+
min: number;
|
|
243
|
+
max: number;
|
|
244
|
+
} | undefined;
|
|
245
|
+
/** Set the range filter. Omit min/max to leave that side unbounded. */
|
|
246
|
+
setRange: (range: {
|
|
247
|
+
min?: number;
|
|
248
|
+
max?: number;
|
|
249
|
+
}) => void;
|
|
250
|
+
/** Remove the range filter entirely. */
|
|
251
|
+
clear: () => void;
|
|
252
|
+
isLoading: boolean;
|
|
253
|
+
}
|
|
254
|
+
/**
|
|
255
|
+
* Numeric range filter for a given attribute.
|
|
256
|
+
*/
|
|
257
|
+
declare function useRange({ attribute }: UseRangeProps): UseRangeReturn;
|
|
258
|
+
|
|
259
|
+
interface UseHighlightProps {
|
|
260
|
+
/** The hit object from useHits. */
|
|
261
|
+
hit: Record<string, unknown>;
|
|
262
|
+
/** The attribute to highlight (e.g. "title"). */
|
|
263
|
+
attribute: string;
|
|
264
|
+
}
|
|
265
|
+
interface UseHighlightReturn {
|
|
266
|
+
/** The highlighted string with <em> tags, or the raw value if no highlight. */
|
|
267
|
+
value: string;
|
|
268
|
+
/** Whether the value actually contains highlight marks. */
|
|
269
|
+
highlighted: boolean;
|
|
270
|
+
}
|
|
271
|
+
/**
|
|
272
|
+
* Extracts the highlighted value for a specific attribute from a hit.
|
|
273
|
+
*/
|
|
274
|
+
declare function useHighlight({ hit, attribute, }: UseHighlightProps): UseHighlightReturn;
|
|
275
|
+
|
|
276
|
+
interface SearchBoxProps {
|
|
277
|
+
/** Placeholder text. */
|
|
278
|
+
placeholder?: string;
|
|
279
|
+
/** Additional CSS class names. */
|
|
280
|
+
className?: string;
|
|
281
|
+
/** Auto-focus the input on mount. */
|
|
282
|
+
autoFocus?: boolean;
|
|
283
|
+
/** Called on every query change (after internal state updates). */
|
|
284
|
+
onQueryChange?: (query: string) => void;
|
|
285
|
+
/** Whether to render the default Tailwind styles. Set false for unstyled. */
|
|
286
|
+
styled?: boolean;
|
|
287
|
+
}
|
|
288
|
+
/**
|
|
289
|
+
* Search input component with built-in clear button.
|
|
290
|
+
*/
|
|
291
|
+
declare function SearchBox({ placeholder, className, autoFocus, onQueryChange, styled, }: SearchBoxProps): react_jsx_runtime.JSX.Element;
|
|
292
|
+
|
|
293
|
+
interface HitsProps<T extends Record<string, unknown> = Record<string, unknown>> {
|
|
294
|
+
/** Custom component to render each hit. Receives the hit as a prop. */
|
|
295
|
+
hitComponent?: ComponentType<{
|
|
296
|
+
hit: Hit<T>;
|
|
297
|
+
}>;
|
|
298
|
+
/** Additional CSS class names for the list container. */
|
|
299
|
+
className?: string;
|
|
300
|
+
/** Content to show when there are no results. */
|
|
301
|
+
emptyComponent?: ReactNode;
|
|
302
|
+
/** Whether to render the default Tailwind styles. Set false for unstyled. */
|
|
303
|
+
styled?: boolean;
|
|
304
|
+
}
|
|
305
|
+
/**
|
|
306
|
+
* Renders search result hits.
|
|
307
|
+
*/
|
|
308
|
+
declare function Hits<T extends Record<string, unknown> = Record<string, unknown>>({ hitComponent, className, emptyComponent, styled, }: HitsProps<T>): react_jsx_runtime.JSX.Element;
|
|
309
|
+
|
|
310
|
+
interface RefinementListProps extends UseRefinementListProps {
|
|
311
|
+
/** Additional CSS class names. */
|
|
312
|
+
className?: string;
|
|
313
|
+
/** Whether to render the default Tailwind styles. Set false for unstyled. */
|
|
314
|
+
styled?: boolean;
|
|
315
|
+
/** Optional heading displayed above the list. */
|
|
316
|
+
header?: string;
|
|
317
|
+
}
|
|
318
|
+
/**
|
|
319
|
+
* Checkbox list of facet values with counts.
|
|
320
|
+
*/
|
|
321
|
+
declare function RefinementList({ attribute, limit, sortBy, className, styled, header, }: RefinementListProps): react_jsx_runtime.JSX.Element;
|
|
322
|
+
|
|
323
|
+
interface PaginationProps {
|
|
324
|
+
/** Additional CSS class names. */
|
|
325
|
+
className?: string;
|
|
326
|
+
/** Whether to render the default Tailwind styles. Set false for unstyled. */
|
|
327
|
+
styled?: boolean;
|
|
328
|
+
}
|
|
329
|
+
/**
|
|
330
|
+
* Page navigation with Previous / Next and numbered page buttons.
|
|
331
|
+
*/
|
|
332
|
+
declare function Pagination({ className, styled }: PaginationProps): react_jsx_runtime.JSX.Element | null;
|
|
333
|
+
|
|
334
|
+
interface StatsProps {
|
|
335
|
+
/** Additional CSS class names. */
|
|
336
|
+
className?: string;
|
|
337
|
+
/** Whether to render the default Tailwind styles. Set false for unstyled. */
|
|
338
|
+
styled?: boolean;
|
|
339
|
+
/** Custom render function. */
|
|
340
|
+
render?: (stats: {
|
|
341
|
+
totalHits: number;
|
|
342
|
+
processingTimeMs: number;
|
|
343
|
+
query: string;
|
|
344
|
+
}) => string;
|
|
345
|
+
}
|
|
346
|
+
/**
|
|
347
|
+
* Displays "X results in Yms".
|
|
348
|
+
*/
|
|
349
|
+
declare function Stats({ className, styled, render }: StatsProps): react_jsx_runtime.JSX.Element | null;
|
|
350
|
+
|
|
351
|
+
interface SortByProps {
|
|
352
|
+
/** Sort options to display. */
|
|
353
|
+
items: SortByItem[];
|
|
354
|
+
/** Additional CSS class names. */
|
|
355
|
+
className?: string;
|
|
356
|
+
/** Whether to render the default Tailwind styles. Set false for unstyled. */
|
|
357
|
+
styled?: boolean;
|
|
358
|
+
/** Label for the sort select (for accessibility). */
|
|
359
|
+
label?: string;
|
|
360
|
+
}
|
|
361
|
+
/**
|
|
362
|
+
* Sort-by select dropdown.
|
|
363
|
+
*/
|
|
364
|
+
declare function SortBy({ items: itemsProp, className, styled, label, }: SortByProps): react_jsx_runtime.JSX.Element;
|
|
365
|
+
|
|
366
|
+
interface HighlightProps {
|
|
367
|
+
/** The hit object from useHits. */
|
|
368
|
+
hit: Record<string, unknown>;
|
|
369
|
+
/** The attribute to highlight (e.g. "title"). */
|
|
370
|
+
attribute: string;
|
|
371
|
+
/** Additional CSS class names. */
|
|
372
|
+
className?: string;
|
|
373
|
+
/** Whether to render the default Tailwind styles. Set false for unstyled. */
|
|
374
|
+
styled?: boolean;
|
|
375
|
+
/** Tag name for the highlight wrapper. Defaults to "span". */
|
|
376
|
+
tagName?: keyof HTMLElementTagNameMap;
|
|
377
|
+
}
|
|
378
|
+
/**
|
|
379
|
+
* Renders highlighted text for a hit attribute.
|
|
380
|
+
*
|
|
381
|
+
* Strips all HTML except `<em>` tags so it is safe to use with
|
|
382
|
+
* dangerouslySetInnerHTML.
|
|
383
|
+
*/
|
|
384
|
+
declare function Highlight({ hit, attribute, className, styled, tagName: Tag, }: HighlightProps): react_jsx_runtime.JSX.Element;
|
|
385
|
+
|
|
386
|
+
interface RangeInputProps extends UseRangeProps {
|
|
387
|
+
/** Additional CSS class names. */
|
|
388
|
+
className?: string;
|
|
389
|
+
/** Whether to render the default Tailwind styles. Set false for unstyled. */
|
|
390
|
+
styled?: boolean;
|
|
391
|
+
/** Optional heading above the inputs. */
|
|
392
|
+
header?: string;
|
|
393
|
+
/** Placeholder for the min input. */
|
|
394
|
+
minPlaceholder?: string;
|
|
395
|
+
/** Placeholder for the max input. */
|
|
396
|
+
maxPlaceholder?: string;
|
|
397
|
+
}
|
|
398
|
+
/**
|
|
399
|
+
* Min/max number inputs for numeric range filtering.
|
|
400
|
+
*/
|
|
401
|
+
declare function RangeInput({ attribute, className, styled, header, minPlaceholder, maxPlaceholder, }: RangeInputProps): react_jsx_runtime.JSX.Element;
|
|
402
|
+
|
|
403
|
+
interface PoweredByProps {
|
|
404
|
+
/** Additional CSS class names. */
|
|
405
|
+
className?: string;
|
|
406
|
+
/** Whether to render the default Tailwind styles. Set false for unstyled. */
|
|
407
|
+
styled?: boolean;
|
|
408
|
+
/** Override the link URL. Defaults to "https://headband.dev". */
|
|
409
|
+
href?: string;
|
|
410
|
+
}
|
|
411
|
+
/**
|
|
412
|
+
* "Powered by Headband" badge with link.
|
|
413
|
+
*/
|
|
414
|
+
declare function PoweredBy({ className, styled, href, }: PoweredByProps): react_jsx_runtime.JSX.Element;
|
|
415
|
+
|
|
416
|
+
export { type HeadbandClient, type HeadbandContextValue, HeadbandError, HeadbandProvider, type HeadbandProviderProps, Highlight, type HighlightProps, type Hit, Hits, type HitsProps, type MultiSearchQuery, type MultiSearchResponse, Pagination, type PaginationProps, PoweredBy, type PoweredByProps, RangeInput, type RangeInputProps, type RangeMap, RefinementList, type RefinementListItem, type RefinementListProps, type RefinementMap, type SearchActions, SearchBox, type SearchBoxProps, type SearchParams, type SearchResponse, type SearchState, SortBy, type SortByItem, type SortByProps, Stats, type StatsProps, type UseHighlightProps, type UseHighlightReturn, type UseHitsReturn, type UsePaginationReturn, type UseRangeProps, type UseRangeReturn, type UseRefinementListProps, type UseRefinementListReturn, type UseSearchBoxReturn, type UseSortByProps, type UseSortByReturn, type UseStatsReturn, headband, useHeadbandContext, useHighlight, useHits, usePagination, useRange, useRefinementList, useSearch, useSearchBox, useSortBy, useStats };
|