Command palette

@yasinsensoy/web

Web · Components

Switch

On/off switch.

2 exports

Live example

Preview

Code

import { Switch } from "@yasinsensoy/web";

<Switch label="Turn on notifications" defaultChecked onCheckedChange={setChecked} />

Events

  1. Events appear here when you interact with the preview.

Import

import { Switch } from "@yasinsensoy/web";

Switch

componentServer component

On/off switch. Used for settings that take effect immediately (e.g. turn on notifications). For choices that need a save button Checkbox fits better.

Built on a real <input type="checkbox" role="switch">; works with forms and even before JavaScript loads. Usable in server components. On touch screens the touch area is enlarged to 44 px height.

Switch({ size, onCheckedChange, onChange, label, description, tooltip, effect, className, "aria-describedby": describedByProp, ...props }: SwitchProps): ReactNode

Examples

<Switch name="notifications" defaultChecked label="Notifications"
  description="Notify me about new messages." />

<Switch checked={dark} onCheckedChange={setDark} aria-label="Dark mode" />

SwitchProps

interface

Props of the Switch component.

Extends: Omit<ComponentProps<"input">, "type" | "size">

NameTypeDefaultDescription
size?"sm" | "md""md"
onCheckedChange?(checked: boolean) => voidCalled with the new value when the state changes. A shortcut for onChange.
label?ReactNodeLabel next to the switch; fully clickable. If given, className is applied to the label itself.
description?ReactNodeDescription below the label; screen readers read it with the switch.
tooltip?TooltipPropTooltip shown on hover, keyboard focus or long press on touch screens. Opens from the whole label and screen readers read it with the switch. See TooltipProp.
effect?EffectPropEffect or effects of the switch (e.g. "pulse", "glow"). If omitted, UIProvider appearance.defaults.switch.effect. See BuiltinEffect.