Skip to content
MarkasUI
Type to search

    Guide

    MarkasUI

    Form

    Feedback

    Overlay

    Navigation

    Data

    Sections

    Animations

    Backgrounds

    Buttons

    Templates

    Pages

    Switch

    On/off toggle built on a native checkbox with role switch, no JavaScript needed in Blade.

    Switch

    <x-markas.switch label="Airplane mode" />
    import { Switch } from "@/components/markas/Switch";
    
    <Switch label="Airplane mode" />
    import Switch from "@/components/markas/Switch.vue";
    
    <Switch label="Airplane mode" />
    <script lang="ts">
      import Switch from "$lib/components/markas/Switch.svelte";
    </script>
    
    <div class="grid gap-4">
      <Switch name="notifications" label="Email notifications" checked />
      <Switch name="saver" label="Data saver mode" />
      <Switch name="beta" label="Beta features" disabled />
    </div>
    ---
    import Switch from "@/components/markas/Switch.astro";
    ---
    
    <div class="grid gap-4">
      <Switch name="notifications" label="Email notifications" checked />
      <Switch name="saver" label="Data saver mode" />
      <Switch name="beta" label="Beta features" disabled />
    </div>
    import { Component } from "@angular/core";
    import { Switch } from "./markas/switch.component";
    
    @Component({
      selector: "markas-example",
      imports: [Switch],
      template: `
        <div class="grid gap-4">
          <markas-switch name="notifications" label="Email notifications" checked />
          <markas-switch name="saver" label="Data saver mode" />
          <markas-switch name="beta" label="Beta features" disabled />
        </div>
      `,
    })
    export default class SwitchExample {}

    Examples

    States

    Settings list

    Push notifications

    Alerts on your phone for new messages.

    Weekly digest

    A summary of activity every Monday.

    Product news

    Occasional emails about new features.

    Push notifications

    Alerts on your phone for new messages.

    Weekly digest

    A summary of activity every Monday.

    Product news

    Occasional emails about new features.

    Push notifications

    Alerts on your phone for new messages.

    Weekly digest

    A summary of activity every Monday.

    Product news

    Occasional emails about new features.

    Push notifications

    Alerts on your phone for new messages.

    Weekly digest

    A summary of activity every Monday.

    Product news

    Occasional emails about new features.

    Push notifications

    Alerts on your phone for new messages.

    Weekly digest

    A summary of activity every Monday.

    Product news

    Occasional emails about new features.

    Feature card

    Two-factor authentication

    Ask for a code from your authenticator app each time you sign in.

    Two-factor authentication

    Ask for a code from your authenticator app each time you sign in.

    Two-factor authentication

    Ask for a code from your authenticator app each time you sign in.

    Two-factor authentication

    Ask for a code from your authenticator app each time you sign in.

    Two-factor authentication

    Ask for a code from your authenticator app each time you sign in.

    Horizontal

    Installation

    npx markasui@latest add switch

    Usage

    <x-markas.switch name="notifications" label="Email notifications" :checked="$user->wants_email" />

    No Alpine.js needed.

    import { Switch } from "@/components/markas/Switch";
    
    <Switch
      name="notifications"
      label="Email notifications"
      defaultChecked
      onCheckedChange={(checked) => console.log(checked)}
    />;
    <script setup lang="ts">
    import { ref } from "vue";
    import Switch from "@/components/markas/Switch.vue";
    
    const notifications = ref(true);
    </script>
    
    <template>
      <Switch
        v-model="notifications"
        name="notifications"
        label="Email notifications"
      />
    </template>
    <script lang="ts">
      import Switch from "$lib/components/markas/Switch.svelte";
    
      let notifications = $state(true);
    </script>
    
    <Switch
      bind:checked={notifications}
      name="notifications"
      label="Email notifications"
    />
    ---
    import Switch from "@/components/markas/Switch.astro";
    ---
    
    <Switch name="notifications" label="Email notifications" checked />

    No client script: the switch is a native checkbox.

    import { Component, signal } from "@angular/core";
    import { Switch } from "./markas/switch.component";
    
    @Component({
      selector: "app-settings",
      imports: [Switch],
      template: `
        <markas-switch
          [(checked)]="notifications"
          name="notifications"
          label="Email notifications"
        />
      `,
    })
    export class Settings {
      readonly notifications = signal(true);
    }
    • Switch is an <input type="checkbox" role="switch">, so its value is submitted with the form like a regular checkbox.
    • The on state is announced from the native checked, so no aria-checked is needed. The look uses checked: and peer-checked:, not data-state.
    • The component has no built-in text. Translate label directly.

    Props

    NameTypeDefaultDescription
    labelstring-Label text next to the switch, linked through for/id.
    checked / defaultChecked / v-model / bind:checkedbooleanfalseOn state. Blade and Astro use the checked attribute, React checked or defaultChecked, Vue v-model, Svelte bind:checked, Angular checked or [(checked)].
    onCheckedChange(checked: boolean) => void-React only. Called with the new state every time the switch changes.
    checkedChangeoutput<boolean>-Angular only. Emits the new state every time the switch changes.
    disabledbooleanfalseDisables the switch and dims the label.
    idstringautoInput id. Generated when empty.

    Accessibility

    • A native checkbox input with role switch, so screen readers announce on or off from the checked state.
    • Space toggles the state, and clicking the label toggles it too.
    • The thumb is decorative (aria-hidden) and moves through CSS peer-checked.

    Related components

    More form components with the same classes in every framework.