@getmicdrop/svelte-components 5.6.0 → 5.6.1

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.
Files changed (77) hide show
  1. package/dist/components/Layout/__tests__/AppShell.test.d.ts +2 -0
  2. package/dist/components/Layout/__tests__/AppShell.test.d.ts.map +1 -0
  3. package/dist/components/Layout/__tests__/AppShell.test.js +95 -0
  4. package/dist/components/Layout/__tests__/ContentSection.test.d.ts +2 -0
  5. package/dist/components/Layout/__tests__/ContentSection.test.d.ts.map +1 -0
  6. package/dist/components/Layout/__tests__/ContentSection.test.js +112 -0
  7. package/dist/components/Layout/__tests__/PageContainer.test.d.ts +2 -0
  8. package/dist/components/Layout/__tests__/PageContainer.test.d.ts.map +1 -0
  9. package/dist/components/Layout/__tests__/PageContainer.test.js +133 -0
  10. package/dist/components/Layout/__tests__/Responsive.test.d.ts +2 -0
  11. package/dist/components/Layout/__tests__/Responsive.test.d.ts.map +1 -0
  12. package/dist/components/Layout/__tests__/Responsive.test.js +123 -0
  13. package/dist/index.d.ts +1 -0
  14. package/dist/index.js +41 -40
  15. package/dist/patterns/navigation/BottomNav.svelte +18 -8
  16. package/dist/patterns/navigation/BottomNav.svelte.d.ts.map +1 -1
  17. package/dist/primitives/Accordion/AccordionItemWrapper.test.svelte +107 -107
  18. package/dist/primitives/Accordion/AccordionItemWrapper.test.svelte.d.ts +2 -2
  19. package/dist/primitives/Accordion/AccordionItemWrapper.test.svelte.d.ts.map +1 -1
  20. package/dist/primitives/Checkbox/Checkbox.svelte +3 -3
  21. package/dist/primitives/Dropdown/DropdownDivider.svelte +9 -0
  22. package/dist/primitives/Dropdown/DropdownDivider.svelte.d.ts +7 -0
  23. package/dist/primitives/Dropdown/DropdownDivider.svelte.d.ts.map +1 -0
  24. package/dist/primitives/Helper/Helper.svelte +33 -0
  25. package/dist/primitives/Helper/Helper.svelte.d.ts +18 -0
  26. package/dist/primitives/Helper/Helper.svelte.d.ts.map +1 -0
  27. package/dist/primitives/Input/Input.svelte +416 -417
  28. package/dist/primitives/Input/Input.svelte.d.ts +2 -4
  29. package/dist/primitives/Input/Input.svelte.d.ts.map +1 -1
  30. package/dist/primitives/Modal/Modal.svelte +157 -158
  31. package/dist/primitives/Modal/Modal.svelte.d.ts +6 -8
  32. package/dist/primitives/Modal/Modal.svelte.d.ts.map +1 -1
  33. package/dist/primitives/NumberInput/NumberInput.svelte +105 -106
  34. package/dist/primitives/NumberInput/NumberInput.svelte.d.ts +0 -2
  35. package/dist/primitives/NumberInput/NumberInput.svelte.d.ts.map +1 -1
  36. package/dist/primitives/Toggle.svelte +70 -71
  37. package/dist/primitives/Toggle.svelte.d.ts +2 -4
  38. package/dist/primitives/Toggle.svelte.d.ts.map +1 -1
  39. package/dist/primitives/Tooltip/Tooltip.svelte +83 -0
  40. package/dist/primitives/Tooltip/Tooltip.svelte.d.ts +15 -0
  41. package/dist/primitives/Tooltip/Tooltip.svelte.d.ts.map +1 -0
  42. package/dist/primitives/index.d.ts +3 -0
  43. package/dist/primitives/index.js +7 -0
  44. package/dist/recipes/ImageUploader/ImageUploader.spec.js +6 -5
  45. package/dist/recipes/inputs/MultiSelect.svelte +277 -256
  46. package/dist/recipes/inputs/MultiSelect.svelte.d.ts +54 -21
  47. package/dist/recipes/inputs/MultiSelect.svelte.d.ts.map +1 -1
  48. package/dist/recipes/inputs/PlaceAutocomplete/PlaceAutocomplete.spec.js +9 -4
  49. package/dist/recipes/inputs/PlaceAutocomplete/PlaceAutocomplete.svelte +332 -327
  50. package/dist/recipes/inputs/PlaceAutocomplete/PlaceAutocomplete.svelte.d.ts +12 -1
  51. package/dist/recipes/inputs/PlaceAutocomplete/PlaceAutocomplete.svelte.d.ts.map +1 -1
  52. package/dist/stores/auth.js +8 -0
  53. package/dist/stores/auth.svelte.d.ts +39 -0
  54. package/dist/stores/auth.svelte.d.ts.map +1 -0
  55. package/dist/stores/auth.svelte.js +60 -0
  56. package/dist/stores/formDataStore.d.ts.map +1 -1
  57. package/dist/stores/formDataStore.js +8 -0
  58. package/dist/stores/formDataStore.svelte.d.ts +47 -0
  59. package/dist/stores/formDataStore.svelte.d.ts.map +1 -0
  60. package/dist/stores/formDataStore.svelte.js +84 -0
  61. package/dist/stores/formSave.d.ts.map +1 -1
  62. package/dist/stores/formSave.js +8 -0
  63. package/dist/stores/formSave.svelte.d.ts +33 -0
  64. package/dist/stores/formSave.svelte.d.ts.map +1 -0
  65. package/dist/stores/formSave.svelte.js +113 -0
  66. package/dist/stores/navigation.d.ts.map +1 -1
  67. package/dist/stores/navigation.js +8 -0
  68. package/dist/stores/navigation.svelte.d.ts +35 -0
  69. package/dist/stores/navigation.svelte.d.ts.map +1 -0
  70. package/dist/stores/navigation.svelte.js +69 -0
  71. package/dist/telemetry.server.spec.js +11 -8
  72. package/dist/telemetry.spec.js +75 -50
  73. package/dist/utils/imageValidation.spec.js +62 -59
  74. package/dist/utils/logger.d.ts +19 -0
  75. package/dist/utils/logger.d.ts.map +1 -0
  76. package/dist/utils/logger.js +47 -0
  77. package/package.json +296 -292
@@ -1,337 +1,342 @@
1
- <script lang="ts">
2
- import { SearchOutline } from "../../../primitives/Icons";
3
- import * as GMaps from '@googlemaps/js-api-loader';
4
- import { PUBLIC_GOOGLE_MAPS_API_KEY } from '../../../config.js';
5
- import { typography } from '../../../tokens/typography';
6
- const { Loader } = GMaps;
7
-
8
- interface Props {
9
- fetchFields?: string[];
10
- placeholder?: string;
11
- language?: string;
12
- region?: string;
13
- autocomplete?: string;
14
- initialValue?: string;
15
- onResponse?: (data: any) => void;
16
- onError?: (error: string) => void;
17
- mode?: 'full' | 'cityState';
18
- animateFocus?: boolean;
19
- disabled?: boolean;
20
- }
21
-
22
- let {
23
- fetchFields = ['formattedAddress', 'addressComponents'],
24
- placeholder = $bindable('Search for location...'),
25
- language = 'en-US',
26
- region = 'US',
27
- autocomplete = 'off',
28
- initialValue = '',
29
- onResponse = () => {},
30
- onError = () => {},
31
- mode = 'full',
32
- animateFocus = true,
33
- disabled = false,
34
- }: Props = $props();
35
-
36
- let shouldAnimate = $derived(animateFocus && !disabled);
37
-
38
- let inputRef: HTMLInputElement;
39
- let containerRef: HTMLDivElement;
40
- let currentSuggestion = $state(-1);
41
- let title = $state('');
42
- let results = $state<any[]>([]);
43
- let token: any;
44
- let loader: GMaps.Loader;
45
- let placesApi: { [key: string]: any } = {};
46
-
1
+ <script lang="ts">
2
+ import { SearchOutline } from "../../../primitives/Icons";
3
+ import * as GMaps from '@googlemaps/js-api-loader';
4
+ import { PUBLIC_GOOGLE_MAPS_API_KEY } from '../../../config.js';
5
+ import { typography } from '../../../tokens/typography';
6
+ const { Loader } = GMaps;
7
+ /** Google Places API address component */ interface AddressComponent { longText: string; shortText: string; types: string[]; } /** Google Places API response data */ interface PlaceData { formattedAddress?: string; addressComponents?: AddressComponent[]; text?: string; [key: string]: unknown; } /** Google Places autocomplete suggestion */ interface AutocompleteSuggestion { placePrediction: { types: string[]; text: { toString(): string }; toPlace(): PlaceObject; }; } /** Google Places place object */ interface PlaceObject { fetchFields(options: { fields: string[] }): Promise<void>; toJSON(): PlaceData; } /** Google Places API interface */ interface PlacesApiInterface { AutocompleteSessionToken: new () => AutocompleteSessionToken; AutocompleteSuggestion: { fetchAutocompleteSuggestions(request: AutocompleteRequest): Promise<{ suggestions: AutocompleteSuggestion[] }>; }; } /** Google Places autocomplete session token */ interface AutocompleteSessionToken {} /** Autocomplete request parameters */ interface AutocompleteRequest { input: string; language: string; region: string; sessionToken: AutocompleteSessionToken | string; } /** Search result item */ interface SearchResult { to_place: PlaceObject; text: string; originalText?: string; }
8
+
9
+ interface Props {
10
+ fetchFields?: string[];
11
+ placeholder?: string;
12
+ language?: string;
13
+ region?: string;
14
+ autocomplete?: string;
15
+ initialValue?: string;
16
+ onResponse?: (data: PlaceData) => void;
17
+ onError?: (error: string) => void;
18
+ mode?: 'full' | 'cityState';
19
+ animateFocus?: boolean;
20
+ disabled?: boolean;
21
+ }
22
+
23
+ let {
24
+ fetchFields = ['formattedAddress', 'addressComponents'],
25
+ placeholder = $bindable('Search for location...'),
26
+ language = 'en-US',
27
+ region = 'US',
28
+ autocomplete = 'off',
29
+ initialValue = '',
30
+ onResponse = () => {},
31
+ onError = () => {},
32
+ mode = 'full',
33
+ animateFocus = true,
34
+ disabled = false,
35
+ }: Props = $props();
36
+
37
+ let shouldAnimate = $derived(animateFocus && !disabled);
38
+
39
+ let inputRef: HTMLInputElement;
40
+ let containerRef: HTMLDivElement;
41
+ let currentSuggestion = $state(-1);
42
+ let title = $state('');
43
+ let results = $state<SearchResult[]>([]);
44
+ let token: AutocompleteSessionToken | undefined;
45
+ let loader: GMaps.Loader;
46
+ let placesApi: Partial<PlacesApiInterface> = {};
47
+
47
48
  let request = $state<{
48
49
  input: string;
49
50
  language: string;
50
51
  region: string;
51
- sessionToken: any;
52
+ sessionToken: AutocompleteSessionToken | string;
52
53
  }>({
53
54
  input: (() => initialValue)(),
54
55
  language: (() => language)(),
55
56
  region: (() => region)(),
56
57
  sessionToken: '',
57
58
  });
58
-
59
- let isInitialized = $state(false);
60
-
61
- $effect(() => {
62
- if (initialValue && !isInitialized) {
63
- request.input = initialValue;
64
- title = initialValue;
65
- isInitialized = true;
66
- }
67
- });
68
-
69
- $effect(() => {
70
- if (mode === 'cityState' && placeholder === 'Search for location...') {
71
- placeholder = 'Search for city, state...';
72
- }
73
- });
74
-
75
- const reset = () => {
76
- currentSuggestion = -1;
77
- results = [];
78
- refreshToken(request).catch(() => {});
79
- };
80
-
81
- const makeAcRequest = async (
82
- event: Event & { currentTarget: HTMLInputElement }
83
- ): Promise<void> => {
84
- const target = event.currentTarget as HTMLInputElement;
85
- if (target?.value === '') {
86
- title = '';
87
- request.input = '';
88
- results = [];
89
- return;
90
- }
91
-
92
- title = target.value;
93
- request.input = target.value;
94
-
95
- try {
96
- const { suggestions } =
97
- await placesApi.AutocompleteSuggestion.fetchAutocompleteSuggestions(
98
- request
99
- );
100
-
101
- if (mode === 'cityState') {
102
- results = suggestions
103
- .filter((s: any) => {
104
- const types = s.placePrediction.types || [];
105
-
106
- // Accept cities (locality) and city-level neighborhoods (sublocality)
107
- // like Van Nuys, North Hollywood - but not states or counties
108
- return types.some(
109
- (type: string) =>
110
- type === 'locality' ||
111
- type === 'sublocality' ||
112
- type === 'sublocality_level_1'
113
- );
114
- })
115
- .map((s: any) => {
116
- let displayText = s.placePrediction.text.toString();
117
-
118
- // Format as "City, State" (first two parts)
119
- const parts = displayText.split(', ');
120
- if (parts.length >= 2) {
121
- displayText = parts.slice(0, 2).join(', ');
122
- }
123
-
124
- return {
125
- to_place: s.placePrediction.toPlace(),
126
- text: displayText,
127
- originalText: s.placePrediction.text.toString(),
128
- };
129
- });
130
- } else {
131
- results = suggestions.map((s: any) => ({
132
- to_place: s.placePrediction.toPlace(),
133
- text: s.placePrediction.text.toString(),
134
- }));
135
- }
136
- } catch (e: any) {
137
- onError(
138
- (e.name || 'An error occurred') +
139
- ' - ' +
140
- (e.message || 'see console for details.')
141
- );
142
- }
143
- };
144
-
145
- const onPlaceSelected = async (
146
- place: any,
147
- originalText?: string
148
- ): Promise<void> => {
149
- results = [];
150
- currentSuggestion = -1;
151
-
152
- try {
153
- await place.fetchFields({
154
- fields: fetchFields,
155
- });
156
- let placeData = place.toJSON();
157
- request.input = originalText || placeData.text || '';
158
- title = originalText || placeData.text || '';
159
- onResponse(placeData);
160
- } catch (e: any) {
161
- onError(
162
- (e.name || 'An error occurred') +
163
- ' - ' +
164
- (e.message || 'error fetching place details')
165
- );
166
- }
167
-
168
- setTimeout(() => {
169
- refreshToken(request).catch(() => {});
170
- }, 100);
171
- };
172
-
173
- const refreshToken = async (req: any) => {
174
- try {
175
- token = new placesApi.AutocompleteSessionToken();
176
- req.sessionToken = token;
177
- return req;
178
- } catch (e: any) {
179
- onError(
180
- (e.name || 'An error occurred') +
181
- ' - ' +
182
- (e.message || 'error fetch token')
183
- );
184
- return req;
185
- }
186
- };
187
-
188
- function onKeyDown(e: KeyboardEvent) {
189
- if (results.length === 0) {
190
- return;
191
- }
192
-
193
- if (e.key === 'ArrowDown') {
194
- e.preventDefault();
195
- if (currentSuggestion === -1) {
196
- currentSuggestion = 0;
197
- } else {
198
- currentSuggestion =
199
- currentSuggestion < results.length - 1 ? currentSuggestion + 1 : 0;
200
- }
201
- } else if (e.key === 'ArrowUp') {
202
- e.preventDefault();
203
- if (currentSuggestion === -1) {
204
- currentSuggestion = results.length - 1;
205
- } else {
206
- currentSuggestion =
207
- currentSuggestion > 0 ? currentSuggestion - 1 : results.length - 1;
208
- }
209
- } else if (e.key === 'Enter') {
210
- e.preventDefault();
211
- if (currentSuggestion >= 0 && currentSuggestion < results.length) {
212
- onPlaceSelected(
213
- results[currentSuggestion].to_place,
214
- results[currentSuggestion].text
215
- );
216
- }
217
- } else if (e.key === 'Escape') {
218
- e.preventDefault();
219
- reset();
220
- }
221
- }
222
-
223
- function handleClickOutside(event: MouseEvent) {
224
- if (containerRef && !containerRef.contains(event.target as Node)) {
225
- reset();
226
- }
227
- }
228
-
229
- $effect(() => {
230
- if (initialValue) {
231
- request.input = initialValue;
232
- title = initialValue;
233
- }
234
-
235
- // Add click outside listener
236
- document.addEventListener('click', handleClickOutside);
237
-
238
- // Initialize Google Maps API
239
- const initGoogleMaps = async () => {
240
- if (!PUBLIC_GOOGLE_MAPS_API_KEY) {
241
- onError('Google Maps API key is required');
242
- return;
243
- }
244
-
245
- try {
246
- loader = new Loader({
247
- apiKey: PUBLIC_GOOGLE_MAPS_API_KEY,
248
- version: 'weekly',
249
- libraries: ['places'],
250
- });
251
-
252
- const { AutocompleteSessionToken, AutocompleteSuggestion } =
253
- await loader.importLibrary('places');
254
- placesApi.AutocompleteSessionToken = AutocompleteSessionToken;
255
- placesApi.AutocompleteSuggestion = AutocompleteSuggestion;
256
- token = new placesApi.AutocompleteSessionToken();
257
- request.sessionToken = token;
258
- } catch (e: any) {
259
- onError(
260
- (e.name || 'An error occurred') +
261
- ' - ' +
262
- (e.message || 'Error loading Google Maps API')
263
- );
264
- }
265
- };
266
-
267
- initGoogleMaps();
268
-
269
- return () => {
270
- document.removeEventListener('click', handleClickOutside);
271
- };
272
- });
273
- </script>
274
-
275
- <div class="w-full">
276
- <div
277
- bind:this={containerRef}
278
- class="relative rounded-lg {shouldAnimate ? 'transition-transform duration-200 focus-within:scale-[1.01]' : ''}"
279
- >
280
- <div class="pointer-events-none absolute inset-y-0 left-0 flex items-center pl-3 z-10">
281
- <SearchOutline class={`w-5 h-5 ${typography.iconMuted}`} />
282
- </div>
283
-
284
- <input
285
- bind:this={inputRef}
286
- type="text"
287
- name="location"
288
- class="block w-full h-10 pl-10 pr-2.5 py-2.5 bg-gray-50 border border-gray-300 rounded-lg {typography.label}
289
- focus:ring-blue-500 focus:border-blue-500 focus:outline-none
290
- hover:border-blue-500
291
- disabled:opacity-50 disabled:cursor-not-allowed
292
- placeholder:text-gray-500
293
- dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400
294
- dark:focus:ring-blue-500 dark:focus:border-blue-500
295
- dark:hover:border-blue-500"
296
- {placeholder}
297
- autocomplete={autocomplete as 'off' | 'on'}
298
- {disabled}
299
- aria-controls="options"
300
- bind:value={title}
301
- oninput={makeAcRequest}
302
- onkeydown={onKeyDown}
303
- />
304
-
305
- {#if results.length > 0}
306
- <div class="absolute inset-y-0 right-0 hidden md:flex items-center p-1.5 gap-1">
307
- <kbd class="inline-flex items-center justify-center px-1 border border-gray-300 rounded {typography.xsMuted} bg-white dark:bg-gray-800 dark:border-gray-600">Esc</kbd>
308
- <kbd class="inline-flex items-center justify-center w-6 border border-gray-300 rounded {typography.xsMuted} bg-white dark:bg-gray-800 dark:border-gray-600">&uArr;</kbd>
309
- <kbd class="inline-flex items-center justify-center w-6 border border-gray-300 rounded {typography.xsMuted} bg-white dark:bg-gray-800 dark:border-gray-600">&dArr;</kbd>
310
- </div>
311
-
312
- <ul
313
- class="absolute top-full left-0 right-0 z-50 mt-1 py-1 bg-white border border-gray-200 rounded-lg shadow-lg max-h-60 overflow-y-auto
314
- dark:bg-gray-800 dark:border-gray-600"
315
- id="options"
316
- >
317
- {#each results as place, i}
318
- <li
319
- class="cursor-pointer transition-colors duration-100 {typography.body}
320
- {i === currentSuggestion ? 'bg-gray-100 dark:bg-gray-700' : 'hover:bg-gray-100 dark:hover:bg-gray-700'}"
321
- id="option-{i + 1}"
322
- >
323
- <button
324
- type="button"
325
- class="block w-full text-left px-4 py-3 bg-transparent border-none cursor-pointer {typography.sm}
326
- focus:outline-none focus:bg-gray-100 dark:focus:bg-gray-700"
327
- tabindex={i + 1}
328
- onclick={() => onPlaceSelected(place.to_place, place.text)}
329
- >
330
- <span class="font-medium">{place.text}</span>
331
- </button>
332
- </li>
333
- {/each}
334
- </ul>
335
- {/if}
336
- </div>
337
- </div>
59
+
60
+ let isInitialized = $state(false);
61
+
62
+ $effect(() => {
63
+ if (initialValue && !isInitialized) {
64
+ request.input = initialValue;
65
+ title = initialValue;
66
+ isInitialized = true;
67
+ }
68
+ });
69
+
70
+ $effect(() => {
71
+ if (mode === 'cityState' && placeholder === 'Search for location...') {
72
+ placeholder = 'Search for city, state...';
73
+ }
74
+ });
75
+
76
+ const reset = () => {
77
+ currentSuggestion = -1;
78
+ results = [];
79
+ refreshToken(request).catch(() => {});
80
+ };
81
+
82
+ const makeAcRequest = async (
83
+ event: Event & { currentTarget: HTMLInputElement }
84
+ ): Promise<void> => {
85
+ const target = event.currentTarget as HTMLInputElement;
86
+ if (target?.value === '') {
87
+ title = '';
88
+ request.input = '';
89
+ results = [];
90
+ return;
91
+ }
92
+
93
+ title = target.value;
94
+ request.input = target.value;
95
+
96
+ try {
97
+ const { suggestions } =
98
+ await placesApi.AutocompleteSuggestion!.fetchAutocompleteSuggestions(
99
+ request
100
+ );
101
+
102
+ if (mode === 'cityState') {
103
+ results = suggestions
104
+ .filter((s: AutocompleteSuggestion) => {
105
+ const types = s.placePrediction.types || [];
106
+
107
+ // Accept cities (locality) and city-level neighborhoods (sublocality)
108
+ // like Van Nuys, North Hollywood - but not states or counties
109
+ return types.some(
110
+ (type: string) =>
111
+ type === 'locality' ||
112
+ type === 'sublocality' ||
113
+ type === 'sublocality_level_1'
114
+ );
115
+ })
116
+ .map((s: AutocompleteSuggestion) => {
117
+ let displayText = s.placePrediction.text.toString();
118
+
119
+ // Format as "City, State" (first two parts)
120
+ const parts = displayText.split(', ');
121
+ if (parts.length >= 2) {
122
+ displayText = parts.slice(0, 2).join(', ');
123
+ }
124
+
125
+ return {
126
+ to_place: s.placePrediction.toPlace(),
127
+ text: displayText,
128
+ originalText: s.placePrediction.text.toString(),
129
+ };
130
+ });
131
+ } else {
132
+ results = suggestions.map((s: AutocompleteSuggestion) => ({
133
+ to_place: s.placePrediction.toPlace(),
134
+ text: s.placePrediction.text.toString(),
135
+ }));
136
+ }
137
+ } catch (e: unknown) {
138
+ const error = e as { name?: string; message?: string };
139
+ onError(
140
+ (error.name || 'An error occurred') +
141
+ ' - ' +
142
+ (error.message || 'see console for details.')
143
+ );
144
+ }
145
+ };
146
+
147
+ const onPlaceSelected = async (
148
+ place: PlaceObject,
149
+ originalText?: string
150
+ ): Promise<void> => {
151
+ results = [];
152
+ currentSuggestion = -1;
153
+
154
+ try {
155
+ await place.fetchFields({
156
+ fields: fetchFields,
157
+ });
158
+ let placeData = place.toJSON();
159
+ request.input = originalText || placeData.text || '';
160
+ title = originalText || placeData.text || '';
161
+ onResponse(placeData);
162
+ } catch (e: unknown) {
163
+ const error = e as { name?: string; message?: string };
164
+ onError(
165
+ (error.name || 'An error occurred') +
166
+ ' - ' +
167
+ (error.message || 'error fetching place details')
168
+ );
169
+ }
170
+
171
+ setTimeout(() => {
172
+ refreshToken(request).catch(() => {});
173
+ }, 100);
174
+ };
175
+
176
+ const refreshToken = async (req: AutocompleteRequest) => {
177
+ try {
178
+ token = new placesApi.AutocompleteSessionToken!();
179
+ req.sessionToken = token;
180
+ return req;
181
+ } catch (e: unknown) {
182
+ const error = e as { name?: string; message?: string };
183
+ onError(
184
+ (error.name || 'An error occurred') +
185
+ ' - ' +
186
+ (error.message || 'error fetch token')
187
+ );
188
+ return req;
189
+ }
190
+ };
191
+
192
+ function onKeyDown(e: KeyboardEvent) {
193
+ if (results.length === 0) {
194
+ return;
195
+ }
196
+
197
+ if (e.key === 'ArrowDown') {
198
+ e.preventDefault();
199
+ if (currentSuggestion === -1) {
200
+ currentSuggestion = 0;
201
+ } else {
202
+ currentSuggestion =
203
+ currentSuggestion < results.length - 1 ? currentSuggestion + 1 : 0;
204
+ }
205
+ } else if (e.key === 'ArrowUp') {
206
+ e.preventDefault();
207
+ if (currentSuggestion === -1) {
208
+ currentSuggestion = results.length - 1;
209
+ } else {
210
+ currentSuggestion =
211
+ currentSuggestion > 0 ? currentSuggestion - 1 : results.length - 1;
212
+ }
213
+ } else if (e.key === 'Enter') {
214
+ e.preventDefault();
215
+ if (currentSuggestion >= 0 && currentSuggestion < results.length) {
216
+ onPlaceSelected(
217
+ results[currentSuggestion].to_place,
218
+ results[currentSuggestion].text
219
+ );
220
+ }
221
+ } else if (e.key === 'Escape') {
222
+ e.preventDefault();
223
+ reset();
224
+ }
225
+ }
226
+
227
+ function handleClickOutside(event: MouseEvent) {
228
+ if (containerRef && !containerRef.contains(event.target as Node)) {
229
+ reset();
230
+ }
231
+ }
232
+
233
+ $effect(() => {
234
+ if (initialValue) {
235
+ request.input = initialValue;
236
+ title = initialValue;
237
+ }
238
+
239
+ // Add click outside listener
240
+ document.addEventListener('click', handleClickOutside);
241
+
242
+ // Initialize Google Maps API
243
+ const initGoogleMaps = async () => {
244
+ if (!PUBLIC_GOOGLE_MAPS_API_KEY) {
245
+ onError('Google Maps API key is required');
246
+ return;
247
+ }
248
+
249
+ try {
250
+ loader = new Loader({
251
+ apiKey: PUBLIC_GOOGLE_MAPS_API_KEY,
252
+ version: 'weekly',
253
+ libraries: ['places'],
254
+ });
255
+
256
+ const { AutocompleteSessionToken, AutocompleteSuggestion } =
257
+ await loader.importLibrary('places');
258
+ placesApi.AutocompleteSessionToken = AutocompleteSessionToken;
259
+ placesApi.AutocompleteSuggestion = AutocompleteSuggestion;
260
+ token = new placesApi.AutocompleteSessionToken!();
261
+ request.sessionToken = token;
262
+ } catch (e: unknown) {
263
+ const error = e as { name?: string; message?: string };
264
+ onError(
265
+ (error.name || 'An error occurred') +
266
+ ' - ' +
267
+ (error.message || 'Error loading Google Maps API')
268
+ );
269
+ }
270
+ };
271
+
272
+ initGoogleMaps();
273
+
274
+ return () => {
275
+ document.removeEventListener('click', handleClickOutside);
276
+ };
277
+ });
278
+ </script>
279
+
280
+ <div class="w-full">
281
+ <div
282
+ bind:this={containerRef}
283
+ class="relative rounded-lg {shouldAnimate ? 'transition-transform duration-200 focus-within:scale-[1.01]' : ''}"
284
+ >
285
+ <div class="pointer-events-none absolute inset-y-0 left-0 flex items-center pl-3 z-10">
286
+ <SearchOutline class={`w-5 h-5 ${typography.iconMuted}`} />
287
+ </div>
288
+
289
+ <input
290
+ bind:this={inputRef}
291
+ type="text"
292
+ name="location"
293
+ class="block w-full h-10 pl-10 pr-2.5 py-2.5 bg-gray-50 border border-gray-300 rounded-lg {typography.label}
294
+ focus:ring-blue-500 focus:border-blue-500 focus:outline-none
295
+ hover:border-blue-500
296
+ disabled:opacity-50 disabled:cursor-not-allowed
297
+ placeholder:text-gray-500
298
+ dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400
299
+ dark:focus:ring-blue-500 dark:focus:border-blue-500
300
+ dark:hover:border-blue-500"
301
+ {placeholder}
302
+ autocomplete={autocomplete as 'off' | 'on'}
303
+ {disabled}
304
+ aria-controls="options"
305
+ bind:value={title}
306
+ oninput={makeAcRequest}
307
+ onkeydown={onKeyDown}
308
+ />
309
+
310
+ {#if results.length > 0}
311
+ <div class="absolute inset-y-0 right-0 hidden md:flex items-center p-1.5 gap-1">
312
+ <kbd class="inline-flex items-center justify-center px-1 border border-gray-300 rounded {typography.xsMuted} bg-white dark:bg-gray-800 dark:border-gray-600">Esc</kbd>
313
+ <kbd class="inline-flex items-center justify-center w-6 border border-gray-300 rounded {typography.xsMuted} bg-white dark:bg-gray-800 dark:border-gray-600">&uArr;</kbd>
314
+ <kbd class="inline-flex items-center justify-center w-6 border border-gray-300 rounded {typography.xsMuted} bg-white dark:bg-gray-800 dark:border-gray-600">&dArr;</kbd>
315
+ </div>
316
+
317
+ <ul
318
+ class="absolute top-full left-0 right-0 z-50 mt-1 py-1 bg-white border border-gray-200 rounded-lg shadow-lg max-h-60 overflow-y-auto
319
+ dark:bg-gray-800 dark:border-gray-600"
320
+ id="options"
321
+ >
322
+ {#each results as place, i}
323
+ <li
324
+ class="cursor-pointer transition-colors duration-100 {typography.body}
325
+ {i === currentSuggestion ? 'bg-gray-100 dark:bg-gray-700' : 'hover:bg-gray-100 dark:hover:bg-gray-700'}"
326
+ id="option-{i + 1}"
327
+ >
328
+ <button
329
+ type="button"
330
+ class="block w-full text-left px-4 py-3 bg-transparent border-none cursor-pointer {typography.sm}
331
+ focus:outline-none focus:bg-gray-100 dark:focus:bg-gray-700"
332
+ tabindex={i + 1}
333
+ onclick={() => onPlaceSelected(place.to_place, place.text)}
334
+ >
335
+ <span class="font-medium">{place.text}</span>
336
+ </button>
337
+ </li>
338
+ {/each}
339
+ </ul>
340
+ {/if}
341
+ </div>
342
+ </div>