Skip to content

Toast ​

A declarative lightweight toast. @Binding var visible controls visibility; it disappears automatically after a duration, and its dark background takes the color.ink token.

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

Types ​

The default type has no icon; the other types show a semantic-colored dot to the left of the text. A dark background with white text, with contrast guaranteed by color.ink.

9:41Toast types
Saved

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

9:41Toast semantic
Operation succeeded

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

swift
@State private var visible = false

LhToast(visible: $visible, text: "Saved", type: .success)

Position ​

Three positions: top (top), center (centered, the default), bottom (bottom).

9:41Toast position
Bottom toast

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

Auto-dismiss ​

When visible becomes true, a .task timer starts; after duration milliseconds it sets visible back to false and fires onClose (golden case auto-hides-after-duration); while the timer has not elapsed it stays visible without firing any callback (tick-before-duration-keeps-visible); setting it to false manually from the outside fires no callback (hide-is-silent).

Duration unit

The toast.duration token is in milliseconds (2000), while SwiftUI's Task.sleep needs seconds, so the end converts with Double(duration) / 1000 (see LhToastStyle).

swift
LhToast(visible: $visible, text: "Operation succeeded", type: .success, duration: 2000) {
  // onClose (fired only on auto-dismiss)
}

API ​

Props ​

PropDescriptionTypeDefault
visibleWhether visible (controlled two-way binding; set back to false automatically when the duration elapses)Bool—
textToast textString—
typeToast type, decides whether a dot is shown and its colorLhToastType.default
lhPositionDisplay positionLhToastPosition.center
durationDisplay duration (milliseconds)Int2000 (from the toast.duration token)
onCloseAuto-dismiss callback (not fired when hidden manually from the outside)(() -> Void)?nil

LhToastType ​

ValueDot color
.defaultNo dot
.successcolor.success.default
.warningcolor.warning.default
.dangercolor.danger.default
.infocolor.info.default

LhToastPosition ​

ValuePosition
.topTop (offset spacing.xl)
.centerCentered
.bottomBottom (offset spacing.xl)

Token Reference ​

TokenPurpose
toast.paddingHorizontal / toast.paddingVerticalPadding
toast.fontSizeToast text font size
toast.borderRadiusCorner radius (follows radius.small)
toast.gapGap between the dot and the text
toast.durationDefault display duration (milliseconds)
color.inkDark background (with a dedicated dark override, to separate it from the page background)
color.onPrimaryWhite text
color.{success,warning,danger,info}.defaultDot semantic colors
zIndex.toastStacking order
spacing.xlTop / bottom position offset

Golden Case Mapping ​

case idAssertionLocal test
show-makes-visibleShown means renderedtestShowMakesVisible
auto-hides-after-durationHides automatically when the duration elapses and fires onClosetestDurationConvertsToSeconds
tick-before-duration-keeps-visibleStays visible while the timer has not elapsedtestVisibleDuringDuration
hide-is-silentA manual hide from the outside fires no callbacktestHideIsSilent
success-type-uses-success-tokenThe success type consumes the success color and the ink tokentestSuccessTypeUsesSuccessToken / testInkBackgroundUsesInkToken
hidden-emits-no-commandsWhen hidden, the draw commands are emptytestHiddenNotRendered

Plus dedicated tests: testDefaultTypeHasNoDot / testLayoutUsesToastTokens / testDarkColorsComeFromDarkTokens / testDarkInkDiffersFromLightInk / testDimensionsAreIdenticalInDarkMode / testToastViewInitAcceptsConstant.

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

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