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.
HTML mock preview (Android / Compose look), not an on-device render
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.
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.
HTML mock preview (Android / Compose look), not an on-device render
API
Props
| Prop | Description | Type | Default |
|---|---|---|---|
state | Toast state machine (holds visibility, text, and timing) | LhToastState | — |
type | Semantic type; for any value other than default, a semantic-color dot is drawn | LhToastType | default |
lhPosition | Position within the available area | LhToastPosition | center |
modifier | Modifier (applied to the capsule) | Modifier | Modifier |
LhToastState
| Member | Description |
|---|---|
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): Boolean | Advances the timer; hides, fires onClose, and returns true when it expires |
visible / text / duration | Current state (observable) |
Factory: rememberLhToastState(duration = 2000, onClose = {}).
LhToastType / LhToastPosition
| Value | Dot color |
|---|---|
default | No dot |
primary / success / warning / danger / info | The corresponding semantic color |
LhToastPosition: top / center / bottom.
Token reference
| Token | Usage |
|---|---|
toast.maxWidth | Capsule maximum width (240dp; in practice the smaller of this and 80% of the available width) |
toast.borderRadius | Capsule corner radius (follows radius.base) |
toast.paddingHorizontal / toast.paddingVertical | Capsule padding |
toast.fontSize | Text font size (the dot diameter equals this value) |
toast.gap | Gap between the dot and the text |
zIndex.toast | Layering 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.onPrimary | Text color |
color.{primary,success,warning,danger,info}.default | Semantic dot colors |
Golden case reference
| case id | Assertion | Local test |
|---|---|---|
show-makes-visible | After show, the toast is visible and renders its text | LhToastDefaultsTest.show_makes_visible_and_renders_text |
auto-hides-after-duration | Hides automatically when the timer expires and fires onClose | LhToastDefaultsTest.auto_hides_after_duration_and_emits_onClose + LhToastTest.auto_hides_after_duration |
tick-before-duration-keeps-visible | Before the timer expires, it stays visible and does not fire onClose | LhToastDefaultsTest.tick_before_duration_keeps_visible_and_does_not_emit |
hide-is-silent | hide does not fire onClose | LhToastDefaultsTest.hide_is_silent |
success-type-uses-success-token | The success type consumes the success color | LhToastDefaultsTest.success_type_uses_success_tokens |
hidden-emits-no-commands | When hidden, nothing is rendered | LhToastTest.hidden_toast_renders_nothing |
empty-text-fallback-locale | show("") 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-fallback | The default type does not fall back for an empty string | LhToastTest.empty_text_default_type_has_no_fallback |
explicit-text-wins | An explicit text takes precedence over the semantic fallback | LhToastTest.explicit_text_wins_over_fallback |
./gradlew :components:toast:test # JVM part
./gradlew :components:toast:connectedAndroidTest # Interaction part (requires a device)
