Lewati ke konten
MarkasUI
Ketik untuk mencari

    Panduan

    MarkasUI

    Form

    Feedback

    Overlay

    Navigasi

    Data

    Section

    Animasi

    Latar

    Tombol

    Template

    Halaman

    Toggle

    Tombol dua keadaan dengan aria-pressed untuk opsi seperti tebal atau tandai, dalam varian default dan outline serta tiga ukuran.

    <div class="flex items-center justify-center gap-2">
        <x-markas.toggle aria-label="Bold" pressed><svg class="size-4" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M7 5h6a3.5 3.5 0 0 1 0 7H7zm0 7h7a3.5 3.5 0 0 1 0 7H7z" /></svg></x-markas.toggle>
        <x-markas.toggle aria-label="Italic"><svg class="size-4" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M10 4h9M5 20h9M15 4 9 20" /></svg></x-markas.toggle>
        <x-markas.toggle variant="outline"><svg class="size-4" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M6 4h12v16l-6-4-6 4z" /></svg>Bookmark</x-markas.toggle>
    </div>
    import { Toggle } from "@/components/markas/Toggle";
    
    export default function ToggleExample() {
      return (
        <div className="flex items-center justify-center gap-2">
          <Toggle aria-label="Bold" defaultPressed>
            <svg
              className="size-4"
              viewBox="0 0 24 24"
              fill="none"
              stroke="currentColor"
              strokeWidth="1.5"
              strokeLinecap="round"
              strokeLinejoin="round"
              aria-hidden="true"
            >
              <path d="M7 5h6a3.5 3.5 0 0 1 0 7H7zm0 7h7a3.5 3.5 0 0 1 0 7H7z" />
            </svg>
          </Toggle>
          <Toggle aria-label="Italic">
            <svg
              className="size-4"
              viewBox="0 0 24 24"
              fill="none"
              stroke="currentColor"
              strokeWidth="1.5"
              strokeLinecap="round"
              strokeLinejoin="round"
              aria-hidden="true"
            >
              <path d="M10 4h9M5 20h9M15 4 9 20" />
            </svg>
          </Toggle>
          <Toggle variant="outline">
            <svg
              className="size-4"
              viewBox="0 0 24 24"
              fill="none"
              stroke="currentColor"
              strokeWidth="1.5"
              strokeLinecap="round"
              strokeLinejoin="round"
              aria-hidden="true"
            >
              <path d="M6 4h12v16l-6-4-6 4z" />
            </svg>
            Bookmark
          </Toggle>
        </div>
      );
    }
    <script setup lang="ts">
    import Toggle from "@/components/markas/Toggle.vue";
    </script>
    
    <template>
      <div class="flex items-center justify-center gap-2">
        <Toggle aria-label="Bold" default-pressed
          ><svg
            class="size-4"
            viewBox="0 0 24 24"
            fill="none"
            stroke="currentColor"
            stroke-width="1.5"
            stroke-linecap="round"
            stroke-linejoin="round"
            aria-hidden="true"
          >
            <path d="M7 5h6a3.5 3.5 0 0 1 0 7H7zm0 7h7a3.5 3.5 0 0 1 0 7H7z" /></svg
        ></Toggle>
        <Toggle aria-label="Italic"
          ><svg
            class="size-4"
            viewBox="0 0 24 24"
            fill="none"
            stroke="currentColor"
            stroke-width="1.5"
            stroke-linecap="round"
            stroke-linejoin="round"
            aria-hidden="true"
          >
            <path d="M10 4h9M5 20h9M15 4 9 20" /></svg
        ></Toggle>
        <Toggle variant="outline"
          ><svg
            class="size-4"
            viewBox="0 0 24 24"
            fill="none"
            stroke="currentColor"
            stroke-width="1.5"
            stroke-linecap="round"
            stroke-linejoin="round"
            aria-hidden="true"
          >
            <path d="M6 4h12v16l-6-4-6 4z" /></svg
          >Bookmark</Toggle
        >
      </div>
    </template>
    <script lang="ts">
      import Toggle from "$lib/components/markas/Toggle.svelte";
    </script>
    
    <div class="flex items-center justify-center gap-2">
      <Toggle aria-label="Bold" pressed>
        <svg
          class="size-4"
          viewBox="0 0 24 24"
          fill="none"
          stroke="currentColor"
          stroke-width="1.5"
          stroke-linecap="round"
          stroke-linejoin="round"
          aria-hidden="true"
        >
          <path d="M7 5h6a3.5 3.5 0 0 1 0 7H7zm0 7h7a3.5 3.5 0 0 1 0 7H7z" />
        </svg>
      </Toggle>
      <Toggle aria-label="Italic">
        <svg
          class="size-4"
          viewBox="0 0 24 24"
          fill="none"
          stroke="currentColor"
          stroke-width="1.5"
          stroke-linecap="round"
          stroke-linejoin="round"
          aria-hidden="true"
        >
          <path d="M10 4h9M5 20h9M15 4 9 20" />
        </svg>
      </Toggle>
      <Toggle variant="outline">
        <svg
          class="size-4"
          viewBox="0 0 24 24"
          fill="none"
          stroke="currentColor"
          stroke-width="1.5"
          stroke-linecap="round"
          stroke-linejoin="round"
          aria-hidden="true"
        >
          <path d="M6 4h12v16l-6-4-6 4z" />
        </svg>
        Bookmark
      </Toggle>
    </div>
    ---
    import Toggle from "@/components/markas/Toggle.astro";
    ---
    
    <div class="flex items-center justify-center gap-2">
      <Toggle aria-label="Bold" pressed>
        <svg
          class="size-4"
          viewBox="0 0 24 24"
          fill="none"
          stroke="currentColor"
          stroke-width="1.5"
          stroke-linecap="round"
          stroke-linejoin="round"
          aria-hidden="true"
        >
          <path d="M7 5h6a3.5 3.5 0 0 1 0 7H7zm0 7h7a3.5 3.5 0 0 1 0 7H7z" />
        </svg>
      </Toggle>
      <Toggle aria-label="Italic">
        <svg
          class="size-4"
          viewBox="0 0 24 24"
          fill="none"
          stroke="currentColor"
          stroke-width="1.5"
          stroke-linecap="round"
          stroke-linejoin="round"
          aria-hidden="true"
        >
          <path d="M10 4h9M5 20h9M15 4 9 20" />
        </svg>
      </Toggle>
      <Toggle variant="outline">
        <svg
          class="size-4"
          viewBox="0 0 24 24"
          fill="none"
          stroke="currentColor"
          stroke-width="1.5"
          stroke-linecap="round"
          stroke-linejoin="round"
          aria-hidden="true"
        >
          <path d="M6 4h12v16l-6-4-6 4z" />
        </svg>
        Bookmark
      </Toggle>
    </div>
    import { Component } from "@angular/core";
    import { Toggle } from "./markas/toggle.component";
    
    @Component({
      selector: "markas-example",
      imports: [Toggle],
      template: `
        <div class="flex items-center justify-center gap-2">
          <button markas-toggle aria-label="Bold" pressed>
            <svg
              class="size-4"
              viewBox="0 0 24 24"
              fill="none"
              stroke="currentColor"
              stroke-width="1.5"
              stroke-linecap="round"
              stroke-linejoin="round"
              aria-hidden="true"
            >
              <path d="M7 5h6a3.5 3.5 0 0 1 0 7H7zm0 7h7a3.5 3.5 0 0 1 0 7H7z" />
            </svg>
          </button>
          <button markas-toggle aria-label="Italic">
            <svg
              class="size-4"
              viewBox="0 0 24 24"
              fill="none"
              stroke="currentColor"
              stroke-width="1.5"
              stroke-linecap="round"
              stroke-linejoin="round"
              aria-hidden="true"
            >
              <path d="M10 4h9M5 20h9M15 4 9 20" />
            </svg>
          </button>
          <button markas-toggle variant="outline">
            <svg
              class="size-4"
              viewBox="0 0 24 24"
              fill="none"
              stroke="currentColor"
              stroke-width="1.5"
              stroke-linecap="round"
              stroke-linejoin="round"
              aria-hidden="true"
            >
              <path d="M6 4h12v16l-6-4-6 4z" />
            </svg>
            Bookmark
          </button>
        </div>
      `,
    })
    export default class ToggleFixture {}

    Contoh

    Varian

    Ukuran

    Toolbar format

    Instalasi

    npx markasui@latest add toggle

    Pemakaian

    <x-markas.toggle aria-label="Tebal" pressed>
        <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M7 5h6a3.5 3.5 0 0 1 0 7H7zm0 7h7a3.5 3.5 0 0 1 0 7H7z" /></svg>
    </x-markas.toggle>
    <x-markas.toggle variant="outline">Tandai</x-markas.toggle>

    Butuh Alpine.js.

    Livewire

    <x-markas.toggle wire:model.live="bold" aria-label="Bold">B</x-markas.toggle>

    wire:model terhubung ke status ditekan sebagai boolean.

    import { Toggle } from "@/components/markas/Toggle";
    
    <Toggle aria-label="Tebal" defaultPressed onPressedChange={setTebal}>
      <BoldIcon aria-hidden="true" />
    </Toggle>;
    <Toggle variant="outline">Tandai</Toggle>;
    <script setup lang="ts">
    import { ref } from "vue";
    import Toggle from "@/components/markas/Toggle.vue";
    
    const tebal = ref(true);
    </script>
    
    <template>
      <Toggle v-model="tebal" aria-label="Tebal">
        <BoldIcon aria-hidden="true" />
      </Toggle>
      <Toggle variant="outline">Tandai</Toggle>
    </template>
    <script lang="ts">
      import Toggle from "$lib/components/markas/Toggle.svelte";
    
      let tebal = $state(true);
    </script>
    
    <Toggle bind:pressed={tebal} aria-label="Tebal">
      <svg
        viewBox="0 0 24 24"
        fill="none"
        stroke="currentColor"
        stroke-width="1.5"
        aria-hidden="true"
        ><path d="M7 5h6a3.5 3.5 0 0 1 0 7H7zm0 7h7a3.5 3.5 0 0 1 0 7H7z" /></svg
      >
    </Toggle>
    <Toggle variant="outline">Tandai</Toggle>
    ---
    import Toggle from "@/components/markas/Toggle.astro";
    ---
    
    <Toggle variant="outline" pressed>Tandai</Toggle>

    Script kecil di halaman membalik aria-pressed dan data-state saat diklik.

    import { Component, signal } from "@angular/core";
    import { Toggle } from "./markas/toggle.component";
    
    @Component({
      selector: "app-penyunting",
      imports: [Toggle],
      template: `
        <button markas-toggle [(pressed)]="tebal" aria-label="Tebal">B</button>
        <button markas-toggle variant="outline">Tandai</button>
      `,
    })
    export class Penyunting {
      readonly tebal = signal(true);
    }
    • Toggle adalah <button aria-pressed>. Tampilannya mengikuti data-state="on" atau "off".
    • Ikon di dalamnya otomatis berukuran size-4. Toggle yang hanya berisi ikon wajib diberi aria-label.
    • Komponen ini tidak punya teks bawaan. Cukup isi konten dan aria-label dalam Bahasa Indonesia.

    Props

    NamaTipeDefaultKeterangan
    variant'default' | 'outline''default'Tampilan toggle. Outline menambahkan border dan latar surface.
    size'sm' | 'md' | 'lg''md'Tinggi 32, 36, atau 40px. Lebar minimum sama dengan tinggi untuk toggle berisi ikon saja.
    pressed / defaultPressed / v-model / bind:pressedbooleanfalseKeadaan ditekan. Blade dan Astro memakai pressed, React pressed atau defaultPressed, Vue v-model atau defaultPressed, Svelte bind:pressed, Angular pressed atau [(pressed)].
    onPressedChange(pressed: boolean) => void-Khusus React. Dipanggil dengan keadaan baru setiap kali diklik.
    pressedChangeoutput<boolean>-Khusus Angular. Memancarkan keadaan baru setiap kali diklik.
    disabledbooleanfalseMenonaktifkan toggle.

    Aksesibilitas

    • Button native dengan aria-pressed, sehingga screen reader membacakannya sebagai tombol toggle yang ditekan atau tidak.
    • Spasi dan Enter mengubah keadaan secara native.
    • Toggle yang hanya berisi ikon wajib diberi aria-label, dan ikonnya aria-hidden.

    Komponen terkait

    Komponen form lain dengan class yang sama di setiap framework.