Toggle
A button that stays pressed, in the look of the app's style.
Usage
import { Component, ViewEncapsulation, signal } from '@angular/core';import { ScToggle } from '@semantic-components/ui';import { SiBookmarkIcon } from '@semantic-icons/lucide-icons';
/** A toggle with an icon and its text; the icon filled when pressed. */@Component({ selector: 'app-toggle-usage-demo', imports: [ScToggle, SiBookmarkIcon], template: ` <button scToggle variant="outline" size="sm" [(pressed)]="bookmarked"> <svg siBookmarkIcon class="group-aria-pressed/toggle:fill-foreground" ></svg> Bookmark </button> `, host: { class: 'flex w-full justify-center' }, encapsulation: ViewEncapsulation.None,})export class ToggleUsageDemo { private readonly bookmarked = signal(false);}Anatomy
A button of yours: its icon and text are yours; [(pressed)] keeps its state, aria-pressed says it.
import { ScToggle } from '@semantic-components/ui';<button scToggle [(pressed)]="…">…</button>Accessibility
- A native button with aria-pressed: announced as a toggle button, pressed or not; Tab reaches it, Space and Enter press it.
- Its name stays the same as it toggles: its text, or an icon with sr-only text.
- Disabled: out of the Tab order, dimmed.
Examples
Outline Variant
import { Component, ViewEncapsulation, signal } from '@angular/core';import { ScToggle } from '@semantic-components/ui';import { SiBoldIcon, SiItalicIcon } from '@semantic-icons/lucide-icons';
/** variant="outline": a border round it. */@Component({ selector: 'app-outline-toggle-demo', imports: [ScToggle, SiBoldIcon, SiItalicIcon], template: ` <div class="flex flex-wrap items-center gap-2">With Text
import { Component, ViewEncapsulation, signal } from '@angular/core';import { ScToggle } from '@semantic-components/ui';import { SiItalicIcon } from '@semantic-icons/lucide-icons';
/** An icon and its text: the text names it. */@Component({ selector: 'app-with-text-toggle-demo', imports: [ScToggle, SiItalicIcon], template: ` <button scToggle [(pressed)]="italic">Sizes
import { Component, ViewEncapsulation } from '@angular/core';import { ScToggle } from '@semantic-components/ui';
/** size: sm, default, lg. */@Component({ selector: 'app-sizes-toggle-demo', imports: [ScToggle], template: ` <div class="flex flex-wrap items-center gap-2"> <button scToggle variant="outline" size="sm">Small</button>Disabled
import { Component, ViewEncapsulation } from '@angular/core';import { ScToggle } from '@semantic-components/ui';
/** Disabled: dimmed, out of the Tab order. */@Component({ selector: 'app-disabled-toggle-demo', imports: [ScToggle], template: ` <div class="flex flex-wrap items-center gap-2"> <button scToggle disabled>Disabled</button>Text Formatting Toolbar
import { Component, ViewEncapsulation, signal } from '@angular/core';import { ScFrame, ScToggle } from '@semantic-components/ui';import { SiBoldIcon, SiItalicIcon, SiStrikethroughIcon, SiUnderlineIcon,} from '@semantic-icons/lucide-icons';
@Component({RTL
import { Dir } from '@angular/cdk/bidi';import { Component, LOCALE_ID, ViewEncapsulation, signal } from '@angular/core';import { ScToggle } from '@semantic-components/ui';import { SiBookmarkIcon } from '@semantic-icons/lucide-icons';
/** In a right-to-left page: the icon at the start, on the right. */@Component({ selector: 'app-rtl-toggle-demo', imports: [Dir, ScToggle, SiBookmarkIcon], template: `API reference
Each part takes class, merged over the style (cn: a later class wins).
| Part | Selector | Description |
|---|---|---|
ScToggle | button[scToggle] | A button that stays pressed: pressed (two-way); variant ('default', 'outline'); size ('sm', 'default', 'lg'); disabled. In the style's toggle look. |