@wherabouts/react 0.1.1 → 0.2.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/dist/index.d.cts CHANGED
@@ -1,18 +1,156 @@
1
- import { AddressSuggestion, WheraboutsClient, ReverseGeocodeAddress, ZoneRecord } from '@wherabouts/sdk';
1
+ import { KeyboardEvent, MouseEvent } from 'react';
2
+ import { AddressSuggestion, WheraboutsClient, ReverseGeocodeAddress, DirectionsParams, DirectionsResponse, IsochroneParams, IsochroneResponse, MatrixParams, MatrixResponse, ZoneRecord } from '@wherabouts/sdk';
2
3
 
4
+ /**
5
+ * Minimal structural view of the Web Storage API (sessionStorage/localStorage).
6
+ * Declared locally so the package type-checks with no DOM lib.
7
+ */
8
+ interface StorageLike {
9
+ getItem(key: string): string | null;
10
+ removeItem(key: string): void;
11
+ setItem(key: string, value: string): void;
12
+ }
13
+
14
+ /**
15
+ * Headless WAI-ARIA combobox helper for autocomplete dropdowns. Provides the
16
+ * keyboard state machine (↑/↓/Home/End/Enter/Esc, wrapping) and ARIA prop
17
+ * getters so an input + listbox is accessible by default. Bring your own
18
+ * markup and data; this hook owns only the open/active-option state.
19
+ *
20
+ * @see https://www.w3.org/WAI/ARIA/apg/patterns/combobox/
21
+ */
22
+ interface ComboboxState {
23
+ /** Index of the visually-active option, or -1 when none is active. */
24
+ activeIndex: number;
25
+ isOpen: boolean;
26
+ }
27
+ type ComboboxAction = {
28
+ type: "next";
29
+ count: number;
30
+ } | {
31
+ type: "prev";
32
+ count: number;
33
+ } | {
34
+ type: "first";
35
+ count: number;
36
+ } | {
37
+ type: "last";
38
+ count: number;
39
+ } | {
40
+ type: "set";
41
+ index: number;
42
+ } | {
43
+ type: "open";
44
+ } | {
45
+ type: "close";
46
+ } | {
47
+ type: "select";
48
+ } | {
49
+ type: "reset";
50
+ };
51
+ declare const INITIAL_COMBOBOX_STATE: ComboboxState;
52
+ /** Pure transition function for the combobox keyboard state machine. */
53
+ declare function comboboxReducer(state: ComboboxState, action: ComboboxAction): ComboboxState;
54
+ /** Map a keyboard key to a combobox action, or null when it is irrelevant. */
55
+ declare function keyToAction(key: string, count: number): ComboboxAction | null;
56
+ interface ComboboxInputProps {
57
+ "aria-activedescendant": string | undefined;
58
+ "aria-autocomplete": "list";
59
+ "aria-controls": string;
60
+ "aria-expanded": boolean;
61
+ role: "combobox";
62
+ }
63
+ interface ComboboxListboxProps {
64
+ id: string;
65
+ role: "listbox";
66
+ }
67
+ interface ComboboxItemProps {
68
+ "aria-selected": boolean;
69
+ id: string;
70
+ role: "option";
71
+ }
72
+ /** Build the ARIA attributes for the combobox input — pure. */
73
+ declare function buildInputProps(id: string, state: ComboboxState): ComboboxInputProps;
74
+ /** Build the ARIA attributes for the listbox container — pure. */
75
+ declare function buildListboxProps(id: string): ComboboxListboxProps;
76
+ /** Build the ARIA attributes for an option at `index` — pure. */
77
+ declare function buildItemProps(id: string, index: number, state: ComboboxState): ComboboxItemProps;
78
+ interface UseComboboxOptions {
79
+ /** Number of options currently rendered (drives wrapping/bounds). */
80
+ count: number;
81
+ /** Stable id root; option/listbox ids derive from it. */
82
+ id: string;
83
+ /** Called when an option is chosen via Enter or pointer. */
84
+ onSelect?: (index: number) => void;
85
+ }
86
+ interface UseComboboxResult {
87
+ activeIndex: number;
88
+ close: () => void;
89
+ getInputProps: () => ComboboxInputProps & {
90
+ onBlur: () => void;
91
+ onFocus: () => void;
92
+ onKeyDown: (event: KeyboardEvent) => void;
93
+ };
94
+ getItemProps: (index: number) => ComboboxItemProps & {
95
+ onMouseDown: (event: MouseEvent) => void;
96
+ onMouseEnter: () => void;
97
+ };
98
+ getListboxProps: () => ComboboxListboxProps;
99
+ isOpen: boolean;
100
+ open: () => void;
101
+ reset: () => void;
102
+ setActiveIndex: (index: number) => void;
103
+ }
104
+ declare function useCombobox(options: UseComboboxOptions): UseComboboxResult;
105
+
106
+ type AutocompleteStatus = "idle" | "loading" | "success" | "empty" | "error";
107
+ /** Opt-in client-side cache for repeated/back-and-forth queries. */
108
+ interface AutocompleteCacheConfig {
109
+ /** A `sessionStorage`-like store. Entries are namespaced under `wh:ac:`. */
110
+ storage: StorageLike;
111
+ /** Entry lifetime in ms (default 60_000). */
112
+ ttlMs?: number;
113
+ }
3
114
  interface UseAutocompleteOptions {
115
+ cache?: AutocompleteCacheConfig;
4
116
  country?: string;
5
117
  debounceMs?: number;
118
+ /** Keep the previous results visible while a new search runs / on error. */
119
+ keepPreviousData?: boolean;
120
+ /** Latitude for proximity boosting (pair with `lng`). */
121
+ lat?: number;
6
122
  limit?: number;
123
+ /** Longitude for proximity boosting (pair with `lat`). */
124
+ lng?: number;
125
+ /** Minimum trimmed query length before a request fires (default 2). */
126
+ minLength?: number;
127
+ /** Groups keystrokes into one billable session; see `newSessionToken()`. */
128
+ sessionToken?: string;
7
129
  state?: string;
8
130
  }
9
131
  interface UseAutocompleteResult {
10
132
  error: Error | null;
11
133
  loading: boolean;
12
134
  query: string;
135
+ /** True when the last request was rejected with HTTP 429. */
136
+ rateLimited: boolean;
137
+ /** Clear the query, results, and any in-flight request. */
138
+ reset: () => void;
13
139
  results: AddressSuggestion[];
14
140
  setQuery: (q: string) => void;
141
+ status: AutocompleteStatus;
15
142
  }
143
+ /**
144
+ * Derive a single coarse status from the hook's raw flags. Exported for
145
+ * unit-testing and for consumers who prefer a state machine to booleans.
146
+ */
147
+ declare function deriveStatus(s: {
148
+ error: Error | null;
149
+ loading: boolean;
150
+ minLength: number;
151
+ query: string;
152
+ results: AddressSuggestion[];
153
+ }): AutocompleteStatus;
16
154
  declare function useAutocomplete(client: WheraboutsClient, options?: UseAutocompleteOptions): UseAutocompleteResult;
17
155
 
18
156
  interface LatLng {
@@ -27,6 +165,18 @@ interface UseReverseGeocodeResult {
27
165
  }
28
166
  declare function useReverseGeocode(client: WheraboutsClient, coords: LatLng | null): UseReverseGeocodeResult;
29
167
 
168
+ interface RoutingResourceResult<T> {
169
+ data: T | null;
170
+ error: Error | null;
171
+ loading: boolean;
172
+ }
173
+ /** Fetch turn-by-turn directions; pass `null` to stay idle. */
174
+ declare function useDirections(client: WheraboutsClient, params: DirectionsParams | null): RoutingResourceResult<DirectionsResponse>;
175
+ /** Fetch a duration/distance matrix; pass `null` to stay idle. */
176
+ declare function useMatrix(client: WheraboutsClient, params: MatrixParams | null): RoutingResourceResult<MatrixResponse>;
177
+ /** Fetch an isochrone polygon; pass `null` to stay idle. */
178
+ declare function useIsochrone(client: WheraboutsClient, params: IsochroneParams | null): RoutingResourceResult<IsochroneResponse>;
179
+
30
180
  interface UseZoneContainsResult {
31
181
  error: Error | null;
32
182
  loading: boolean;
@@ -34,4 +184,4 @@ interface UseZoneContainsResult {
34
184
  }
35
185
  declare function useZoneContains(client: WheraboutsClient, coords: LatLng | null): UseZoneContainsResult;
36
186
 
37
- export { type LatLng, type UseAutocompleteOptions, type UseAutocompleteResult, type UseReverseGeocodeResult, type UseZoneContainsResult, useAutocomplete, useReverseGeocode, useZoneContains };
187
+ export { type AutocompleteCacheConfig, type AutocompleteStatus, type ComboboxAction, type ComboboxInputProps, type ComboboxItemProps, type ComboboxListboxProps, type ComboboxState, INITIAL_COMBOBOX_STATE, type LatLng, type RoutingResourceResult, type StorageLike, type UseAutocompleteOptions, type UseAutocompleteResult, type UseComboboxOptions, type UseComboboxResult, type UseReverseGeocodeResult, type UseZoneContainsResult, buildInputProps, buildItemProps, buildListboxProps, comboboxReducer, deriveStatus, keyToAction, useAutocomplete, useCombobox, useDirections, useIsochrone, useMatrix, useReverseGeocode, useZoneContains };
package/dist/index.d.ts CHANGED
@@ -1,18 +1,156 @@
1
- import { AddressSuggestion, WheraboutsClient, ReverseGeocodeAddress, ZoneRecord } from '@wherabouts/sdk';
1
+ import { KeyboardEvent, MouseEvent } from 'react';
2
+ import { AddressSuggestion, WheraboutsClient, ReverseGeocodeAddress, DirectionsParams, DirectionsResponse, IsochroneParams, IsochroneResponse, MatrixParams, MatrixResponse, ZoneRecord } from '@wherabouts/sdk';
2
3
 
4
+ /**
5
+ * Minimal structural view of the Web Storage API (sessionStorage/localStorage).
6
+ * Declared locally so the package type-checks with no DOM lib.
7
+ */
8
+ interface StorageLike {
9
+ getItem(key: string): string | null;
10
+ removeItem(key: string): void;
11
+ setItem(key: string, value: string): void;
12
+ }
13
+
14
+ /**
15
+ * Headless WAI-ARIA combobox helper for autocomplete dropdowns. Provides the
16
+ * keyboard state machine (↑/↓/Home/End/Enter/Esc, wrapping) and ARIA prop
17
+ * getters so an input + listbox is accessible by default. Bring your own
18
+ * markup and data; this hook owns only the open/active-option state.
19
+ *
20
+ * @see https://www.w3.org/WAI/ARIA/apg/patterns/combobox/
21
+ */
22
+ interface ComboboxState {
23
+ /** Index of the visually-active option, or -1 when none is active. */
24
+ activeIndex: number;
25
+ isOpen: boolean;
26
+ }
27
+ type ComboboxAction = {
28
+ type: "next";
29
+ count: number;
30
+ } | {
31
+ type: "prev";
32
+ count: number;
33
+ } | {
34
+ type: "first";
35
+ count: number;
36
+ } | {
37
+ type: "last";
38
+ count: number;
39
+ } | {
40
+ type: "set";
41
+ index: number;
42
+ } | {
43
+ type: "open";
44
+ } | {
45
+ type: "close";
46
+ } | {
47
+ type: "select";
48
+ } | {
49
+ type: "reset";
50
+ };
51
+ declare const INITIAL_COMBOBOX_STATE: ComboboxState;
52
+ /** Pure transition function for the combobox keyboard state machine. */
53
+ declare function comboboxReducer(state: ComboboxState, action: ComboboxAction): ComboboxState;
54
+ /** Map a keyboard key to a combobox action, or null when it is irrelevant. */
55
+ declare function keyToAction(key: string, count: number): ComboboxAction | null;
56
+ interface ComboboxInputProps {
57
+ "aria-activedescendant": string | undefined;
58
+ "aria-autocomplete": "list";
59
+ "aria-controls": string;
60
+ "aria-expanded": boolean;
61
+ role: "combobox";
62
+ }
63
+ interface ComboboxListboxProps {
64
+ id: string;
65
+ role: "listbox";
66
+ }
67
+ interface ComboboxItemProps {
68
+ "aria-selected": boolean;
69
+ id: string;
70
+ role: "option";
71
+ }
72
+ /** Build the ARIA attributes for the combobox input — pure. */
73
+ declare function buildInputProps(id: string, state: ComboboxState): ComboboxInputProps;
74
+ /** Build the ARIA attributes for the listbox container — pure. */
75
+ declare function buildListboxProps(id: string): ComboboxListboxProps;
76
+ /** Build the ARIA attributes for an option at `index` — pure. */
77
+ declare function buildItemProps(id: string, index: number, state: ComboboxState): ComboboxItemProps;
78
+ interface UseComboboxOptions {
79
+ /** Number of options currently rendered (drives wrapping/bounds). */
80
+ count: number;
81
+ /** Stable id root; option/listbox ids derive from it. */
82
+ id: string;
83
+ /** Called when an option is chosen via Enter or pointer. */
84
+ onSelect?: (index: number) => void;
85
+ }
86
+ interface UseComboboxResult {
87
+ activeIndex: number;
88
+ close: () => void;
89
+ getInputProps: () => ComboboxInputProps & {
90
+ onBlur: () => void;
91
+ onFocus: () => void;
92
+ onKeyDown: (event: KeyboardEvent) => void;
93
+ };
94
+ getItemProps: (index: number) => ComboboxItemProps & {
95
+ onMouseDown: (event: MouseEvent) => void;
96
+ onMouseEnter: () => void;
97
+ };
98
+ getListboxProps: () => ComboboxListboxProps;
99
+ isOpen: boolean;
100
+ open: () => void;
101
+ reset: () => void;
102
+ setActiveIndex: (index: number) => void;
103
+ }
104
+ declare function useCombobox(options: UseComboboxOptions): UseComboboxResult;
105
+
106
+ type AutocompleteStatus = "idle" | "loading" | "success" | "empty" | "error";
107
+ /** Opt-in client-side cache for repeated/back-and-forth queries. */
108
+ interface AutocompleteCacheConfig {
109
+ /** A `sessionStorage`-like store. Entries are namespaced under `wh:ac:`. */
110
+ storage: StorageLike;
111
+ /** Entry lifetime in ms (default 60_000). */
112
+ ttlMs?: number;
113
+ }
3
114
  interface UseAutocompleteOptions {
115
+ cache?: AutocompleteCacheConfig;
4
116
  country?: string;
5
117
  debounceMs?: number;
118
+ /** Keep the previous results visible while a new search runs / on error. */
119
+ keepPreviousData?: boolean;
120
+ /** Latitude for proximity boosting (pair with `lng`). */
121
+ lat?: number;
6
122
  limit?: number;
123
+ /** Longitude for proximity boosting (pair with `lat`). */
124
+ lng?: number;
125
+ /** Minimum trimmed query length before a request fires (default 2). */
126
+ minLength?: number;
127
+ /** Groups keystrokes into one billable session; see `newSessionToken()`. */
128
+ sessionToken?: string;
7
129
  state?: string;
8
130
  }
9
131
  interface UseAutocompleteResult {
10
132
  error: Error | null;
11
133
  loading: boolean;
12
134
  query: string;
135
+ /** True when the last request was rejected with HTTP 429. */
136
+ rateLimited: boolean;
137
+ /** Clear the query, results, and any in-flight request. */
138
+ reset: () => void;
13
139
  results: AddressSuggestion[];
14
140
  setQuery: (q: string) => void;
141
+ status: AutocompleteStatus;
15
142
  }
143
+ /**
144
+ * Derive a single coarse status from the hook's raw flags. Exported for
145
+ * unit-testing and for consumers who prefer a state machine to booleans.
146
+ */
147
+ declare function deriveStatus(s: {
148
+ error: Error | null;
149
+ loading: boolean;
150
+ minLength: number;
151
+ query: string;
152
+ results: AddressSuggestion[];
153
+ }): AutocompleteStatus;
16
154
  declare function useAutocomplete(client: WheraboutsClient, options?: UseAutocompleteOptions): UseAutocompleteResult;
17
155
 
18
156
  interface LatLng {
@@ -27,6 +165,18 @@ interface UseReverseGeocodeResult {
27
165
  }
28
166
  declare function useReverseGeocode(client: WheraboutsClient, coords: LatLng | null): UseReverseGeocodeResult;
29
167
 
168
+ interface RoutingResourceResult<T> {
169
+ data: T | null;
170
+ error: Error | null;
171
+ loading: boolean;
172
+ }
173
+ /** Fetch turn-by-turn directions; pass `null` to stay idle. */
174
+ declare function useDirections(client: WheraboutsClient, params: DirectionsParams | null): RoutingResourceResult<DirectionsResponse>;
175
+ /** Fetch a duration/distance matrix; pass `null` to stay idle. */
176
+ declare function useMatrix(client: WheraboutsClient, params: MatrixParams | null): RoutingResourceResult<MatrixResponse>;
177
+ /** Fetch an isochrone polygon; pass `null` to stay idle. */
178
+ declare function useIsochrone(client: WheraboutsClient, params: IsochroneParams | null): RoutingResourceResult<IsochroneResponse>;
179
+
30
180
  interface UseZoneContainsResult {
31
181
  error: Error | null;
32
182
  loading: boolean;
@@ -34,4 +184,4 @@ interface UseZoneContainsResult {
34
184
  }
35
185
  declare function useZoneContains(client: WheraboutsClient, coords: LatLng | null): UseZoneContainsResult;
36
186
 
37
- export { type LatLng, type UseAutocompleteOptions, type UseAutocompleteResult, type UseReverseGeocodeResult, type UseZoneContainsResult, useAutocomplete, useReverseGeocode, useZoneContains };
187
+ export { type AutocompleteCacheConfig, type AutocompleteStatus, type ComboboxAction, type ComboboxInputProps, type ComboboxItemProps, type ComboboxListboxProps, type ComboboxState, INITIAL_COMBOBOX_STATE, type LatLng, type RoutingResourceResult, type StorageLike, type UseAutocompleteOptions, type UseAutocompleteResult, type UseComboboxOptions, type UseComboboxResult, type UseReverseGeocodeResult, type UseZoneContainsResult, buildInputProps, buildItemProps, buildListboxProps, comboboxReducer, deriveStatus, keyToAction, useAutocomplete, useCombobox, useDirections, useIsochrone, useMatrix, useReverseGeocode, useZoneContains };