Confirm dialogs
"Delete 3 orders?" in one line. Momo.confirm(), Momo.alert() and Momo.prompt() show a Momo dialog and give you the answer, with no markup to write. Or put data-momo-confirm on a button, a link or a form and it asks before going ahead.
Ask before a click
Add data-momo-confirm with the explanation. The click, the link or the form only goes ahead after OK: your own handlers run then, not before.
<div class="cluster">
<button class="btn btn-red" type="button" id="cf-delete" data-momo-confirm="The orders and their invoices will be gone for good." data-confirm-title="Delete 3 orders?" data-confirm-ok="Delete orders" data-confirm-icon="trash-2"><i class="mi mi-trash-2"></i> Delete 3 orders</button>
<a class="btn btn-ghost" href="#ask-before-a-click" id="cf-leave" data-momo-confirm="Your changes to this order are not saved yet." data-confirm-title="Leave this page?" data-confirm-ok="Leave" data-confirm-cancel="Stay"><i class="mi mi-log-out"></i> Leave page</a>
<form id="cf-news" data-momo-confirm="1,248 customers will get this email right away." data-confirm-title="Send the newsletter?" data-confirm-ok="Send now" data-confirm-icon="send" data-confirm-color="violet">
<button class="btn btn-brand" type="submit"><i class="mi mi-send"></i> Send newsletter</button>
</form>
</div>
<script>
(function () {
document.getElementById('cf-delete').addEventListener('click', function () {
Momo.toast({ title: '3 orders deleted', color: 'red', icon: 'trash-2' });
});
document.getElementById('cf-news').addEventListener('submit', function (e) {
e.preventDefault(); // your form would really be sent here
Momo.toast({ title: 'Newsletter sent', text: 'On its way to 1,248 customers.', color: 'green', icon: 'check' });
});
})();
</script><button class="btn btn-red" data-momo-confirm="The orders and their invoices will be gone for good."
data-confirm-title="Delete 3 orders?" data-confirm-ok="Delete orders">Delete 3 orders</button>
<a href="/orders" data-momo-confirm="Your changes are not saved yet." data-confirm-ok="Leave" data-confirm-cancel="Stay">Leave page</a>
<form action="/newsletter" method="post" data-momo-confirm="1,248 customers will get this email right away.">…</form>- A red button (
.btn-red) asks with a red OK and starts on Cancel, so a stray Enter cannot delete anything. - Esc means Cancel. Clicking outside does nothing: an answer has to be chosen.
- It is a real dialog: focus stays inside and goes back to your button afterwards, and screen readers announce it. On phones it is a bottom sheet with full-width buttons.
From code
Each one returns a promise: confirm gives true or false, prompt gives the text or null, alert finishes when it is closed.
Collection: Spring
<div class="cluster">
<button class="btn" type="button" id="cf-archive"><i class="mi mi-archive"></i> Archive order</button>
<button class="btn" type="button" id="cf-rename"><i class="mi mi-pencil"></i> Rename collection</button>
<button class="btn" type="button" id="cf-alert"><i class="mi mi-info"></i> Show alert</button>
<span class="text-sm text-muted">Collection: <b id="cf-name">Spring</b></span>
</div>
<script>
(function () {
document.getElementById('cf-archive').addEventListener('click', async function () {
var yes = await Momo.confirm({ title: 'Archive order #10421?', message: 'You can find it later under Archived.', ok: 'Archive', icon: 'archive', color: 'yellow' });
if (yes) Momo.toast({ title: 'Order #10421 archived', color: 'green', icon: 'check' });
});
document.getElementById('cf-rename').addEventListener('click', async function () {
var out = document.getElementById('cf-name');
var name = await Momo.prompt({ title: 'Rename collection', label: 'Name', value: out.textContent, required: true, maxlength: 30, ok: 'Rename', hint: 'Customers see this name in the shop.' });
if (name !== null) out.textContent = name;
});
document.getElementById('cf-alert').addEventListener('click', function () {
Momo.alert({ title: 'Import finished', message: '248 products were added. 3 were skipped because they had no price.', icon: 'circle-check', color: 'green', ok: 'Got it' });
});
})();
</script>if (await Momo.confirm({ title: 'Archive order #10421?', message: 'You can find it later under Archived.', ok: 'Archive' })) {
archive(10421);
}
const name = await Momo.prompt({ title: 'Rename collection', label: 'Name', value: 'Spring', required: true });
if (name !== null) rename(name);
await Momo.alert({ title: 'Import finished', message: '248 products were added.', icon: 'circle-check', color: 'green' });
// the short way
if (await Momo.confirm('Discard this draft?')) discard();Good to know
- A prompt checks its field before it closes (
required,type: 'email',pattern) and says what is wrong under the field. - When someone cancels an attribute confirm, the element fires
momo:cancel. - For anything bigger than a question (a form with several fields, a preview), write a normal dialog.
- Something that is easy to undo does not need a question: do it and offer Undo in a toast.
Reference
| Option / attribute | What it does |
|---|---|
title, data-confirm-title | The question ("Are you sure?" by default) |
message, data-momo-confirm="…" | The explanation under it |
ok, cancel, data-confirm-ok, data-confirm-cancel | The button words |
danger: true, color, data-confirm-color | A red OK that starts on Cancel; any Momo color for the icon and the OK button |
icon, data-confirm-icon | A Moey icon beside the question (trash-2, archive) |
label, value, placeholder, hint, type, required, pattern, maxlength | The field of a prompt |
Momo.confirm(), Momo.prompt(), Momo.alert() | Promises: true / false, the text or null, nothing |
Event momo:cancel | On the button, link or form when an attribute confirm is cancelled |