Add a fullscreen control to the map

Open in CodeSandbox

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1" />
    <script crossorigin src="https://cdn.jsdelivr.net/npm/@babel/standalone@7/babel.min.js"></script>
    <!-- To make the map appear, you must add your apikey -->
    <script src="https://js.api.mappable.world/v3/?apikey=<YOUR_APIKEY>&lang=en_US" type="text/javascript"></script>

    <script
      data-plugins="transform-modules-umd"
      data-presets="typescript"
      type="text/babel"
      src="../variables.ts"
    ></script>
    <script
      data-plugins="transform-modules-umd"
      data-presets="typescript"
      type="text/babel"
      src="./common.ts"
    ></script>
    <script data-plugins="transform-modules-umd" data-presets="typescript" type="text/babel">
      import {LOCATION} from '../variables';

      window.map = null;

      main();

      async function main() {
          // Waiting for all api elements to be loaded
          await mappable.ready;
          const {MMap, MMapDefaultSchemeLayer, MMapControls, MMapControl, MMapComplexEntity} = mappable;
          // Initialize the map
          map = new MMap(
              // Pass the link to the HTMLElement of the container
              document.getElementById('app'),
              // Pass the map initialization parameters
              {location: LOCATION, showScaleInCopyrights: true},
              // Add a map scheme layer
              [new MMapDefaultSchemeLayer({})]
          );

          // Add a container for MMapControlButton and add it to the map
          const controls = new MMapControls({position: 'top right'});
          map.addChild(controls);

          function fullScreenBtnHandler() {
              // The document.fullscreenElement returns the Element that is currently being presented in fullscreen mode in this document, or null if fullscreen mode is not currently in use
              if (document.fullscreenElement) {
                  // The document.exitFullscreen() requests that the element on this document which is currently being presented in fullscreen mode be taken out of fullscreen mode
                  document.exitFullscreen();
              } else {
                  // The element.requestFullscreen() method issues an asynchronous request to make the element be displayed in fullscreen mode
                  map.container.requestFullscreen();
              }
          }

          class FullscreenButton extends MMapComplexEntity<{}> {
              private _element: HTMLButtonElement;

              private _detachDom: () => void;

              // Method for create a DOM control element
              _createElement() {
                  // Create an div element that will be passed to the MMapControlButton
                  const fullScreenButtonElement = document.createElement('button');
                  fullScreenButtonElement.type = 'button';
                  fullScreenButtonElement.onclick = fullScreenBtnHandler;
                  fullScreenButtonElement.classList.add('button', 'fullscreen');

                  // The fullscreenchange event is fired immediately after the browser switches into or out of fullscreen mode
                  document.addEventListener('fullscreenchange', function () {
                      fullScreenButtonElement.classList.toggle('exit-fullscreen');
                  });

                  return fullScreenButtonElement;
              }

              // Method for attaching the control to the map
              _onAttach() {
                  this._element = this._createElement();
                  this._detachDom = mappable.useDomContext(this, this._element, this._element);
              }

              // Method for detaching control from the map
              _onDetach() {
                  this._detachDom();
                  this._detachDom = null;
                  this._element = null;
              }
          }

          // Add MMapControlButton that will enable or disable fullscreen mode
          const fullScreenBtn = new MMapControl();
          fullScreenBtn.addChild(new FullscreenButton({}));

          controls.addChild(fullScreenBtn);
      }
    </script>

    <!-- prettier-ignore -->
    <style> html, body, #app { width: 100%; height: 100%; margin: 0; padding: 0; font-family: Arial, Helvetica, sans-serif; } .toolbar { position: absolute; z-index: 1000; top: 0; left: 0; display: flex; align-items: center; padding: 16px; } .toolbar a { padding: 16px; }  </style>
    <link rel="stylesheet" href="./common.css" />
    <link rel="stylesheet" href="../variables.css" />
  </head>
  <body>
    <div id="app"></div>
  </body>
</html>
<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1" />
    <script crossorigin src="https://cdn.jsdelivr.net/npm/react@17/umd/react.production.min.js"></script>
    <script crossorigin src="https://cdn.jsdelivr.net/npm/react-dom@17/umd/react-dom.production.min.js"></script>
    <script crossorigin src="https://cdn.jsdelivr.net/npm/@babel/standalone@7/babel.min.js"></script>
    <!-- To make the map appear, you must add your apikey -->
    <script src="https://js.api.mappable.world/v3/?apikey=<YOUR_APIKEY>&lang=en_US" type="text/javascript"></script>

    <script
      data-plugins="transform-modules-umd"
      data-presets="typescript"
      type="text/babel"
      src="../variables.ts"
    ></script>
    <script
      data-plugins="transform-modules-umd"
      data-presets="typescript"
      type="text/babel"
      src="./common.ts"
    ></script>
    <script data-plugins="transform-modules-umd" data-presets="react, typescript" type="text/babel">
      import {LOCATION} from '../variables';

      window.map = null;

      main();

      async function main() {
        // For each object in the JS API, there is a React counterpart
        // To use the React version of the API, include the module @mappable-world/mappable-reactify
        const [mappableReact] = await Promise.all([
          mappable.import('@mappable-world/mappable-reactify'),
          mappable.ready
        ]);
        const reactify = mappableReact.reactify.bindTo(React, ReactDOM);
        const {MMap, MMapDefaultSchemeLayer, MMapControls, MMapControl} = reactify.module(mappable);
        const {useEffect, useState, useCallback} = React;

        function App() {
          // isFullscreen will indicate the map is in fullscreen mode or not
          const [isFullscreen, setIsFullscreen] = useState(false);

          useEffect(() => {
            // The fullscreenchange event is fired immediately after the browser switches into or out of fullscreen mode
            const onFullscreenChange = () => {
              setIsFullscreen(Boolean(document.fullscreenElement));
            };
            document.addEventListener('fullscreenchange', onFullscreenChange);

            // Remove event on component unmount
            return () => document.removeEventListener('fullscreenchange', onFullscreenChange);
          }, []);

          const onClickHandler = useCallback(() => {
            // The document.fullscreenElement returns the Element that is currently being presented in fullscreen mode in this document, or null if fullscreen mode is not currently in use
            if (isFullscreen) {
              // The document.exitFullscreen() requests that the element on this document which is currently being presented in fullscreen mode be taken out of fullscreen mode
              document.exitFullscreen();
            } else {
              // The element.requestFullscreen() method issues an asynchronous request to make the element be displayed in fullscreen mode
              map.container.requestFullscreen();
            }
          }, [isFullscreen]);

          return (
            // Initialize the map and pass initialization parameters
            <MMap location={LOCATION} showScaleInCopyrights={true} ref={(x) => (map = x)}>
              {/* Add a map scheme layer */}
              <MMapDefaultSchemeLayer />

              {/* Add a container for MMapControlButton */}
              <MMapControls position="top right">
                {/* Add MMapControlButton that will enable or disable fullscreen mode */}
                <MMapControl>
                  <button
                    type="button"
                    onClick={onClickHandler}
                    className={`button ${isFullscreen ? 'exit-fullscreen' : 'fullscreen'}`}
                  />
                </MMapControl>
              </MMapControls>
            </MMap>
          );
        }

        ReactDOM.render(
          <React.StrictMode>
            <App />
          </React.StrictMode>,
          document.getElementById('app')
        );
      }
    </script>

    <!-- prettier-ignore -->
    <style> html, body, #app { width: 100%; height: 100%; margin: 0; padding: 0; font-family: Arial, Helvetica, sans-serif; } .toolbar { position: absolute; z-index: 1000; top: 0; left: 0; display: flex; align-items: center; padding: 16px; } .toolbar a { padding: 16px; }  </style>
    <link rel="stylesheet" href="./common.css" />
    <link rel="stylesheet" href="../variables.css" />
  </head>
  <body>
    <div id="app"></div>
  </body>
</html>
<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1" />
    <script crossorigin src="https://cdn.jsdelivr.net/npm/vue@3/dist/vue.global.js"></script>
    <script crossorigin src="https://cdn.jsdelivr.net/npm/@babel/standalone@7/babel.min.js"></script>
    <!-- To make the map appear, you must add your apikey -->
    <script src="https://js.api.mappable.world/v3/?apikey=<YOUR_APIKEY>&lang=en_US" type="text/javascript"></script>

    <script
      data-plugins="transform-modules-umd"
      data-presets="typescript"
      type="text/babel"
      src="../variables.ts"
    ></script>
    <script
      data-plugins="transform-modules-umd"
      data-presets="typescript"
      type="text/babel"
      src="./common.ts"
    ></script>
    <script data-plugins="transform-modules-umd" data-presets="typescript" type="text/babel">
      import {LOCATION} from '../variables';

      window.map = null;

      async function main() {
        // For each object in the JS API, there is a Vue counterpart
        // To use the Vue version of the API, include the module @mappable-world/mappable-vuefy
        const [mappableVue] = await Promise.all([mappable.import('@mappable-world/mappable-vuefy'), mappable.ready]);
        const vuefy = mappableVue.vuefy.bindTo(Vue);
        const {MMap, MMapDefaultSchemeLayer, MMapControls, MMapControl} = vuefy.module(mappable);

        const isFullscreen = Vue.ref(false);

        const onClickHandler = () => {
          if (isFullscreen.value) {
            // The document.exitFullscreen() requests that the element on this document which is currently being presented in fullscreen mode be taken out of fullscreen mode
            document.exitFullscreen();
          } else {
            // The element.requestFullscreen() method issues an asynchronous request to make the element be displayed in fullscreen mode
            map.container.requestFullscreen();
          }
        };

        const onFullscreenChange = () => {
          isFullscreen.value = Boolean(document.fullscreenElement);
        };

        const app = Vue.createApp({
          components: {
            MMap,
            MMapDefaultSchemeLayer,
            MMapControls,
            MMapControl
          },
          setup() {
            const refMap = (ref) => {
              window.map = ref?.entity;
            };
            Vue.onMounted(() => {
              document.addEventListener('fullscreenchange', onFullscreenChange);
            });
            Vue.onUnmounted(() => {
              document.removeEventListener('fullscreenchange', onFullscreenChange);
            });
            return {
              LOCATION,
              refMap,
              onClickHandler,
              isFullscreen
            };
          },
          template: `
      <!-- Initialize the map and pass initialization parameters -->
      <MMap :location="LOCATION" :showScaleInCopyrights="true" :ref="refMap">
        <!-- Add a map scheme layer -->
        <MMapDefaultSchemeLayer/>

        <!-- Add a container for MMapControlButton -->
        <MMapControls position="top right">
          <!-- Add MMapControlButton that will enable or disable fullscreen mode -->
          <MMapControl>
            <button 
              @click="onClickHandler"
              :class="['button', isFullscreen ? 'exit-fullscreen' : 'fullscreen']"
              type="button"
            />
          </MMapControl>
        </MMapControls>
      </MMap>`
        });
        app.mount('#app');
      }
      main();
    </script>

    <!-- prettier-ignore -->
    <style> html, body, #app { width: 100%; height: 100%; margin: 0; padding: 0; font-family: Arial, Helvetica, sans-serif; } .toolbar { position: absolute; z-index: 1000; top: 0; left: 0; display: flex; align-items: center; padding: 16px; } .toolbar a { padding: 16px; }  </style>
    <link rel="stylesheet" href="./common.css" />
    <link rel="stylesheet" href="../variables.css" />
  </head>
  <body>
    <div id="app"></div>
  </body>
</html>
.button {
  width: 52px;
  height: 49px;
  background-color: #ffffff;
  background-position: 50% 50%;
  background-repeat: no-repeat;
  border: none;
  border-radius: 12px;
  cursor: pointer;
}

.button.fullscreen {
  display: block;
  background-image: url('./fullscreen.svg');
}

.button.exit-fullscreen {
  display: block;
  background-image: url('./fullscreen-exit.svg');
}
:root {
}
import type {MMapLocationRequest} from '@mappable-world/mappable-types';

export const LOCATION: MMapLocationRequest = {
  center: [55.159, 25.077], // starting position [lng, lat]
  zoom: 14 // starting zoom
};