Notifications and dialogs
Ask before a destructive action, show progress while it runs, and confirm the result, using Prompt for the questions and MessageBox for the messages.
What you will build
A Delete project button that:
- asks for confirmation, with the button styled as destructive,
- shows a "Deleting…" message while the work runs,
- reports success, or an error that stays until it is closed.
And a Rename button that asks for a new name and checks it before accepting it.
1. Set up both modules
<script type="module" src="main.js" data-id="vUX" data-library-root="/lib/vUX/"></script>
<button id="deleteBtn">Delete project</button>
<button id="renameBtn">Rename</button>import { MessageBox } from "/lib/vUX/vUX-messageBox.js";
import { Prompt } from "/lib/vUX/vUX-prompt.js";
const box = new MessageBox();
box.config.alignment = "right"; // "left" | "center" | "right"
box.config.duration = 4000; // ms on screen; 0 keeps a message until it is closed
box.initialize();
const ask = new Prompt();
ask.initialize();Create each once and reuse it everywhere. Prompt queues questions when two parts of the page ask at once, and MessageBox stacks messages without jumps.
2. Confirm before deleting
Every Prompt method returns a Promise, so the click handler is async:
$$.ss("#deleteBtn").addEventListener("click", async () => {
const sure = await ask.confirm("Delete this project and all its files?", {
title: "Delete project",
okText: "Delete",
danger: true // styles the confirm button as destructive
});
if (!sure) return; // cancelled: nothing happens
await deleteProject();
});confirm() resolves to true or false. The dialog sits above any open modal and handles the keyboard for you.
3. Show progress, then the result
A message with duration: 0 stays until you close it. show() returns it, so you can close it when the work finishes:
async function deleteProject(){
const working = box.show("Deleting…", { duration: 0, closeButton: false });
try {
await fetch("/api/project", { method: "DELETE" });
working.close();
box.success("The project was deleted.", { title: "Done" });
} catch (e) {
working.close();
box.error("The server did not answer. Try again in a moment.", { title: "Delete failed", duration: 0 });
}
}success(), error(), info() and warning() are shorthands for show() with a type. An error that stays (duration: 0) is a good default: the reader may look away while it is shown.
4. Ask for a value
input() asks for one value and can check it before the dialog closes. Return an error message to keep the dialog open, or an empty string to accept:
$$.ss("#renameBtn").addEventListener("click", async () => {
const name = await ask.input("Project name", {
value: "My project",
required: true,
maxLength: 40,
validate: (v) => v.trim().toLowerCase() === "untitled" ? "Pick a more specific name" : ""
});
if (name === null) return; // cancelled
box.info("Renamed to " + name);
});validate can also be async, for a check that asks the server whether a name is taken.
Going further
- Restyle messages with custom properties on
.vmb-msg: see the MessageBox reference. alert()for a single acknowledgement: see the Prompt reference.- Try every option live: the MessageBox and Prompt examples.