Skip to content

Picker ​

A declarative bottom wheel picker. @Binding var visible controls visibility and @Binding var value is the selected index; the wheel is self-drawn (DragGesture + snap on release), with no dependency on system controls.

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

Basic usage ​

It pops up from the bottom: a top toolbar (cancel / title / confirm), with the wheel and two indicator lines in the middle.

9:41Picker basic
CancelSelectConfirm
Option one
Option two
Option three

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

swift
@State private var visible = false
@State private var index = 1
let options = ["Option one", "Option two", "Option three"]

LhPicker(
  visible: $visible,
  options: options,
  value: $index,
  title: "Select"
)

Wheel interaction ​

  • Dragging changes the overall offset; on release it snaps to the nearest candidate (animation duration motion.duration.base), and when the selected index changes onValueChange fires (golden case scroll-emits-onvaluechange).
  • The gap between the indicator lines equals picker.itemHeight, and the selected item always lands exactly between them.

Callbacks and boundaries ​

  • "Confirm" fires onConfirm (payload is the current index) and onClose (confirm-also-emits-onclose).
  • "Cancel" fires onCancel.
  • When value is out of range, the end clamps it: negatives go to zero (clamp-negative-value-to-zero), overflows go to the last item (clamp-overflow-value-to-last); with an empty options list the index is always 0.
  • When visible = false, no draw commands are emitted at all (hidden-not-rendered).
swift
LhPicker(
  visible: $visible,
  options: options,
  value: $index,
  title: "Select",
  confirmText: "Confirm",
  cancelText: "Cancel",
  onValueChange: { newIndex in
    // Scroll snap callback
  },
  onConfirm: { newIndex in
    // Confirm callback (payload is the current index)
  },
  onCancel: {
    // Cancel callback
  }
)

API ​

Props ​

PropDescriptionTypeDefault
visibleWhether visible (controlled two-way binding)Bool—
optionsArray of candidate texts (a single column)[String]—
valueSelected index (controlled two-way binding; clamped when out of range)Int—
titleToolbar titleString""
confirmTextConfirm button text; nil (default) uses the language packString?Language pack (Simplified Chinese "确认")
cancelTextCancel button text; nil (default) uses the language packString?Language pack (Simplified Chinese "取消")
onValueChangeScroll snap callback; the payload is the new index((Int) -> Void)?nil
onConfirmConfirm callback; the payload is the current index((Int) -> Void)?nil
onCancelCancel callback(() -> Void)?nil
onCloseClose callback (fired by both confirm and cancel)(() -> Void)?nil

Colors ​

AreaColor
Panel backgroundcolor.background.default
Backdropcolor.background.overlay
Cancel button / candidate textscolor.text.primary
Title / confirm buttoncolor.primary.default
Indicator linescolor.border.lighter (width from cell.borderWidth)

Token Reference ​

TokenPurpose
picker.heightPanel height (260pt)
picker.toolbarHeightToolbar height (44pt)
picker.itemHeightPer-item height (determines the wheel and the indicator gap)
picker.itemFontSizeCandidate and toolbar font size
picker.borderRadiusTop corner radius
color.background.overlay / color.background.defaultBackdrop and panel background
color.border.lighter / cell.borderWidthIndicator line color and width
color.primary.defaultConfirm button and selected item text color
zIndex.{modalBackdrop,modal}Backdrop and panel stacking order
motion.duration.baseSnap animation duration (seconds on the iOS end)

Golden Case Mapping ​

case idAssertionLocal test
confirm-emits-onconfirm-with-valueConfirming fires onConfirm(1)testConfirmUsesPrimaryToken
confirm-also-emits-oncloseConfirming also fires onClosetestConfirmUsesPrimaryToken
cancel-emits-oncancelCanceling fires onCanceltestCancelUsesNeutralColor
scroll-emits-onvaluechangeA scroll snap fires onValueChange(2)View layer DragGesture.onEnded (gesture assertions need a simulator)
clamp-negative-value-to-zeroA negative index clamps to 0testClampNegativeValueToZero
clamp-overflow-value-to-lastAn overflowing index clamps to the last itemtestClampOverflowValueToLast
title-renders-textThe title renders correctlytestTitleAndTextsAreExposed
hidden-not-renderedWhen hidden, nothing is renderedtestHiddenNotRendered

Plus dedicated tests: testVisibleRendersAppearance / testClampedValueIsExposed / testLayoutUsesPickerTokens / testEmptyOptionsClampsToZero / testDarkColorsComeFromDarkTokens / testDimensionsAreIdenticalInDarkMode / testPickerViewInitAcceptsConstant.

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

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