@ttoss/google-maps 0.7.0 → 0.9.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 CHANGED
@@ -1,8 +1,4 @@
1
- <h1 align="center">
2
-
3
- `@ttoss/google-maps`
4
-
5
- </h1>
1
+ # @ttoss/google-maps
6
2
 
7
3
  ## 📚 About
8
4
 
@@ -10,18 +6,235 @@
10
6
 
11
7
  ## 🚀 Get Started
12
8
 
13
- - Install Dependencies
9
+ ### Install @ttoss/google-maps
14
10
 
15
- ```sh
16
- $ yarn
11
+ ```shell
12
+ $ yarn add
13
+ # or
14
+ $ npm install @ttoss/google-maps
17
15
  ```
18
16
 
19
- - Build @ttoss/google-maps
17
+ ## 📄 Examples of use
20
18
 
21
- ```sh
22
- $ yarn build
23
- # or
24
- $ npm run build
19
+ ### Map
20
+
21
+ ```tsx
22
+ export type MapProps = {
23
+ width?: number | string;
24
+ height?: number | string;
25
+ image?: Omit<MapImageOptions, 'map'>;
26
+ options?: google.maps.MapOptions;
27
+ };
28
+
29
+ const Map = ({ height, width, image, options }: MapProps) => {
30
+ const { googleMaps } = useGoogleMaps();
31
+
32
+ const [mapDivRef, setMapDivRef] = React.useState<HTMLDivElement | null>(null);
33
+
34
+ const map = React.useMemo(() => {
35
+ if (googleMaps && mapDivRef) {
36
+ const googleMapsMap = new googleMaps.Map(mapDivRef, defaultOptions);
37
+ return googleMapsMap;
38
+ }
39
+
40
+ return null;
41
+ }, [googleMaps, mapDivRef]);
42
+
43
+ React.useEffect(() => {
44
+ if (map && options?.center) {
45
+ map.setCenter(options.center);
46
+ }
47
+ }, [map, options?.center]);
48
+
49
+ return (
50
+ <div id="google-maps" ref={(r) => setMapDivRef(r)} sx={{ width, height }} />
51
+ );
52
+ };
53
+
54
+ Map.defaultProps = {
55
+ width: 400,
56
+ height: 300,
57
+ } as MapProps;
58
+
59
+ export default Map;
60
+ ```
61
+
62
+ ### Latitude and Longitude
63
+
64
+ ```ts
65
+ import { useGeocoder } from '@ttoss/google-maps';
66
+
67
+ type MapGeocoderResult = google.maps.GeocoderResult;
68
+ type MapGeocoderStatus = google.maps.GeocoderStatus;
69
+
70
+ export type LatLng = {
71
+ lat: number;
72
+ lng: number;
73
+ };
74
+
75
+ export const useGeocoding = () => {
76
+ const { geocoder } = useGeocoder();
77
+
78
+ const getLatLong = ({ address }: { address: string }): Promise<LatLng> => {
79
+ return new Promise((resolve, reject) => {
80
+ if (!geocoder) {
81
+ return reject(new Error('Geocoder not defined'));
82
+ }
83
+
84
+ geocoder.geocode(
85
+ { address },
86
+ (results: MapGeocoderResult[] | null, status: MapGeocoderStatus) => {
87
+ if (results && status === window.google.maps.GeocoderStatus.OK) {
88
+ const lat = results[0].geometry.location.lat();
89
+ const lng = results[0].geometry.location.lng();
90
+
91
+ return resolve({
92
+ lat,
93
+ lng,
94
+ });
95
+ } else {
96
+ console.log(
97
+ 'Geocode was not successful for the following reason: ',
98
+ status
99
+ );
100
+ return reject(new Error('Result not found'));
101
+ }
102
+ }
103
+ );
104
+ });
105
+ };
106
+
107
+ return { getLatLong };
108
+ };
109
+ ```
110
+
111
+ ### Providers
112
+
113
+ ```tsx
114
+ import * as React from 'react';
115
+
116
+ import { GoogleMapsProvider } from '@ttoss/google-maps';
117
+
118
+ const Providers: React.FC = ({ children }) => {
119
+ const apiKey = 'YOUR_API_KEY';
120
+
121
+ if (!apiKey) {
122
+ return <>{children}</>;
123
+ }
124
+
125
+ return (
126
+ <GoogleMapsProvider apiKey={apiKey} libraries={['places']}>
127
+ {children}
128
+ </GoogleMapsProvider>
129
+ );
130
+ };
131
+
132
+ export default Providers;
133
+ ```
134
+
135
+ ### Autocomplete
136
+
137
+ ```tsx
138
+ import React from 'react';
139
+
140
+ import { usePlacesAutocomplete } from '@ttoss/google-maps';
141
+
142
+ const Component = () => {
143
+ const [text, setText] = React.useState('');
144
+ const clearText = () => setText('');
145
+
146
+ const apiKey = 'YOUR_API_KEY';
147
+
148
+ const {
149
+ ready,
150
+ value,
151
+ suggestions: { status, data },
152
+ setValue,
153
+ clearSuggestions,
154
+ } = usePlacesAutocomplete({
155
+ debounce: 300,
156
+ cacheKey: apiKey,
157
+ });
158
+
159
+ const handleInput = (e: any) => {
160
+ setValue(e.target.value);
161
+ };
162
+
163
+ const handleSelect =
164
+ ({ description }: { description: string }) =>
165
+ () => {
166
+ setValue(description, false);
167
+ clearSuggestions();
168
+ };
169
+
170
+ const renderSuggestions = () =>
171
+ data.map((suggestion) => {
172
+ const {
173
+ place_id,
174
+ structured_formatting: { main_text, secondary_text },
175
+ } = suggestion;
176
+
177
+ return (
178
+ <li key={place_id} onClick={() => handleSelect(suggestion)}>
179
+ <strong>{main_text}</strong>
180
+
181
+ <small>{secondary_text}</small>
182
+ </li>
183
+ );
184
+ });
185
+
186
+ return (
187
+ <div>
188
+ <input value={value} onChange={handleInput} disabled={!ready} />
189
+
190
+ {status === 'OK' && <ul>{renderSuggestions()}</ul>}
191
+ </div>
192
+ );
193
+ };
194
+
195
+ export default Component;
196
+ ```
197
+
198
+ ## 📘 Types
199
+
200
+ ```ts
201
+ import { ScriptStatus } from '@ttoss/hooks';
202
+
203
+ const useMap: (options: google.maps.MapOptions) => {
204
+ map: google.maps.Map | null;
205
+ ref: React.RefObject<HTMLDivElement>;
206
+ isMapInitialized: boolean;
207
+ };
208
+
209
+ const useGeocoder: () => {
210
+ geocoder: google.maps.Geocoder | null;
211
+ isGeocoderInitialized: boolean;
212
+ };
213
+
214
+ type Libraries = 'places' | 'visualization' | 'drawing' | 'geometry';
215
+
216
+ type Extends<T, U extends T> = U;
217
+
218
+ type LoadedMapsStatus = Extends<ScriptStatus, 'ready'>;
219
+
220
+ type NotLoadedMapStatus = Extends<ScriptStatus, 'idle' | 'error' | 'loading'>;
221
+
222
+ export type GoogleMaps = typeof google.maps;
223
+
224
+ const GoogleMapsProvider: React.FC<{
225
+ apiKey: string;
226
+ libraries?: Libraries[] | undefined;
227
+ }>;
228
+
229
+ const useGoogleMaps: () =>
230
+ | {
231
+ status: LoadedMapsStatus;
232
+ googleMaps: GoogleMaps;
233
+ }
234
+ | {
235
+ status: NotLoadedMapStatus;
236
+ googleMaps: null;
237
+ };
25
238
  ```
26
239
 
27
240
  ---
@@ -33,5 +246,5 @@ $ npm run build
33
246
  <br/>
34
247
 
35
248
  <h4 align="center">
36
- Special thanks for the inspiring <a href="https://github.com/jmarceli/react-hook-google-maps">react-hook-google-maps</a> documentation.
249
+ Special thanks for the inspiring <a href="https://github.com/jmarceli/react-hook-google-maps">react-hook-google-maps</a> and <a href="https://github.com/wellyshen/use-places-autocomplete">use-places-autocomplete</a> documentation.
37
250
  </h4>
package/dist/esm/index.js CHANGED
@@ -3,26 +3,21 @@ import * as React from "react";
3
3
 
4
4
  // src/GoogleMapsProvider.tsx
5
5
  import { useScript } from "@ttoss/hooks";
6
- import {
7
- createContext,
8
- createElement,
9
- useContext,
10
- useMemo
11
- } from "react";
12
- var GoogleMapsContext = createContext({
6
+ import * as React2 from "react";
7
+ var GoogleMapsContext = React2.createContext({
13
8
  status: "idle",
14
9
  googleMaps: null
15
10
  });
16
11
  var GoogleMapsProvider = ({ children, apiKey, libraries }) => {
17
12
  const src = `https://maps.googleapis.com/maps/api/js?key=${apiKey}&libraries=${libraries == null ? void 0 : libraries.join(",")}`;
18
13
  const { status } = useScript(src);
19
- const googleMaps = useMemo(() => {
14
+ const googleMaps = React2.useMemo(() => {
20
15
  if (status === "ready" && window.google.maps) {
21
16
  return window.google.maps;
22
17
  }
23
18
  return null;
24
19
  }, [status]);
25
- const value = useMemo(() => {
20
+ const value = React2.useMemo(() => {
26
21
  if (status === "ready" && googleMaps) {
27
22
  return {
28
23
  status,
@@ -34,21 +29,18 @@ var GoogleMapsProvider = ({ children, apiKey, libraries }) => {
34
29
  googleMaps: null
35
30
  };
36
31
  }, [googleMaps, status]);
37
- return /* @__PURE__ */ createElement(GoogleMapsContext.Provider, {
32
+ return /* @__PURE__ */ React2.createElement(GoogleMapsContext.Provider, {
38
33
  value
39
34
  }, children);
40
35
  };
41
- var useGoogleMaps = () => useContext(GoogleMapsContext);
36
+ var useGoogleMaps = () => React2.useContext(GoogleMapsContext);
42
37
 
43
38
  // src/useGeocoder.ts
44
- import {
45
- useMemo as useMemo2,
46
- useState
47
- } from "react";
39
+ import * as React3 from "react";
48
40
  var useGeocoder = () => {
49
41
  const { googleMaps } = useGoogleMaps();
50
- const [isGeocoderInitialized, setIsGeocoderInitialized] = useState(false);
51
- const geocoder = useMemo2(() => {
42
+ const [isGeocoderInitialized, setIsGeocoderInitialized] = React3.useState(false);
43
+ const geocoder = React3.useMemo(() => {
52
44
  if (googleMaps) {
53
45
  const googleMapsGeocoder = new googleMaps.Geocoder();
54
46
  setIsGeocoderInitialized(true);
@@ -60,17 +52,13 @@ var useGeocoder = () => {
60
52
  };
61
53
 
62
54
  // src/useMap.ts
63
- import {
64
- useMemo as useMemo3,
65
- useRef,
66
- useState as useState2
67
- } from "react";
55
+ import * as React4 from "react";
68
56
  var useMap = (options) => {
69
- const ref = useRef(null);
57
+ const ref = React4.useRef(null);
70
58
  const { googleMaps } = useGoogleMaps();
71
- const [isMapInitialized, setIsMapInitialized] = useState2(false);
59
+ const [isMapInitialized, setIsMapInitialized] = React4.useState(false);
72
60
  const optionsStringify = JSON.stringify(options);
73
- const map = useMemo3(() => {
61
+ const map = React4.useMemo(() => {
74
62
  if (googleMaps && ref.current) {
75
63
  const parsedOptions = JSON.parse(optionsStringify);
76
64
  const googleMapsMap = new googleMaps.Map(ref.current, parsedOptions);
package/dist/index.js CHANGED
@@ -1,28 +1,60 @@
1
- "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { newObj[key] = obj[key]; } } } newObj.default = obj; return newObj; } }// tsup.inject.js
2
- var _react = require('react'); var React = _interopRequireWildcard(_react);
3
-
4
- // src/GoogleMapsProvider.tsx
5
- var _hooks = require('@ttoss/hooks');
6
-
7
-
8
-
9
-
1
+ var __create = Object.create;
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __getProtoOf = Object.getPrototypeOf;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __markAsModule = (target) => __defProp(target, "__esModule", { value: true });
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __reExport = (target, module2, copyDefault, desc) => {
13
+ if (module2 && typeof module2 === "object" || typeof module2 === "function") {
14
+ for (let key of __getOwnPropNames(module2))
15
+ if (!__hasOwnProp.call(target, key) && (copyDefault || key !== "default"))
16
+ __defProp(target, key, { get: () => module2[key], enumerable: !(desc = __getOwnPropDesc(module2, key)) || desc.enumerable });
17
+ }
18
+ return target;
19
+ };
20
+ var __toESM = (module2, isNodeMode) => {
21
+ return __reExport(__markAsModule(__defProp(module2 != null ? __create(__getProtoOf(module2)) : {}, "default", !isNodeMode && module2 && module2.__esModule ? { get: () => module2.default, enumerable: true } : { value: module2, enumerable: true })), module2);
22
+ };
23
+ var __toCommonJS = /* @__PURE__ */ ((cache) => {
24
+ return (module2, temp) => {
25
+ return cache && cache.get(module2) || (temp = __reExport(__markAsModule({}), module2, 1), cache && cache.set(module2, temp), temp);
26
+ };
27
+ })(typeof WeakMap !== "undefined" ? /* @__PURE__ */ new WeakMap() : 0);
28
+
29
+ // src/index.ts
30
+ var src_exports = {};
31
+ __export(src_exports, {
32
+ GoogleMapsProvider: () => GoogleMapsProvider,
33
+ useGeocoder: () => useGeocoder,
34
+ useGoogleMaps: () => useGoogleMaps,
35
+ useMap: () => useMap
36
+ });
10
37
 
38
+ // tsup.inject.js
39
+ var React = __toESM(require("react"));
11
40
 
12
- var GoogleMapsContext = _react.createContext.call(void 0, {
41
+ // src/GoogleMapsProvider.tsx
42
+ var import_hooks = require("@ttoss/hooks");
43
+ var React2 = __toESM(require("react"));
44
+ var GoogleMapsContext = React2.createContext({
13
45
  status: "idle",
14
46
  googleMaps: null
15
47
  });
16
48
  var GoogleMapsProvider = ({ children, apiKey, libraries }) => {
17
49
  const src = `https://maps.googleapis.com/maps/api/js?key=${apiKey}&libraries=${libraries == null ? void 0 : libraries.join(",")}`;
18
- const { status } = _hooks.useScript.call(void 0, src);
19
- const googleMaps = _react.useMemo.call(void 0, () => {
50
+ const { status } = (0, import_hooks.useScript)(src);
51
+ const googleMaps = React2.useMemo(() => {
20
52
  if (status === "ready" && window.google.maps) {
21
53
  return window.google.maps;
22
54
  }
23
55
  return null;
24
56
  }, [status]);
25
- const value = _react.useMemo.call(void 0, () => {
57
+ const value = React2.useMemo(() => {
26
58
  if (status === "ready" && googleMaps) {
27
59
  return {
28
60
  status,
@@ -34,21 +66,18 @@ var GoogleMapsProvider = ({ children, apiKey, libraries }) => {
34
66
  googleMaps: null
35
67
  };
36
68
  }, [googleMaps, status]);
37
- return /* @__PURE__ */ _react.createElement.call(void 0, GoogleMapsContext.Provider, {
69
+ return /* @__PURE__ */ React2.createElement(GoogleMapsContext.Provider, {
38
70
  value
39
71
  }, children);
40
72
  };
41
- var useGoogleMaps = () => _react.useContext.call(void 0, GoogleMapsContext);
73
+ var useGoogleMaps = () => React2.useContext(GoogleMapsContext);
42
74
 
43
75
  // src/useGeocoder.ts
44
-
45
-
46
-
47
-
76
+ var React3 = __toESM(require("react"));
48
77
  var useGeocoder = () => {
49
78
  const { googleMaps } = useGoogleMaps();
50
- const [isGeocoderInitialized, setIsGeocoderInitialized] = _react.useState.call(void 0, false);
51
- const geocoder = _react.useMemo.call(void 0, () => {
79
+ const [isGeocoderInitialized, setIsGeocoderInitialized] = React3.useState(false);
80
+ const geocoder = React3.useMemo(() => {
52
81
  if (googleMaps) {
53
82
  const googleMapsGeocoder = new googleMaps.Geocoder();
54
83
  setIsGeocoderInitialized(true);
@@ -60,17 +89,13 @@ var useGeocoder = () => {
60
89
  };
61
90
 
62
91
  // src/useMap.ts
63
-
64
-
65
-
66
-
67
-
92
+ var React4 = __toESM(require("react"));
68
93
  var useMap = (options) => {
69
- const ref = _react.useRef.call(void 0, null);
94
+ const ref = React4.useRef(null);
70
95
  const { googleMaps } = useGoogleMaps();
71
- const [isMapInitialized, setIsMapInitialized] = _react.useState.call(void 0, false);
96
+ const [isMapInitialized, setIsMapInitialized] = React4.useState(false);
72
97
  const optionsStringify = JSON.stringify(options);
73
- const map = _react.useMemo.call(void 0, () => {
98
+ const map = React4.useMemo(() => {
74
99
  if (googleMaps && ref.current) {
75
100
  const parsedOptions = JSON.parse(optionsStringify);
76
101
  const googleMapsMap = new googleMaps.Map(ref.current, parsedOptions);
@@ -81,9 +106,11 @@ var useMap = (options) => {
81
106
  }, [googleMaps, optionsStringify]);
82
107
  return { map, ref, isMapInitialized };
83
108
  };
84
-
85
-
86
-
87
-
88
-
89
- exports.GoogleMapsProvider = GoogleMapsProvider; exports.useGeocoder = useGeocoder; exports.useGoogleMaps = useGoogleMaps; exports.useMap = useMap;
109
+ module.exports = __toCommonJS(src_exports);
110
+ // Annotate the CommonJS export names for ESM import in node:
111
+ 0 && (module.exports = {
112
+ GoogleMapsProvider,
113
+ useGeocoder,
114
+ useGoogleMaps,
115
+ useMap
116
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ttoss/google-maps",
3
- "version": "0.7.0",
3
+ "version": "0.9.0",
4
4
  "author": "ttoss",
5
5
  "contributors": [
6
6
  {
@@ -31,12 +31,12 @@
31
31
  "build": "tsup"
32
32
  },
33
33
  "dependencies": {
34
- "@ttoss/hooks": "^0.7.0",
34
+ "@ttoss/hooks": "^0.9.0",
35
35
  "@types/google.maps": "^3.45.6"
36
36
  },
37
37
  "peerDependencies": {
38
38
  "react": ">=16.8.0",
39
39
  "react-dom": ">=16.8.0"
40
40
  },
41
- "gitHead": "a30e8313b3d18efe8914133e56951cbc419d86da"
41
+ "gitHead": "bb45872f0106803249cb8ef1e6971ca17609cb3d"
42
42
  }