Skip to content

Commit 567e62b

Browse files
feat: add TypeScript definitions for Google Maps integration in Next.js
1 parent bbe76b3 commit 567e62b

File tree

3 files changed

+1307
-86
lines changed

3 files changed

+1307
-86
lines changed

dist/index.d.cts

Lines changed: 296 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,296 @@
1+
import * as React from 'react';
2+
import React__default from 'react';
3+
4+
declare global {
5+
interface Window {
6+
google?: any;
7+
googleMapsCallback?: () => void;
8+
}
9+
}
10+
type MapMouseEvent = google.maps.MapMouseEvent;
11+
type Map = google.maps.Map;
12+
type MapsLibrary = typeof google.maps;
13+
type MapPanes = google.maps.MapPanes;
14+
type MapOptions = google.maps.MapOptions;
15+
type LatLng = google.maps.LatLng;
16+
type LatLngLiteral = google.maps.LatLngLiteral;
17+
type LatLngBounds = google.maps.LatLngBounds;
18+
type LatLngBoundsLiteral = google.maps.LatLngBoundsLiteral;
19+
/**
20+
* The drag configuration of the overlay.
21+
*/
22+
type Drag = {
23+
/**
24+
* Whether the overlay is draggable.
25+
* @default false
26+
*/
27+
draggable: boolean;
28+
/**
29+
* Callback fired repeatedly when the overlay is dragged.
30+
* @param e The event.
31+
* @param props The props.
32+
*/
33+
onDrag: (e: MouseEvent, props: {}) => void;
34+
/**
35+
* Callback fired when the drag has ended.
36+
* @param e The event.
37+
* @param props The props.
38+
*/
39+
onDragEnd: (e: MouseEvent, props: {}) => void;
40+
/**
41+
* Callback fired when the drag has started.
42+
* @param e The event.
43+
* @param props The props.
44+
*/
45+
onDragStart: (e: MouseEvent, props: {}) => void;
46+
};
47+
type UseScriptStatus = 'idle' | 'loading' | 'ready' | 'error';
48+
type Pane = 'floatPane' | 'mapPane' | 'markerLayer' | 'overlayLayer' | 'overlayMouseTarget';
49+
interface IUseGoogleMaps {
50+
/**
51+
* The Google Maps API key.
52+
*/
53+
apiKey?: string;
54+
/**
55+
* The Google Maps API callback.
56+
*/
57+
callback?: () => void;
58+
/**
59+
* The Google Maps API params to be appended to the script URL.
60+
* @example
61+
* {
62+
* language: 'en',
63+
* region: 'GB',
64+
* }
65+
*/
66+
externalApiParams?: {
67+
[key: string]: any;
68+
};
69+
/**
70+
* The Google Maps API libraries to be loaded.
71+
* @default ['places', 'geometry']
72+
*/
73+
libraries?: string[];
74+
/**
75+
* Whether to manage the script loading externally.
76+
* @default false
77+
*/
78+
loadScriptExternally?: boolean;
79+
/**
80+
* The status of the script loading.
81+
* To be used only if `loadScriptExternally` is `true`.
82+
*/
83+
status?: UseScriptStatus;
84+
}
85+
interface ScriptProps {
86+
/**
87+
* The attributes to be passed to the script element.
88+
*/
89+
attributes?: {
90+
[key: string]: string;
91+
};
92+
/**
93+
* The callback to be called when the script has loaded
94+
* successfully or has failed to load.
95+
*/
96+
callbacks?: {
97+
onErrorCallback?: () => void;
98+
onLoadCallback?: () => void;
99+
};
100+
/**
101+
* The ID of the HTML element where the script will be appended.
102+
* If not provided, the script will be appended to the `body` element.
103+
*/
104+
elementIdToAppend?: string;
105+
/**
106+
* The URL of the script to be loaded.
107+
*/
108+
src: string;
109+
}
110+
interface UseScriptOptions {
111+
/**
112+
* Whether to remove the script when the component unmounts.
113+
*/
114+
removeOnUnmount?: boolean;
115+
/**
116+
* Whether to prevent the script from loading.
117+
* @default false
118+
*/
119+
shouldPreventLoad?: boolean;
120+
}
121+
interface MapContextProps {
122+
/**
123+
* The Google Maps instance.
124+
*/
125+
map: Map;
126+
/**
127+
* The Google Maps API object.
128+
*/
129+
maps: MapsLibrary;
130+
}
131+
interface OverlayViewProps extends MapContextProps {
132+
/**
133+
* The children to be rendered within the overlay.
134+
*/
135+
children?: React__default.ReactElement;
136+
/**
137+
* The drag configuration of the overlay.
138+
* @default { draggable: false }
139+
*/
140+
drag?: Drag;
141+
/**
142+
* The map pane in which to render the overlay.
143+
* @default 'floatPane'
144+
*/
145+
pane?: Pane | undefined;
146+
/**
147+
* The geographical coordinates of the overlay.
148+
*/
149+
position: LatLng;
150+
/**
151+
* The z-index of the overlay.
152+
* @default 0
153+
*/
154+
zIndex?: number | 0;
155+
}
156+
interface createOverlayProps {
157+
/**
158+
* The HTML container element in which to render the overlay.
159+
*/
160+
container: HTMLDivElement;
161+
/**
162+
* The drag configuration of the overlay.
163+
* @default { draggable: false }
164+
*/
165+
drag?: Drag;
166+
/**
167+
* The Google Maps API object.
168+
*/
169+
maps: MapContextProps['maps'];
170+
/**
171+
* The map pane in which to render the overlay.
172+
* @default 'floatPane'
173+
*/
174+
pane: Pane;
175+
/**
176+
* The geographical coordinates of the overlay.
177+
*/
178+
position: LatLng;
179+
}
180+
interface EventProps {
181+
/**
182+
* The event handler.
183+
* @param e The event.
184+
*/
185+
handler: (e: any) => void;
186+
/**
187+
* The HTML Event attribute name.
188+
* @reference https://www.w3schools.com/tags/ref_eventattributes.asp
189+
* @example
190+
* 'onClick'
191+
*/
192+
name: string;
193+
}
194+
interface onGoogleApiLoadedProps extends MapContextProps {
195+
/**
196+
* The ref of the Map.
197+
*/
198+
ref: HTMLDivElement | null;
199+
}
200+
interface MapMarkersProps extends MapContextProps {
201+
/**
202+
* The Markers to be rendered on the map.
203+
*/
204+
children: React__default.ReactNode;
205+
}
206+
interface MapProps {
207+
/**
208+
* The Markers to be rendered on the map
209+
*/
210+
children?: React__default.ReactNode;
211+
/**
212+
* The default center of the map.
213+
*/
214+
defaultCenter: LatLngLiteral;
215+
/**
216+
* The default zoom of the map.
217+
*/
218+
defaultZoom: number;
219+
/**
220+
* The events to pass to the Google Maps instance (`div`).
221+
* @type {Array}
222+
* @example
223+
* [
224+
* {
225+
* name: 'onClick',
226+
* handler: (event) => { ... }
227+
* }
228+
* ]
229+
*/
230+
events?: EventProps[];
231+
/**
232+
* The callback fired when the map changes (center, zoom, bounds).
233+
*/
234+
onChange?: (options: {
235+
bounds: LatLngBounds;
236+
center: (number | undefined)[];
237+
zoom: number;
238+
}) => void;
239+
/**
240+
* The callback fired when the map is loaded.
241+
*/
242+
onGoogleApiLoaded?: (props: onGoogleApiLoadedProps) => void;
243+
/**
244+
* The options to pass to the Google Maps instance.
245+
* @reference https://developers.google.com/maps/documentation/javascript/reference/map#MapOptions
246+
*/
247+
options?: MapOptions;
248+
/**
249+
* The style of the map container.
250+
* @default
251+
* {
252+
* width: '100%',
253+
* height: '100%',
254+
* left: 0,
255+
* top: 0,
256+
* margin: 0,
257+
* padding: 0,
258+
* position: 'absolute'
259+
* }
260+
*/
261+
style?: React__default.CSSProperties;
262+
}
263+
interface GoogleMapProps extends MapProps, IUseGoogleMaps {
264+
/**
265+
* The props to pass to the `div` container element.
266+
*/
267+
containerProps?: React__default.HTMLAttributes<HTMLDivElement>;
268+
/**
269+
* The content to be rendered when the script fails to load.
270+
* @default 'Google Maps is on error'
271+
*/
272+
errorContent?: React__default.ReactNode;
273+
/**
274+
* The content to be rendered when the script is on idle.
275+
* @default 'Google Maps is on idle'
276+
*/
277+
idleContent?: React__default.ReactNode;
278+
/**
279+
* The content to be rendered while the script is loading.
280+
* @default 'Google Maps is loading'
281+
*/
282+
loadingContent?: React__default.ReactNode;
283+
/**
284+
* The minimum height of the map container.
285+
* @default 'unset'
286+
*/
287+
mapMinHeight?: string;
288+
/**
289+
* The Google Maps API script callback
290+
*/
291+
scriptCallback?: () => void;
292+
}
293+
294+
declare const GoogleMap: React.ForwardRefExoticComponent<GoogleMapProps & React.RefAttributes<HTMLDivElement>>;
295+
296+
export { type Drag, type EventProps, type GoogleMapProps, type IUseGoogleMaps, type LatLng, type LatLngBounds, type LatLngBoundsLiteral, type LatLngLiteral, type Map, type MapContextProps, type MapMarkersProps, type MapMouseEvent, type MapOptions, type MapPanes, type MapProps, type MapsLibrary, type OverlayViewProps, type Pane, type ScriptProps, type UseScriptOptions, type UseScriptStatus, type createOverlayProps, GoogleMap as default, type onGoogleApiLoadedProps };

0 commit comments

Comments
 (0)