Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <title>My React Project on CodePen</title>
  <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&key=AIzaSyARATzKOf2nxzpW1uZ708KiRpodi9ZkzgE&sensor=false" type="text/javascript"></script>
</head>

<body>
  <div id="root"></div>
  <div id="content">
    <h2>Custom Popup</h2>
    <p>Can be used to render HTML markup.</p>
  </div>
</body>

</html>
              
            
!

CSS

              
                body {
  margin: 0;
}

#map {
  height: 100vh;
  width: 100vw;
}

/* The popup bubble styling. */
.popup-bubble {
  /* Position the bubble centred-above its parent. */
  position: absolute;
  top: 0;
  left: 0;
  transform: translate(-50%, -100%);
  /* Style the bubble. */
  background-color: white;
  padding: 10px 10px 20px;
  border-radius: 5px;
  font-family: sans-serif;
  overflow-y: auto;
  max-height: 60px;
  box-shadow: 0px 2px 10px 1px rgba(0, 0, 0, 0.5);
  background: pink;
}
/* The parent of the bubble. A zero-height div at the top of the tip. */
.popup-bubble-anchor {
  /* Position the div a fixed distance above the tip. */
  position: absolute;
  width: 100%;
  bottom: 8px;
  left: 0;
}
/* This element draws the tip. */
.popup-bubble-anchor::after {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  /* Center the tip horizontally. */
  transform: translate(-50%, 0);
  /* The tip is a https://css-tricks.com/snippets/css/css-triangle/ */
  width: 0;
  height: 0;
  /* The tip is 8px high, and 12px wide. */
  border-left: 8px solid transparent;
  border-right: 8px solid transparent;
  border-top: 10px solid black;
}
/* JavaScript will position this div at the bottom of the popup tip. */
.popup-container {
  cursor: auto;
  height: 0;
  position: absolute;
  /* The max width of the info window. */
  width: 200px;
}

              
            
!

JS

              
                (function() {
   
   
   /** 
    * Sample App to demonstrate using react portal  
    */
   class App extends React.Component {
      state = {
         time: new Date()
      }
      position = new google.maps.LatLng(37.814, -122.478)

      componentDidMount() {
         /** Update time every time every second */
         setInterval(() => {
            this.setState({
               time: new Date()
            })
         }, 1000)
      }

      render() {
         return (
            <Map zoom={13} center={this.position}>
               <OverlayViewContainer 
                  position={this.position}>
                  <CustomPopupComponent>
                     <div>
                        <h2>Timer Popup</h2>
                        <p>A custom react popup that shows the current time. <br/>Updates every second.</p>
                        <pre><u>{this.state.time.toLocaleString()}</u></pre>
                     </div>
                  </CustomPopupComponent>
               </OverlayViewContainer>
            </Map>
         );
      }
   }
 
   class OverlayViewContainer extends React.Component {
     /** Overlay instance. */
     overlay = null;

     /** Dom element reference to the content rendered in the overlay. */
     el = null;

     componentWillUnmount() {
      /** remove overlay from the map. */
       this.overlay.setMap(null);
       delete this.overlay;
     }

     render() {
       return (
         <MapContext.Consumer>
           {map => {
             if (map) {
               this.el = this.el || createOverlayElement();
                
               this.overlay = this.overlay ||
                 new OverlayView({ position: this.props.position, content: this.el });
               this.overlay.setMap(map);

               return ReactDOM.createPortal(this.props.children, this.el);
             } else {
               return null;
             }
           }}
         </MapContext.Consumer>
       );
     }
   }

   function createOverlayElement() {
     const el = document.createElement('div');
     el.style.position = 'absolute';
     el.style.display = 'inline-block';
     el.style.width = '9999px';
     return el;
   }

   class OverlayView extends window.google.maps.OverlayView {
      position = null;
      content = null;

      constructor(props) {
        super(props);
        props.position && (this.position = props.position);
        props.content && (this.content = props.content);
      }

      /** Called when the popup is added to the map. */
      onAdd = () => {
        this.getPanes().floatPane.appendChild(this.content);
      };

      /** Called when the popup is removed from the map. */
      onRemove = () => {
        if (this.content.parentElement) {
          this.content.parentElement.removeChild(this.content);
        }
      };

      /** Called each frame when the popup needs to draw itself. */
      draw = () => {
        const divPosition = this.getProjection().fromLatLngToDivPixel(
          this.position
        );
        this.content.style.left = divPosition.x + 'px';
        this.content.style.top = divPosition.y + 'px';
      };
    }

   /** Context to pass down the map instance to the OverlayViewContainer component */
   const MapContext = React.createContext(null);

   /**
    * Map componen renders the map 
    * and passes down the instance of the created map down to the children via `MapContext`.
    */
   class Map extends React.Component {
     map = null;
     /** DOM container where the map canvas gets rendered. */
     mapContainer = React.createRef();

     componentDidMount() {
       /** Create new google map. */
       if (window.google) {
         /** Draw a popup in the container */
         this.map = new google.maps.Map(this.mapContainer.current, {
           zoom: this.props.zoom,
           center: this.props.center
         });
         this.setState({map: this.map})
       }
     }
     render() {
       // reference to the DOM element where the map will be rendered
       return (
         <div
           ref={this.mapContainer}
           style={{ height: "100vh", width: "100vw" }}
         >
           <MapContext.Provider value={this.map}>{this.props.children}</MapContext.Provider>
         </div>
       );
     }
   }
   
   /** Custom popup component */
   CustomPopupComponent = styled.div`
     padding: 8px 16px;
     background-color: aliceblue;
     position: absolute;
     border: 1px solid #ccc;
     border-radius: 4px;

     p, pre {
       font-size: 16px;
     }

     pre {
       font-weight: 700;
     }
   `

   ReactDOM.render( <App />, document.getElementById("root"));


})();

              
            
!
999px

Console