@ttoss/google-maps 0.6.0 → 0.8.3

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/esm/index.js CHANGED
@@ -1 +1,89 @@
1
- import*as c from"react";import{useScript as M}from"@ttoss/hooks";import{createContext as m,createElement as f,useContext as S,useMemo as u}from"react";var i=m({status:"idle",googleMaps:null}),d=({children:a,apiKey:s,libraries:t})=>{let r=`https://maps.googleapis.com/maps/api/js?key=${s}&libraries=${t==null?void 0:t.join(",")}`,{status:e}=M(r),o=u(()=>e==="ready"&&window.google.maps?window.google.maps:null,[e]),n=u(()=>e==="ready"&&o?{status:e,googleMaps:o}:{status:e,googleMaps:null},[o,e]);return f(i.Provider,{value:n},a)},p=()=>S(i);import{useMemo as G,useRef as y,useState as R}from"react";var x=a=>{let s=y(null),{googleMaps:t}=p(),[r,e]=R(!1),o=JSON.stringify(a);return{map:G(()=>{if(t&&s.current){let l=JSON.parse(o),g=new t.Map(s.current,l);return e(!0),g}return null},[t,o]),ref:s,isMapInitialized:r}};export{d as GoogleMapsProvider,p as useGoogleMaps,x as useMap};
1
+ // tsup.inject.js
2
+ import * as React from "react";
3
+
4
+ // src/GoogleMapsProvider.tsx
5
+ import { useScript } from "@ttoss/hooks";
6
+ import {
7
+ createContext,
8
+ createElement,
9
+ useContext,
10
+ useMemo
11
+ } from "react";
12
+ var GoogleMapsContext = createContext({
13
+ status: "idle",
14
+ googleMaps: null
15
+ });
16
+ var GoogleMapsProvider = ({ children, apiKey, libraries }) => {
17
+ const src = `https://maps.googleapis.com/maps/api/js?key=${apiKey}&libraries=${libraries == null ? void 0 : libraries.join(",")}`;
18
+ const { status } = useScript(src);
19
+ const googleMaps = useMemo(() => {
20
+ if (status === "ready" && window.google.maps) {
21
+ return window.google.maps;
22
+ }
23
+ return null;
24
+ }, [status]);
25
+ const value = useMemo(() => {
26
+ if (status === "ready" && googleMaps) {
27
+ return {
28
+ status,
29
+ googleMaps
30
+ };
31
+ }
32
+ return {
33
+ status,
34
+ googleMaps: null
35
+ };
36
+ }, [googleMaps, status]);
37
+ return /* @__PURE__ */ createElement(GoogleMapsContext.Provider, {
38
+ value
39
+ }, children);
40
+ };
41
+ var useGoogleMaps = () => useContext(GoogleMapsContext);
42
+
43
+ // src/useGeocoder.ts
44
+ import {
45
+ useMemo as useMemo2,
46
+ useState
47
+ } from "react";
48
+ var useGeocoder = () => {
49
+ const { googleMaps } = useGoogleMaps();
50
+ const [isGeocoderInitialized, setIsGeocoderInitialized] = useState(false);
51
+ const geocoder = useMemo2(() => {
52
+ if (googleMaps) {
53
+ const googleMapsGeocoder = new googleMaps.Geocoder();
54
+ setIsGeocoderInitialized(true);
55
+ return googleMapsGeocoder;
56
+ }
57
+ return null;
58
+ }, [googleMaps]);
59
+ return { geocoder, isGeocoderInitialized };
60
+ };
61
+
62
+ // src/useMap.ts
63
+ import {
64
+ useMemo as useMemo3,
65
+ useRef,
66
+ useState as useState2
67
+ } from "react";
68
+ var useMap = (options) => {
69
+ const ref = useRef(null);
70
+ const { googleMaps } = useGoogleMaps();
71
+ const [isMapInitialized, setIsMapInitialized] = useState2(false);
72
+ const optionsStringify = JSON.stringify(options);
73
+ const map = useMemo3(() => {
74
+ if (googleMaps && ref.current) {
75
+ const parsedOptions = JSON.parse(optionsStringify);
76
+ const googleMapsMap = new googleMaps.Map(ref.current, parsedOptions);
77
+ setIsMapInitialized(true);
78
+ return googleMapsMap;
79
+ }
80
+ return null;
81
+ }, [googleMaps, optionsStringify]);
82
+ return { map, ref, isMapInitialized };
83
+ };
84
+ export {
85
+ GoogleMapsProvider,
86
+ useGeocoder,
87
+ useGoogleMaps,
88
+ useMap
89
+ };
package/dist/index.d.ts CHANGED
@@ -23,10 +23,15 @@ declare const useGoogleMaps: () => {
23
23
  googleMaps: null;
24
24
  };
25
25
 
26
+ declare const useGeocoder: () => {
27
+ geocoder: google.maps.Geocoder | null;
28
+ isGeocoderInitialized: boolean;
29
+ };
30
+
26
31
  declare const useMap: (options: google.maps.MapOptions) => {
27
32
  map: google.maps.Map | null;
28
33
  ref: React.RefObject<HTMLDivElement>;
29
34
  isMapInitialized: boolean;
30
35
  };
31
36
 
32
- export { GoogleMapsProvider, useGoogleMaps, useMap };
37
+ export { GoogleMapsProvider, useGeocoder, useGoogleMaps, useMap };
package/dist/index.js CHANGED
@@ -1 +1,89 @@
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; } }var _react = require('react'); var c = _interopRequireWildcard(_react);var _hooks = require('@ttoss/hooks');var i=_react.createContext.call(void 0, {status:"idle",googleMaps:null}),d= exports.GoogleMapsProvider =({children:a,apiKey:s,libraries:t})=>{let r=`https://maps.googleapis.com/maps/api/js?key=${s}&libraries=${t==null?void 0:t.join(",")}`,{status:e}=_hooks.useScript.call(void 0, r),o=_react.useMemo.call(void 0, ()=>e==="ready"&&window.google.maps?window.google.maps:null,[e]),n=_react.useMemo.call(void 0, ()=>e==="ready"&&o?{status:e,googleMaps:o}:{status:e,googleMaps:null},[o,e]);return _react.createElement.call(void 0, i.Provider,{value:n},a)},p= exports.useGoogleMaps =()=>_react.useContext.call(void 0, i);var x=a=>{let s=_react.useRef.call(void 0, null),{googleMaps:t}=p(),[r,e]=_react.useState.call(void 0, !1),o=JSON.stringify(a);return{map:_react.useMemo.call(void 0, ()=>{if(t&&s.current){let l=JSON.parse(o),g=new t.Map(s.current,l);return e(!0),g}return null},[t,o]),ref:s,isMapInitialized:r}};exports.GoogleMapsProvider = d; exports.useGoogleMaps = p; exports.useMap = x;
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
+
10
+
11
+
12
+ var GoogleMapsContext = _react.createContext.call(void 0, {
13
+ status: "idle",
14
+ googleMaps: null
15
+ });
16
+ var GoogleMapsProvider = ({ children, apiKey, libraries }) => {
17
+ 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, () => {
20
+ if (status === "ready" && window.google.maps) {
21
+ return window.google.maps;
22
+ }
23
+ return null;
24
+ }, [status]);
25
+ const value = _react.useMemo.call(void 0, () => {
26
+ if (status === "ready" && googleMaps) {
27
+ return {
28
+ status,
29
+ googleMaps
30
+ };
31
+ }
32
+ return {
33
+ status,
34
+ googleMaps: null
35
+ };
36
+ }, [googleMaps, status]);
37
+ return /* @__PURE__ */ _react.createElement.call(void 0, GoogleMapsContext.Provider, {
38
+ value
39
+ }, children);
40
+ };
41
+ var useGoogleMaps = () => _react.useContext.call(void 0, GoogleMapsContext);
42
+
43
+ // src/useGeocoder.ts
44
+
45
+
46
+
47
+
48
+ var useGeocoder = () => {
49
+ const { googleMaps } = useGoogleMaps();
50
+ const [isGeocoderInitialized, setIsGeocoderInitialized] = _react.useState.call(void 0, false);
51
+ const geocoder = _react.useMemo.call(void 0, () => {
52
+ if (googleMaps) {
53
+ const googleMapsGeocoder = new googleMaps.Geocoder();
54
+ setIsGeocoderInitialized(true);
55
+ return googleMapsGeocoder;
56
+ }
57
+ return null;
58
+ }, [googleMaps]);
59
+ return { geocoder, isGeocoderInitialized };
60
+ };
61
+
62
+ // src/useMap.ts
63
+
64
+
65
+
66
+
67
+
68
+ var useMap = (options) => {
69
+ const ref = _react.useRef.call(void 0, null);
70
+ const { googleMaps } = useGoogleMaps();
71
+ const [isMapInitialized, setIsMapInitialized] = _react.useState.call(void 0, false);
72
+ const optionsStringify = JSON.stringify(options);
73
+ const map = _react.useMemo.call(void 0, () => {
74
+ if (googleMaps && ref.current) {
75
+ const parsedOptions = JSON.parse(optionsStringify);
76
+ const googleMapsMap = new googleMaps.Map(ref.current, parsedOptions);
77
+ setIsMapInitialized(true);
78
+ return googleMapsMap;
79
+ }
80
+ return null;
81
+ }, [googleMaps, optionsStringify]);
82
+ return { map, ref, isMapInitialized };
83
+ };
84
+
85
+
86
+
87
+
88
+
89
+ exports.GoogleMapsProvider = GoogleMapsProvider; exports.useGeocoder = useGeocoder; exports.useGoogleMaps = useGoogleMaps; exports.useMap = useMap;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ttoss/google-maps",
3
- "version": "0.6.0",
3
+ "version": "0.8.3",
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.6.0",
34
+ "@ttoss/hooks": "^0.8.3",
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": "416aef959159ac96e70b59f38c85a1f6155fb12b"
41
+ "gitHead": "4a0b5b1f51c9241b0b8d4a35b88a84d856d46d44"
42
42
  }
@@ -20,7 +20,7 @@ const googleMock = {
20
20
  };
21
21
 
22
22
  beforeAll(() => {
23
- (global.google as any) = googleMock;
23
+ (global.google as unknown) = googleMock;
24
24
  });
25
25
 
26
26
  const RenderStatus = () => {
@@ -0,0 +1,8 @@
1
+ import * as googleMaps from './index';
2
+
3
+ test('should export all methods', () => {
4
+ expect(googleMaps.GoogleMapsProvider).toBeDefined();
5
+ expect(googleMaps.useGeocoder).toBeDefined();
6
+ expect(googleMaps.useGoogleMaps).toBeDefined();
7
+ expect(googleMaps.useMap).toBeDefined();
8
+ });
package/src/index.ts CHANGED
@@ -1,2 +1,3 @@
1
1
  export { GoogleMapsProvider, useGoogleMaps } from './GoogleMapsProvider';
2
+ export { useGeocoder } from './useGeocoder';
2
3
  export { useMap } from './useMap';
@@ -0,0 +1,22 @@
1
+ import * as React from 'react';
2
+
3
+ import { useGoogleMaps } from './GoogleMapsProvider';
4
+
5
+ export const useGeocoder = () => {
6
+ const { googleMaps } = useGoogleMaps();
7
+
8
+ const [isGeocoderInitialized, setIsGeocoderInitialized] =
9
+ React.useState(false);
10
+
11
+ const geocoder = React.useMemo(() => {
12
+ if (googleMaps) {
13
+ const googleMapsGeocoder = new googleMaps.Geocoder();
14
+ setIsGeocoderInitialized(true);
15
+ return googleMapsGeocoder;
16
+ }
17
+
18
+ return null;
19
+ }, [googleMaps]);
20
+
21
+ return { geocoder, isGeocoderInitialized };
22
+ };