Composables
useConfirm
Ask the user to confirm an action in a small modal, and wait for the answer.
Usage
useConfirm gives a confirm function. Call it with a title: it opens a small Modal and resolves with true when the user confirms, or false when they cancel.
The transaction is still there.
<script setup lang="ts">
import { useConfirm } from '@kuina-ui/vue'
import { ref } from 'vue'
const confirm = useConfirm()
const transactionStatus = ref('The transaction is still there.')
async function deleteTransaction() {
const isConfirmed = await confirm({
title: 'Delete transaction?',
description: "This will permanently delete the Acme Corp payment of $4,850.00. This can't be undone.",
confirmLabel: 'Delete transaction',
destructive: true,
})
transactionStatus.value = isConfirmed ? 'The transaction was deleted.' : 'The transaction is still there.'
}
</script>
<template>
<div class="flex flex-wrap items-center gap-3">
<KButton label="Delete transaction" variant="soft" color="danger" @click="deleteTransaction" />
<p class="text-sm text-muted">{{ transactionStatus }}</p>
</div>
</template>
useConfirm shows its modal through KOverlayHost: add it once at the root of your app, as explained in useOverlay. With the Nuxt module, useConfirm is auto-imported. In a Vue app, import it from @kuina-ui/vue.
Options
| Option | Type | Default |
|---|---|---|
title | string | |
description | string | |
confirmLabel | string | 'Confirm' |
cancelLabel | string | 'Cancel' |
destructive | boolean | false |
Without destructive, the confirm button is the main button. With it, the button turns red, for an action that deletes or can't be undone.
<script setup lang="ts">
import { useConfirm } from '@kuina-ui/vue'
import { ref } from 'vue'
const confirm = useConfirm()
const isBudgetArchived = ref(false)
async function archiveBudget() {
isBudgetArchived.value = await confirm({
title: 'Archive budget?',
description: 'You can find it again in your archived budgets.',
confirmLabel: 'Archive',
})
}
</script>
<template>
<KButton label="Archive budget" variant="soft" color="gray" :disabled="isBudgetArchived" @click="archiveBudget" />
</template>
Behavior
- The modal is an alert for screen readers, and the focus starts on Cancel, so a quick Enter does no harm.
- Esc, the close button and a click outside count as a cancel, and resolve with
false. - The promise resolves as soon as the user answers, while the modal is still fading out.
To ask for more than yes or no, like a reason or a new name, write your own modal and open it with useOverlay.