All files / Components ConfirmDialog.vue

100% Statements 18/18
100% Branches 10/10
100% Functions 6/6
100% Lines 17/17

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63      33x             33x   33x 20x 15x       33x 43x   43x 11x   32x           33x         43x         3x 3x   3x       3x             3x                
<script setup>
import { onBeforeUnmount, watch } from 'vue';
 
const props = defineProps({
    open: { type: Boolean, default: false },
    title: { type: String, default: 'Are you sure?' },
    message: { type: String, default: '' },
    confirmLabel: { type: String, default: 'Confirm' },
});
 
const emit = defineEmits(['confirm', 'cancel']);
 
const onKeydown = (event) => {
    if (event.key === 'Escape') {
        emit('cancel');
    }
};
 
watch(
    () => props.open,
    (open) => {
        if (open) {
            document.addEventListener('keydown', onKeydown);
        } else {
            document.removeEventListener('keydown', onKeydown);
        }
    },
    { immediate: true }
);
 
onBeforeUnmount(() => document.removeEventListener('keydown', onKeydown));
</script>
 
<template>
    <div
        v-if="open"
        role="dialog"
        aria-modal="true"
        class="fixed inset-0 z-50 flex items-center justify-center bg-slate-900/40 p-4"
    >
        <div class="w-full max-w-sm rounded-lg bg-white p-5 shadow-xl">
            <h2 class="text-base font-semibold text-slate-900">{{ title }}</h2>
            <p v-if="message" class="mt-1 text-sm text-slate-600">{{ message }}</p>
            <div class="mt-4 flex justify-end gap-2">
                <button
                    type="button"
                    class="rounded-md border border-slate-300 px-3 py-1.5 text-sm text-slate-700 hover:bg-slate-50"
                    @click="emit('cancel')"
                >
                    Cancel
                </button>
                <button
                    type="button"
                    class="rounded-md bg-red-600 px-3 py-1.5 text-sm font-medium text-white hover:bg-red-500"
                    @click="emit('confirm')"
                >
                    {{ confirmLabel }}
                </button>
            </div>
        </div>
    </div>
</template>