Toggle
The <smart-toggle> component provides a boolean on/off control. It follows the Base + Standard + Wrapper pattern with an InjectionToken-based extension mechanism. The abstract ToggleBaseComponent defines the shared API — value (two-way ModelSignal<boolean>), disabled, optional IToggleOptions, cssClass (alias class), and a toggle() method that flips the value while respecting the disabled state. ToggleStandardComponent is a barebones placeholder concrete implementation. ToggleComponent is the public wrapper that renders ToggleStandardComponent by default and accepts a custom replacement via TOGGLE_STANDARD_COMPONENT_TOKEN.
Usage
<smart-toggle [options]="options" [(value)]="emailNotifications" />
<p>Email notifications are {{ emailNotifications() ? 'on' : 'off' }}.</p>
The preview renders the code above in an application that registers every preset (provideSmartPresets(), see Installation) and the English translations; without the presets, a component that has one renders its standard implementation instead, for most components unstyled markup.
Components
ToggleComponent (<smart-toggle>)
Main wrapper component. Renders ToggleStandardComponent by default. When TOGGLE_STANDARD_COMPONENT_TOKEN is provided, renders the injected component via NgComponentOutlet, hands it value, disabled, options and class, and forwards two-way value of whichever implementation renders (the standard, the preset or a custom one).
ToggleStandardComponent (<smart-toggle-standard>)
Barebones native-HTML implementation. Renders a <span class="smart-toggle" data-label-position="…"> containing a minimal <input type="checkbox"> bound to value and disabled, with the external cssClass applied to the input element. options.label renders in a <label class="smart-toggle-label" for="…"> associated with the checkbox (so it is the accessible name), and options.description in a <span class="smart-toggle-description"> referenced by the checkbox's aria-describedby. Both live in a <span class="smart-toggle-text" data-role="text"> placed after the checkbox, or before it when options.labelPosition === 'left' (default 'right'). options.ariaLabel is applied as the checkbox's aria-label only when there is no label. It adds no styles.
TogglePresetComponent (<smart-toggle-preset>)
Styled variation that extends ToggleBaseComponent and is a drop-in replacement for ToggleStandardComponent. Register it via TOGGLE_STANDARD_COMPONENT_TOKEN (or every preset at once with provideSmartPresets()) to restyle every <smart-toggle>, or use the <smart-toggle-preset> selector directly. It renders the Preline default switch: a visually hidden <input type="checkbox"> (with peer sr-only) drives the track / thumb visuals through peer-checked / peer-disabled states, while the value model holds the checked state (updated via the checkbox change event). Like the standard, options.label is a <label for> of the checkbox (its accessible name), options.description is referenced by the checkbox's aria-describedby, both sit beside the switch on the side given by options.labelPosition (default 'right'), and options.ariaLabel names the checkbox only when there is no label. The external cssClass goes on the root <div>. All classes are smart:-prefixed Tailwind with explicit dark: variants; the class recipes are internal to the preset and not exported.
TogglePresetComponentdeclarescssClassasinput<string>('')without theclassalias. Bind it as[cssClass]when using the<smart-toggle-preset>selector directly, or just passclasson<smart-toggle>(the wrapper forwards it). With the preset registered through the token,[(value)]on<smart-toggle>works as with the standard. The Preline doc's size, soft-color, rounded, icon, tooltip and validation-state variants are not exposed, becauseIToggleOptionshas no size/variant/color fields; the preset renders the default medium pill switch in the primary (blue) color.
ToggleBaseComponent (abstract)
Abstract base directive for extending custom toggle implementations. Exposes value as a two-way ModelSignal<boolean> (default false), disabled as an InputSignal<boolean> (default false), options as an InputSignal<IToggleOptions | undefined>, cssClass as an InputSignal<string> (with alias class), and a toggle() method that flips value unless disabled() is true.
API
Inputs
| Input | Type | Default | Description |
|---|---|---|---|
value | ModelSignal<boolean> | false | Toggle on/off state (two-way bindable) |
disabled | InputSignal<boolean> | false | Whether the toggle is disabled |
options | InputSignal<IToggleOptions | undefined> | - | Optional configuration (label, description, ariaLabel, position) |
class | InputSignal<string> | '' | External CSS classes (alias for cssClass) |
IToggleOptions
| Field | Type | Default | Description |
|---|---|---|---|
label | string | - | Visible label, a <label for> of the checkbox, so it is the accessible name. |
description | string | - | Help text under the label, referenced by the checkbox's aria-describedby. |
labelPosition | 'left' | 'right' | 'right' | 'left' puts the label and description before the switch, otherwise they go after it. |
ariaLabel | string | - | aria-label of the checkbox, applied only when there is no label; set it for label-less toggles. |
Both ToggleStandardComponent and TogglePresetComponent honour every field.
TOGGLE_STANDARD_COMPONENT_TOKEN
InjectionToken that replaces the default ToggleStandardComponent with a custom implementation. Provide a component class extending ToggleBaseComponent; every <smart-toggle> below that injector renders it.
Extending the base class
import {
ChangeDetectionStrategy,
Component,
computed,
signal,
ViewEncapsulation,
} from '@angular/core';
import {
IToggleOptions,
TOGGLE_STANDARD_COMPONENT_TOKEN,
ToggleBaseComponent,
ToggleComponent,
} from '@smartsoft001/angular';
@Component({
selector: 'docs-custom-toggle',
template: `
<label [class]="containerClasses()">
@if (options()?.labelPosition === 'left' && options()?.label) {
<span class="docs-toggle__label">{{ options()!.label }}</span>
}
<input
type="checkbox"
class="docs-toggle__input"
[checked]="value()"
[disabled]="disabled()"
[attr.aria-label]="options()?.ariaLabel ?? null"
(change)="onChange($event)"
/>
@if (options()?.labelPosition !== 'left' && options()?.label) {
<span class="docs-toggle__label">{{ options()!.label }}</span>
}
@if (options()?.description) {
<span class="docs-toggle__description">{{
options()!.description
}}</span>
}
</label>
`,
encapsulation: ViewEncapsulation.None,
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class CustomToggleComponent extends ToggleBaseComponent {
containerClasses = computed(() =>
[
'docs-toggle',
`docs-toggle--label-${this.options()?.labelPosition ?? 'right'}`,
this.disabled() ? 'docs-toggle--disabled' : '',
this.cssClass(),
]
.filter(Boolean)
.join(' '),
);
// `value` is a model on the base: the wrapper writes the new state back to
// `<smart-toggle [(value)]="...">`. A change handler reads the checkbox
// state; a click handler would instead call the inherited toggle(), which
// already respects `disabled`.
protected onChange(event: Event): void {
this.value.set((event.target as HTMLInputElement).checked);
}
}
@Component({
selector: 'docs-toggle-custom-example',
changeDetection: ChangeDetectionStrategy.OnPush,
imports: [ToggleComponent],
// The token swaps the standard toggle for the custom one everywhere below
// this component, so consumers keep writing `<smart-toggle>`.
providers: [
{
provide: TOGGLE_STANDARD_COMPONENT_TOKEN,
useValue: CustomToggleComponent,
},
],
// The wrapper forwards `value` both ways.
template: `
<smart-toggle [(value)]="enabled" [options]="options" />
<p>Notifications are {{ enabled() ? 'on' : 'off' }}.</p>
`,
})
export class ToggleCustomExampleComponent {
readonly enabled = signal(false);
options: IToggleOptions = {
label: 'Allow notifications',
description: 'Send me an email when someone comments on my work.',
ariaLabel: 'Allow notifications',
};
}
Source
The component lives in packages/shared/angular/src/lib/components/toggle and is documented for Claude Code by the angular-components-toggle skill.
SmartToggle is an on/off switch. The value is controlled (value + onValueChange) or kept inside (defaultValue, false by default); disabled blocks it. options.label and options.description render beside it (right by default, left with labelPosition: 'left'), and options.ariaLabel names it when there is no visible label. SmartToggleStandard is a native checkbox; SmartTogglePreset renders a switch track and thumb driven by a visually hidden checkbox.
Usage
import { useState } from 'react';
import { IToggleOptions, SmartToggle } from '@smartsoft001/react';
const options: IToggleOptions = {
label: 'Email notifications',
description: 'Get an email when someone comments on your post.',
};
export function ToggleUsageExample() {
const [emailNotifications, setEmailNotifications] = useState(true);
return (
<>
<SmartToggle
options={options}
value={emailNotifications}
onValueChange={setEmailNotifications}
/>
<p>Email notifications are {emailNotifications ? 'on' : 'off'}.</p>
</>
);
}
The preview renders the code above in an application that registers every preset (SMART_PRESET_COMPONENTS on SmartProvider, see Installation) and the English translations; without the presets, a component that has one renders its standard implementation instead, for most components unstyled markup.
Exports
All from @smartsoft001/react.
| Export | Kind | What it is |
|---|---|---|
SmartToggle | component | Renders the implementation registered as components.toggle on SmartProvider, SmartToggleStandard by default. |
SmartTogglePreset | component | Styled toggle (switch) variation (preset). |
SmartToggleStandard | component | Barebones native-HTML toggle (checkbox). |
useToggle | hook | The behaviour every toggle variant shares: the value, controlled through value / onValueChange or kept internally from defaultValue, and toggle(), which flips it unless disabled. |
The preset's class helpers (getToggleContainerClasses, getToggleSwitchClasses, getToggleTrackClasses, getToggleThumbClasses, getToggleTextWrapClasses, getToggleLabelClasses, getToggleDescriptionClasses, TOGGLE_INPUT_CLASSES) are exported too, for a custom implementation that wants the preset look.
Props and Types
SmartToggleProps
| Prop | Type | Default | Description |
|---|---|---|---|
value? | boolean | — | Whether the toggle is on. Leave it undefined for an uncontrolled toggle that starts from defaultValue. |
defaultValue? | boolean | false | Initial value of an uncontrolled toggle. |
onValueChange? | (value: boolean) => void | — | Called when the toggle is switched on or off. |
disabled? | boolean | false | Disables the switch. |
options? | IToggleOptions | — | Label, description and accessible name. |
className? | string | — | Classes on the checkbox (standard) or the root (preset). |
IToggleOptions
| Field | Type | Default | Description |
|---|---|---|---|
label? | string | '' | Visible label (the accessible name). |
description? | string | '' | Text under the label (aria-describedby). |
labelPosition? | 'left' | 'right' | 'right' | Side of the label and description. |
ariaLabel? | string | — | Accessible name when there is no visible label. |
Replacing the Implementation
SmartToggle renders the component registered under the 'toggle' key of SmartProvider's components, and SmartToggleStandard when nothing is registered there. Every SmartToggle below the provider then renders the registered component, which receives the same props.
SmartTogglePreset is the styled (preset) implementation: register it under the 'toggle' key of SmartProvider's components, render it directly in place of SmartToggle, or spread SMART_PRESET_COMPONENTS on the provider to register every preset at once (see the react-provider skill). Pass components as a stable object (a module constant or a memoised value), or the context changes on every render.
The useToggle hook
The behaviour every toggle variant shares: the value, controlled through value / onValueChange or kept internally from defaultValue, and toggle(), which flips it unless disabled.
| Returns | Type | Description |
|---|---|---|
value | boolean | The current value (controlled or internal). |
setValue | (next: boolean) => void | Sets the value and calls onValueChange. |
toggle | () => void | Flips the value unless disabled. |
import { useState } from 'react';
import {
IToggleOptions,
SmartProvider,
SmartToggle,
SmartToggleProps,
useToggle,
} from '@smartsoft001/react';
export function CustomToggle(props: SmartToggleProps) {
const { disabled = false, options, className } = props;
// useToggle keeps the value (controlled or internal) and reports it through
// onValueChange; its toggle() would also respect `disabled`.
const { value, setValue } = useToggle(props);
const labelPosition = options?.labelPosition ?? 'right';
const containerClasses = [
'docs-toggle',
`docs-toggle--label-${labelPosition}`,
disabled && 'docs-toggle--disabled',
className,
]
.filter(Boolean)
.join(' ');
const label = options?.label && (
<span className="docs-toggle__label">{options.label}</span>
);
return (
<label className={containerClasses}>
{labelPosition === 'left' && label}
<input
type="checkbox"
className="docs-toggle__input"
checked={value}
disabled={disabled}
aria-label={options?.ariaLabel}
onChange={(event) => setValue(event.target.checked)}
/>
{labelPosition !== 'left' && label}
{options?.description && (
<span className="docs-toggle__description">{options.description}</span>
)}
</label>
);
}
// A module constant: a new object on every render would change the context.
const components = { toggle: CustomToggle };
const options: IToggleOptions = {
label: 'Allow notifications',
description: 'Send me an email when someone comments on my work.',
ariaLabel: 'Allow notifications',
};
// Every <SmartToggle> below the provider renders CustomToggle. Without value
// the state lives in the implementation and starts at defaultValue;
// onValueChange still reports every change.
export function ToggleCustomExample() {
const [enabled, setEnabled] = useState(false);
return (
<SmartProvider components={components}>
<SmartToggle
defaultValue={false}
options={options}
onValueChange={setEnabled}
/>
<p>Notifications are {enabled ? 'on' : 'off'}.</p>
</SmartProvider>
);
}
Styling
SmartToggleStandardis an unstyled native checkbox;SmartTogglePresetdraws the track and thumb withpeer-*states andsmart:dark:variants.- In both,
options.labelis a<label htmlFor>of the checkbox (its accessible name) andoptions.descriptionis referenced by the checkbox'saria-describedby.
Source
The component lives in packages/shared/react/src/lib/components/toggle and is documented for Claude Code by the react-components-toggle skill.