Skip to content
MarkasUI
Type to search

    Guide

    MarkasUI

    Form

    Feedback

    Overlay

    Navigation

    Data

    Sections

    Animations

    Backgrounds

    Buttons

    Templates

    Pages

    Toggle

    Two-state button with aria-pressed for options like bold or bookmark, in default and outline variants and three sizes.

    <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 {}

    Examples

    Variants

    Sizes

    Formatting toolbar

    Installation

    npx markasui@latest add toggle

    Usage

    <x-markas.toggle aria-label="Bold" 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">Bookmark</x-markas.toggle>

    Needs Alpine.js.

    Livewire

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

    wire:model binds the pressed state as a boolean.

    import { Toggle } from "@/components/markas/Toggle";
    
    <Toggle aria-label="Bold" defaultPressed onPressedChange={setBold}>
      <BoldIcon aria-hidden="true" />
    </Toggle>;
    <Toggle variant="outline">Bookmark</Toggle>;
    <script setup lang="ts">
    import { ref } from "vue";
    import Toggle from "@/components/markas/Toggle.vue";
    
    const bold = ref(true);
    </script>
    
    <template>
      <Toggle v-model="bold" aria-label="Bold">
        <BoldIcon aria-hidden="true" />
      </Toggle>
      <Toggle variant="outline">Bookmark</Toggle>
    </template>
    <script lang="ts">
      import Toggle from "$lib/components/markas/Toggle.svelte";
    
      let bold = $state(true);
    </script>
    
    <Toggle bind:pressed={bold} aria-label="Bold">
      <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">Bookmark</Toggle>
    ---
    import Toggle from "@/components/markas/Toggle.astro";
    ---
    
    <Toggle variant="outline" pressed>Bookmark</Toggle>

    A small page script flips aria-pressed and data-state on click.

    import { Component, signal } from "@angular/core";
    import { Toggle } from "./markas/toggle.component";
    
    @Component({
      selector: "app-editor",
      imports: [Toggle],
      template: `
        <button markas-toggle [(pressed)]="bold" aria-label="Bold">B</button>
        <button markas-toggle variant="outline">Bookmark</button>
      `,
    })
    export class Editor {
      readonly bold = signal(true);
    }
    • Toggle is a <button aria-pressed>. The look follows data-state="on" or "off".
    • Icons inside get size-4 automatically. An icon-only toggle must have aria-label.
    • The component has no built-in text. Translate the content and aria-label directly.

    Props

    NameTypeDefaultDescription
    variant'default' | 'outline''default'Look of the toggle. Outline adds a border and a surface background.
    size'sm' | 'md' | 'lg''md'Height 32, 36, or 40px. The minimum width matches the height for icon-only toggles.
    pressed / defaultPressed / v-model / bind:pressedbooleanfalsePressed state. Blade and Astro use pressed, React pressed or defaultPressed, Vue v-model or defaultPressed, Svelte bind:pressed, Angular pressed or [(pressed)].
    onPressedChange(pressed: boolean) => void-React only. Called with the new state on every click.
    pressedChangeoutput<boolean>-Angular only. Emits the new state on every click.
    disabledbooleanfalseDisables the toggle.

    Accessibility

    • A native button with aria-pressed, so screen readers announce it as a toggle button that is pressed or not pressed.
    • Space and Enter toggle the state natively.
    • An icon-only toggle needs aria-label, and the icon gets aria-hidden.

    Related components

    More form components with the same classes in every framework.