ConfirmPopup(確認彈出視窗)

ConfirmPopup 顯示相對於目標的確認覆蓋層。


import ConfirmPopup from 'primevue/confirmpopup';

ConfirmPopup 是透過需要安裝為應用程式外掛程式的 ConfirmationService 來控制。


import {createApp} from 'vue';
import ConfirmationService from 'primevue/confirmationservice';

const app = createApp(App);
app.use(ConfirmationService);

該服務可透過 Composition API 的 useConfirm 函數,或 Options API 的應用程式的 $confirm 屬性取得。


import { useConfirm } from "primevue/useconfirm";

const confirm = useConfirm();

呼叫 $confirm 實例的 require 方法並傳遞選項來自訂彈出視窗,即可顯示 ConfirmPopup。target 屬性是必要的,以便將彈出視窗對齊其參照器。


<ConfirmPopup></ConfirmPopup>
<Button @click="confirm1($event)" label="Save" outlined></Button>
<Button @click="confirm2($event)" label="Delete" severity="danger" outlined></Button>

範本允許自訂訊息內容。


<ConfirmPopup group="templating">
    <template #message="slotProps">
        <div class="flex flex-col items-center w-full gap-4 border-b border-surface-200 dark:border-surface-700 p-4 mb-4 pb-0">
            <i :class="slotProps.message.icon" class="text-6xl text-primary-500"></i>
            <p>{{ slotProps.message.message }}</p>
        </div>
    </template>
</ConfirmPopup>
<Button @click="showTemplate($event)" label="Save"></Button>

透過定義 container 插槽啟用無頭模式,讓您可以實作整個確認 UI,而不是預設元素。


<ConfirmPopup group="headless">
    <template #container="{ message, acceptCallback, rejectCallback }">
        <div class="rounded p-4">
            <span>{{ message.message }}</span>
            <div class="flex items-center gap-2 mt-4">
                <Button label="Save" @click="acceptCallback" size="small"></Button>
                <Button label="Cancel" outlined @click="rejectCallback" severity="secondary" size="small" text></Button>
            </div>
        </div>
    </template>
</ConfirmPopup>
<Button @click="requireConfirmation($event)" label="Save"></Button>

螢幕閱讀器

ConfirmPopup 元件使用 alertdialog 角色,由於任何屬性都會傳遞至根元素,因此您可以定義屬性,例如 aria-labelaria-labelledby,以描述彈出視窗內容。此外,由於焦點會保持在彈出視窗內,因此會新增 aria-modal

當使用 $confirm 實例的 require 方法,並將觸發器做為參數傳遞時,ConfirmPopup 會將 aria-expanded 狀態屬性和 aria-controls 新增至觸發器,以便定義觸發器和對話方塊之間的關係。


<ConfirmPopup id="confirm" aria-label="popup" />

<Button @click="openPopup($event)" label="Confirm" id="confirmButton" :aria-expanded="isVisible" :aria-controls="isVisible ? 'confirm' : null" />


<script setup>
const confirm = useConfirm();
const isVisible = ref(false);
const openPopup = (event) => {
    confirm.require({
        target: event.currentTarget,
        message: 'Are you sure you want to proceed?',
        header: 'Confirmation',
        onShow: () => {
            isVisible.value = true;
        },
        onHide: () => {
            isVisible.value = false;
        }
    });
}
</script>

覆蓋層鍵盤支援

按鍵功能
tab將焦點移至彈出視窗內的下一個可聚焦元素。
shift + tab將焦點移至彈出視窗內的前一個可聚焦元素。
escape關閉彈出視窗並將焦點移至觸發器。

按鈕鍵盤支援

按鍵功能
enter觸發動作、關閉彈出視窗並將焦點移至觸發器。
space觸發動作、關閉彈出視窗並將焦點移至觸發器。