Learn how to create a component that toggles Calcite Components and ArcGIS API for JavaScript between light and dark themes.
You will use Calcite Components to design an elegant theme switcher that toggles between light and dark for Calcite Components, as well as ArcGIS JavaScript API widgets and basemaps. This application builds on the one created in the Create a mapping app tutorial.
Calcite provides light and dark themes which can be changed using CSS classes: calcite-theme-light (default) and calcite-theme-dark. There is also a calcite-theme-auto class which defers to the browser's CSS prefers-color-scheme media query. Setting the theme class on an element changes all of their child nodes as well.
For more information about theme and styling, visit Calcite Design System's colors and themes foundation and ArcGIS API for JavaScript's styling documentation.
Prerequisites
ArcGIS developer account
You need a free ArcGIS developer account or an account associated with an ArcGIS Online organization to access the services used in this tutorial.
An API key is required to access ArcGIS services if you are using a developer account. You can skip this step if you have an account associated with an ArcGIS Online organization.
The ArcGIS API for JavaScript uses separate theme stylesheets. Add both light and dark stylesheets, which will be dynamically toggled in JavaScript using their id attributes.
Add the dark theme stylesheet, set the disabled attribute, and add an id.
Add an id to the preexisting light theme stylesheet.
The primary component in the theme switcher is calcite-switch. The theme switcher will also use calcite-icon for the light and dark icons, and calcite-label to tie it all together.
In the calcite-shell component, add a div element and place it in the shell's header slot with an id attribute used later for styling.
Read the slots FAQ section for more information about the concept. After adding CSS in the next step, you can position the theme switcher over the map by removing the component from shell's header slot.
Add a nested div element within the header slot. Include an id attribute used later for styling.
Add a calcite-label component with the disable-spacing attribute, set the layout attribute to "inline", and add a class attribute used later for styling.
Add the calcite-icon for the light theme, setting the icon attribute to "brightness" and scale attribute to "s". Add a class, which you will use for styling the component.
At this point, if you run the app you will see the theme switcher in the top, left corner of the application.
Next, add styling with CSS to position the component using Flexbox.
In the <style> element, add CSS for the header using the id created for the header slot. To space content in the header, add padding and set the header slot's background-color to the --calcite-ui-foreground-1 CSS variable.
Add styles to the header controls using the id for the header slot's nested div. The additional div element's styling can accommodate one, or multiple components. To space the header controls, use the margin-inline-start and align-self CSS properties.
To configure the dark mode switch's layout, set the margin-inline and padding CSS properties. Add a border using the --calcite-ui-border-1 CSS variable for the color. Next, add a margin CSS property to the calcite-switch, which will accommodate future controls in the interface.
Read the CSS variables FAQ section for more information about the concept. Calcite's theme colors have both light and dark values, which automatically switch when toggling themes. Use the theme color variables for all non-Calcite elements in applications that switch themes.
Finally, set the switch's cursor property to "pointer" to communicate that the switch container is clickable.
Now you have a good looking component; but it doesn't switch any themes. Next, wire up the theme switcher with JavaScript.
In the <script> element, below the existing JavaScript code, create a new function to toggle the themes.
Toggle the calcite-theme-dark class on the <body> element. No need to add the calcite-theme-light class, since it is the default.
Access the ArcGIS JavaScript API's light and dark stylesheets using the id attributes you added above.
Negate the disabled attribute on both stylesheets to toggle their boolean values.
Change the basemap between gray-vector and dark-gray-vector depending on the current theme.
Add an event listener to the calcite-switch component. Listen for the calciteSwitchChange event and provide the function you created.
Calcite provides custom events for many of the components. The custom events will all be prefixed with calcite, then the component name, and finally the type of event. You can check a component's API reference to find out if they have events, and what they are.
In CodePen, run your code to display the application.
In the top, right corner of the screen you will see the new, beautiful theme switcher component. Clicking anywhere on the component will toggle the calcite-switch. The function you created for the event listener will run, which will toggle the Calcite Components theme, as well as the ArcGIS JavaScript API basemap and widgets.