Skip to content

Toast ​

An ink-colored capsule overlay that manages its own visibility and timing, used for lightweight action feedback. It does not block the user.

Spec source: lh-ui-core/specs/toast.json · Golden cases: cases/toast.json (9 cases — 4 state-machine + 2 rendering translated into JVM tests; 3 label-semantics fallbacks asserted in Compose UI Test)

Usage ​

A toast is a self-contained state machine: the state is held by LhToastState — the host calls show(text) to display it and hide() to hide it, and it hides itself automatically when the timer expires, firing onClose. While visible, the component drives tick on its own frame by frame; the host can also call it itself to take over the timing.

9:41Toast
Success

HTML mock preview (Android / Compose look), not an on-device render

kotlin
val state = rememberLhToastState(onClose = { /* optional: log the close */ })
Column {
  LhButton(lhOnClick = { state.show("已保存") }) { Text("触发") }
  LhToast(state = state)
}

Semantic types ​

For any type other than default, a semantic-color dot with a diameter equal to the font size is drawn in front of the text.

9:41Toast types
Success message
Warning message
Error message
Primary message

HTML mock preview (Android / Compose look), not an on-device render

Positioning ​

lhPosition takes top / center / bottom, leaving a spacing.xl margin at both ends; the width is derived from text measurement and capped at 80% of the available width.

9:41Toast positions
Top
Bottom

HTML mock preview (Android / Compose look), not an on-device render

API ​

Props ​

PropDescriptionTypeDefault
stateToast state machine (holds visibility, text, and timing)LhToastState—
typeSemantic type; for any value other than default, a semantic-color dot is drawnLhToastTypedefault
lhPositionPosition within the available areaLhToastPositioncenter
modifierModifier (applied to the capsule)ModifierModifier

LhToastState ​

MemberDescription
show(text: String)Shows the toast and resets the timer; an empty string falls back to the locale pack's semantic text by type (default has no fallback)
hide()Hides immediately — does not trigger onClose
tick(deltaMs: Int): BooleanAdvances the timer; hides, fires onClose, and returns true when it expires
visible / text / durationCurrent state (observable)

Factory: rememberLhToastState(duration = 2000, onClose = {}).

LhToastType / LhToastPosition ​

ValueDot color
defaultNo dot
primary / success / warning / danger / infoThe corresponding semantic color

LhToastPosition: top / center / bottom.

Token reference ​

TokenUsage
toast.maxWidthCapsule maximum width (240dp; in practice the smaller of this and 80% of the available width)
toast.borderRadiusCapsule corner radius (follows radius.base)
toast.paddingHorizontal / toast.paddingVerticalCapsule padding
toast.fontSizeText font size (the dot diameter equals this value)
toast.gapGap between the dot and the text
zIndex.toastLayering semantics alignment (the Compose Popup provides window-level layering on its own)
color.background.overlay (mapped to ink by the theme layer)Capsule background color
color.onPrimaryText color
color.{primary,success,warning,danger,info}.defaultSemantic dot colors

Golden case reference ​

case idAssertionLocal test
show-makes-visibleAfter show, the toast is visible and renders its textLhToastDefaultsTest.show_makes_visible_and_renders_text
auto-hides-after-durationHides automatically when the timer expires and fires onCloseLhToastDefaultsTest.auto_hides_after_duration_and_emits_onClose + LhToastTest.auto_hides_after_duration
tick-before-duration-keeps-visibleBefore the timer expires, it stays visible and does not fire onCloseLhToastDefaultsTest.tick_before_duration_keeps_visible_and_does_not_emit
hide-is-silenthide does not fire onCloseLhToastDefaultsTest.hide_is_silent
success-type-uses-success-tokenThe success type consumes the success colorLhToastDefaultsTest.success_type_uses_success_tokens
hidden-emits-no-commandsWhen hidden, nothing is renderedLhToastTest.hidden_toast_renders_nothing
empty-text-fallback-localeshow("") falls back to the locale pack's semantic text by type (e.g. success → "Success")LhToastTest.empty_text_falls_back_to_locale_by_type
empty-text-default-type-no-fallbackThe default type does not fall back for an empty stringLhToastTest.empty_text_default_type_has_no_fallback
explicit-text-winsAn explicit text takes precedence over the semantic fallbackLhToastTest.explicit_text_wins_over_fallback
bash
./gradlew :components:toast:test                                  # JVM part
./gradlew :components:toast:connectedAndroidTest                 # Interaction part (requires a device)

lh-ui native bloodline · Android (Kotlin + Compose)