Skip to content

Switch ​

A controlled switch. @Binding var checked provides two-way binding; the track has a 2:1 ratio and the thumb diameter is height − 2 × switch.gap.

Spec source: lh-ui-core/specs/switch.json · Golden cases: lh-ui-core/cases/switch.json (6 entries, translated into XCTest, runnable on the macOS host)

States ​

When on, the track is primary-colored and the thumb moves right; when off, the track takes the border color. The loading state shows a small indicator inside the track and does not respond to toggles.

9:41Switch states

HTML mock preview (iOS / SwiftUI appearance), not an on-device render

swift
@State private var on = true

LhSwitch(checked: $on) { newValue in
  // onChange
}

Sizes ​

Three sizes map to the switch.width / switch.height tokens; the corner radius automatically becomes the track radius from the height.

9:41Switch sizes

HTML mock preview (iOS / SwiftUI appearance), not an on-device render

Loading state ​

When loading = true, an LhLoading(lhSize: .small, tone: .primary) is embedded in the track and taps are blocked by the gate (golden case loading-blocks-change).

swift
LhSwitch(checked: $submitting, loading: submitting) { _ in }

API ​

Props ​

PropDescriptionTypeDefault
checkedSwitch state (controlled two-way binding)Bool—
disabledWhether disabled (does not fire onChange)Boolfalse
loadingWhether loading (embeds an indicator, does not fire onChange)Boolfalse
lhSizeSwitch size, mapped to the size tokensLhSwitchSize.medium
onChangeState-change callback; the payload is the new value((Bool) -> Void)?nil

LhSwitchSize ​

ValuewidthheightThumb diameter
.smallswitch.width.small (36pt)switch.height.small (20pt)height − 2 × switch.gap (16pt)
.mediumswitch.width.medium (44pt)switch.height.medium (24pt)height − 2 × switch.gap (20pt)
.largeswitch.width.large (52pt)switch.height.large (28pt)height − 2 × switch.gap (24pt)

Colors ​

StateTrackThumb
Offcolor.border.basecolor.onPrimary
Oncolor.primary.defaultcolor.onPrimary
Disabledcolor.disabled.bgcolor.disabled.text

Token Reference ​

TokenPurpose
switch.width.{small,medium,large}Three track widths
switch.height.{small,medium,large}Three track heights
switch.gapInner gap between the thumb and the track (determines the thumb diameter)
color.primary.defaultOn track color
color.border.baseOff track color
color.onPrimaryThumb color
color.disabled.{bg,text}Two disabled colors
motion.duration.baseThumb slide animation duration (seconds on the iOS end)

Golden Case Mapping ​

case idAssertionLocal test
tap-toggles-and-emits-onchangeA tap turns it on and fires onChange(true)testDefaultIsInteractive
tap-checked-turns-offTapping the on state turns it offtestCheckedUsesPrimaryTrack
disabled-blocks-changeThe disabled state does not fire onChangetestDisabledBlocksChange
loading-blocks-changeThe loading state does not fire onChangetestLoadingBlocksChange
checked-uses-primary-tokenThe on state consumes the primary token, not the border tokentestCheckedUsesPrimaryToken
unchecked-uses-border-tokenThe off state consumes the border tokentestUncheckedUsesBorderToken

Plus dedicated tests: testSizeUsesSwitchTokens / testThumbDiameterIsHeightMinusTwoGaps / testDarkColorsComeFromDarkTokens / testDimensionsAreIdenticalInDarkMode / testSwitchViewInitAcceptsConstant.

bash
swift test --filter LhSwitchTests    # Runs on the macOS host, no simulator required

lh-ui native bloodline · iOS (Swift + SwiftUI)