Switch
An on/off toggle on a native checkbox.
View as MarkdownExample
- Preview
- Code
Installation
npx fizzy-ui add switch Copies switch.ts into src/app/ui/ and installs what it needs. Run init once first.
Keyboard
| Key | Action |
|---|---|
| Space | Toggle |
Accessibility
No axe-core violations axe-core 4.14.0 checks this example, closed and with its panels open, on every build; the site is not published otherwise.
- A checkbox with role="switch", announced as on/off.
Source
The exact file add copies. It is yours to edit.
switch.ts
import { Directive } from '@angular/core';
/**
* An on/off switch on a native checkbox: `<input type="checkbox" fzSwitch>`. `role="switch"`
* makes screen readers say on/off; checked state, Space, labels and forms stay native.
*/
@Directive({
selector: 'input[type=checkbox][fzSwitch]',
host: {
role: 'switch',
class:
'inline-flex h-5 w-9 shrink-0 cursor-pointer appearance-none items-center rounded-full bg-muted-foreground/40 p-0.5 shadow-xs ' +
'outline-none transition-colors checked:bg-primary ' +
'focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 ring-offset-background disabled:cursor-not-allowed disabled:opacity-50 ' +
"before:size-4 before:rounded-full before:bg-background before:shadow-sm before:transition-transform before:duration-(--fz-duration-control) before:ease-spring-snappy before:content-[''] " +
'checked:before:translate-x-4 rtl:checked:before:-translate-x-4',
},
})
export class FzSwitch {}
export const FZ_SWITCH = [FzSwitch] as const;