Forms
Toggle group
Turns several options on and off, like bold, italic and underline.
Installation
npx sinaui add toggle-groupCopy src/lib/ui/ToggleGroup.svelte, Tooltip.svelte, floating.ts, Icon.svelte and tokens.css. No other
dependencies.
To choose exactly one option, use a Segmented control.
Examples
Icons
Icon buttons are named by their tooltip, shown on hover or keyboard focus.
Bold
Italic
Underline
Strikethrough
Medicine is the science by which we learn the states of the human body.
<script lang="ts">
import {
TextBoldIcon,
TextItalicIcon,
TextStrikethroughIcon,
TextUnderlineIcon
} from '@hugeicons/core-free-icons';
import ToggleGroup from '#lib/ui/ToggleGroup.svelte';
let style = $state(['bold']);
</script>
<div class="stack">
<ToggleGroup
label="Text style"
bind:value={style}
items={[
{ value: 'bold', label: 'Bold', icon: TextBoldIcon },
{ value: 'italic', label: 'Italic', icon: TextItalicIcon },
{ value: 'underline', label: 'Underline', icon: TextUnderlineIcon },
{ value: 'strike', label: 'Strikethrough', icon: TextStrikethroughIcon }
]}
/>
<p
class:bold={style.includes('bold')}
class:italic={style.includes('italic')}
class:underline={style.includes('underline')}
class:strike={style.includes('strike')}
>
Medicine is the science by which we learn the states of the human body.
</p>
</div>
<style>
.stack {
display: grid;
gap: 1rem;
}
p {
margin: 0;
}
.bold {
font-weight: 600;
}
.italic {
font-style: italic;
}
.underline {
text-decoration: underline;
}
.strike {
text-decoration: line-through;
}
.underline.strike {
text-decoration: underline line-through;
}
</style>Text labels
A disabled button is skipped by the arrow keys.
Showing: arabic, latin
<script lang="ts">
import ToggleGroup from '#lib/ui/ToggleGroup.svelte';
let columns = $state(['arabic', 'latin']);
</script>
<div class="stack">
<ToggleGroup
label="Columns to show"
size="sm"
bind:value={columns}
items={[
{ value: 'arabic', label: 'Arabic' },
{ value: 'latin', label: 'Latin' },
{ value: 'commentary', label: 'Commentary' },
{ value: 'facsimile', label: 'Facsimile', disabled: true }
]}
/>
<p>Showing: {columns.length ? columns.join(', ') : 'nothing'}</p>
</div>
<style>
.stack {
display: grid;
gap: 1rem;
justify-items: start;
}
p {
margin: 0;
color: var(--ui-muted);
font-size: 0.875rem;
}
</style>Props
| Prop | Type | Default |
|---|---|---|
label | string (required) | |
items | { value, label, icon?, disabled? }[] | |
value | string[], bindable | [] |
size | 'sm' | 'md' | 'md' |
Accessibility
- A toolbar with one Tab stop; each button says whether it's on (
aria-pressed). - Buttons are 44px on touch screens.
| Key | Action |
|---|---|
| Tab | Moves into the group, to the last button used. |
| ← → | Previous / next button (mirrored right to left), wrapping round. |
| Home End | First / last button. |
| Space Enter | Turn the button on or off. |