toggle
zard/toggleFreeComponent
@zard publishes no description for this item.
Live preview
live · rendered by the registry
Rendered by @zard on their own page — this is the component running, not a screenshot of it.Install it
npx shadcn@latest add https://www.zardui.com/r/toggle.jsonSource
1import {2 booleanAttribute,3 ChangeDetectionStrategy,4 Component,5 computed,6 forwardRef,7 input,8 linkedSignal,9 model,10 output,11 ViewEncapsulation,12} from '@angular/core';13import { type ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';1415import type { ClassValue } from 'clsx';1617import { mergeClasses } from '@/shared/utils/merge-classes';1819import { toggleVariants, type ZardToggleSizeVariants, type ZardToggleTypeVariants } from './toggle.variants';2021type OnTouchedType = () => void;22type OnChangeType = (value: boolean) => void;2324@Component({25 selector: 'z-toggle',26 template: `27 <button28 type="button"29 data-slot="toggle"30 [attr.aria-label]="zAriaLabel()"31 [attr.aria-pressed]="zValue()"32 [attr.data-state]="state()"33 [class]="classes()"34 [disabled]="disabled()"35 (click)="toggle()"36 >37 <ng-content />38 </button>39 `,40 providers: [41 {42 provide: NG_VALUE_ACCESSOR,43 useExisting: forwardRef(() => ZardToggleComponent),44 multi: true,45 },46 ],47 changeDetection: ChangeDetectionStrategy.OnPush,48 encapsulation: ViewEncapsulation.None,49 host: {50 '(mouseenter)': 'handleHover()',51 },52 exportAs: 'zToggle',53})54export class ZardToggleComponent implements ControlValueAccessor {55 readonly zValue = model(false);56 readonly zDisabled = input(false, { transform: booleanAttribute });57 readonly zType = input<ZardToggleTypeVariants>('default');58 readonly zSize = input<ZardToggleSizeVariants>('default');59 readonly zAriaLabel = input.required<string>();60 readonly class = input<ClassValue>('');6162 readonly zToggleClick = output<void>();63 readonly zToggleHover = output<void>();64 readonly zToggleChange = output<boolean>();6566 protected readonly state = computed(() => (this.zValue() ? 'on' : 'off'));6768 protected readonly disabled = linkedSignal(() => this.zDisabled());6970 protected readonly classes = computed(() =>71 mergeClasses(toggleVariants({ zSize: this.zSize(), zType: this.zType() }), this.class()),72 );7374 // eslint-disable-next-line @typescript-eslint/no-empty-function75 private onTouched: OnTouchedType = () => {};76 // eslint-disable-next-line @typescript-eslint/no-empty-function77 private onChangeFn: OnChangeType = () => {};7879 handleHover() {80 this.zToggleHover.emit();81 }8283 toggle() {84 if (this.disabled()) {85 return;86 }8788 this.zValue.update(v => !v);8990 this.zToggleClick.emit();91 this.zToggleChange.emit(this.zValue());92// … truncated
Files it writes
More from @zard
All 49 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| accordionaccordion | @zard | Components | Free | no deps · 5 files | |
| alertalert | @zard | Components | Free | no deps · 3 files | |
| alert-dialogalert-dialog | @zard | Components | Free | no deps · 5 files | |
| avataravatar | @zard | Components | Free | no deps · 4 files | |
| badgebadge | @zard | Components | Free | no deps · 3 files | |
| breadcrumbbreadcrumb | @zard | Components | Free | no deps · 4 files | |
| buttonbutton | @zard | Components | Free | no deps · 3 files | |
| button-groupbutton-group | @zard | Components | Free | no deps · 3 files |
