Skip to content
MarkasUI
Type to search

    Guide

    MarkasUI

    Form

    Feedback

    Overlay

    Navigation

    Data

    Sections

    Animations

    Backgrounds

    Buttons

    Templates

    Pages

    Theme Toggle

    Light and dark theme switch with six distinct icon animations: rotate, morph, flip, slide, eclipse, and spring.

    Rotate
    Morph
    Flip
    Slide
    Eclipse
    Spring
    Rotate
    Morph
    Flip
    Slide
    Eclipse
    Spring
    Rotate
    Morph
    Flip
    Slide
    Eclipse
    Spring
    Rotate
    Morph
    Flip
    Slide
    Eclipse
    Spring
    Rotate
    Morph
    Flip
    Slide
    Eclipse
    Spring
    <div class="flex flex-col items-center gap-5">
        <div class="grid w-full grid-cols-6 gap-2">
            <div class="flex flex-col items-center gap-2">
                <x-markas.theme-toggle animation="rotate" variant="outline" size="lg" />
                <span class="text-xs text-muted">Rotate</span>
            </div>
            <div class="flex flex-col items-center gap-2">
                <x-markas.theme-toggle animation="morph" variant="outline" size="lg" />
                <span class="text-xs text-muted">Morph</span>
            </div>
            <div class="flex flex-col items-center gap-2">
                <x-markas.theme-toggle animation="flip" variant="outline" size="lg" />
                <span class="text-xs text-muted">Flip</span>
            </div>
            <div class="flex flex-col items-center gap-2">
                <x-markas.theme-toggle animation="slide" variant="outline" size="lg" />
                <span class="text-xs text-muted">Slide</span>
            </div>
            <div class="flex flex-col items-center gap-2">
                <x-markas.theme-toggle animation="eclipse" variant="outline" size="lg" />
                <span class="text-xs text-muted">Eclipse</span>
            </div>
            <div class="flex flex-col items-center gap-2">
                <x-markas.theme-toggle animation="spring" variant="outline" size="lg" />
                <span class="text-xs text-muted">Spring</span>
            </div>
        </div>
        <x-markas.button variant="secondary" size="sm" x-data x-on:click="document.dispatchEvent(new Event('markas-theme-demo'))">Play all</x-markas.button>
    </div>
    import { Button } from "@/components/markas/Button";
    import { ThemeToggle } from "@/components/markas/ThemeToggle";
    
    /** Plays every toggle there and back without changing the page theme. */
    const playAll = () => document.dispatchEvent(new Event("markas-theme-demo"));
    
    export default function ThemeToggleExample() {
      return (
        <div className="flex flex-col items-center gap-5">
          <div className="grid w-full grid-cols-6 gap-2">
            <div className="flex flex-col items-center gap-2">
              <ThemeToggle animation="rotate" variant="outline" size="lg" />
              <span className="text-xs text-muted">Rotate</span>
            </div>
            <div className="flex flex-col items-center gap-2">
              <ThemeToggle animation="morph" variant="outline" size="lg" />
              <span className="text-xs text-muted">Morph</span>
            </div>
            <div className="flex flex-col items-center gap-2">
              <ThemeToggle animation="flip" variant="outline" size="lg" />
              <span className="text-xs text-muted">Flip</span>
            </div>
            <div className="flex flex-col items-center gap-2">
              <ThemeToggle animation="slide" variant="outline" size="lg" />
              <span className="text-xs text-muted">Slide</span>
            </div>
            <div className="flex flex-col items-center gap-2">
              <ThemeToggle animation="eclipse" variant="outline" size="lg" />
              <span className="text-xs text-muted">Eclipse</span>
            </div>
            <div className="flex flex-col items-center gap-2">
              <ThemeToggle animation="spring" variant="outline" size="lg" />
              <span className="text-xs text-muted">Spring</span>
            </div>
          </div>
          <Button variant="secondary" size="sm" onClick={playAll}>
            Play all
          </Button>
        </div>
      );
    }
    <script setup lang="ts">
    import Button from "@/components/markas/Button.vue";
    import ThemeToggle from "@/components/markas/ThemeToggle.vue";
    
    /** Plays every toggle there and back without changing the page theme. */
    const playAll = () => document.dispatchEvent(new Event("markas-theme-demo"));
    </script>
    
    <template>
      <div class="flex flex-col items-center gap-5">
        <div class="grid w-full grid-cols-6 gap-2">
          <div class="flex flex-col items-center gap-2">
            <ThemeToggle animation="rotate" variant="outline" size="lg" />
            <span class="text-xs text-muted">Rotate</span>
          </div>
          <div class="flex flex-col items-center gap-2">
            <ThemeToggle animation="morph" variant="outline" size="lg" />
            <span class="text-xs text-muted">Morph</span>
          </div>
          <div class="flex flex-col items-center gap-2">
            <ThemeToggle animation="flip" variant="outline" size="lg" />
            <span class="text-xs text-muted">Flip</span>
          </div>
          <div class="flex flex-col items-center gap-2">
            <ThemeToggle animation="slide" variant="outline" size="lg" />
            <span class="text-xs text-muted">Slide</span>
          </div>
          <div class="flex flex-col items-center gap-2">
            <ThemeToggle animation="eclipse" variant="outline" size="lg" />
            <span class="text-xs text-muted">Eclipse</span>
          </div>
          <div class="flex flex-col items-center gap-2">
            <ThemeToggle animation="spring" variant="outline" size="lg" />
            <span class="text-xs text-muted">Spring</span>
          </div>
        </div>
        <Button variant="secondary" size="sm" @click="playAll">Play all</Button>
      </div>
    </template>
    <script lang="ts">
      import Button from "$lib/components/markas/Button.svelte";
      import ThemeToggle from "$lib/components/markas/ThemeToggle.svelte";
    
      /** Plays every toggle there and back without changing the page theme. */
      const playAll = () => document.dispatchEvent(new Event("markas-theme-demo"));
    </script>
    
    <div class="flex flex-col items-center gap-5">
      <div class="grid w-full grid-cols-6 gap-2">
        <div class="flex flex-col items-center gap-2">
          <ThemeToggle animation="rotate" variant="outline" size="lg" />
          <span class="text-xs text-muted">Rotate</span>
        </div>
        <div class="flex flex-col items-center gap-2">
          <ThemeToggle animation="morph" variant="outline" size="lg" />
          <span class="text-xs text-muted">Morph</span>
        </div>
        <div class="flex flex-col items-center gap-2">
          <ThemeToggle animation="flip" variant="outline" size="lg" />
          <span class="text-xs text-muted">Flip</span>
        </div>
        <div class="flex flex-col items-center gap-2">
          <ThemeToggle animation="slide" variant="outline" size="lg" />
          <span class="text-xs text-muted">Slide</span>
        </div>
        <div class="flex flex-col items-center gap-2">
          <ThemeToggle animation="eclipse" variant="outline" size="lg" />
          <span class="text-xs text-muted">Eclipse</span>
        </div>
        <div class="flex flex-col items-center gap-2">
          <ThemeToggle animation="spring" variant="outline" size="lg" />
          <span class="text-xs text-muted">Spring</span>
        </div>
      </div>
      <Button variant="secondary" size="sm" onclick={playAll}>Play all</Button>
    </div>
    ---
    import Button from "@/components/markas/Button.astro";
    import ThemeToggle from "@/components/markas/ThemeToggle.astro";
    
    ---
    
      <div class="flex flex-col items-center gap-5">
        <div class="grid w-full grid-cols-6 gap-2">
          <div class="flex flex-col items-center gap-2">
            <ThemeToggle animation="rotate" variant="outline" size="lg" />
            <span class="text-xs text-muted">Rotate</span>
          </div>
          <div class="flex flex-col items-center gap-2">
            <ThemeToggle animation="morph" variant="outline" size="lg" />
            <span class="text-xs text-muted">Morph</span>
          </div>
          <div class="flex flex-col items-center gap-2">
            <ThemeToggle animation="flip" variant="outline" size="lg" />
            <span class="text-xs text-muted">Flip</span>
          </div>
          <div class="flex flex-col items-center gap-2">
            <ThemeToggle animation="slide" variant="outline" size="lg" />
            <span class="text-xs text-muted">Slide</span>
          </div>
          <div class="flex flex-col items-center gap-2">
            <ThemeToggle animation="eclipse" variant="outline" size="lg" />
            <span class="text-xs text-muted">Eclipse</span>
          </div>
          <div class="flex flex-col items-center gap-2">
            <ThemeToggle animation="spring" variant="outline" size="lg" />
            <span class="text-xs text-muted">Spring</span>
          </div>
        </div>
        <Button variant="secondary" size="sm" data-play-all>Play all</Button>
      </div>
    
    <script>
      /** Plays every toggle there and back without changing the page theme. */
      document
        .querySelector("[data-play-all]")
        ?.addEventListener("click", () =>
          document.dispatchEvent(new Event("markas-theme-demo")),
        );
    </script>
    import { Component } from "@angular/core";
    import { Button } from "./markas/button.component";
    import { ThemeToggle } from "./markas/theme-toggle.component";
    
    @Component({
      selector: "markas-example",
      imports: [Button, ThemeToggle],
      template: `
        <div class="flex flex-col items-center gap-5">
          <div class="grid w-full grid-cols-6 gap-2">
            <div class="flex flex-col items-center gap-2">
              <button
                markas-theme-toggle
                animation="rotate"
                variant="outline"
                size="lg"
              ></button>
              <span class="text-xs text-muted">Rotate</span>
            </div>
            <div class="flex flex-col items-center gap-2">
              <button
                markas-theme-toggle
                animation="morph"
                variant="outline"
                size="lg"
              ></button>
              <span class="text-xs text-muted">Morph</span>
            </div>
            <div class="flex flex-col items-center gap-2">
              <button
                markas-theme-toggle
                animation="flip"
                variant="outline"
                size="lg"
              ></button>
              <span class="text-xs text-muted">Flip</span>
            </div>
            <div class="flex flex-col items-center gap-2">
              <button
                markas-theme-toggle
                animation="slide"
                variant="outline"
                size="lg"
              ></button>
              <span class="text-xs text-muted">Slide</span>
            </div>
            <div class="flex flex-col items-center gap-2">
              <button
                markas-theme-toggle
                animation="eclipse"
                variant="outline"
                size="lg"
              ></button>
              <span class="text-xs text-muted">Eclipse</span>
            </div>
            <div class="flex flex-col items-center gap-2">
              <button
                markas-theme-toggle
                animation="spring"
                variant="outline"
                size="lg"
              ></button>
              <span class="text-xs text-muted">Spring</span>
            </div>
          </div>
          <button markas-button variant="secondary" size="sm" (click)="playAll()">
            Play all
          </button>
        </div>
      `,
    })
    export default class ThemeToggleExample {
      /** Plays every toggle there and back without changing the page theme. */
      protected playAll() {
        document.dispatchEvent(new Event("markas-theme-demo"));
      }
    }

    Examples

    Sizes and variants

    In a navbar

    MarkasUI
    MarkasUI
    MarkasUI
    MarkasUI

    With text

    Installation

    npx markasui@latest add theme-toggle

    Usage

    <x-markas.theme-toggle />
    
    <x-markas.theme-toggle animation="eclipse" variant="outline" size="sm" />
    
    <x-markas.theme-toggle :show-text="true" dark-label="Dark mode" light-label="Light mode" animation="flip" />
    import { ThemeToggle } from "@/components/markas/ThemeToggle";
    
    <ThemeToggle />
    <ThemeToggle animation="eclipse" variant="outline" size="sm" />
    <ThemeToggle showText darkLabel="Dark mode" lightLabel="Light mode" animation="flip" onThemeChange={(theme) => console.log(theme)} />
    <script setup lang="ts">
    import ThemeToggle from "@/components/markas/ThemeToggle.vue";
    </script>
    
    <template>
      <ThemeToggle />
      <ThemeToggle animation="eclipse" variant="outline" size="sm" />
      <ThemeToggle
        show-text
        dark-label="Dark mode"
        light-label="Light mode"
        animation="flip"
        @change="(theme) => console.log(theme)"
      />
    </template>
    <script lang="ts">
      import ThemeToggle from "$lib/components/markas/ThemeToggle.svelte";
    </script>
    
    <ThemeToggle animation="morph" variant="outline" />
    ---
    import ThemeToggle from "@/components/markas/ThemeToggle.astro";
    ---
    
    <ThemeToggle animation="morph" variant="outline" />

    A small bundled script runs each toggle through the markas-theme-toggle element.

    import { Component } from "@angular/core";
    import { ThemeToggle } from "./markas/theme-toggle.component";
    
    @Component({
      selector: "app-header",
      imports: [ThemeToggle],
      template: `
        <button markas-theme-toggle animation="morph" variant="outline"></button>
      `,
    })
    export class Header {}
    • The toggle adds or removes the dark class on <html>, saves the choice in localStorage (storageKey), and dispatches markas-theme-change on document with the theme as detail.
    • On load it reads the saved choice, then a dark class the page already set, then prefers-color-scheme. Add this to <head> so the page never flashes the wrong theme:
    <script>
      try {
        const saved = localStorage.getItem("markas-theme");
        document.documentElement.classList.toggle(
          "dark",
          saved
            ? saved === "dark"
            : matchMedia("(prefers-color-scheme: dark)").matches,
        );
      } catch {}
    </script>
    • Animations: rotate, morph, flip, slide, eclipse, spring. Each click plays keyframes with the Web Animations API (transform and opacity only). Users with reduced motion get an instant swap.
    • document.dispatchEvent(new Event("markas-theme-demo")) plays every toggle there and back without changing the theme, handy for a “Play all” button.
    • The button keeps its own data-state (light or dark) and follows the dark class on <html>, so several toggles, or a theme set elsewhere, stay in sync.

    Props

    NameTypeDefaultDescription
    animation'rotate' | 'morph' | 'flip' | 'slide' | 'eclipse' | 'spring'rotateIcon animation, played on click with the Web Animations API (transform and opacity only, 650ms).
    variant'ghost' | 'outline' | 'solid'ghostVisual style.
    size'sm' | 'md' | 'lg'mdSize 32, 40, or 48px, the same heights as Button.
    theme'light' | 'dark'-Controlled theme in React and Vue: the parent saves it and handles clicks. Blade applies it once on load.
    storageKeystringmarkas-themelocalStorage key that keeps the choice.
    showTextbooleanfalseShows the label as visible text next to the icon. Pair it with short labels such as Dark mode.
    lightLabelstringSwitch to light themeName while the dark theme is on. Visible text with showText.
    darkLabelstringSwitch to dark themeName while the light theme is on. Visible text with showText.
    onThemeChange(theme: 'light' | 'dark') => void-React callback after a click. Vue emits change. Every version also dispatches markas-theme-change on document.
    themeChangeoutput<"light" | "dark">-Angular only. Emits the theme a click asked for. Svelte uses onThemeChange.

    Accessibility

    • A native button element, so Enter and Space work and focus shows a 2px primary outline.
    • The name says the action, Switch to dark theme or Switch to light theme, and follows the button data-state.
    • The icon is aria-hidden, so screen readers only hear the label.
    • With prefers-reduced-motion the icon swaps at once, without animation.

    Related components

    More markasui components with the same classes in every framework.