--- layout: layouts/page.njk title: Theme Switcher description: A component that allows the user to switch between light and dark mode. toc: - label: Usage id: usage children: - label: HTML + JavaScript id: usage-html-js children: - label: "Step 1: Include the JavaScript script" id: usage-html-js-1 - label: "Step 2: Add your switcher" id: usage-html-js-2 - label: JavaScript events id: usage-html-js-3 ---
Instead, we offer a short JavaScript <script> block you can insert in the <head> of your page.
Insert the following JavaScript block right at the beginning of the <head> your page. We do this instead of using a separate file to ensure the theme is defined before the page loads, avoiding a flash of unstyled or mist-styled content.
To change the theme, you need to dispatch a basecoat:theme event:
basecoat:themeBy default, the event will toggle the theme, but you can also add a mode to the detail to set the theme explicitly: