Hulian UIv0.93.5413 components0 components
  • Start
  • Components
  • Blocks
  • Pages
  • Demos
  • Changelog
ChineseEnglish
  • ComponentsComponents
  • ThemeTheme

Buttons

  • Button
  • ShimmerButton
  • InteractiveHoverButton
  • RainbowButton
  • PulsatingButton
  • RippleButton
  • ButtonGroup
  • SocialButton

Basic Inputs

  • Input
  • Textarea
  • CellEditor
  • Select
  • Checkbox
  • CheckboxGroup
  • Radio
  • Switch
  • Toggle
  • Segmented
  • Slider
  • NumberField
  • ElasticSlider

Advanced Inputs

  • MathTextarea
  • QuestionEditor
  • QuestionAnswer
  • MathField
  • SecretField
  • PasswordGenerator
  • Combobox
  • RemoteSelect
  • Listbox
  • Mentions
  • InputOTP
  • Rating
  • Upload
  • RegionSelect
  • ImageCropper
  • ScopeMatrix
  • Transfer
  • Cascader
  • TreeSelect
  • RegionCascader
  • CountrySelect
  • MarkdownEditor
  • Novel editor
  • RichTextEditor
  • CodeEditor
  • IssueReporter
  • ColorPicker
  • ColorField
  • ColorSwatchPicker
  • Choicebox
  • EmojiPicker
  • VoiceRecord
  • IconPicker
  • InspectorPanel

Date and Time

  • Calendar
  • DatePicker
  • DateTimePicker
  • DateRangePicker
  • TimePicker
  • TimeField

Form Frameworks

  • Form
  • ModalForm / DrawerForm
  • ProForm
  • StepsForm
  • LoginForm
  • AuthPanel
  • ClickCaptcha
  • Label
  • Field
  • SearchForm

Switch

switch

Toggles a binary setting with a compact track-and-thumb control.

Usage

Basic usage

Off by default, click the toggle switch.

tsx
<Switch aria-label="Switch" />

Enabled by default

For uncontrolled writing, use defaultChecked to set the initial open state.

tsx
<Switch defaultChecked aria-label="Switch" />

Disabled

disabled Lock switch, both off/on states can be disabled.

tsx
<>
  <Switch disabled aria-label="Disable-Off" />
  <Switch disabled defaultChecked aria-label="Disable-On" />
</>

With caption

Use label to associate copywriting, and you can switch by clicking on the text.

tsx
<label className="inline-flex items-center gap-2">
  <Switch defaultChecked aria-label="Receive notification" />
  <span className="text-sm text-foreground">Receive notification</span>
</label>

When to use

Use Switch for a Boolean setting that takes effect immediately, such as enabling notifications, without waiting for form submission. Use Radio for two mutually exclusive choices with equal semantic weight, or Checkbox for a Boolean field submitted with a form, such as accepting terms.

Import

ts
import { Switch } from "@hulianui/ui"

Props

NameTypeDefaultDescription
checkedboolean-Checked state in controlled mode.
defaultCheckedbooleanfalseInitial checked state in uncontrolled mode.
disabledbooleanfalseWhether to disable the switch.
idstring-HTML identifier used to associate the switch with a label.
classNamestring-Additional class name for the switch root.
aria-labelstring-Accessible label when no visible label is present.
size"sm" | "md" | "lg""md"Visual size: 36×20, 40×24, or 48×28 px. The default md size preserves the original dimensions.
touchTargetbooleanfalseExpands the invisible hit area to at least 44 px without changing layout or appearance; enable it for touch interfaces when spacing permits.

Events

EventTypeDescription
onCheckedChange(checked: boolean) => voidCalled with the new checked state.

Slots

SlotTypeDescription
labelReactNodeInline label rendered to the right of the track with a native <label> association. Supplying it makes aria-label unnecessary.
childrenReactNodeEquivalent to label: <Switch checked={v}>Enabled</Switch>. When both are given, label wins.

Example

tsx
<Switch defaultChecked aria-label="Turn on notifications" />

Controlled usage:

tsx
const [on, setOn] = useState(false);
<Switch checked={on} onCheckedChange={setOn} aria-label="Turn on notifications" />

Usage guidelines

  • Pair controlled checked with onCheckedChange. Use defaultChecked only for uncontrolled initial state; do not mix the two patterns.
  • Provide aria-label when there is no visible label so assistive technology can identify the control.
  • Enable touchTarget on mobile. The default md track is only 24 px high, below the recommended 44 px touch target; the invisible expansion improves finger accuracy without changing layout.
  • The expanded hit area extends about 10 px above and below. In a dense desktop form it may overlap nearby controls, so the option is off by default and should be enabled by context.

Related

Input · Textarea · Select · Checkbox · CheckboxGroup · Radio

Playground

<Switch />
PreviousRadioFormsNextToggle

On this page

  • Usage
  • Documentation
    • When to use
    • Import
    • Props
    • Events
    • Slots
    • Example
    • Usage guidelines
    • Related
  • Playground