@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 +89 -1
- package/dist/index.d.ts +6 -1
- package/dist/index.js +89 -1
- package/package.json +3 -3
- package/src/GoogleMapsProvider.spec.tsx +1 -1
- package/src/index.spec.ts +8 -0
- package/src/index.ts +1 -0
- package/src/useGeocoder.ts +22 -0
package/dist/esm/index.js
CHANGED
|
@@ -1 +1,89 @@
|
|
|
1
|
-
|
|
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; } }
|
|
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.
|
|
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.
|
|
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": "
|
|
41
|
+
"gitHead": "4a0b5b1f51c9241b0b8d4a35b88a84d856d46d44"
|
|
42
42
|
}
|
|
@@ -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
|
@@ -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
|
+
};
|