Skip to content
MarkasUI
Type to search

    Guide

    MarkasUI

    Form

    Feedback

    Overlay

    Navigation

    Data

    Sections

    Animations

    Backgrounds

    Buttons

    Templates

    Pages

    Toggle Group

    Segmented group of toggle buttons with single or multiple selection and arrow key navigation between items.

    <div class="flex justify-center">
        <x-markas.toggle-group
            label="Calendar view"
            value="week"
            :items="[
                ['value' => 'day', 'label' => 'Day'],
                ['value' => 'week', 'label' => 'Week'],
                ['value' => 'month', 'label' => 'Month'],
            ]"
        />
    </div>
    import { ToggleGroup } from "@/components/markas/ToggleGroup";
    
    export default function ToggleGroupExample() {
      return (
        <div className="flex justify-center">
          <ToggleGroup
            label="Calendar view"
            defaultValue="week"
            items={[
              { value: "day", label: "Day" },
              { value: "week", label: "Week" },
              { value: "month", label: "Month" },
            ]}
          />
        </div>
      );
    }
    <script setup lang="ts">
    import ToggleGroup from "@/components/markas/ToggleGroup.vue";
    </script>
    
    <template>
      <div class="flex justify-center">
        <ToggleGroup
          label="Calendar view"
          default-value="week"
          :items="[
            { value: 'day', label: 'Day' },
            { value: 'week', label: 'Week' },
            { value: 'month', label: 'Month' },
          ]"
        />
      </div>
    </template>
    <script lang="ts">
      import ToggleGroup from "$lib/components/markas/ToggleGroup.svelte";
    </script>
    
    <div class="flex justify-center">
      <ToggleGroup
        label="Calendar view"
        value="week"
        items={[
          { value: "day", label: "Day" },
          { value: "week", label: "Week" },
          { value: "month", label: "Month" },
        ]}
      />
    </div>
    ---
    import ToggleGroup from "@/components/markas/ToggleGroup.astro";
    ---
    
    <div class="flex justify-center">
      <ToggleGroup
        label="Calendar view"
        value="week"
        items={[
          { value: "day", label: "Day" },
          { value: "week", label: "Week" },
          { value: "month", label: "Month" },
        ]}
      />
    </div>
    import { Component } from "@angular/core";
    import { ToggleGroup } from "./markas/toggle-group.component";
    
    @Component({
      selector: "markas-example",
      imports: [ToggleGroup],
      template: `
        <div class="flex justify-center">
          <markas-toggle-group label="Calendar view" value="week" [items]="items" />
        </div>
      `,
    })
    export default class ToggleGroupFixture {
      protected readonly items = [
        { value: "day", label: "Day" },
        { value: "week", label: "Week" },
        { value: "month", label: "Month" },
      ];
    }

    Examples

    Multiple selection

    Icons only

    Icons with text

    Installation

    npx markasui@latest add toggle-group

    Usage

    <x-markas.toggle-group
        label="Calendar view"
        value="week"
        :items="[
            ['value' => 'day', 'label' => 'Day'],
            ['value' => 'week', 'label' => 'Week'],
            ['value' => 'month', 'label' => 'Month'],
        ]"
    />

    Needs Alpine.js. Replace a label with an icon through a slot named after its value, such as <x-slot:left>, and pass ariaLabel in that item.

    Livewire

    <x-markas.toggle-group label="View" wire:model.live="view" :items="$views" />

    wire:model binds a string (or null) for type="single" and an array for type="multiple".

    import { ToggleGroup } from "@/components/markas/ToggleGroup";
    
    <ToggleGroup
      label="Calendar view"
      defaultValue="week"
      onValueChange={(value) => console.log(value)}
      items={[
        { value: "day", label: "Day" },
        { value: "week", label: "Week" },
        { value: "month", label: "Month" },
      ]}
    />;
    
    <ToggleGroup
      type="multiple"
      label="Text style"
      defaultValue={["bold"]}
      items={[
        {
          value: "bold",
          label: <BoldIcon aria-hidden="true" />,
          ariaLabel: "Bold",
        },
        {
          value: "italic",
          label: <ItalicIcon aria-hidden="true" />,
          ariaLabel: "Italic",
        },
      ]}
    />;
    <script setup lang="ts">
    import { ref } from "vue";
    import ToggleGroup from "@/components/markas/ToggleGroup.vue";
    
    const view = ref("week");
    </script>
    
    <template>
      <ToggleGroup
        v-model="view"
        label="Calendar view"
        :items="[
          { value: 'day', label: 'Day' },
          { value: 'week', label: 'Week' },
          { value: 'month', label: 'Month' },
        ]"
      />
    </template>
    <script lang="ts">
      import ToggleGroup from "$lib/components/markas/ToggleGroup.svelte";
    
      let view = $state("week");
    </script>
    
    {#snippet leftIcon()}
      <svg
        viewBox="0 0 24 24"
        fill="none"
        stroke="currentColor"
        stroke-width="1.5"
        aria-hidden="true"><path d="M4 6h16M4 12h10M4 18h14" /></svg
      >
    {/snippet}
    
    <ToggleGroup
      bind:value={view}
      label="Calendar view"
      items={[
        { value: "day", label: "Day" },
        { value: "week", label: "Week" },
        { value: "month", label: "Month" },
      ]}
    />
    
    <ToggleGroup
      label="Text alignment"
      value="left"
      items={[{ value: "left", label: leftIcon, ariaLabel: "Align left" }]}
    />
    ---
    import ToggleGroup from "@/components/markas/ToggleGroup.astro";
    ---
    
    <ToggleGroup
      label="Calendar view"
      value="week"
      items={[
        { value: "day", label: "Day" },
        { value: "week", label: "Week" },
        { value: "month", label: "Month" },
      ]}
    />

    Replace a label with an icon through a slot named after its value, such as <Fragment slot="left">. A small page script handles clicks and arrow keys.

    import { Component, signal } from "@angular/core";
    import {
      ToggleGroup,
      ToggleGroupItemTemplate,
    } from "./markas/toggle-group.component";
    
    @Component({
      selector: "app-calendar",
      imports: [ToggleGroup, ToggleGroupItemTemplate],
      template: `
        <markas-toggle-group
          [(value)]="view"
          label="Calendar view"
          [items]="views"
        />
        <markas-toggle-group label="Text alignment" value="left" [items]="align">
          <ng-template markasToggleGroupItem="left">
            <svg viewBox="0 0 24 24" aria-hidden="true">...</svg>
          </ng-template>
        </markas-toggle-group>
      `,
    })
    export class Calendar {
      readonly view = signal<string | string[]>("week");
      readonly views = [
        { value: "day", label: "Day" },
        { value: "week", label: "Week" },
        { value: "month", label: "Month" },
      ];
      readonly align = [{ value: "left", label: "Left", ariaLabel: "Align left" }];
    }
    • Single returns a string, empty when the pressed item is clicked again. Multiple returns an array in item order.
    • Values become slot names in Blade, Vue, and Astro, so use only lowercase letters, digits, or underscores.
    • The component has no built-in text. Pass label and the item labels in your site’s language.

    Props

    NameTypeDefaultDescription
    items{ value, label, ariaLabel? }[][]Buttons in the group. ariaLabel is required when the label is only an icon. Blade, Vue, and Astro can replace a label through a slot named after its value, Svelte accepts a snippet as label, Angular an ng-template with markasToggleGroupItem set to the value.
    type'single' | 'multiple''single'Single allows one pressed item, or none. Multiple allows any number.
    value / defaultValue / v-model / bind:valuestring | string[]-Pressed items. A string for single (empty when none) and an array for multiple. Blade and Astro use value, React value or defaultValue, Vue v-model or defaultValue, Svelte bind:value, Angular value or [(value)].
    onValueChange(value: string | string[]) => void-React only. Called with the new value on every change, in item order for multiple.
    labelstring-Accessible name of the group, set as aria-label.

    Accessibility

    • The container has role group and an aria-label from label, each item is a button with aria-pressed.
    • Tab reaches the group once. Left and Right arrows move focus between items (roving tabindex), Space and Enter press the focused item.
    • Icon-only items need ariaLabel so every button has a name.

    Related components

    More form components with the same classes in every framework.