@ttoss/google-maps 0.9.2 → 1.4.1

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,77 @@
1
+ // tsup.inject.js
2
+ import * as React from "react";
3
+
4
+ // src/GoogleMapsProvider.tsx
5
+ import { useScript } from "@ttoss/hooks";
6
+ import * as React2 from "react";
7
+ var GoogleMapsContext = React2.createContext({
8
+ status: "idle",
9
+ googleMaps: null
10
+ });
11
+ var GoogleMapsProvider = ({ children, apiKey, libraries }) => {
12
+ const src = `https://maps.googleapis.com/maps/api/js?key=${apiKey}&libraries=${libraries == null ? void 0 : libraries.join(",")}`;
13
+ const { status } = useScript(src);
14
+ const googleMaps = React2.useMemo(() => {
15
+ if (status === "ready" && window.google.maps) {
16
+ return window.google.maps;
17
+ }
18
+ return null;
19
+ }, [status]);
20
+ const value = React2.useMemo(() => {
21
+ if (status === "ready" && googleMaps) {
22
+ return {
23
+ status,
24
+ googleMaps
25
+ };
26
+ }
27
+ return {
28
+ status,
29
+ googleMaps: null
30
+ };
31
+ }, [googleMaps, status]);
32
+ return /* @__PURE__ */ React2.createElement(GoogleMapsContext.Provider, {
33
+ value
34
+ }, children);
35
+ };
36
+ var useGoogleMaps = () => React2.useContext(GoogleMapsContext);
37
+
38
+ // src/useGeocoder.ts
39
+ import * as React3 from "react";
40
+ var useGeocoder = () => {
41
+ const { googleMaps } = useGoogleMaps();
42
+ const [isGeocoderInitialized, setIsGeocoderInitialized] = React3.useState(false);
43
+ const geocoder = React3.useMemo(() => {
44
+ if (googleMaps) {
45
+ const googleMapsGeocoder = new googleMaps.Geocoder();
46
+ setIsGeocoderInitialized(true);
47
+ return googleMapsGeocoder;
48
+ }
49
+ return null;
50
+ }, [googleMaps]);
51
+ return { geocoder, isGeocoderInitialized };
52
+ };
53
+
54
+ // src/useMap.ts
55
+ import * as React4 from "react";
56
+ var useMap = (options) => {
57
+ const ref = React4.useRef(null);
58
+ const { googleMaps } = useGoogleMaps();
59
+ const [isMapInitialized, setIsMapInitialized] = React4.useState(false);
60
+ const optionsStringify = JSON.stringify(options);
61
+ const map = React4.useMemo(() => {
62
+ if (googleMaps && ref.current) {
63
+ const parsedOptions = JSON.parse(optionsStringify);
64
+ const googleMapsMap = new googleMaps.Map(ref.current, parsedOptions);
65
+ setIsMapInitialized(true);
66
+ return googleMapsMap;
67
+ }
68
+ return null;
69
+ }, [googleMaps, optionsStringify]);
70
+ return { map, ref, isMapInitialized };
71
+ };
72
+ export {
73
+ GoogleMapsProvider,
74
+ useGeocoder,
75
+ useGoogleMaps,
76
+ useMap
77
+ };
@@ -0,0 +1,37 @@
1
+ import { ScriptStatus } from '@ttoss/hooks';
2
+ import * as React from 'react';
3
+
4
+ declare type Extends<T, U extends T> = U;
5
+ declare type GoogleMaps = typeof google.maps;
6
+ declare type LoadedMapsStatus = Extends<ScriptStatus, 'ready'>;
7
+ declare type NotLoadedMapStatus = Extends<ScriptStatus, 'idle' | 'error' | 'loading'>;
8
+ declare type Libraries = 'places' | 'visualization' | 'drawing' | 'geometry';
9
+ declare const GoogleMapsProvider: React.FC<{
10
+ apiKey: string;
11
+ libraries?: Libraries[];
12
+ }>;
13
+ /**
14
+ *
15
+ * @returns param.googleMaps: GoogleMaps - returns the google maps object which
16
+ * provides access to the [Google Maps API](https://developers.google.com/maps/documentation/javascript/overview).
17
+ */
18
+ declare const useGoogleMaps: () => {
19
+ status: LoadedMapsStatus;
20
+ googleMaps: GoogleMaps;
21
+ } | {
22
+ status: NotLoadedMapStatus;
23
+ googleMaps: null;
24
+ };
25
+
26
+ declare const useGeocoder: () => {
27
+ geocoder: google.maps.Geocoder | null;
28
+ isGeocoderInitialized: boolean;
29
+ };
30
+
31
+ declare const useMap: (options: google.maps.MapOptions) => {
32
+ map: google.maps.Map | null;
33
+ ref: React.RefObject<HTMLDivElement>;
34
+ isMapInitialized: boolean;
35
+ };
36
+
37
+ export { GoogleMapsProvider, useGeocoder, useGoogleMaps, useMap };
package/dist/index.js ADDED
@@ -0,0 +1,116 @@
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
+ });
37
+
38
+ // tsup.inject.js
39
+ var React = __toESM(require("react"));
40
+
41
+ // src/GoogleMapsProvider.tsx
42
+ var import_hooks = require("@ttoss/hooks");
43
+ var React2 = __toESM(require("react"));
44
+ var GoogleMapsContext = React2.createContext({
45
+ status: "idle",
46
+ googleMaps: null
47
+ });
48
+ var GoogleMapsProvider = ({ children, apiKey, libraries }) => {
49
+ const src = `https://maps.googleapis.com/maps/api/js?key=${apiKey}&libraries=${libraries == null ? void 0 : libraries.join(",")}`;
50
+ const { status } = (0, import_hooks.useScript)(src);
51
+ const googleMaps = React2.useMemo(() => {
52
+ if (status === "ready" && window.google.maps) {
53
+ return window.google.maps;
54
+ }
55
+ return null;
56
+ }, [status]);
57
+ const value = React2.useMemo(() => {
58
+ if (status === "ready" && googleMaps) {
59
+ return {
60
+ status,
61
+ googleMaps
62
+ };
63
+ }
64
+ return {
65
+ status,
66
+ googleMaps: null
67
+ };
68
+ }, [googleMaps, status]);
69
+ return /* @__PURE__ */ React2.createElement(GoogleMapsContext.Provider, {
70
+ value
71
+ }, children);
72
+ };
73
+ var useGoogleMaps = () => React2.useContext(GoogleMapsContext);
74
+
75
+ // src/useGeocoder.ts
76
+ var React3 = __toESM(require("react"));
77
+ var useGeocoder = () => {
78
+ const { googleMaps } = useGoogleMaps();
79
+ const [isGeocoderInitialized, setIsGeocoderInitialized] = React3.useState(false);
80
+ const geocoder = React3.useMemo(() => {
81
+ if (googleMaps) {
82
+ const googleMapsGeocoder = new googleMaps.Geocoder();
83
+ setIsGeocoderInitialized(true);
84
+ return googleMapsGeocoder;
85
+ }
86
+ return null;
87
+ }, [googleMaps]);
88
+ return { geocoder, isGeocoderInitialized };
89
+ };
90
+
91
+ // src/useMap.ts
92
+ var React4 = __toESM(require("react"));
93
+ var useMap = (options) => {
94
+ const ref = React4.useRef(null);
95
+ const { googleMaps } = useGoogleMaps();
96
+ const [isMapInitialized, setIsMapInitialized] = React4.useState(false);
97
+ const optionsStringify = JSON.stringify(options);
98
+ const map = React4.useMemo(() => {
99
+ if (googleMaps && ref.current) {
100
+ const parsedOptions = JSON.parse(optionsStringify);
101
+ const googleMapsMap = new googleMaps.Map(ref.current, parsedOptions);
102
+ setIsMapInitialized(true);
103
+ return googleMapsMap;
104
+ }
105
+ return null;
106
+ }, [googleMaps, optionsStringify]);
107
+ return { map, ref, isMapInitialized };
108
+ };
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.9.2",
3
+ "version": "1.4.1",
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.9.2",
35
- "@types/google.maps": "^3.45.6"
34
+ "@ttoss/hooks": "^1.4.1",
35
+ "@types/google.maps": "^3.47.3"
36
36
  },
37
37
  "peerDependencies": {
38
- "react": ">=16.8.0",
39
- "react-dom": ">=16.8.0"
38
+ "react": ">=17.0.2",
39
+ "react-dom": ">=17.0.2"
40
40
  },
41
- "gitHead": "6a904124b42ad72f3ba318ec42b861f803acb218"
41
+ "gitHead": "c5a8ea6093886fc6e500e22db72687e011cdb5bc"
42
42
  }