Skip to content

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 for type from 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:

typescript
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.

9:41Toast types and positions
SuccessSaved locallyNetwork connection failed

HTML mockup preview (HarmonyOS / ArkUI appearance) — not an on-device render

typescript
this.toastCtl.show('Success')

API ​

Props ​

PropDescriptionTypeDefault
typeSemantic type; when not default, a semantic-color dot is drawn before the textLhToastTypeDefault
durationAuto-hide duration (milliseconds)number2000
positionPlacement within the available area; spacing.xl margin at the top and bottom endsLhToastPositionCenter
onCloseFired when the timer expires and it hides automatically; not fired by an active hide()() => voidEmpty function
controllerHandle for host calls (optional), registers show / hideLhToastController?null

Methods (forwarded via controller) ​

MethodDescription
show(text)Shows the message and resets the timer; hides automatically on expiry and fires onClose
hide()Hides immediately, without firing onClose

LhToastType ​

ValueDot color
DefaultNo dot
Successcolor.success.default
Warningcolor.warning.default
Dangercolor.danger.default
Infocolor.info.default

LhToastPosition ​

ValueDevice-side placement
TopTop-aligned, with an spacing.xl margin at the top
CenterCentered, no margins
BottomBottom-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 ​

TokenPurpose
toast.fontSizeMessage font size (14)
toast.paddingHorizontal / toast.paddingVerticalCapsule paddings (16 / 8vp)
toast.borderRadiusCapsule corner radius
toast.gapGap between the semantic dot and the text (8vp)
color.ink / color.onPrimaryCapsule background / text color
color.{success,warning,danger,info}.defaultSemantic dot colors
shadow.baseCapsule shadow
zIndex.toastOverlay elevation (2000)
spacing.xlTop 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 idAssertionLocal test
show-makes-visibleshow makes it visiblecapsule_uses_ink_and_on_primary_tokens
auto-hides-after-durationHides automatically when the timer expiresdimensions_use_tokens (timing carried by setTimeout)
tick-before-duration-keeps-visibleStays visible before expirydimensions_use_tokens
hide-is-silenthide does not fire onClosezindex_and_edge_margin_use_tokens
success-type-uses-success-tokenThe success type's dot takes the success colorsuccess_type_uses_success_token
hidden-emits-no-commandsThe hidden state emits no commandsdefault_type_has_no_dot
empty-text-fallback-localeEmpty show() falls back to the semantic labeltoast_semantic_texts_from_pack(Locale.test.ets)
empty-text-default-type-no-fallbackEmpty text with default type does not fall backtoast_semantic_texts_from_pack(Locale.test.ets)
explicit-text-winsExplicit text wins over the packtoast_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.

lh-ui native bloodline · HarmonyOS (ArkTS + ArkUI)