--- 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 ---
{% lucide "circle-alert" %}

There is no dedicated theme switcher component in Basecoat.

Instead, we offer a short JavaScript <script> block you can insert in the <head> of your page.

{% from "macros/code_preview.njk" import code_preview %} {% from "macros/code_block.njk" import code_block %} {% set code %}{% endset %} {{ code_preview("button", code) }}

Usage

HTML + JavaScript

Step 1: Include the JavaScript script

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.

{% set code %}{% endset %} {{ code_block(code) }}

Step 2: Add your switcher

To change the theme, you need to dispatch a basecoat:theme event:

{% set code %}{% endset %} {{ code_block(code) }}

JavaScript events

basecoat:theme

By default, the event will toggle the theme, but you can also add a mode to the detail to set the theme explicitly:

{% set code_trigger %} {% endset %} {{ code_block(code_trigger | prettyHtml, "html") }}