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>Examples
Variants
<div class="grid gap-4">
<div class="flex items-center justify-center gap-2">
<x-markas.toggle>Wrap lines</x-markas.toggle>
<x-markas.toggle pressed>Show grid</x-markas.toggle>
</div>
<div class="flex items-center justify-center gap-2">
<x-markas.toggle variant="outline">Subscribe</x-markas.toggle>
<x-markas.toggle variant="outline" pressed>Following</x-markas.toggle>
</div>
</div>Sizes
<div class="flex items-center justify-center gap-2">
<x-markas.toggle variant="outline" size="sm"><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="m12 3 2.8 5.7 6.2.9-4.5 4.4 1 6.2-5.5-2.9-5.5 2.9 1-6.2L3 9.6l6.2-.9z" /></svg>Small</x-markas.toggle>
<x-markas.toggle variant="outline" size="md" 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="m12 3 2.8 5.7 6.2.9-4.5 4.4 1 6.2-5.5-2.9-5.5 2.9 1-6.2L3 9.6l6.2-.9z" /></svg>Medium</x-markas.toggle>
<x-markas.toggle variant="outline" size="lg"><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="m12 3 2.8 5.7 6.2.9-4.5 4.4 1 6.2-5.5-2.9-5.5 2.9 1-6.2L3 9.6l6.2-.9z" /></svg>Large</x-markas.toggle>
</div>Formatting toolbar
<div class="flex items-center justify-center">
<div role="group" aria-label="Formatting" class="flex items-center gap-1 rounded-lg border border-line bg-surface p-1">
<x-markas.toggle size="sm" 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 size="sm" 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 size="sm" aria-label="Underline"><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 4v7a5 5 0 0 0 10 0V4M5 20h14" /></svg></x-markas.toggle>
<x-markas.toggle size="sm" aria-label="Strikethrough" disabled><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="M5 12h14M16 6.5A4 4 0 0 0 12 5h-1a3.5 3.5 0 0 0 0 7h2a3.5 3.5 0 0 1 0 7h-1a4 4 0 0 1-4-1.5" /></svg></x-markas.toggle>
</div>
</div>Installation
npx markasui@latest add togglepnpm dlx markasui@latest add toggleyarn dlx markasui@latest add togglebunx --bun markasui@latest add toggle- Make sure Tailwind CSS v4 and markas.css are installed. See Installation.
- Install the dependency:
npm install alpinejs - Copy the component source below to
resources/views/components/markas/toggle.blade.php.
resources/views/components/markas/toggle.blade.php
- Make sure Tailwind CSS v4 and markas.css are installed. See Installation.
- Copy the component source below to
src/components/markas/Toggle.tsx.
src/components/markas/Toggle.tsx
- Make sure Tailwind CSS v4 and markas.css are installed. See Installation.
- Copy the component source below to
src/components/markas/Toggle.vue.
src/components/markas/Toggle.vue
- Make sure Tailwind CSS v4 and markas.css are installed. See Installation.
- Copy the component source below to
src/lib/components/markas/Toggle.svelte.
src/lib/components/markas/Toggle.svelte
- Make sure Tailwind CSS v4 and markas.css are installed. See Installation.
- Copy the component source below to
src/components/markas/Toggle.astro.
src/components/markas/Toggle.astro
- Make sure Tailwind CSS v4 and markas.css are installed. See Installation.
- Copy the component source below to
src/app/markas/toggle.component.ts.
src/app/markas/toggle.component.ts
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 followsdata-state="on"or"off". - Icons inside get
size-4automatically. An icon-only toggle must havearia-label. - The component has no built-in text. Translate the content and
aria-labeldirectly.
Props
| Name | Type | Default | Description |
|---|---|---|---|
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:pressed | boolean | false | Pressed 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. |
pressedChange | output<boolean> | - | Angular only. Emits the new state on every click. |
disabled | boolean | false | Disables 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.
- ButtonButton with primary, secondary, ghost, and danger variants.
- CheckboxNative checkbox with an optional label and description, custom styled without JavaScript.
- ComboboxText input with a filtered suggestion list in a popover, following the WAI-ARIA Combobox pattern with list autocomplete.
- InputText field with a label, hint, and error message wired up through ARIA.
- Input OTPOne-time code input split into single-digit boxes, with auto advance, paste support, and one hidden field for form submit.
- LabelForm label with consistent styling and optional required or optional markers that screen readers announce correctly.