Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Apply VueUse composables in Vue 3/Nuxt projects to replace custom implementations with battle-tested utilities.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/useConfirmDialog.md
1---2category: Utilities3---45# useConfirmDialog67Creates event hooks to support modals and confirmation dialog chains.89Functions can be used on the template, and hooks are a handy skeleton for the business logic of modals dialog or other actions that require user confirmation.1011## Functions and hooks1213- `reveal()` - triggers `onReveal` hook and sets `revealed.value` to `true`. Returns promise that resolves by `confirm()` or `cancel()`.14- `confirm()` - sets `isRevealed.value` to `false` and triggers `onConfirm` hook.15- `cancel()` - sets `isRevealed.value` to `false` and triggers `onCancel` hook.1617## Basic Usage1819### Using hooks2021```vue22<script setup lang="ts">23import { useConfirmDialog } from '@vueuse/core'2425const { isRevealed, reveal, confirm, cancel, onReveal, onConfirm, onCancel }26= useConfirmDialog()27</script>2829<template>30<button @click="reveal">31Reveal Modal32</button>3334<teleport to="body">35<div v-if="isRevealed" class="modal-bg">36<div class="modal">37<h2>Confirm?</h2>38<button @click="confirm">39Yes40</button>41<button @click="cancel">42Cancel43</button>44</div>45</div>46</teleport>47</template>48```4950### Promise5152If you prefer working with promises:5354```vue55<script setup lang="ts">56import { useConfirmDialog } from '@vueuse/core'5758const {59isRevealed,60reveal,61confirm,62cancel,63} = useConfirmDialog()6465async function openDialog() {66const { data, isCanceled } = await reveal()67if (!isCanceled)68console.log(data)69}70</script>7172<template>73<button @click="openDialog">74Show Modal75</button>7677<teleport to="body">78<div v-if="isRevealed" class="modal-layout">79<div class="modal">80<h2>Confirm?</h2>81<button @click="confirm(true)">82Yes83</button>84<button @click="confirm(false)">85No86</button>87</div>88</div>89</teleport>90</template>91```9293## Type Declarations9495```ts96export type UseConfirmDialogRevealResult<C, D> =97| {98data?: C99isCanceled: false100}101| {102data?: D103isCanceled: true104}105export interface UseConfirmDialogReturn<RevealData, ConfirmData, CancelData> {106/**107* Revealing state108*/109isRevealed: ComputedRef<boolean>110/**111* Opens the dialog.112* Create promise and return it. Triggers `onReveal` hook.113*/114reveal: (115data?: RevealData,116) => Promise<UseConfirmDialogRevealResult<ConfirmData, CancelData>>117/**118* Confirms and closes the dialog. Triggers a callback inside `onConfirm` hook.119* Resolves promise from `reveal()` with `data` and `isCanceled` ref with `false` value.120* Can accept any data and to pass it to `onConfirm` hook.121*/122confirm: (data?: ConfirmData) => void123/**124* Cancels and closes the dialog. Triggers a callback inside `onCancel` hook.125* Resolves promise from `reveal()` with `data` and `isCanceled` ref with `true` value.126* Can accept any data and to pass it to `onCancel` hook.127*/128cancel: (data?: CancelData) => void129/**130* Event Hook to be triggered right before dialog creating.131*/132onReveal: EventHookOn<RevealData>133/**134* Event Hook to be called on `confirm()`.135* Gets data object from `confirm` function.136*/137onConfirm: EventHookOn<ConfirmData>138/**139* Event Hook to be called on `cancel()`.140* Gets data object from `cancel` function.141*/142onCancel: EventHookOn<CancelData>143}144/**145* Hooks for creating confirm dialogs. Useful for modal windows, popups and logins.146*147* @see https://vueuse.org/useConfirmDialog/148* @param revealed `boolean` `ref` that handles a modal window149*150* @__NO_SIDE_EFFECTS__151*/152export declare function useConfirmDialog<153RevealData = any,154ConfirmData = any,155CancelData = any,156>(157revealed?: ShallowRef<boolean>,158): UseConfirmDialogReturn<RevealData, ConfirmData, CancelData>159```160