Skip to content

Commit 5ce2776

Browse files
committed
Add custom overlay
1 parent 624bebe commit 5ce2776

File tree

3 files changed

+56
-27
lines changed

3 files changed

+56
-27
lines changed

examples/components/withCustomPopup.js

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,36 @@
1-
import React from 'react';
1+
import React, { useState } from 'react';
22
import Map from '../../src/index';
33
import CustomPopup from '../../src/components/CustomPopup';
4+
import styles from './withCustomPopup.module.css';
45

56
const WithCustomPopup = (props) => {
7+
const [showPopup, setShowPopup] = useState(true);
68
if (!props.loaded) return <div>Loading...</div>;
79

810
return (
9-
<Map
10-
google={props.google}
11-
className="map"
12-
style={{ height: '100%', position: 'relative', width: '100%' }}
13-
zoom={14}
14-
>
15-
<CustomPopup position={{ lat: 37.782551, lng: -122.445368 }}>
16-
<div
17-
style={{
18-
backgroundColor: 'white',
19-
padding: '20px',
20-
borderRadius: '10px',
21-
}}
11+
<div>
12+
<Map
13+
google={props.google}
14+
className="map"
15+
style={{ height: '100%', position: 'relative', width: '100%' }}
16+
zoom={14}
17+
>
18+
<CustomPopup
19+
position={{ lat: 37.782551, lng: -122.425368 }}
20+
visible={showPopup}
2221
>
23-
Hi there. I'm a custom popup.
24-
</div>
25-
</CustomPopup>
26-
</Map>
22+
<div className={styles.customPopup}>
23+
Hi there. I'm a custom popup.
24+
</div>
25+
</CustomPopup>
26+
<button
27+
className={styles.button}
28+
onClick={() => setShowPopup(!showPopup)}
29+
>
30+
Toggle Popup
31+
</button>
32+
</Map>
33+
</div>
2734
);
2835
};
2936

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
.customPopup {
2+
background-color: white;
3+
padding: 20px;
4+
border-radius: 10px;
5+
}
6+
7+
.button {
8+
position: absolute;
9+
bottom: 10px;
10+
left: 10px;
11+
z-index: 1;
12+
}

src/components/CustomPopup.js

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import React, { useRef, useEffect } from 'react';
2+
import PropTypes from 'prop-types'
23

34
function createPopupClass() {
45
function Popup({ position, content, map }) {
@@ -39,26 +40,35 @@ function createPopupClass() {
3940
return Popup;
4041
}
4142

42-
const CustomPopup = ({ map, position, children }) => {
43+
const CustomPopup = ({ map, position, children, visible, google }) => {
4344
const containerEl = useRef(null);
4445
useEffect(() => {
46+
if (map) {
4547
const pos = new google.maps.LatLng(position.lat, position.lng);
4648
const Popup = createPopupClass();
47-
const popup = new Popup({
49+
new Popup({
4850
position: pos,
4951
content: containerEl.current,
5052
map,
5153
});
52-
});
54+
}
55+
}, [map]);
5356
return (
54-
<div
55-
style={{ position: 'absolute' }}
56-
className="custom-popup"
57-
ref={containerEl}
58-
>
59-
{children}
57+
<div style={{ position: 'absolute' }} ref={containerEl}>
58+
{visible && children}
6059
</div>
6160
);
6261
};
6362

63+
CustomPopup.propTypes = {
64+
children: PropTypes.element.isRequired,
65+
map: PropTypes.object,
66+
marker: PropTypes.object,
67+
position: PropTypes.object,
68+
visible: PropTypes.bool,
69+
}
70+
71+
CustomPopup.defaultProps = {
72+
visible: true
73+
}
6474
export default CustomPopup;

0 commit comments

Comments
 (0)