Confirm
Confirmation prompt built on Dialog. Call ArcConfirm.open() and await a true or false answer.
<arc-confirm>Overview
Confirm asks a yes-or-no question in a small dialog with a cancel button and a confirm button. It comes in two shapes. await ArcConfirm.open({ heading, message, ... }) creates a prompt, resolves to true on confirm and false on cancel, then removes the element. Use it at a call site that has to decide before it continues. The <arc-confirm> element is for templates: bind open and listen for arc-confirm and arc-cancel. Its default slot takes body markup the message string can't carry, such as a filename in <code>.
It's built on Dialog, so it has the same blurred backdrop, raised panel and focus trap. Escape, a backdrop click and the X button all count as cancel. variant="error" turns the confirm button the error color.
Upgrading from v3: the arc-dialog tag used to be this prompt. In v4, arc-dialog is the overlay primitive (renamed from arc-modal), and the prompt's props (heading, message, confirm-label, cancel-label, variant) moved here. variant="danger" is now variant="error". A leftover "danger" falls back to the default variant with no warning, so the confirm button loses its error color.
Guidelines
Do
- Use `variant="error"` when the action deletes something
- Write a heading that names the action: "Delete project?", not "Are you sure?"
- Label the buttons with what they do, such as "Delete" and "Keep", not "OK" and "Cancel"
- With the element, one `<arc-confirm>` can serve every prompt. Set its props before you open it
Don't
- Don't use it for a message that needs no decision. Use Alert or Dialog
- Don't chain confirmations. If the action needs more context, use a Dialog with a form
- Don't confirm every action. Save it for ones that are destructive or can't be undone
- Don't use the browser's `confirm()`. It blocks the page and can't be styled
Features
- `ArcConfirm.open()` takes `heading`, `message`, `confirmLabel`, `cancelLabel` and `variant`. Its body can only be the `message` string
- Button labels default to "Confirm" and "Cancel"
- In the slot, `<code>` gets the mono font and `<strong>` the primary text color
Preview
Usage
This component needs JavaScript. Use the Web Component or a framework wrapper.
<script type="module" src="@arclux/arc-ui"></script>
<arc-confirm id="confirm"></arc-confirm>
<arc-button variant="primary"
onclick="Object.assign(document.getElementById('confirm'), {
heading: 'Delete this item?',
message: 'This action cannot be undone.',
variant: 'error',
confirmLabel: 'Delete',
cancelLabel: 'Keep',
open: true
})">
Delete Item
</arc-button>import { Confirm, Button } from '@arclux/arc-ui-react';
import { useState } from 'react';
export function ConfirmDemo() {
const [open, setOpen] = useState(false);
return (
<>
<Confirm
open={open}
heading="Delete this item?"
message="This action cannot be undone."
variant="error"
confirmLabel="Delete"
cancelLabel="Keep"
onArcConfirm={() => { setOpen(false); /* perform delete */ }}
onArcCancel={() => setOpen(false)}
/>
<Button variant="primary" onClick={() => setOpen(true)}>Delete Item</Button>
</>
);
}<script setup>
import { ref } from 'vue';
import { Button, Confirm } from '@arclux/arc-ui-vue';
const open = ref(false);
const handleConfirm = () => { open.value = false; /* perform delete */ };
</script>
<template>
<Confirm :open="open" heading="Delete this item?"
message="This action cannot be undone." variant="error"
confirm-label="Delete" cancel-label="Keep"
@arc-confirm="handleConfirm" @arc-cancel="open = false" />
<Button variant="primary" @click="open = true">Delete Item</Button>
</template><script>
import { Button, Confirm } from '@arclux/arc-ui-svelte';
let open = false;
const handleConfirm = () => { open = false; /* perform delete */ };
</script>
<Confirm {open} heading="Delete this item?"
message="This action cannot be undone." variant="error"
confirmLabel="Delete" cancelLabel="Keep"
on:arc-confirm={handleConfirm}
on:arc-cancel={() => open = false} />
<Button variant="primary" on:click={() => open = true}>Delete Item</Button>import { Component } from '@angular/core';
import { Button, Confirm } from '@arclux/arc-ui-angular';
@Component({
imports: [Button, Confirm],
template: `
<arc-confirm [open]="open" heading="Delete this item?"
message="This action cannot be undone." variant="error"
confirmLabel="Delete" cancelLabel="Keep"
(arc-confirm)="onConfirm()" (arc-cancel)="open = false"></arc-confirm>
<arc-button variant="primary" (click)="open = true">Delete Item</arc-button>
`,
})
export class ConfirmDemoComponent {
open = false;
onConfirm() {
this.open = false;
// perform delete
}
}import { Button, Confirm } from '@arclux/arc-ui-solid';
import { createSignal } from 'solid-js';
export function ConfirmDemo() {
const [open, setOpen] = createSignal(false);
return (
<>
<Confirm
open={open()}
heading="Delete this item?"
message="This action cannot be undone."
variant="error"
confirmLabel="Delete"
cancelLabel="Keep"
onArcConfirm={() => { setOpen(false); /* perform delete */ }}
onArcCancel={() => setOpen(false)}
/>
<Button variant="primary" onClick={() => setOpen(true)}>Delete Item</Button>
</>
);
}import { Button, Confirm } from '@arclux/arc-ui-preact';
import { useState } from 'preact/hooks';
export function ConfirmDemo() {
const [open, setOpen] = useState(false);
return (
<>
<Confirm
open={open}
heading="Delete this item?"
message="This action cannot be undone."
variant="error"
confirmLabel="Delete"
cancelLabel="Keep"
onArcConfirm={() => { setOpen(false); /* perform delete */ }}
onArcCancel={() => setOpen(false)}
/>
<Button variant="primary" onClick={() => setOpen(true)}>Delete Item</Button>
</>
);
}API
openbooleanfalse- Controls whether the confirmation dialog is visible. For declarative usage; the imperative API manages this automatically.
headingstring''- The heading text displayed at the top of the confirmation dialog.
messagestring''- The body message explaining what the user is confirming. Used as the fallback for the default slot, so it is the simplest way to set the body and the only one available to the imperative
ArcConfirm.open()API. confirm-labelstring'Confirm'- Label for the confirm button. Use a specific verb like "Delete" or "Publish" instead of generic "OK".
cancel-labelstring'Cancel'- Label for the cancel button. Use a specific alternative like "Keep" or "Go back" when possible.
variant'default' | 'error''default'- Controls the confirm button style. Use "error" for destructive actions; the confirm button renders in the error color.
Events
arc-confirm- Fired when the user clicks the confirm button
arc-cancel- Fired when the user clicks cancel, presses Escape, or clicks the backdrop