Toggle
A button that stays pressed until it is pressed again.
Full screen Code
Installation
Copy code
npx @mielui/svelte add toggleyarn dlx @mielui/svelte add togglebunx @mielui/svelte add toggleUsage
Bind pressed to read or set the state. Put a label or an icon in children, and give an icon-only toggle an aria-label.
<script lang="ts">
import { TextBoldIcon as Bold } from '@hugeicons/core-free-icons';
import { Toggle } from '@mielui/svelte/components/toggle';
import HugeiconsIcon from '@mielui/svelte/hugeicons-icon';
let bold = $state(false);
</script>
<Toggle bind:pressed={bold} aria-label="Bold">
<HugeiconsIcon icon={Bold} size={14} />
</Toggle>Examples
Text formatting
Three toggles set the style of the sentence below them.
Full screen Code
The quick brown fox jumps over the lazy dog.
Icon toggle
Full screen Code
Text toggle
Full screen Code
Sizes
Full screen Code
Disabled
Full screen Code
Events
onPressedChange runs with the new state on every press, for when you want to react without binding pressed.
Full screen Code
Notifications are on
API reference
Toggle
Toggles a pressed state.
pressedBindablebooleanWhether it is on. Bindable.
Default
falsedisabledbooleanPrevents interaction and dims the control.
Default
falsesize"sm" | "md" | "lg"Control height.
Default
'md'variant"default" | "outline"outlineadds a border.Default
'default'childrenSnippet<[]>Content rendered inside.
onPressedChange((pressed: boolean) => void)Called with the new state.
class(string & (ClassValue | null))Classes added to the element.
pnpm dlx @mielui/svelte add toggle