Toast
A self-contained ink-colored capsule overlay that manages its own visibility and timing. Imperative interface show(text) / hide(); hides automatically when the timer expires.
show()with no argument or an empty string falls back to the semantic label fortypefrom the language pack (e.g. success → “Success” under en-US); explicit text always wins.
Spec source:
lh-ui-core/specs/toast.json· Golden cases:lh-ui-core/cases/toast.json(9 cases, translated into hypium tests)
Device-side implementation note
The spec's imperative interface show / hide / tick lands on the device side as methods + an internal timer: show(text) makes it visible and resets the timer (setTimeout(duration)); when the timer expires it hides automatically and fires onClose; hide() hides immediately but does not fire onClose. ArkUI does not provide references to child component instances, so the host calls methods through an LhToastController handle:
private toastCtl: LhToastController = new LhToastController()
build() {
Stack() {
// ...page content
LhToast({ type: LhToastType.Success, position: LhToastPosition.Top, controller: this.toastCtl })
}
}The component must be mounted as the last child of the page's Stack; elevation is zIndex.toast.
Type and position
When type is not default, a semantic-color dot is drawn before the text; position decides the placement within the available area.
HTML mockup preview (HarmonyOS / ArkUI appearance) — not an on-device render
this.toastCtl.show('Success')API
Props
| Prop | Description | Type | Default |
|---|---|---|---|
type | Semantic type; when not default, a semantic-color dot is drawn before the text | LhToastType | Default |
duration | Auto-hide duration (milliseconds) | number | 2000 |
position | Placement within the available area; spacing.xl margin at the top and bottom ends | LhToastPosition | Center |
onClose | Fired when the timer expires and it hides automatically; not fired by an active hide() | () => void | Empty function |
controller | Handle for host calls (optional), registers show / hide | LhToastController? | null |
Methods (forwarded via controller)
| Method | Description |
|---|---|
show(text) | Shows the message and resets the timer; hides automatically on expiry and fires onClose |
hide() | Hides immediately, without firing onClose |
LhToastType
| Value | Dot color |
|---|---|
Default | No dot |
Success | color.success.default |
Warning | color.warning.default |
Danger | color.danger.default |
Info | color.info.default |
LhToastPosition
| Value | Device-side placement |
|---|---|
Top | Top-aligned, with an spacing.xl margin at the top |
Center | Centered, no margins |
Bottom | Bottom-aligned, with an spacing.xl margin at the bottom |
The capsule background takes color.ink (neutral black), and the text takes color.onPrimary.
Token reference
| Token | Purpose |
|---|---|
toast.fontSize | Message font size (14) |
toast.paddingHorizontal / toast.paddingVertical | Capsule paddings (16 / 8vp) |
toast.borderRadius | Capsule corner radius |
toast.gap | Gap between the semantic dot and the text (8vp) |
color.ink / color.onPrimary | Capsule background / text color |
color.{success,warning,danger,info}.default | Semantic dot colors |
shadow.base | Capsule shadow |
zIndex.toast | Overlay elevation (2000) |
spacing.xl | Top and bottom end margins |
Golden case reference
How the 9 assertions in cases/toast.json map onto hypium tests (entry/src/test/Toast.test.ets):
| case id | Assertion | Local test |
|---|---|---|
show-makes-visible | show makes it visible | capsule_uses_ink_and_on_primary_tokens |
auto-hides-after-duration | Hides automatically when the timer expires | dimensions_use_tokens (timing carried by setTimeout) |
tick-before-duration-keeps-visible | Stays visible before expiry | dimensions_use_tokens |
hide-is-silent | hide does not fire onClose | zindex_and_edge_margin_use_tokens |
success-type-uses-success-token | The success type's dot takes the success color | success_type_uses_success_token |
hidden-emits-no-commands | The hidden state emits no commands | default_type_has_no_dot |
empty-text-fallback-locale | Empty show() falls back to the semantic label | toast_semantic_texts_from_pack(Locale.test.ets) |
empty-text-default-type-no-fallback | Empty text with default type does not fall back | toast_semantic_texts_from_pack(Locale.test.ets) |
explicit-text-wins | Explicit text wins over the pack | toast_semantic_texts_from_pack(Locale.test.ets) |
Plus dark-mode specifics: dark_colors_come_from_dark_tokens / dimensions_are_identical_in_dark_mode. To run: in DevEco Studio, right-click entry/src/test → Run Tests.

