@ttoss/google-maps 2.0.3 → 2.0.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.
package/README.md CHANGED
@@ -7,7 +7,7 @@
7
7
  Install `@ttoss/google-maps` in your project:
8
8
 
9
9
  ```shell
10
- $ pnpm install @ttoss/google-maps
10
+ $ pnpm add @ttoss/google-maps
11
11
  ```
12
12
 
13
13
  If you use TypeScript, add the following to a declaration file (generally `typings.d.ts`):
@@ -16,7 +16,9 @@ If you use TypeScript, add the following to a declaration file (generally `typin
16
16
  /// <reference types="google.maps" />
17
17
  ```
18
18
 
19
- ## Configuring `GoogleMapsProvider`
19
+ ## Usage
20
+
21
+ ### Configuring `GoogleMapsProvider`
20
22
 
21
23
  At the root of your application, configure `GoogleMapsProvider` with your Google Maps API key. This way, the whole application can access the `google` variable.
22
24
 
@@ -34,7 +36,7 @@ const App = ({ children }) => {
34
36
  };
35
37
  ```
36
38
 
37
- ## Rendering the Map
39
+ ### Rendering the Map
38
40
 
39
41
  At the component level, render Google Maps using `useMap` hook:
40
42
 
@@ -46,7 +48,12 @@ const MyComponent = () => {
46
48
  const { ref, map } = useMap();
47
49
 
48
50
  React.useEffect(() => {
49
- // You have access to every map methods, like `setOptions`, through `map`.
51
+ if (map) {
52
+ map.setOptions({
53
+ center: { lat: -34.397, lng: 150.644 },
54
+ zoom: 8,
55
+ });
56
+ }
50
57
  }, [map]);
51
58
 
52
59
  return (
@@ -57,3 +64,21 @@ const MyComponent = () => {
57
64
  );
58
65
  };
59
66
  ```
67
+
68
+ If everything is set up correctly, you should see a map centered at the specified coordinates.
69
+
70
+ ### Retrieve `google.maps` object
71
+
72
+ If you need to access the `google.maps` object, you can use the `useGoogleMaps` hook:
73
+
74
+ ```tsx
75
+ import { useGoogleMaps } from '@ttoss/google-maps';
76
+
77
+ const MyComponent = () => {
78
+ const { google } = useGoogleMaps();
79
+
80
+ return <Text>{google.maps.version}</Text>;
81
+ };
82
+ ```
83
+
84
+ With this, you can perform any operation that the `google.maps` object allows, such as creating markers, drawing polygons, etc.
package/dist/esm/index.js CHANGED
@@ -6,7 +6,9 @@ import { useScript } from "@ttoss/react-hooks";
6
6
  import { jsx } from "react/jsx-runtime";
7
7
  var GoogleMapsContext = React.createContext({
8
8
  status: "idle",
9
- googleMaps: null
9
+ google: {
10
+ maps: null
11
+ }
10
12
  });
11
13
  var GoogleMapsProvider = ({
12
14
  children,
@@ -27,48 +29,68 @@ var GoogleMapsProvider = ({
27
29
  const {
28
30
  status
29
31
  } = useScript(src);
30
- const googleMaps = React.useMemo(() => {
31
- if (status === "ready" && window.google.maps) {
32
- return window.google.maps;
32
+ const google = React.useMemo(() => {
33
+ if (status === "ready" && window.google) {
34
+ return window.google;
33
35
  }
34
36
  return null;
35
37
  }, [status]);
36
38
  const value = React.useMemo(() => {
37
- if (status === "ready" && googleMaps) {
39
+ if (status === "ready" && google?.maps) {
38
40
  return {
39
41
  status,
40
- googleMaps
42
+ google: {
43
+ maps: google.maps
44
+ }
41
45
  };
42
46
  }
43
47
  return {
44
48
  status,
45
- googleMaps: null
49
+ google: {
50
+ maps: null
51
+ }
46
52
  };
47
- }, [googleMaps, status]);
53
+ }, [google, status]);
48
54
  return /* @__PURE__ */jsx(GoogleMapsContext.Provider, {
49
55
  value,
50
56
  children
51
57
  });
52
58
  };
59
+
60
+ // src/useGeocoder.ts
61
+ import * as React3 from "react";
62
+
63
+ // src/useGoogleMaps.ts
64
+ import * as React2 from "react";
53
65
  var useGoogleMaps = () => {
54
- return React.useContext(GoogleMapsContext);
66
+ const {
67
+ status,
68
+ google
69
+ } = React2.useContext(GoogleMapsContext);
70
+ return {
71
+ status,
72
+ google,
73
+ /**
74
+ * @deprecated Use google.maps instead.
75
+ */
76
+ googleMaps: google.maps
77
+ };
55
78
  };
56
79
 
57
80
  // src/useGeocoder.ts
58
- import * as React2 from "react";
59
81
  var useGeocoder = () => {
60
82
  const {
61
- googleMaps
83
+ google
62
84
  } = useGoogleMaps();
63
- const [isGeocoderInitialized, setIsGeocoderInitialized] = React2.useState(false);
64
- const geocoder = React2.useMemo(() => {
65
- if (googleMaps) {
66
- const googleMapsGeocoder = new googleMaps.Geocoder();
85
+ const [isGeocoderInitialized, setIsGeocoderInitialized] = React3.useState(false);
86
+ const geocoder = React3.useMemo(() => {
87
+ if (google.maps) {
88
+ const googleMapsGeocoder = new google.maps.Geocoder();
67
89
  setIsGeocoderInitialized(true);
68
90
  return googleMapsGeocoder;
69
91
  }
70
92
  return null;
71
- }, [googleMaps]);
93
+ }, [google.maps]);
72
94
  return {
73
95
  geocoder,
74
96
  isGeocoderInitialized
@@ -76,26 +98,26 @@ var useGeocoder = () => {
76
98
  };
77
99
 
78
100
  // src/useMap.ts
79
- import * as React3 from "react";
101
+ import * as React4 from "react";
80
102
  import { useCallbackRef } from "use-callback-ref";
81
103
  var useMap = (options = {}) => {
82
- const [, forceUpdate] = React3.useState(0);
104
+ const [, forceUpdate] = React4.useState(0);
83
105
  const ref = useCallbackRef(null, () => {
84
106
  return forceUpdate(n => {
85
107
  return n + 1;
86
108
  });
87
109
  });
88
110
  const {
89
- googleMaps
111
+ google
90
112
  } = useGoogleMaps();
91
- const map = React3.useMemo(() => {
92
- if (googleMaps && ref.current) {
93
- return new googleMaps.Map(ref.current, options);
113
+ const map = React4.useMemo(() => {
114
+ if (google?.maps && ref.current) {
115
+ return new google.maps.Map(ref.current, options);
94
116
  }
95
117
  return null;
96
- }, [googleMaps, ref.current]);
118
+ }, [google?.maps, ref.current]);
97
119
  const optionsStringify = JSON.stringify(options);
98
- React3.useEffect(() => {
120
+ React4.useEffect(() => {
99
121
  if (map) {
100
122
  const parsedOptions = JSON.parse(optionsStringify);
101
123
  map.setOptions(parsedOptions);
@@ -103,18 +125,19 @@ var useMap = (options = {}) => {
103
125
  }, [optionsStringify, map]);
104
126
  return {
105
127
  /**
106
- * asss
128
+ * Returns the map object which provides access to the [Google Maps API](https://developers.google.com/maps/documentation/javascript/overview).
107
129
  */
108
130
  map,
109
131
  /**
110
- * hhhh
132
+ * Returns the ref object which provides access to the HTMLDivElement element
133
+ * that the map is rendered in.
111
134
  */
112
135
  ref
113
136
  };
114
137
  };
115
138
 
116
139
  // src/usePlacesAutocomplete/index.ts
117
- import { useCallback, useEffect as useEffect2, useRef as useRef2, useState as useState3 } from "react";
140
+ import * as React5 from "react";
118
141
 
119
142
  // src/usePlacesAutocomplete/debounce.ts
120
143
  var debounce = (fn, delay) => {
@@ -148,63 +171,69 @@ var usePlacesAutocomplete = ({
148
171
  defaultValue = "",
149
172
  initOnMount = true
150
173
  } = {}) => {
151
- const [ready, setReady] = useState3(false);
152
- const [value, setVal] = useState3(defaultValue);
153
- const [suggestions, setSuggestions] = useState3({
174
+ const [ready, setReady] = React5.useState(false);
175
+ const [value, setVal] = React5.useState(defaultValue);
176
+ const [suggestions, setSuggestions] = React5.useState({
154
177
  loading: false,
155
178
  status: "",
156
179
  data: []
157
180
  });
158
- const asRef = useRef2(null);
181
+ const asRef = React5.useRef(null);
159
182
  const requestOptionsRef = useLatest_default(requestOptions);
160
183
  const {
161
- googleMaps
184
+ google
162
185
  } = useGoogleMaps();
163
- const googleMapsRef = useLatest_default(googleMaps);
186
+ const googleMapsRef = useLatest_default(google.maps);
164
187
  const upaCacheKey = cacheKey ? `upa-${cacheKey}` : "upa";
165
- const init = useCallback(() => {
166
- if (asRef.current) return;
167
- if (!googleMaps) {
188
+ const init = React5.useCallback(() => {
189
+ if (asRef.current) {
190
+ return;
191
+ }
192
+ if (!google.maps) {
168
193
  return;
169
194
  }
170
195
  const {
171
196
  current: gMaps
172
197
  } = googleMapsRef;
173
- const placesLib = gMaps?.places || googleMaps.places;
198
+ const placesLib = gMaps?.places || google.maps.places;
174
199
  if (!placesLib) {
175
200
  return;
176
201
  }
177
202
  asRef.current = new placesLib.AutocompleteService();
178
203
  setReady(true);
179
- }, [googleMaps]);
180
- const clearSuggestions = useCallback(() => {
204
+ }, [google.maps]);
205
+ const clearSuggestions = React5.useCallback(() => {
181
206
  setSuggestions({
182
207
  loading: false,
183
208
  status: "",
184
209
  data: []
185
210
  });
186
211
  }, []);
187
- const clearCache = useCallback(() => {
212
+ const clearCache = React5.useCallback(() => {
188
213
  try {
189
214
  sessionStorage.removeItem(upaCacheKey);
190
215
  } catch (error) {}
191
216
  }, []);
192
- const fetchPredictions = useCallback(debounce_default(val => {
217
+ const fetchPredictions = React5.useCallback(debounce_default(val => {
193
218
  if (!val) {
194
219
  clearSuggestions();
195
220
  return;
196
221
  }
197
- setSuggestions(prevState => ({
198
- ...prevState,
199
- loading: true
200
- }));
222
+ setSuggestions(prevState => {
223
+ return {
224
+ ...prevState,
225
+ loading: true
226
+ };
227
+ });
201
228
  let cachedData = {};
202
229
  try {
203
230
  cachedData = JSON.parse(sessionStorage.getItem(upaCacheKey) || "{}");
204
231
  } catch (error) {}
205
232
  if (cache) {
206
233
  cachedData = Object.keys(cachedData).reduce((acc, key) => {
207
- if (cachedData[key].maxAge - Date.now() >= 0) acc[key] = cachedData[key];
234
+ if (cachedData[key].maxAge - Date.now() >= 0) {
235
+ acc[key] = cachedData[key];
236
+ }
208
237
  return acc;
209
238
  }, {});
210
239
  if (cachedData[val]) {
@@ -236,21 +265,27 @@ var usePlacesAutocomplete = ({
236
265
  }
237
266
  });
238
267
  }, debounce2), [debounce2, clearSuggestions]);
239
- const setValue = useCallback((val, shouldFetchData = true) => {
268
+ const setValue = React5.useCallback((val, shouldFetchData = true) => {
240
269
  setVal(val);
241
- if (asRef.current && shouldFetchData) fetchPredictions(val);
270
+ if (asRef.current && shouldFetchData) {
271
+ fetchPredictions(val);
272
+ }
242
273
  }, [fetchPredictions]);
243
- useEffect2(() => {
274
+ React5.useEffect(() => {
244
275
  if (!initOnMount) {
245
- return () => null;
276
+ return () => {
277
+ return null;
278
+ };
246
279
  }
247
- if (!googleMapsRef.current && !googleMaps && callbackName) {
280
+ if (!googleMapsRef.current && !google.maps && callbackName) {
248
281
  window[callbackName] = init;
249
282
  } else {
250
283
  init();
251
284
  }
252
285
  return () => {
253
- if (window[callbackName]) delete window[callbackName];
286
+ if (window[callbackName]) {
287
+ delete window[callbackName];
288
+ }
254
289
  };
255
290
  }, [callbackName, init]);
256
291
  return {
package/dist/index.d.ts CHANGED
@@ -1,11 +1,7 @@
1
1
  import * as react_jsx_runtime from 'react/jsx-runtime';
2
2
  import * as React from 'react';
3
- import { ScriptStatus } from '@ttoss/react-hooks';
4
3
 
5
- type Extends<T, U extends T> = U;
6
4
  type GoogleMaps = typeof google.maps;
7
- type LoadedMapsStatus = Extends<ScriptStatus, 'ready'>;
8
- type NotLoadedMapStatus = Extends<ScriptStatus, 'idle' | 'error' | 'loading'>;
9
5
  type Libraries = 'places' | 'visualization' | 'drawing' | 'geometry';
10
6
  declare const GoogleMapsProvider: ({ children, apiKey, libraries, language, }: {
11
7
  children: React.ReactNode;
@@ -16,18 +12,6 @@ declare const GoogleMapsProvider: ({ children, apiKey, libraries, language, }: {
16
12
  */
17
13
  language?: string;
18
14
  }) => react_jsx_runtime.JSX.Element;
19
- /**
20
- *
21
- * @returns param.googleMaps: GoogleMaps - returns the google maps object which
22
- * provides access to the [Google Maps API](https://developers.google.com/maps/documentation/javascript/overview).
23
- */
24
- declare const useGoogleMaps: () => {
25
- status: LoadedMapsStatus;
26
- googleMaps: GoogleMaps;
27
- } | {
28
- status: NotLoadedMapStatus;
29
- googleMaps: null;
30
- };
31
15
 
32
16
  declare const useGeocoder: () => {
33
17
  geocoder: google.maps.Geocoder | null;
@@ -36,11 +20,12 @@ declare const useGeocoder: () => {
36
20
 
37
21
  declare const useMap: (options?: google.maps.MapOptions) => {
38
22
  /**
39
- * asss
23
+ * Returns the map object which provides access to the [Google Maps API](https://developers.google.com/maps/documentation/javascript/overview).
40
24
  */
41
25
  map: google.maps.Map | null;
42
26
  /**
43
- * hhhh
27
+ * Returns the ref object which provides access to the HTMLDivElement element
28
+ * that the map is rendered in.
44
29
  */
45
30
  ref: React.MutableRefObject<HTMLDivElement | null>;
46
31
  };
@@ -75,4 +60,23 @@ interface HookReturn {
75
60
  }
76
61
  declare const usePlacesAutocomplete: ({ requestOptions, debounce, cache, cacheKey, callbackName, defaultValue, initOnMount, }?: HookArgs) => HookReturn;
77
62
 
63
+ /**
64
+ * Returns the status of the Google Maps API and the google object which
65
+ * provides access to the [Google Maps API](https://developers.google.com/maps/documentation/javascript/overview).
66
+ *
67
+ * @returns An object containing the API status and the google object.
68
+ */
69
+ declare const useGoogleMaps: () => {
70
+ status: "idle" | "loading" | "ready" | "error";
71
+ google: {
72
+ maps: GoogleMaps;
73
+ } | {
74
+ maps: null;
75
+ };
76
+ /**
77
+ * @deprecated Use google.maps instead.
78
+ */
79
+ googleMaps: typeof google.maps | null;
80
+ };
81
+
78
82
  export { GoogleMapsProvider, useGeocoder, useGoogleMaps, useMap, usePlacesAutocomplete };
package/package.json CHANGED
@@ -1,6 +1,7 @@
1
1
  {
2
2
  "name": "@ttoss/google-maps",
3
- "version": "2.0.3",
3
+ "version": "2.0.5",
4
+ "license": "MIT",
4
5
  "author": "ttoss",
5
6
  "contributors": [
6
7
  "Pedro Arantes <pedro@arantespp.com> (https://arantespp.com/contact)",
@@ -23,8 +24,8 @@
23
24
  "src"
24
25
  ],
25
26
  "dependencies": {
26
- "@types/google.maps": "^3.54.10",
27
- "use-callback-ref": "^1.3.0",
27
+ "@types/google.maps": "^3.58.1",
28
+ "use-callback-ref": "^1.3.2",
28
29
  "@ttoss/react-hooks": "^2.0.3"
29
30
  },
30
31
  "peerDependencies": {
@@ -36,8 +37,8 @@
36
37
  "jest": "^29.7.0",
37
38
  "react": "^18.3.1",
38
39
  "tsup": "^8.3.0",
39
- "@ttoss/config": "^1.34.0",
40
- "@ttoss/test-utils": "^2.1.16"
40
+ "@ttoss/test-utils": "^2.1.16",
41
+ "@ttoss/config": "^1.34.0"
41
42
  },
42
43
  "keywords": [
43
44
  "Google",
@@ -9,18 +9,24 @@ type LoadedMapsStatus = Extends<ScriptStatus, 'ready'>;
9
9
 
10
10
  type NotLoadedMapStatus = Extends<ScriptStatus, 'idle' | 'error' | 'loading'>;
11
11
 
12
- const GoogleMapsContext = React.createContext<
12
+ export const GoogleMapsContext = React.createContext<
13
13
  | {
14
14
  status: LoadedMapsStatus;
15
- googleMaps: GoogleMaps;
15
+ google: {
16
+ maps: GoogleMaps;
17
+ };
16
18
  }
17
19
  | {
18
20
  status: NotLoadedMapStatus;
19
- googleMaps: null;
21
+ google: {
22
+ maps: null;
23
+ };
20
24
  }
21
25
  >({
22
26
  status: 'idle',
23
- googleMaps: null,
27
+ google: {
28
+ maps: null,
29
+ },
24
30
  });
25
31
 
26
32
  type Libraries = 'places' | 'visualization' | 'drawing' | 'geometry';
@@ -55,27 +61,31 @@ export const GoogleMapsProvider = ({
55
61
 
56
62
  const { status } = useScript(src);
57
63
 
58
- const googleMaps = React.useMemo(() => {
59
- if (status === 'ready' && window.google.maps) {
60
- return window.google.maps;
64
+ const google = React.useMemo(() => {
65
+ if (status === 'ready' && window.google) {
66
+ return window.google;
61
67
  }
62
68
 
63
69
  return null;
64
70
  }, [status]);
65
71
 
66
72
  const value = React.useMemo(() => {
67
- if (status === 'ready' && googleMaps) {
73
+ if (status === 'ready' && google?.maps) {
68
74
  return {
69
75
  status,
70
- googleMaps,
76
+ google: {
77
+ maps: google.maps,
78
+ },
71
79
  };
72
80
  }
73
81
 
74
82
  return {
75
83
  status: status as NotLoadedMapStatus,
76
- googleMaps: null,
84
+ google: {
85
+ maps: null,
86
+ },
77
87
  };
78
- }, [googleMaps, status]);
88
+ }, [google, status]);
79
89
 
80
90
  return (
81
91
  <GoogleMapsContext.Provider value={value}>
@@ -83,12 +93,3 @@ export const GoogleMapsProvider = ({
83
93
  </GoogleMapsContext.Provider>
84
94
  );
85
95
  };
86
-
87
- /**
88
- *
89
- * @returns param.googleMaps: GoogleMaps - returns the google maps object which
90
- * provides access to the [Google Maps API](https://developers.google.com/maps/documentation/javascript/overview).
91
- */
92
- export const useGoogleMaps = () => {
93
- return React.useContext(GoogleMapsContext);
94
- };
package/src/index.ts CHANGED
@@ -1,4 +1,5 @@
1
- export { GoogleMapsProvider, useGoogleMaps } from './GoogleMapsProvider';
1
+ export { GoogleMapsProvider } from './GoogleMapsProvider';
2
2
  export { useGeocoder } from './useGeocoder';
3
3
  export { useMap } from './useMap';
4
4
  export { usePlacesAutocomplete } from './usePlacesAutocomplete';
5
+ export { useGoogleMaps } from './useGoogleMaps';
@@ -1,21 +1,21 @@
1
1
  import * as React from 'react';
2
- import { useGoogleMaps } from './GoogleMapsProvider';
2
+ import { useGoogleMaps } from './useGoogleMaps';
3
3
 
4
4
  export const useGeocoder = () => {
5
- const { googleMaps } = useGoogleMaps();
5
+ const { google } = useGoogleMaps();
6
6
 
7
7
  const [isGeocoderInitialized, setIsGeocoderInitialized] =
8
8
  React.useState(false);
9
9
 
10
10
  const geocoder = React.useMemo(() => {
11
- if (googleMaps) {
12
- const googleMapsGeocoder = new googleMaps.Geocoder();
11
+ if (google.maps) {
12
+ const googleMapsGeocoder = new google.maps.Geocoder();
13
13
  setIsGeocoderInitialized(true);
14
14
  return googleMapsGeocoder;
15
15
  }
16
16
 
17
17
  return null;
18
- }, [googleMaps]);
18
+ }, [google.maps]);
19
19
 
20
20
  return { geocoder, isGeocoderInitialized };
21
21
  };
@@ -0,0 +1,20 @@
1
+ import * as React from 'react';
2
+ import { GoogleMapsContext } from './GoogleMapsProvider';
3
+
4
+ /**
5
+ * Returns the status of the Google Maps API and the google object which
6
+ * provides access to the [Google Maps API](https://developers.google.com/maps/documentation/javascript/overview).
7
+ *
8
+ * @returns An object containing the API status and the google object.
9
+ */
10
+ export const useGoogleMaps = () => {
11
+ const { status, google } = React.useContext(GoogleMapsContext);
12
+ return {
13
+ status,
14
+ google,
15
+ /**
16
+ * @deprecated Use google.maps instead.
17
+ */
18
+ googleMaps: google.maps,
19
+ };
20
+ };
package/src/useMap.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import * as React from 'react';
2
2
  import { useCallbackRef } from 'use-callback-ref';
3
- import { useGoogleMaps } from './GoogleMapsProvider';
3
+ import { useGoogleMaps } from './useGoogleMaps';
4
4
 
5
5
  export const useMap = (options: google.maps.MapOptions = {}) => {
6
6
  /**
@@ -15,16 +15,16 @@ export const useMap = (options: google.maps.MapOptions = {}) => {
15
15
  });
16
16
  });
17
17
 
18
- const { googleMaps } = useGoogleMaps();
18
+ const { google } = useGoogleMaps();
19
19
 
20
20
  const map = React.useMemo(() => {
21
- if (googleMaps && ref.current) {
22
- return new googleMaps.Map(ref.current, options);
21
+ if (google?.maps && ref.current) {
22
+ return new google.maps.Map(ref.current, options);
23
23
  }
24
24
 
25
25
  return null;
26
26
  // eslint-disable-next-line react-hooks/exhaustive-deps
27
- }, [googleMaps, ref.current]);
27
+ }, [google?.maps, ref.current]);
28
28
 
29
29
  /**
30
30
  * To avoid re-initializing the map because shallow object comparison.
@@ -44,11 +44,12 @@ export const useMap = (options: google.maps.MapOptions = {}) => {
44
44
 
45
45
  return {
46
46
  /**
47
- * asss
47
+ * Returns the map object which provides access to the [Google Maps API](https://developers.google.com/maps/documentation/javascript/overview).
48
48
  */
49
49
  map,
50
50
  /**
51
- * hhhh
51
+ * Returns the ref object which provides access to the HTMLDivElement element
52
+ * that the map is rendered in.
52
53
  */
53
54
  ref,
54
55
  };