Добавление контрола 'полноэкранный режим' на карту

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://api-maps.yandex.ru/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 ymaps3.ready;
          const {YMap, YMapDefaultSchemeLayer, YMapControls, YMapControl, YMapComplexEntity} = ymaps3;
          // Initialize the map
          map = new YMap(
              // 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 YMapDefaultSchemeLayer({})]
          );

          // Add a container for YMapControlButton and add it to the map
          const controls = new YMapControls({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 YMapComplexEntity<{}> {
              private _element: HTMLButtonElement;

              private _detachDom: () => void;

              // Method for create a DOM control element
              _createElement() {
                  // Create an div element that will be passed to the YMapControlButton
                  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 = ymaps3.useDomContext(this, this._element, this._element);
              }

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

          // Add YMapControlButton that will enable or disable fullscreen mode
          const fullScreenBtn = new YMapControl();
          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://api-maps.yandex.ru/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 @yandex/ymaps3-reactify
        const [ymaps3React] = await Promise.all([ymaps3.import('@yandex/ymaps3-reactify'), ymaps3.ready]);
        const reactify = ymaps3React.reactify.bindTo(React, ReactDOM);
        const {YMap, YMapDefaultSchemeLayer, YMapControls, YMapControl} = reactify.module(ymaps3);
        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
            <YMap location={LOCATION} showScaleInCopyrights={true} ref={(x) => (map = x)}>
              {/* Add a map scheme layer */}
              <YMapDefaultSchemeLayer />

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

        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://api-maps.yandex.ru/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 @yandex/ymaps3-vuefy
        const [ymaps3Vue] = await Promise.all([ymaps3.import('@yandex/ymaps3-vuefy'), ymaps3.ready]);
        const vuefy = ymaps3Vue.vuefy.bindTo(Vue);
        const {YMap, YMapDefaultSchemeLayer, YMapControls, YMapControl} = vuefy.module(ymaps3);

        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: {
            YMap,
            YMapDefaultSchemeLayer,
            YMapControls,
            YMapControl
          },
          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 -->
      <YMap :location="LOCATION" :showScaleInCopyrights="true" :ref="refMap">
        <!-- Add a map scheme layer -->
        <YMapDefaultSchemeLayer/>

        <!-- Add a container for YMapControlButton -->
        <YMapControls position="top right">
          <!-- Add YMapControlButton that will enable or disable fullscreen mode -->
          <YMapControl>
            <button 
              @click="onClickHandler"
              :class="['button', isFullscreen ? 'exit-fullscreen' : 'fullscreen']"
              type="button"
            />
          </YMapControl>
        </YMapControls>
      </YMap>`
        });
        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 {YMapLocationRequest} from '@yandex/ymaps3-types';

export const LOCATION: YMapLocationRequest = {
  center: [104.284, 52.289], // starting position [lng, lat]
  zoom: 13.5 // starting zoom
};