Select to view content in your preferred language

Map Theme Toggle with React

226
2
Jump to solution
4 weeks ago
jjgarrett0
New Contributor III

I'm looking for some help with dynamically setting the map theme for dark and light modes in a React application. The documentation says to import the css file for whichever theme you want, but I would like to be able to toggle a button in the react application to switch it between light and dark. Does anyone have some suggestions? 

0 Kudos
1 Solution

Accepted Solutions
ReneRubalcava
Frequent Contributor II

There is a calcite tutorial on how to do this you can apply to a react application.

https://developers.arcgis.com/calcite-design-system/tutorials/build-a-dark-mode-switch/

The main thing to know is this part of the tutorial.

        const updateDarkMode = () => {
          // Calcite mode
          document.body.classList.toggle("calcite-mode-dark");
          // ArcGIS Maps SDK theme
          const dark = document.querySelector("#arcgis-maps-sdk-theme-dark");
          const light = document.querySelector("#arcgis-maps-sdk-theme-light");
          dark.disabled = !dark.disabled;
          light.disabled = !light.disabled;
          // ArcGIS Maps SDK basemap
          map.basemap = dark.disabled ? "gray-vector" : "dark-gray-vector";
          // Toggle ArcGIS Maps SDK widgets mode
          const widgets = document.getElementsByClassName("esri-ui");
          for (let i = 0; i < widgets.length; i++) {
            widgets.item(i).classList.toggle("calcite-mode-dark");
          }
        };

        document.querySelector("calcite-switch").addEventListener("calciteSwitchChange", updateDarkMode);

View solution in original post

0 Kudos
2 Replies
ReneRubalcava
Frequent Contributor II

There is a calcite tutorial on how to do this you can apply to a react application.

https://developers.arcgis.com/calcite-design-system/tutorials/build-a-dark-mode-switch/

The main thing to know is this part of the tutorial.

        const updateDarkMode = () => {
          // Calcite mode
          document.body.classList.toggle("calcite-mode-dark");
          // ArcGIS Maps SDK theme
          const dark = document.querySelector("#arcgis-maps-sdk-theme-dark");
          const light = document.querySelector("#arcgis-maps-sdk-theme-light");
          dark.disabled = !dark.disabled;
          light.disabled = !light.disabled;
          // ArcGIS Maps SDK basemap
          map.basemap = dark.disabled ? "gray-vector" : "dark-gray-vector";
          // Toggle ArcGIS Maps SDK widgets mode
          const widgets = document.getElementsByClassName("esri-ui");
          for (let i = 0; i < widgets.length; i++) {
            widgets.item(i).classList.toggle("calcite-mode-dark");
          }
        };

        document.querySelector("calcite-switch").addEventListener("calciteSwitchChange", updateDarkMode);
0 Kudos
jjgarrett0
New Contributor III

Thanks I'll give that a shot.

0 Kudos