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
<div class="flex justify-center">
<x-markas.toggle-group
type="multiple"
label="Text style"
:value="['bold', 'underline']"
:items="[
['value' => 'bold', 'label' => 'Bold', 'ariaLabel' => 'Bold'],
['value' => 'italic', 'label' => 'Italic', 'ariaLabel' => 'Italic'],
['value' => 'underline', 'label' => 'Underline', 'ariaLabel' => 'Underline'],
]"
>
<x-slot:bold><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-slot:bold>
<x-slot: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-slot:italic>
<x-slot: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-slot:underline>
</x-markas.toggle-group>
</div>import { ToggleGroup } from "@/components/markas/ToggleGroup";
export default function Example() {
return (
<div className="flex justify-center">
<ToggleGroup
type="multiple"
label="Text style"
defaultValue={["bold", "underline"]}
items={[
{
value: "bold",
label: (
<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>
),
ariaLabel: "Bold",
},
{
value: "italic",
label: (
<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>
),
ariaLabel: "Italic",
},
{
value: "underline",
label: (
<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 4v7a5 5 0 0 0 10 0V4M5 20h14" />
</svg>
),
ariaLabel: "Underline",
},
]}
/>
</div>
);
}<script setup lang="ts">
import ToggleGroup from "@/components/markas/ToggleGroup.vue";
</script>
<template>
<div class="flex justify-center">
<ToggleGroup
type="multiple"
label="Text style"
:default-value="['bold', 'underline']"
:items="[
{ value: 'bold', label: 'Bold', ariaLabel: 'Bold' },
{ value: 'italic', label: 'Italic', ariaLabel: 'Italic' },
{ value: 'underline', label: 'Underline', ariaLabel: 'Underline' },
]"
>
<template #bold
><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
></template>
<template #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
></template>
<template #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
></template>
</ToggleGroup>
</div>
</template><script lang="ts">
import ToggleGroup from "$lib/components/markas/ToggleGroup.svelte";
</script>
{#snippet boldIcon()}
<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>
{/snippet}
{#snippet italicIcon()}
<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>
{/snippet}
{#snippet underlineIcon()}
<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>
{/snippet}
<div class="flex justify-center">
<ToggleGroup
type="multiple"
label="Text style"
value={["bold", "underline"]}
items={[
{ value: "bold", label: boldIcon, ariaLabel: "Bold" },
{ value: "italic", label: italicIcon, ariaLabel: "Italic" },
{ value: "underline", label: underlineIcon, ariaLabel: "Underline" },
]}
/>
</div>---
import ToggleGroup from "@/components/markas/ToggleGroup.astro";
---
<div class="flex justify-center">
<ToggleGroup
type="multiple"
label="Text style"
value={["bold", "underline"]}
items={[
{ value: "bold", label: "Bold", ariaLabel: "Bold" },
{ value: "italic", label: "Italic", ariaLabel: "Italic" },
{ value: "underline", label: "Underline", ariaLabel: "Underline" },
]}
>
<Fragment slot="bold">
<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>
</Fragment>
<Fragment slot="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>
</Fragment>
<Fragment slot="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>
</Fragment>
</ToggleGroup>
</div>import { Component } from "@angular/core";
import {
ToggleGroup,
ToggleGroupItemTemplate,
} from "./markas/toggle-group.component";
@Component({
selector: "markas-example",
imports: [ToggleGroup, ToggleGroupItemTemplate],
template: `
<div class="flex justify-center">
<markas-toggle-group
type="multiple"
label="Text style"
[value]="value"
[items]="items"
>
<ng-template markasToggleGroupItem="bold">
<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>
</ng-template>
<ng-template markasToggleGroupItem="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>
</ng-template>
<ng-template markasToggleGroupItem="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>
</ng-template>
</markas-toggle-group>
</div>
`,
})
export default class ToggleGroupMultipleExample {
protected readonly value = ["bold", "underline"];
protected readonly items = [
{ value: "bold", label: "Bold", ariaLabel: "Bold" },
{ value: "italic", label: "Italic", ariaLabel: "Italic" },
{ value: "underline", label: "Underline", ariaLabel: "Underline" },
];
}Icons only
<div class="flex justify-center">
<x-markas.toggle-group
label="Text alignment"
value="left"
:items="[
['value' => 'left', 'label' => 'Align left', 'ariaLabel' => 'Align left'],
['value' => 'center', 'label' => 'Align center', 'ariaLabel' => 'Align center'],
['value' => 'right', 'label' => 'Align right', 'ariaLabel' => 'Align right'],
]"
>
<x-slot:left><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="M4 6h16M4 12h10M4 18h14" /></svg></x-slot:left>
<x-slot:center><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="M4 6h16M7 12h10M5 18h14" /></svg></x-slot:center>
<x-slot:right><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="M4 6h16M10 12h10M6 18h14" /></svg></x-slot:right>
</x-markas.toggle-group>
</div>import { ToggleGroup } from "@/components/markas/ToggleGroup";
export default function Example() {
return (
<div className="flex justify-center">
<ToggleGroup
label="Text alignment"
defaultValue="left"
items={[
{
value: "left",
label: (
<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="M4 6h16M4 12h10M4 18h14" />
</svg>
),
ariaLabel: "Align left",
},
{
value: "center",
label: (
<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="M4 6h16M7 12h10M5 18h14" />
</svg>
),
ariaLabel: "Align center",
},
{
value: "right",
label: (
<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="M4 6h16M10 12h10M6 18h14" />
</svg>
),
ariaLabel: "Align right",
},
]}
/>
</div>
);
}<script setup lang="ts">
import ToggleGroup from "@/components/markas/ToggleGroup.vue";
</script>
<template>
<div class="flex justify-center">
<ToggleGroup
label="Text alignment"
default-value="left"
:items="[
{ value: 'left', label: 'Align left', ariaLabel: 'Align left' },
{ value: 'center', label: 'Align center', ariaLabel: 'Align center' },
{ value: 'right', label: 'Align right', ariaLabel: 'Align right' },
]"
>
<template #left
><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="M4 6h16M4 12h10M4 18h14" /></svg
></template>
<template #center
><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="M4 6h16M7 12h10M5 18h14" /></svg
></template>
<template #right
><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="M4 6h16M10 12h10M6 18h14" /></svg
></template>
</ToggleGroup>
</div>
</template><script lang="ts">
import ToggleGroup from "$lib/components/markas/ToggleGroup.svelte";
</script>
{#snippet leftIcon()}
<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="M4 6h16M4 12h10M4 18h14" />
</svg>
{/snippet}
{#snippet centerIcon()}
<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="M4 6h16M7 12h10M5 18h14" />
</svg>
{/snippet}
{#snippet rightIcon()}
<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="M4 6h16M10 12h10M6 18h14" />
</svg>
{/snippet}
<div class="flex justify-center">
<ToggleGroup
label="Text alignment"
value="left"
items={[
{ value: "left", label: leftIcon, ariaLabel: "Align left" },
{ value: "center", label: centerIcon, ariaLabel: "Align center" },
{ value: "right", label: rightIcon, ariaLabel: "Align right" },
]}
/>
</div>---
import ToggleGroup from "@/components/markas/ToggleGroup.astro";
---
<div class="flex justify-center">
<ToggleGroup
label="Text alignment"
value="left"
items={[
{ value: "left", label: "Align left", ariaLabel: "Align left" },
{ value: "center", label: "Align center", ariaLabel: "Align center" },
{ value: "right", label: "Align right", ariaLabel: "Align right" },
]}
>
<Fragment slot="left">
<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="M4 6h16M4 12h10M4 18h14" />
</svg>
</Fragment>
<Fragment slot="center">
<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="M4 6h16M7 12h10M5 18h14" />
</svg>
</Fragment>
<Fragment slot="right">
<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="M4 6h16M10 12h10M6 18h14" />
</svg>
</Fragment>
</ToggleGroup>
</div>import { Component } from "@angular/core";
import {
ToggleGroup,
ToggleGroupItemTemplate,
} from "./markas/toggle-group.component";
@Component({
selector: "markas-example",
imports: [ToggleGroup, ToggleGroupItemTemplate],
template: `
<div class="flex justify-center">
<markas-toggle-group label="Text alignment" value="left" [items]="items">
<ng-template markasToggleGroupItem="left">
<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="M4 6h16M4 12h10M4 18h14" />
</svg>
</ng-template>
<ng-template markasToggleGroupItem="center">
<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="M4 6h16M7 12h10M5 18h14" />
</svg>
</ng-template>
<ng-template markasToggleGroupItem="right">
<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="M4 6h16M10 12h10M6 18h14" />
</svg>
</ng-template>
</markas-toggle-group>
</div>
`,
})
export default class ToggleGroupIconsExample {
protected readonly items = [
{ value: "left", label: "Align left", ariaLabel: "Align left" },
{ value: "center", label: "Align center", ariaLabel: "Align center" },
{ value: "right", label: "Align right", ariaLabel: "Align right" },
];
}Icons with text
<div class="flex justify-center">
<x-markas.toggle-group
label="Layout"
value="list"
:items="[
['value' => 'list', 'label' => 'List'],
['value' => 'grid', 'label' => 'Grid'],
]"
>
<x-slot:list><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="M9 6h11M9 12h11M9 18h11M4 6h1M4 12h1M4 18h1" /></svg>List</x-slot:list>
<x-slot:grid><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="M4 4h6v6H4zm10 0h6v6h-6zM4 14h6v6H4zm10 0h6v6h-6z" /></svg>Grid</x-slot:grid>
</x-markas.toggle-group>
</div>import { ToggleGroup } from "@/components/markas/ToggleGroup";
export default function Example() {
return (
<div className="flex justify-center">
<ToggleGroup
label="Layout"
defaultValue="list"
items={[
{
value: "list",
label: (
<>
<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="M9 6h11M9 12h11M9 18h11M4 6h1M4 12h1M4 18h1" />
</svg>
List
</>
),
},
{
value: "grid",
label: (
<>
<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="M4 4h6v6H4zm10 0h6v6h-6zM4 14h6v6H4zm10 0h6v6h-6z" />
</svg>
Grid
</>
),
},
]}
/>
</div>
);
}<script setup lang="ts">
import ToggleGroup from "@/components/markas/ToggleGroup.vue";
</script>
<template>
<div class="flex justify-center">
<ToggleGroup
label="Layout"
default-value="list"
:items="[
{ value: 'list', label: 'List' },
{ value: 'grid', label: 'Grid' },
]"
>
<template #list
><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="M9 6h11M9 12h11M9 18h11M4 6h1M4 12h1M4 18h1" /></svg
>List</template
>
<template #grid
><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="M4 4h6v6H4zm10 0h6v6h-6zM4 14h6v6H4zm10 0h6v6h-6z" /></svg
>Grid</template
>
</ToggleGroup>
</div>
</template><script lang="ts">
import ToggleGroup from "$lib/components/markas/ToggleGroup.svelte";
</script>
{#snippet list()}
<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="M9 6h11M9 12h11M9 18h11M4 6h1M4 12h1M4 18h1" />
</svg>
List
{/snippet}
{#snippet grid()}
<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="M4 4h6v6H4zm10 0h6v6h-6zM4 14h6v6H4zm10 0h6v6h-6z" />
</svg>
Grid
{/snippet}
<div class="flex justify-center">
<ToggleGroup
label="Layout"
value="list"
items={[
{ value: "list", label: list },
{ value: "grid", label: grid },
]}
/>
</div>---
import ToggleGroup from "@/components/markas/ToggleGroup.astro";
---
<div class="flex justify-center">
<ToggleGroup
label="Layout"
value="list"
items={[
{ value: "list", label: "List" },
{ value: "grid", label: "Grid" },
]}
>
<Fragment slot="list">
<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="M9 6h11M9 12h11M9 18h11M4 6h1M4 12h1M4 18h1" />
</svg>
List
</Fragment>
<Fragment slot="grid">
<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="M4 4h6v6H4zm10 0h6v6h-6zM4 14h6v6H4zm10 0h6v6h-6z" />
</svg>
Grid
</Fragment>
</ToggleGroup>
</div>import { Component } from "@angular/core";
import {
ToggleGroup,
ToggleGroupItemTemplate,
} from "./markas/toggle-group.component";
@Component({
selector: "markas-example",
imports: [ToggleGroup, ToggleGroupItemTemplate],
template: `
<div class="flex justify-center">
<markas-toggle-group label="Layout" value="list" [items]="items">
<ng-template markasToggleGroupItem="list">
<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="M9 6h11M9 12h11M9 18h11M4 6h1M4 12h1M4 18h1" />
</svg>
List
</ng-template>
<ng-template markasToggleGroupItem="grid">
<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="M4 4h6v6H4zm10 0h6v6h-6zM4 14h6v6H4zm10 0h6v6h-6z" />
</svg>
Grid
</ng-template>
</markas-toggle-group>
</div>
`,
})
export default class ToggleGroupWithTextExample {
protected readonly items = [
{ value: "list", label: "List" },
{ value: "grid", label: "Grid" },
];
}Installation
npx markasui@latest add toggle-grouppnpm dlx markasui@latest add toggle-groupyarn dlx markasui@latest add toggle-groupbunx --bun markasui@latest add toggle-group- 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-group.blade.php.
resources/views/components/markas/toggle-group.blade.php
@props([
'items' => [],
'type' => 'single',
'value' => null,
'label' => null,
])
@php
$root = 'inline-flex items-center gap-1 rounded-lg bg-subtle p-1';
$item = 'inline-flex h-8 items-center justify-center gap-2 rounded-md px-3 text-sm font-medium whitespace-nowrap text-muted transition-colors duration-150 ease-out hover:text-ink focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-primary data-[state=on]:bg-surface data-[state=on]:text-ink motion-reduce:transition-none [&_svg]:size-4 [&_svg]:shrink-0';
$values = array_map(fn ($entry) => (string) $entry['value'], $items);
$selected = array_values(array_intersect($values, array_map('strval', (array) ($value ?? []))));
if ($type !== 'multiple') {
$selected = array_slice($selected, 0, 1);
}
$tabStop = $selected[0] ?? ($values[0] ?? null);
@endphp
<div
role="group"
x-data="{
selected: @js($selected),
focused: @js($tabStop),
multiple: @js($type === 'multiple'),
values: @js($values),
{{-- Bound value for wire:model and x-model: a string (or null) for single, an array for multiple. --}}
value: @js($type === 'multiple' ? $selected : ($selected[0] ?? null)),
init() {
this.$watch('value', (next) => {
const list = (Array.isArray(next) ? next : next === null || next === '' ? [] : [next]).map(String);
this.selected = this.values.filter((entry) => list.includes(entry));
});
},
toggle(key) {
this.focused = key;
if (this.selected.includes(key)) {
this.selected = this.selected.filter((entry) => entry !== key);
} else {
const next = this.multiple ? [...this.selected, key] : [key];
this.selected = this.values.filter((entry) => next.includes(entry));
}
this.value = this.multiple ? [...this.selected] : (this.selected[0] ?? null);
},
move(step) {
const buttons = Array.from($root.querySelectorAll(':scope > button'));
const index = buttons.indexOf(document.activeElement);
const target = buttons[(index + step + buttons.length) % buttons.length];
if (!target) return;
this.focused = target.dataset.value;
target.focus();
},
}"
x-modelable="value"
x-on:keydown.right.prevent="move(1)"
x-on:keydown.left.prevent="move(-1)"
@if ($label) aria-label="{{ $label }}" @endif
{{ $attributes->class([$root]) }}
>
@foreach ($items as $entry)
@php
$key = (string) $entry['value'];
$on = in_array($key, $selected, true);
@endphp
<button
type="button"
data-value="{{ $key }}"
aria-pressed="{{ $on ? 'true' : 'false' }}"
data-state="{{ $on ? 'on' : 'off' }}"
tabindex="{{ $key === $tabStop ? '0' : '-1' }}"
@isset($entry['ariaLabel']) aria-label="{{ $entry['ariaLabel'] }}" @endisset
x-bind:aria-pressed="selected.includes(@js($key)) ? 'true' : 'false'"
x-bind:data-state="selected.includes(@js($key)) ? 'on' : 'off'"
x-bind:tabindex="focused === @js($key) ? 0 : -1"
x-on:click="toggle(@js($key))"
class="{{ $item }}"
>{{ $__laravel_slots[$key] ?? $entry['label'] ?? '' }}</button>
@endforeach
</div>- Make sure Tailwind CSS v4 and markas.css are installed. See Installation.
- Copy the component source below to
src/components/markas/ToggleGroup.tsx.
src/components/markas/ToggleGroup.tsx
"use client";
import {
useState,
type ComponentProps,
type KeyboardEvent,
type ReactNode,
} from "react";
const root = "inline-flex items-center gap-1 rounded-lg bg-subtle p-1";
const item =
"inline-flex h-8 items-center justify-center gap-2 rounded-md px-3 text-sm font-medium whitespace-nowrap text-muted transition-colors duration-150 ease-out hover:text-ink focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-primary data-[state=on]:bg-surface data-[state=on]:text-ink motion-reduce:transition-none [&_svg]:size-4 [&_svg]:shrink-0";
const cx = (...c: (string | false | null | undefined)[]) =>
c.filter(Boolean).join(" ");
const STEPS: Record<string, number> = { ArrowRight: 1, ArrowLeft: -1 };
export type ToggleGroupItem = {
value: string;
label: ReactNode;
ariaLabel?: string;
};
type BaseProps = Omit<
ComponentProps<"div">,
"defaultValue" | "onChange" | "role"
> & {
items: ToggleGroupItem[];
label?: string;
};
export type ToggleGroupProps = BaseProps &
(
| {
type?: "single";
value?: string;
defaultValue?: string;
onValueChange?: (value: string) => void;
}
| {
type: "multiple";
value?: string[];
defaultValue?: string[];
onValueChange?: (value: string[]) => void;
}
);
const toArray = (value: string | string[] | undefined) =>
Array.isArray(value) ? value : value ? [value] : [];
export function ToggleGroup(props: ToggleGroupProps) {
const {
items,
label,
type = "single",
value,
defaultValue,
onValueChange,
className,
...rest
} = props;
const [internal, setInternal] = useState(() => toArray(defaultValue));
const [focused, setFocused] = useState<string>();
const selected = value === undefined ? internal : toArray(value);
const tabStop = focused ?? selected[0] ?? items[0]?.value;
const toggle = (key: string) => {
setFocused(key);
const isOn = selected.includes(key);
const wanted = isOn
? selected.filter((entry) => entry !== key)
: type === "multiple"
? [...selected, key]
: [key];
const next = items
.map((entry) => entry.value)
.filter((entry) => wanted.includes(entry));
setInternal(next);
(onValueChange as ((value: string | string[]) => void) | undefined)?.(
type === "multiple" ? next : (next[0] ?? ""),
);
};
const handleKeyDown = (event: KeyboardEvent<HTMLDivElement>) => {
const step = STEPS[event.key];
if (step === undefined) return;
event.preventDefault();
const buttons = Array.from(
event.currentTarget.querySelectorAll<HTMLButtonElement>(
":scope > button",
),
);
const index = buttons.indexOf(document.activeElement as HTMLButtonElement);
const target = buttons[(index + step + buttons.length) % buttons.length];
if (!target) return;
setFocused(target.dataset.value);
target.focus();
};
return (
<div
role="group"
aria-label={label}
onKeyDown={handleKeyDown}
className={cx(root, className)}
{...rest}
>
{items.map((entry) => {
const isOn = selected.includes(entry.value);
return (
<button
key={entry.value}
type="button"
data-value={entry.value}
aria-pressed={isOn}
data-state={isOn ? "on" : "off"}
tabIndex={entry.value === tabStop ? 0 : -1}
aria-label={entry.ariaLabel}
onClick={() => toggle(entry.value)}
className={item}
>
{entry.label}
</button>
);
})}
</div>
);
}- Make sure Tailwind CSS v4 and markas.css are installed. See Installation.
- Copy the component source below to
src/components/markas/ToggleGroup.vue.
src/components/markas/ToggleGroup.vue
<script setup lang="ts">
import { computed, ref } from "vue";
export type ToggleGroupItem = {
value: string;
label: string;
ariaLabel?: string;
};
const props = withDefaults(
defineProps<{
items: ToggleGroupItem[];
type?: "single" | "multiple";
defaultValue?: string | string[];
label?: string;
}>(),
{ type: "single", defaultValue: undefined, label: undefined },
);
const model = defineModel<string | string[]>();
const root = "inline-flex items-center gap-1 rounded-lg bg-subtle p-1";
const item =
"inline-flex h-8 items-center justify-center gap-2 rounded-md px-3 text-sm font-medium whitespace-nowrap text-muted transition-colors duration-150 ease-out hover:text-ink focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-primary data-[state=on]:bg-surface data-[state=on]:text-ink motion-reduce:transition-none [&_svg]:size-4 [&_svg]:shrink-0";
const STEPS: Record<string, number> = { ArrowRight: 1, ArrowLeft: -1 };
const toArray = (value: string | string[] | undefined) =>
Array.isArray(value) ? value : value ? [value] : [];
const focused = ref<string>();
const selected = computed(() => toArray(model.value ?? props.defaultValue));
const tabStop = computed(
() => focused.value ?? selected.value[0] ?? props.items[0]?.value,
);
function toggle(key: string) {
focused.value = key;
const isOn = selected.value.includes(key);
const wanted = isOn
? selected.value.filter((entry) => entry !== key)
: props.type === "multiple"
? [...selected.value, key]
: [key];
const next = props.items
.map((entry) => entry.value)
.filter((entry) => wanted.includes(entry));
model.value = props.type === "multiple" ? next : (next[0] ?? "");
}
function onKeydown(event: KeyboardEvent) {
const step = STEPS[event.key];
if (step === undefined) return;
event.preventDefault();
const group = event.currentTarget as HTMLElement;
const buttons = Array.from(
group.querySelectorAll<HTMLButtonElement>(":scope > button"),
);
const index = buttons.indexOf(document.activeElement as HTMLButtonElement);
const target = buttons[(index + step + buttons.length) % buttons.length];
if (!target) return;
focused.value = target.dataset.value;
target.focus();
}
</script>
<template>
<div
role="group"
:aria-label="props.label"
:class="root"
@keydown="onKeydown"
>
<button
v-for="entry in props.items"
:key="entry.value"
type="button"
:data-value="entry.value"
:aria-pressed="selected.includes(entry.value) ? 'true' : 'false'"
:data-state="selected.includes(entry.value) ? 'on' : 'off'"
:tabindex="entry.value === tabStop ? 0 : -1"
:aria-label="entry.ariaLabel"
:class="item"
@click="toggle(entry.value)"
>
<slot :name="entry.value">{{ entry.label }}</slot>
</button>
</div>
</template>- Make sure Tailwind CSS v4 and markas.css are installed. See Installation.
- Copy the component source below to
src/lib/components/markas/ToggleGroup.svelte.
src/lib/components/markas/ToggleGroup.svelte
<script module lang="ts">
import type { Snippet } from "svelte";
export type ToggleGroupItem = {
value: string;
/** Text, or a snippet for an icon (pass ariaLabel then). */
label: string | Snippet;
ariaLabel?: string;
};
</script>
<script lang="ts">
import type { HTMLAttributes } from "svelte/elements";
const root = "inline-flex items-center gap-1 rounded-lg bg-subtle p-1";
const item =
"inline-flex h-8 items-center justify-center gap-2 rounded-md px-3 text-sm font-medium whitespace-nowrap text-muted transition-colors duration-150 ease-out hover:text-ink focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-primary data-[state=on]:bg-surface data-[state=on]:text-ink motion-reduce:transition-none [&_svg]:size-4 [&_svg]:shrink-0";
const STEPS: Record<string, number> = { ArrowRight: 1, ArrowLeft: -1 };
type Props = Omit<HTMLAttributes<HTMLDivElement>, "role"> & {
items: ToggleGroupItem[];
label?: string;
type?: "single" | "multiple";
/** Pressed items, bindable with bind:value. A string for single, an array for multiple. */
value?: string | string[];
};
let {
items,
label,
type = "single",
value = $bindable(),
class: className,
onkeydown,
...rest
}: Props = $props();
const toArray = (entry: string | string[] | undefined) =>
Array.isArray(entry) ? entry : entry ? [entry] : [];
let focused = $state<string>();
const selected = $derived(toArray(value));
const tabStop = $derived(focused ?? selected[0] ?? items[0]?.value);
function toggle(key: string) {
focused = key;
const wanted = selected.includes(key)
? selected.filter((entry) => entry !== key)
: type === "multiple"
? [...selected, key]
: [key];
const next = items
.map((entry) => entry.value)
.filter((entry) => wanted.includes(entry));
value = type === "multiple" ? next : (next[0] ?? "");
}
function handleKeydown(
event: KeyboardEvent & { currentTarget: EventTarget & HTMLDivElement },
) {
onkeydown?.(event);
const step = STEPS[event.key];
if (step === undefined) return;
event.preventDefault();
const buttons = Array.from(
event.currentTarget.querySelectorAll<HTMLButtonElement>(
":scope > button",
),
);
const index = buttons.indexOf(document.activeElement as HTMLButtonElement);
const target = buttons[(index + step + buttons.length) % buttons.length];
if (!target) return;
focused = target.dataset.value;
target.focus();
}
</script>
<div
role="group"
aria-label={label}
onkeydown={handleKeydown}
class={[root, className]}
{...rest}
>
{#each items as entry (entry.value)}
{@const isOn = selected.includes(entry.value)}
<button
type="button"
data-value={entry.value}
aria-pressed={isOn ? "true" : "false"}
data-state={isOn ? "on" : "off"}
tabindex={entry.value === tabStop ? 0 : -1}
aria-label={entry.ariaLabel}
onclick={() => toggle(entry.value)}
class={item}
>
{#if typeof entry.label === "string"}{entry.label}{:else}{@render entry.label()}{/if}
</button>
{/each}
</div>- Make sure Tailwind CSS v4 and markas.css are installed. See Installation.
- Copy the component source below to
src/components/markas/ToggleGroup.astro.
src/components/markas/ToggleGroup.astro
---
import type { HTMLAttributes } from "astro/types";
const root = "inline-flex items-center gap-1 rounded-lg bg-subtle p-1";
const item =
"inline-flex h-8 items-center justify-center gap-2 rounded-md px-3 text-sm font-medium whitespace-nowrap text-muted transition-colors duration-150 ease-out hover:text-ink focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-primary data-[state=on]:bg-surface data-[state=on]:text-ink motion-reduce:transition-none [&_svg]:size-4 [&_svg]:shrink-0";
type Item = { value: string; label: string; ariaLabel?: string };
type Props = Omit<HTMLAttributes<"div">, "role"> & {
items: Item[];
label?: string;
type?: "single" | "multiple";
value?: string | string[];
};
const {
items,
label,
type = "single",
value,
class: className,
...rest
} = Astro.props;
const values = items.map((entry) => entry.value);
const wanted = Array.isArray(value) ? value : value ? [value] : [];
const selected = values
.filter((entry) => wanted.includes(entry))
.slice(0, type === "multiple" ? undefined : 1);
const tabStop = selected[0] ?? values[0];
// A slot named after an item value replaces its label, for example an icon.
const labels = await Promise.all(
items.map((entry) =>
Astro.slots.has(entry.value) ? Astro.slots.render(entry.value) : null,
),
);
---
<div
role="group"
aria-label={label}
data-markas-toggle-group={type}
class:list={[root, className]}
{...rest}
>
{
items.map((entry, index) => {
const isOn = selected.includes(entry.value);
return (
<button
type="button"
data-value={entry.value}
aria-pressed={isOn ? "true" : "false"}
data-state={isOn ? "on" : "off"}
tabindex={entry.value === tabStop ? "0" : "-1"}
aria-label={entry.ariaLabel}
class={item}
>
{labels[index] === null ? entry.label : <Fragment set:html={labels[index]} />}
</button>
);
})
}
</div>
<script>
// One set of listeners per page serves every toggle group, including markup added later.
const STEPS: Record<string, number> = { ArrowRight: 1, ArrowLeft: -1 };
const itemsOf = (group: Element) =>
Array.from(group.querySelectorAll<HTMLButtonElement>(":scope > button"));
const groupOf = (target: EventTarget | null) =>
target instanceof Element
? target.closest<HTMLElement>("[data-markas-toggle-group]")
: null;
// Roving tabindex: only the last focused or clicked item is in the tab order.
function rove(group: Element, current: HTMLButtonElement) {
for (const button of itemsOf(group))
button.tabIndex = button === current ? 0 : -1;
}
function press(button: HTMLButtonElement, pressed: boolean) {
button.setAttribute("aria-pressed", String(pressed));
button.dataset.state = pressed ? "on" : "off";
}
document.addEventListener("click", (event) => {
const group = groupOf(event.target);
const button = (event.target as Element).closest("button");
if (!group || !button || button.parentElement !== group) return;
const isOn = button.getAttribute("aria-pressed") === "true";
if (!isOn && group.dataset.markasToggleGroup !== "multiple")
for (const other of itemsOf(group)) press(other, false);
press(button, !isOn);
rove(group, button);
});
document.addEventListener("keydown", (event) => {
const group = groupOf(event.target);
const step = STEPS[event.key];
if (!group || step === undefined) return;
event.preventDefault();
const buttons = itemsOf(group);
const index = buttons.indexOf(document.activeElement as HTMLButtonElement);
const target = buttons[(index + step + buttons.length) % buttons.length];
if (!target) return;
rove(group, target);
target.focus();
});
</script>- Make sure Tailwind CSS v4 and markas.css are installed. See Installation.
- Copy the component source below to
src/app/markas/toggle-group.component.ts.
src/app/markas/toggle-group.component.ts
import { NgTemplateOutlet } from "@angular/common";
import {
ChangeDetectionStrategy,
Component,
computed,
contentChildren,
Directive,
inject,
input,
model,
signal,
TemplateRef,
} from "@angular/core";
const root = "inline-flex items-center gap-1 rounded-lg bg-subtle p-1";
const item =
"inline-flex h-8 items-center justify-center gap-2 rounded-md px-3 text-sm font-medium whitespace-nowrap text-muted transition-colors duration-150 ease-out hover:text-ink focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-primary data-[state=on]:bg-surface data-[state=on]:text-ink motion-reduce:transition-none [&_svg]:size-4 [&_svg]:shrink-0";
const STEPS: Record<string, number> = { ArrowRight: 1, ArrowLeft: -1 };
export type ToggleGroupItem = {
value: string;
label: string;
ariaLabel?: string;
};
const toArray = (value: string | string[] | undefined) =>
Array.isArray(value) ? value : value ? [value] : [];
/**
* Replaces the label of the item with the same value, for example with an icon:
* `<ng-template markasToggleGroupItem="left"><svg ...></svg></ng-template>`.
*/
@Directive({ selector: "ng-template[markasToggleGroupItem]" })
export class ToggleGroupItemTemplate {
readonly value = input.required<string>({ alias: "markasToggleGroupItem" });
readonly template = inject(TemplateRef);
}
/** Host stays out of layout (display: contents). class moves to the group. */
@Component({
selector: "markas-toggle-group",
changeDetection: ChangeDetectionStrategy.OnPush,
imports: [NgTemplateOutlet],
host: { class: "contents", "[attr.label]": "null", "[attr.type]": "null" },
template: `
<div
role="group"
[attr.aria-label]="label() ?? null"
[class]="rootClass()"
(keydown)="handleKeydown($event)"
>
@for (entry of items(); track entry.value) {
<button
type="button"
[attr.data-value]="entry.value"
[attr.aria-pressed]="selected().includes(entry.value)"
[attr.data-state]="selected().includes(entry.value) ? 'on' : 'off'"
[tabIndex]="entry.value === tabStop() ? 0 : -1"
[attr.aria-label]="entry.ariaLabel ?? null"
[class]="item"
(click)="toggle(entry.value)"
>
@if (templateFor(entry.value); as custom) {
<ng-container [ngTemplateOutlet]="custom" />
} @else {
{{ entry.label }}
}
</button>
}
</div>
`,
})
export class ToggleGroup {
readonly items = input.required<ToggleGroupItem[]>();
readonly label = input<string>();
readonly type = input<"single" | "multiple">("single");
readonly userClass = input("", { alias: "class" });
/** Pressed items, two-way bindable with [(value)]. A string for single, an array for multiple. */
readonly value = model<string | string[]>();
protected readonly item = item;
private readonly templates = contentChildren(ToggleGroupItemTemplate);
private readonly focused = signal<string | undefined>(undefined);
protected readonly selected = computed(() => toArray(this.value()));
protected readonly tabStop = computed(
() => this.focused() ?? this.selected()[0] ?? this.items()[0]?.value,
);
protected readonly rootClass = computed(() =>
[root, this.userClass()].filter(Boolean).join(" "),
);
protected templateFor(value: string) {
return this.templates().find((entry) => entry.value() === value)?.template;
}
protected toggle(key: string) {
this.focused.set(key);
const selected = this.selected();
const wanted = selected.includes(key)
? selected.filter((entry) => entry !== key)
: this.type() === "multiple"
? [...selected, key]
: [key];
const next = this.items()
.map((entry) => entry.value)
.filter((entry) => wanted.includes(entry));
this.value.set(this.type() === "multiple" ? next : (next[0] ?? ""));
}
protected handleKeydown(event: KeyboardEvent) {
const step = STEPS[event.key];
if (step === undefined) return;
event.preventDefault();
const group = event.currentTarget as HTMLElement;
const buttons = Array.from(
group.querySelectorAll<HTMLButtonElement>(":scope > button"),
);
const index = buttons.indexOf(document.activeElement as HTMLButtonElement);
const target = buttons[(index + step + buttons.length) % buttons.length];
if (!target) return;
this.focused.set(target.dataset["value"]);
target.focus();
}
}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
labeland the item labels in your site’s language.
Props
| Name | Type | Default | Description |
|---|---|---|---|
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:value | string | 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. |
label | string | - | 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.
- 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.