Skip to content

Switch ​

Toggle switch. Tapping flips the on/off state; supports disabled and loading overlays.

Spec source: lh-ui-core/specs/switch.json · Golden cases: lh-ui-core/cases/switch.json (6 cases, translated into hypium tests)

Basic usage ​

checked is controlled; tapping flips it and fires onChange with the new on/off state. The thumb slide animation uses motion.duration.base.

9:41Switch basics

HTML mockup preview (HarmonyOS / ArkUI appearance) — not an on-device render

typescript
LhSwitch({
  checked: this.enabled,
  onChange: (checked: boolean) => { this.enabled = checked },
})

Sizes ​

Three sizes map to the switch.width / switch.height tokens; the track corner radius and thumb offset are derived from the height.

9:41Switch sizes

HTML mockup preview (HarmonyOS / ArkUI appearance) — not an on-device render

States ​

Both disabled and loading prevent toggling (golden case loading-blocks-change); while loading, a platform indicator is shown in the center of the track.

9:41Switch states

HTML mockup preview (HarmonyOS / ArkUI appearance) — not an on-device render

API ​

Props ​

PropDescriptionTypeDefault
checkedWhether onbooleanfalse
disabledWhether disabled; does not fire onChange when disabledbooleanfalse
loadingWhether loading; does not fire onChange while loading, and the track shows an indicatorbooleanfalse
sizeSwitch size, maps to the size tokensLhSwitchSizeMedium
onChangeFired on tap with the new on/off state(checked: boolean) => voidEmpty function

LhSwitchSize ​

Valuewidthheight
Smallswitch.width.small (36vp)switch.height.small (20vp)
Mediumswitch.width.medium (44vp)switch.height.medium (24vp)
Largeswitch.width.large (52vp)switch.height.large (28vp)

On-track background color.primary.default; off-track background color.background.disabled; thumb color.onPrimary, diameter = height − 2 × switch.gap.

Token reference ​

TokenPurpose
switch.width.{small,medium,large}Three track widths
switch.height.{small,medium,large}Three track heights (the corner radius and thumb offset are derived from them)
switch.gapClearance between the thumb and the track (2vp)
color.primary.defaultOn-track background
color.background.disabledOff-track background
color.onPrimaryThumb color
color.disabled.*Disabled-state colors
motion.duration.baseThumb slide animation duration (300ms)

Golden case reference ​

How the 6 assertions in cases/switch.json map onto hypium tests (entry/src/test/Switch.test.ets):

case idAssertionLocal test
tap-toggles-and-emits-onchangeA tap flips and fires onChangeenabled_is_interactive
tap-checked-turns-offTapping the on state turns it offthumb_offset_follows_checked (offset assertion)
disabled-blocks-changeDisabled state does not fire onChangedisabled_is_not_interactive
loading-blocks-changeLoading state does not fire onChangeloading_is_not_interactive
checked-uses-primary-tokenThe on track consumes the primary tokenchecked_uses_primary_token
unchecked-uses-border-tokenThe off track consumes the disabled-background tokenunchecked_uses_border_token

Plus dark-mode specifics: dark_colors_come_from_dark_tokens / dimensions_are_identical_in_dark_mode. To run: in DevEco Studio, right-click entry/src/test → Run Tests.

lh-ui native bloodline · HarmonyOS (ArkTS + ArkUI)