MessageBox

MessageBox

HkMessageBox is a Windows-style message box: a small 26rem modal that shows a notice, asks a yes/no question, or collects one value. It is not a component on the Vue side — it is a service object with three command methods, each of which mounts its own short-lived host and returns a promise for the user's decision.

  • Reach for it from code that has no render tree of its own to host a dialog: a click handler, a keyboard shortcut handler, a delete confirmation inside a table row callback. The call site awaits the promise and acts on it.
  • The box is deliberately minimal — at most one input, no slots for arbitrary content. Once you need two fields, a checkbox or a list, build a real Modal with a form in it and mount it as a component.
  • The Rust side is the opposite shape: MessageBox is an ordinary #[component] that renders the same box when open, and reports through on_resolve — a component mirror, not a service. The last section below is that difference.
  • For a decision the page does render, ConfirmDialog is the declarative twin with the same button order.

Import

tsx
1
import { HkMessageBox, type HkMessageBoxOptions, type HkMessageBoxPrompt } from "@celestia-island/hikari";
rust
1
2
3
use hikari_components::*;          // MessageBox, MessageBoxKind, MessageBoxTone, MessageBoxOutcome,
                                   // MessageBoxPromptSpec
use hikari_components::prelude::*; // MessageBoxProps, rsx!, VNode, EventHandler

HkMessageBox is an object literal, not a component: HkMessageBox.alert(options) / HkMessageBox.confirm(options) / HkMessageBox.prompt(options) — nothing else is on it. The Rust form is a plain function, MessageBox(MessageBoxProps { … }); inside rsx! every prop may be omitted except message, and the open state lives in the component's own registry rather than in a prop.

Three commands, three promise types

alert resolves void when dismissed. confirm resolves true on the confirm verb and false on cancel, Escape or a backdrop press. prompt resolves the field's string on confirm and null on any dismissal. In every case the dismissal channels settle the promise once — a cancel race with a confirm cannot resolve it twice, because each call resolves its promise behind a one-shot flag.

The box opens on a macrotask (setTimeout(…, 0)), never synchronously inside the caller's click dispatch: a box mounted mid-dispatch would see that same click reach its freshly registered document-level listeners and dismiss itself immediately. The host is reclaimed when the leave transition ends, or after 360ms at the latest, with a five-minute safety net for a box that is never answered — a lost afterLeave (a route change wiping the DOM) must not leak an app.

tone does two jobs at once: it picks the icon ink and the confirm button's voice (danger → danger, everything else → primary). The cancel button is hidden for alert — an alert has no cancel by definition — and for any box with hideCancel.

The TSX card is the whole API: three buttons, each firing one command. Press one and the box appears over the page. The Rust card is the confirm shape — a component with its tone, its two verbs and a request identity — because a snapshot never runs a command; the alert and prompt variants each have their own Rust card further down this page.

Note the order of the two verbs: the confirm button comes first, the cancel second — the desktop OK-left / cancel-right convention shared with ConfirmDialog. That order is not an RTL-dependent flip; the row is laid out in the inline direction.

The prompt field, and what validates it

prompt is at most one field, described by HkMessageBoxPrompt: an initial value, a label, a placeholder, a type of "text" | "password" | "number", prefix / suffix render functions for affixes (a dial-code chip, a unit), and a confirm-time validate(value) hook. On the Rust side the same shape is MessageBoxPromptSpec (value / label / placeholder / input_type / prefix / suffix / validate); its affixes are Option<Element> slots rather than render functions, so a host passes Some(rsx! { … }). validate returns an error string to block the confirmation; the string is shown under the field and the box stays open. A fresh keystroke clears a failed validation, so the user is never trapped by a stale message. Enter confirms (Vue guards on isComposing, so an IME candidate list is not a submit).

Empty is not a validation failure by default: validate is the only rule, so a box with no validator accepts an empty string. Write validate: (value) => (value.trim() ? undefined : "Enter a name") when the value is required.

[D] The Rust mirror renders the crate's Input until the HkInput mirror lands, and its label and validation rows use .hk-message-box-prompt-label / .hk-message-box-prompt-error — classes the Vue sheet does not style, so they paint as plain rows after the field, where Vue puts the error inside the input's own error slot. Enter confirms on both sides. The validator itself is a Rc<dyn Fn(String) -> Option<String>> (MessageBoxPromptSpec::validate); the card below leaves it None because a snapshot cannot type into the field to show a failing case.

The TSX card runs the validator for real — press "Create a key", type one character, press Enter and watch the error block the confirmation until the next keystroke. The Rust card declares the same prompt pre-filled with ci-runner; a snapshot has no keyboard, so it paints the field and the verbs and nothing else.

The Rust mirror: a component, not a service

This is the one thing to get right before comparing the two sides. Vue's HkMessageBox is a service: HkMessageBox.confirm({…}) creates the app, the promise and the teardown, and the only thing a caller ever holds is the promise. Rust has no promise, so MessageBox is a component with an open-ish lifecycle baked into a small module registry:

  • The box renders only while its registry entry is open. The entry is keyed by instance_key (default "message-box"; two boxes on one page must pass different keys) and stamped with request_id. A host that re-shows a settled box must pass a new request_id — the registry keeps the settled flag, and the same request after a settle would render nothing. Vue does not need this because each show() mounts a fresh host.
  • The outcome arrives on on_resolve as a MessageBoxOutcome: AlertDismissed, Confirmed, Cancelled, PromptValue(String) or PromptCancelled. It is the promise payload flattened into an enum, and the one-shot gate lives in the registry, so a confirm and a dismiss racing in one tick still resolve once.
  • The restore path is not a call-site .then() but a host-owned flag: the host keeps "which box is open" — a Signal<String> of request ids, or a Signal<Option<Request>> — and writes it back to false/None from on_resolve. That is the same caller-owned contract as every other dialog in the library.
  • hide_cancel mirrors Vue's hideCancel, and kind decides which verbs exist: Alert has no cancel, Confirm and Prompt show one unless hide_cancel is set.
  • The prompt field's error path is live in the mirror, and so is the enter-to-confirm edge over the crate's Input. That input has no password variant in this release (it falls back to a plain type="password" field rather than Vue's canvas dot surface), so a "password" prompt is masked but not dot-matrixed. The error path itself: MessageBoxPromptSpec::validate is an Rc<dyn Fn(String) -> Option<String>>, run before the confirm settles, and any returned string is stored in the registry and painted under the field; a keystroke clears it. Enter confirms on both sides, but Vue also skips a key that arrives mid-composition (isComposing) while this stack's KeyboardEvent carries no such flag (the same note greets Input's own Enter guard), so an Enter that confirms an IME candidate list would also submit the box.

on_resolve fires exactly once per request: Vue resolves its promise on the settle, and the mirror calls the handler at the same point, once per request_id. A second settle for the same id is a no-op.

Rust

The alert box: one verb, no cancel, the success tone's check glyph. Nothing else on screen — an alert has nothing to decide, which is why its cancel button does not exist rather than being hidden with CSS.

Interaction

HkMessageBox is called from a handler and awaited; there is no render-time state to manage. The example below is the realistic call site: a destructive action that asks first and only then does its work — and, in a second call, a prompt that re-asks while the validator complains. The Rust side of this card is the declarative shape above; a snapshot has no handler to run.

TSX
Nothing asked yet.

API

Vue

Props

NameTypeDefaultRequired
titleStringundefined—
messageString—✓
toneString as PropType<NonNullable<HkMessageBoxOptions["tone"]>>"info"—
confirmTextStringundefined—
cancelTextStringundefined—
hideCancelBooleanfalse—
promptObject as PropType<HkMessageBoxPrompt>undefined—
resolveFunction as PropType<Resolve>—✓
kind<code>String as PropType<"alert" | "confirm" | "prompt"></code>—✓
onSettledFunction as PropType<() => void>—✓

Rust

Props — MessageBox

PropTypeDefaultDescription
titleOption<String>None—
messageString""Body text.
toneMessageBoxToneMessageBoxTone::Info—
confirm_textOption<String>None—
cancel_textOption<String>None—
hide_cancelboolfalseHide the cancel button (pure notice; vue default false).
promptOption<MessageBoxPromptSpec>NonePresent for prompt boxes (exactly one field).
kindMessageBoxKindMessageBoxKind::Alert—
instance_keyString"message-box".to_string()Registry key for the box's transient state (two boxes on one page must pass different keys; the default suits the usual singleton).
request_idOption<String>NoneRequest identity: when it changes, the box resets (open + field value + error).
on_resolveOption<EventHandler<MessageBoxOutcome>>NoneFires once when the box settles.

Accessibility

  • The box is a role="dialog" with aria-modal="true", named by its title (title, otherwise the localized per-kind fallback: "Notice", "Please confirm", "Input"). Escape, the backdrop and the ✕ all count as a cancel, and the box never stays half-open after a dismissal.
  • The message is plain text — it is never interpreted as HTML — and it is the only body content the box accepts. When tone="danger" the text block carries role="alert", so a screen reader announces an urgent message as it appears.
  • The tone glyph is aria-hidden: the urgency is in the words and in the confirm button's voice, not in a colour a screen reader cannot name.
  • Focus lands on the first control inside the frame (the confirm button) and returns to the opener on close — that is the Modal trap. On the Rust side there is no trap; the host places and restores focus.
  • The prompt's error is announced through the input's own error slot, and the field keeps its label; the box's confirm button stays focusable so a keyboard user can retry without moving.
  • confirmText / cancelText are caller-supplied strings, so a command that must be described precisely ("Delete 3 rules", not "OK") can say it. Keep them verbs, not nouns.

Related components

  • ConfirmDialog — the same binary decision as a component, for pages that already render their dialogs.
  • Modal — the frame behind the box, and the right host once a form grows past one field.
  • ProgressDialog — the long-running counterpart, for work that outlives a single answer.
  • Toast — the non-blocking notice, for anything the user does not have to acknowledge.