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.
HTML mockup preview (HarmonyOS / ArkUI appearance) — not an on-device render
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.
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.
HTML mockup preview (HarmonyOS / ArkUI appearance) — not an on-device render
API
Props
| Prop | Description | Type | Default |
|---|---|---|---|
checked | Whether on | boolean | false |
disabled | Whether disabled; does not fire onChange when disabled | boolean | false |
loading | Whether loading; does not fire onChange while loading, and the track shows an indicator | boolean | false |
size | Switch size, maps to the size tokens | LhSwitchSize | Medium |
onChange | Fired on tap with the new on/off state | (checked: boolean) => void | Empty function |
LhSwitchSize
| Value | width | height |
|---|---|---|
Small | switch.width.small (36vp) | switch.height.small (20vp) |
Medium | switch.width.medium (44vp) | switch.height.medium (24vp) |
Large | switch.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
| Token | Purpose |
|---|---|
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.gap | Clearance between the thumb and the track (2vp) |
color.primary.default | On-track background |
color.background.disabled | Off-track background |
color.onPrimary | Thumb color |
color.disabled.* | Disabled-state colors |
motion.duration.base | Thumb 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 id | Assertion | Local test |
|---|---|---|
tap-toggles-and-emits-onchange | A tap flips and fires onChange | enabled_is_interactive |
tap-checked-turns-off | Tapping the on state turns it off | thumb_offset_follows_checked (offset assertion) |
disabled-blocks-change | Disabled state does not fire onChange | disabled_is_not_interactive |
loading-blocks-change | Loading state does not fire onChange | loading_is_not_interactive |
checked-uses-primary-token | The on track consumes the primary token | checked_uses_primary_token |
unchecked-uses-border-token | The off track consumes the disabled-background token | unchecked_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.

