# core/modaldialog (vanilla.js 0.4.0)

> https://vanillapunch.com/docs/vanilla.js/0.4.0/api/core/modaldialog/

Modal dialog. (in place of the browser window.alert, window.confirm and window.prompt that games called, same as core/modaldialog.rs in the Rust edition)
- The engine draws a dialog like the Chrome tab modal dialog (JavaScriptTabModalDialogViewViews) on top of the canvas. (no browser dialog is opened)
  - Position: horizontal center at the top edge of the canvas. The title is "This page says" (in the browser UI language, core/browseruitext.js). There is no close X.
  - alert has only OK, confirm and prompt have OK and Cancel. The default button is OK. The button order follows Chrome: `[OK][Cancel]` on Windows, `[Cancel][OK]` elsewhere.
  - The prompt field gets the default value fully selected and takes focus. Enter confirms, Escape cancels.
  - While open, scenes are not updated (the frozen screen is still drawn) and the dialog holds the keyboard focus. When it closes, pressed keys and buttons are cleared and focus is given back.
  - A hidden DOM input receives the field text (IME, editing keys, undo). Pressing and dragging sets the cursor and selection.
  - Several dialogs requested in one frame are shown one after another.
- The shadow bakes the two layers of the Chrome bubble_border (offset 2 down, blur 8 and 12) as Gaussians with the views sigma. (core/blurshadow.js)

## ModalDialogResult

The dialog answer. (Confirmed(text) and Cancelled of the Rust ModalDialogResult)

Example: Reading a confirmed and a cancelled result

```js
import { ModalDialogResult } from "./build/vanilla.js";

const confirmed = ModalDialogResult.confirmed("hello");
const cancelled = ModalDialogResult.cancelled();
console.log(confirmed.isConfirmed(), confirmed.getText(), cancelled.isConfirmed());
```

- `constructor(isConfirmed, text)` (constructor, since 0.4.0): Constructor. (create with confirmed or cancelled)
  - `isConfirmed` boolean: true if confirmed.
  - `text` string: The confirmed text.
- `static confirmed(text)` (static, since 0.4.0): A confirmed answer.
  - `text` string: The confirmed text. (the entered text for prompt, an empty string for alert and confirm)
  - returns ModalDialogResult: A confirmed answer.
- `static cancelled()` (static, since 0.4.0): A cancelled answer. (null for prompt, false for confirm)
  - returns ModalDialogResult: A cancelled answer.
- `isConfirmed()` (method, since 0.4.0): Returns whether it was confirmed.
  - returns boolean: true if confirmed.
- `getText()` (method, since 0.4.0): Returns the confirmed text.
  - returns string: The confirmed text. (an empty string if cancelled)

## ModalDialogHandle

Modal dialog handle. (ModalDialogHandle in the Rust edition)
- It finishes when answered (isFinished), and the answer is read with getResult, isConfirmed and getPromptText.
- Engine.alert, Engine.confirm and Engine.prompt open an engine-drawn dialog and return it. (same as Engine::alert, confirm and prompt in the Rust edition)
- It finishes when the dialog is answered. In both engines the function to call afterwards is set with setCallback.
- The await form is Engine.alertAsync, confirmAsync and promptAsync.
- Where there is no canvas (node) it finishes right away: alert as confirmed, confirm and prompt as cancelled.

Example: Show a confirm dialog on a button press and delete depending on the answer (the await form and the handle form)

```js
import { Engine, EngineConfiguration } from "./build/vanilla.js";
const engine = new Engine(new EngineConfiguration());
async function deleteSaveData() {
	if (await engine.confirmAsync("Delete the save data?")) {
		console.log("deleted");
	}
}
function askName() {
	engine.prompt("Name?", "Hero").setCallback((result) => {
		console.log(result.isConfirmed() ? result.getText() : "cancelled");
	});
}
console.log(typeof deleteSaveData, typeof askName, engine.isModalDialogOpen()); // function function false
```

- `constructor()` (constructor, since 0.4.0): Constructor. (a handle waiting for the answer)
- `isFinished()` (method, since 0.4.0): Returns whether it has finished.
  - returns boolean: true if answered.
- `getResult()` (method, since 0.4.0): Returns the answer.
  - returns ModalDialogResult | null: The answer. (null before it finishes)
- `isConfirmed()` (method, since 0.4.0): Returns whether it was confirmed.
  - returns boolean: true if finished and confirmed.
- `getPromptText()` (method, since 0.4.0): Returns the entered text. (prompt)
  - returns string | null: The confirmed text. (null before it finishes or if cancelled)
- `setCallback(callback)` (method, since 0.4.0): Sets the function to call after it finishes. (called on the spot if already finished)
  - `callback` Function: (result: ModalDialogResult) => void

## ModalDialogKind (enum)

Dialog kind. (window.alert, window.confirm, window.prompt)

- `alert` = `alert`: Alert dialog. (only OK)
- `confirm` = `confirm`: Confirm dialog. (OK and Cancel)
- `prompt` = `prompt`: Prompt dialog. (an input field, OK and Cancel)
