@aiaiai-pt/design-system 0.5.3 → 0.5.5

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.
@@ -0,0 +1,129 @@
1
+ <!--
2
+ @component GeoSearch
3
+
4
+ Geocoding search input powered by Nominatim (OpenStreetMap).
5
+ Wraps Combobox with built-in address/place search and debounced API calls.
6
+ Emits lon/lat coordinates when a result is selected.
7
+ Consumes --combobox-* tokens from components.css.
8
+
9
+ @example Basic
10
+ <GeoSearch onlocation={(lon, lat) => console.log(lon, lat)} />
11
+
12
+ @example With label and bounding box bias
13
+ <GeoSearch
14
+ label="Search location"
15
+ placeholder="City, address, or place..."
16
+ viewbox={[-9.5, 38.5, -8.8, 39.0]}
17
+ onlocation={(lon, lat) => { mapCenter = [lon, lat]; mapZoom = 15; }}
18
+ />
19
+
20
+ @example Custom provider URL
21
+ <GeoSearch
22
+ providerUrl="https://my-nominatim.example.com/search"
23
+ onlocation={(lon, lat) => console.log(lon, lat)}
24
+ />
25
+ -->
26
+ <script>
27
+ import Combobox from './Combobox.svelte';
28
+
29
+ let {
30
+ /** @type {string | undefined} */
31
+ label = undefined,
32
+ /** @type {string} */
33
+ placeholder = 'Search address or place...',
34
+ /** @type {boolean} */
35
+ disabled = false,
36
+ /** @type {string | undefined} */
37
+ error = undefined,
38
+ /** @type {string | undefined} */
39
+ help = undefined,
40
+ /** @type {string} */
41
+ size = 'md',
42
+ /** @type {string} — Nominatim-compatible search endpoint */
43
+ providerUrl = 'https://nominatim.openstreetmap.org/search',
44
+ /** @type {[number, number, number, number] | undefined} — [minLon, minLat, maxLon, maxLat] bias */
45
+ viewbox = undefined,
46
+ /** @type {((lon: number, lat: number, item: { label: string, value: string }) => void) | undefined} */
47
+ onlocation = undefined,
48
+ /** @type {string} */
49
+ class: className = '',
50
+ ...rest
51
+ } = $props();
52
+
53
+ /** @type {import('./Combobox.svelte').ComboboxItem[]} */
54
+ let items = $state([]);
55
+ let loading = $state(false);
56
+ let value = $state('');
57
+ let debounceTimer = 0;
58
+
59
+ /** Nominatim rate limit: 1 req/sec. Debounce at 400ms. */
60
+ function handleSearch(query) {
61
+ clearTimeout(debounceTimer);
62
+ if (!query || query.length < 3) {
63
+ items = [];
64
+ return;
65
+ }
66
+ loading = true;
67
+ debounceTimer = setTimeout(() => fetchResults(query), 400);
68
+ }
69
+
70
+ async function fetchResults(query) {
71
+ try {
72
+ const params = new URLSearchParams({
73
+ q: query,
74
+ format: 'json',
75
+ limit: '6',
76
+ addressdetails: '1',
77
+ });
78
+ if (viewbox) {
79
+ params.set('viewbox', viewbox.join(','));
80
+ params.set('bounded', '0'); // bias, not restrict
81
+ }
82
+ const resp = await fetch(`${providerUrl}?${params}`, {
83
+ headers: { 'Accept': 'application/json' },
84
+ });
85
+ if (!resp.ok) {
86
+ items = [];
87
+ return;
88
+ }
89
+ /** @type {Array<{lat: string, lon: string, display_name: string, type: string, class: string}>} */
90
+ const results = await resp.json();
91
+ items = results.map((r) => ({
92
+ value: `${r.lon},${r.lat}`,
93
+ label: r.display_name,
94
+ description: r.type !== 'yes' ? r.type.replace(/_/g, ' ') : r.class.replace(/_/g, ' '),
95
+ }));
96
+ } catch {
97
+ items = [];
98
+ } finally {
99
+ loading = false;
100
+ }
101
+ }
102
+
103
+ function handleChange(val) {
104
+ if (!val) return;
105
+ const [lonStr, latStr] = val.split(',');
106
+ const lon = parseFloat(lonStr);
107
+ const lat = parseFloat(latStr);
108
+ if (!Number.isNaN(lon) && !Number.isNaN(lat) && onlocation) {
109
+ const selectedItem = items.find((i) => i.value === val);
110
+ onlocation(lon, lat, selectedItem ?? { label: '', value: val });
111
+ }
112
+ }
113
+ </script>
114
+
115
+ <Combobox
116
+ {label}
117
+ {placeholder}
118
+ {disabled}
119
+ {error}
120
+ {help}
121
+ {size}
122
+ {items}
123
+ {loading}
124
+ bind:value
125
+ onsearch={handleSearch}
126
+ onchange={handleChange}
127
+ class={className}
128
+ {...rest}
129
+ />
@@ -0,0 +1,54 @@
1
+ export default GeoSearch;
2
+ type GeoSearch = {
3
+ $on?(type: string, callback: (e: any) => void): () => void;
4
+ $set?(props: Partial<$$ComponentProps>): void;
5
+ };
6
+ /**
7
+ * GeoSearch
8
+ *
9
+ * Geocoding search input powered by Nominatim (OpenStreetMap).
10
+ * Wraps Combobox with built-in address/place search and debounced API calls.
11
+ * Emits lon/lat coordinates when a result is selected.
12
+ * Consumes --combobox-* tokens from components.css.
13
+ *
14
+ * @example Basic
15
+ * <GeoSearch onlocation={(lon, lat) => console.log(lon, lat)} />
16
+ *
17
+ * @example With label and bounding box bias
18
+ * <GeoSearch
19
+ * label="Search location"
20
+ * placeholder="City, address, or place..."
21
+ * viewbox={[-9.5, 38.5, -8.8, 39.0]}
22
+ * onlocation={(lon, lat) => { mapCenter = [lon, lat]; mapZoom = 15; }}
23
+ * />
24
+ *
25
+ * @example Custom provider URL
26
+ * <GeoSearch
27
+ * providerUrl="https://my-nominatim.example.com/search"
28
+ * onlocation={(lon, lat) => console.log(lon, lat)}
29
+ * />
30
+ */
31
+ declare const GeoSearch: import("svelte").Component<{
32
+ label?: any;
33
+ placeholder?: string;
34
+ disabled?: boolean;
35
+ error?: any;
36
+ help?: any;
37
+ size?: string;
38
+ providerUrl?: string;
39
+ viewbox?: any;
40
+ onlocation?: any;
41
+ class?: string;
42
+ } & Record<string, any>, {}, "">;
43
+ type $$ComponentProps = {
44
+ label?: any;
45
+ placeholder?: string;
46
+ disabled?: boolean;
47
+ error?: any;
48
+ help?: any;
49
+ size?: string;
50
+ providerUrl?: string;
51
+ viewbox?: any;
52
+ onlocation?: any;
53
+ class?: string;
54
+ } & Record<string, any>;
@@ -39,6 +39,37 @@
39
39
  /** @type {HTMLElement | undefined} */
40
40
  let container = $state();
41
41
 
42
+ // Hoisted references for reactive effects
43
+ /** @type {import('ol/Map.js').default | undefined} */
44
+ let _map = $state();
45
+ /** @type {any} — VectorSource */
46
+ let _vectorSource = $state();
47
+ /** @type {any} — Feature constructor */
48
+ let _Feature;
49
+ /** @type {any} — Point constructor */
50
+ let _Point;
51
+
52
+ // Reactive: animate to new center/zoom when props change
53
+ $effect(() => {
54
+ if (!_map) return;
55
+ const view = _map.getView();
56
+ if (!view) return;
57
+ const targetCenter = fromLonLat(center);
58
+ view.animate({ center: targetCenter, zoom, duration: 300 });
59
+ });
60
+
61
+ // Reactive: update markers when props change
62
+ $effect(() => {
63
+ if (!_vectorSource || !_Feature || !_Point) return;
64
+ const features = markers.map(m => {
65
+ const f = new _Feature({ geometry: new _Point(fromLonLat([m.lon, m.lat])) });
66
+ f.set('markerData', m);
67
+ return f;
68
+ });
69
+ _vectorSource.clear();
70
+ _vectorSource.addFeatures(features);
71
+ });
72
+
42
73
  $effect(() => {
43
74
  if (!container) return;
44
75
 
@@ -86,6 +117,11 @@
86
117
  const vectorSource = new VectorSource({ features });
87
118
  const clusterSource = new Cluster({ distance, source: vectorSource });
88
119
 
120
+ // Store refs for reactive effects
121
+ _vectorSource = vectorSource;
122
+ _Feature = Feature;
123
+ _Point = Point;
124
+
89
125
  const clusterLayer = new VectorLayer({
90
126
  source: clusterSource,
91
127
  style: (feature) => {
@@ -133,6 +169,7 @@
133
169
  zoom,
134
170
  }),
135
171
  });
172
+ _map = map;
136
173
 
137
174
  // Hover: show tooltip
138
175
  map.on('pointermove', (evt) => {
@@ -96,6 +96,7 @@ export { default as MapPicker } from "./MapPicker.svelte";
96
96
  export { default as MapCluster } from "./MapCluster.svelte";
97
97
  export { default as MapHeatmap } from "./MapHeatmap.svelte";
98
98
  export { default as MapPopup } from "./MapPopup.svelte";
99
+ export { default as GeoSearch } from "./GeoSearch.svelte";
99
100
 
100
101
  // Scheduling
101
102
  export { default as Calendar } from "./Calendar.svelte";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aiaiai-pt/design-system",
3
- "version": "0.5.3",
3
+ "version": "0.5.5",
4
4
  "description": "Design system tokens and Svelte components for aiaiai products",
5
5
  "license": "MIT",
6
6
  "type": "module",