<theme-toggle>

A custom element that allows you to toggle between light, dark and system theme.

Source code & documentation

Examples

Below are examples of different configurations and use cases of the <theme-toggle> component.

Table of contents
  1. Default configuration
  2. With custom slots
  3. Without label
  4. Without icon
  5. With custom styling
  6. Listen for theme change event

# Example 1 - Default configuration

This is the default configuration. The element will use the system theme by default.

Source code
<theme-toggle></theme-toggle>
↑ Back to top

# Example 2 - With custom slots

You can customize the slots to display your own labels and icons.

Light Dark System
Source code
<theme-toggle>
  <span slot="icon-light" aria-hidden="true">☀️</span>
  <span slot="label-light">Light</span>
  <span slot="icon-dark" aria-hidden="true">🌙</span>
  <span slot="label-dark">Dark</span>
  <span slot="icon-system" aria-hidden="true">🌓</span>
  <span slot="label-system">System</span>
</theme-toggle>
↑ Back to top

# Example 3 - Without label

You can hide the label using the no-label attribute. For accessibility reasons, the label is visually hidden but still available to screen readers.

Source code
<theme-toggle no-label></theme-toggle>
↑ Back to top

# Example 4 - Without icon

You can hide the icon using the no-icon attribute.

Source code
<theme-toggle no-icon></theme-toggle>
↑ Back to top

# Example 5 - With custom styling

You can customize the styling of the element by using CSS Parts. The following example uses Water.css for styling.

Source code
<style>
  theme-toggle.custom-toggle::part(base) {
    border: 1px solid var(--border);
    border-radius: var(--border-radius);
    padding: 0.5rem 0.75rem;
    background-color: var(--button-base);
    font-size: 1.5rem;
    transition: background-color var(--animation-duration) ease-in-out;
  }

  @media (hover: hover) {
    theme-toggle.custom-toggle::part(base):hover {
      background-color: var(--button-hover);
    }
  }
</style>

<theme-toggle class="custom-toggle"></theme-toggle>
↑ Back to top

# Example 6 - Listen for theme change event

You can listen for the tt-theme-change event to get notified when the theme changes. The event will contain the newly selected theme in the detail property.

Source code
<theme-toggle></theme-toggle>

<script>
  const themeToggle = document.querySelector('theme-toggle');

  themeToggle.addEventListener('tt-theme-change', event => {
    console.log(event.detail); // => {theme: 'light' | 'dark' | 'system'}
  });
</script>
↑ Back to top

License

The MIT License (MIT)