Lewati ke konten
MarkasUI
Ketik untuk mencari

    Panduan

    MarkasUI

    Form

    Feedback

    Overlay

    Navigasi

    Data

    Section

    Animasi

    Latar

    Tombol

    Template

    Halaman

    Theme Toggle

    Tombol ganti tema terang dan gelap dengan enam animasi ikon yang berbeda: rotate, morph, flip, slide, eclipse, dan 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"));
      }
    }

    Contoh

    Ukuran dan varian

    Di dalam navbar

    MarkasUI
    MarkasUI
    MarkasUI
    MarkasUI

    Dengan teks

    Instalasi

    npx markasui@latest add theme-toggle

    Pemakaian

    <x-markas.theme-toggle light-label="Ganti ke tema terang" dark-label="Ganti ke tema gelap" />
    
    <x-markas.theme-toggle
        :show-text="true"
        animation="eclipse"
        light-label="Mode terang"
        dark-label="Mode gelap"
    />
    import { ThemeToggle } from "@/components/markas/ThemeToggle";
    
    <ThemeToggle lightLabel="Ganti ke tema terang" darkLabel="Ganti ke tema gelap" />
    <ThemeToggle
      showText
      animation="eclipse"
      lightLabel="Mode terang"
      darkLabel="Mode gelap"
    />
    <script setup lang="ts">
    import ThemeToggle from "@/components/markas/ThemeToggle.vue";
    </script>
    
    <template>
      <ThemeToggle
        light-label="Ganti ke tema terang"
        dark-label="Ganti ke tema gelap"
      />
      <ThemeToggle
        show-text
        animation="eclipse"
        light-label="Mode terang"
        dark-label="Mode gelap"
      />
    </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" />

    Satu script kecil yang dibundel menjalankan setiap toggle lewat elemen markas-theme-toggle.

    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 {}
    • Toggle menambah atau menghapus class dark di <html>, menyimpan pilihan di localStorage (storageKey), dan mengirim event markas-theme-change di document dengan tema sebagai detail.
    • Saat dimuat, toggle membaca pilihan tersimpan, lalu class dark yang sudah dipasang halaman, lalu prefers-color-scheme. Tambahkan script ini di <head> agar halaman tidak berkedip dengan tema yang salah:
    <script>
      try {
        const saved = localStorage.getItem("markas-theme");
        document.documentElement.classList.toggle(
          "dark",
          saved
            ? saved === "dark"
            : matchMedia("(prefers-color-scheme: dark)").matches,
        );
      } catch {}
    </script>
    • Animasi: rotate, morph, flip, slide, eclipse, spring. Setiap klik memutar keyframes lewat Web Animations API (hanya transform dan opacity). Pengguna dengan reduced motion mendapat pergantian langsung.
    • document.dispatchEvent(new Event("markas-theme-demo")) memutar semua toggle bolak-balik tanpa mengganti tema, cocok untuk tombol “Play all”.
    • Tombol menyimpan data-state sendiri (light atau dark) dan mengikuti class dark di <html>, jadi beberapa toggle, atau tema yang diubah dari tempat lain, tetap selaras.

    Props

    NamaTipeDefaultKeterangan
    animation'rotate' | 'morph' | 'flip' | 'slide' | 'eclipse' | 'spring'rotateAnimasi ikon, diputar saat klik lewat Web Animations API (hanya transform dan opacity, 650ms).
    variant'ghost' | 'outline' | 'solid'ghostGaya visual.
    size'sm' | 'md' | 'lg'mdUkuran 32, 40, atau 48px, sama dengan tinggi Button.
    theme'light' | 'dark'-Tema terkontrol di React dan Vue: induk yang menyimpan dan menangani klik. Blade menerapkannya sekali saat dimuat.
    storageKeystringmarkas-themeKey localStorage untuk menyimpan pilihan.
    showTextbooleanfalseMenampilkan label sebagai teks di samping ikon. Pakai label pendek seperti Mode gelap.
    lightLabelstringSwitch to light themeNama tombol saat tema gelap aktif. Menjadi teks terlihat dengan showText.
    darkLabelstringSwitch to dark themeNama tombol saat tema terang aktif. Menjadi teks terlihat dengan showText.
    onThemeChange(theme: 'light' | 'dark') => void-Callback React setelah klik. Vue memancarkan change. Semua versi juga mengirim markas-theme-change di document.
    themeChangeoutput<"light" | "dark">-Khusus Angular. Memancarkan tema yang diminta klik. Svelte memakai onThemeChange.

    Aksesibilitas

    • Elemen button native, jadi Enter dan Spasi berfungsi dan fokus tampil sebagai outline 2px warna primary.
    • Nama tombol menyebut aksinya, Switch to dark theme atau Switch to light theme, dan mengikuti data-state tombol.
    • Ikon aria-hidden, jadi screen reader hanya mendengar label.
    • Dengan prefers-reduced-motion ikon langsung berganti tanpa animasi.

    Komponen terkait

    Komponen markasui lain dengan class yang sama di setiap framework.