確認ポップアップ

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();

ConfirmPopup は、ポップアップをカスタマイズするためのオプションを渡すことによって、_$confirm_ インスタンスの _require_ メソッドを呼び出すことによって表示されます。 _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>

ヘッドレスモードは、デフォルトの要素ではなく確認 UI 全体を実装できる _container_ スロットを定義することで有効になります。


<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-label_ や _aria-labelledby_ などの属性を定義してポップアップの内容を記述できます。さらに、フォーカスがポップアップ内に保持されるため、_aria-modal_ が追加されます。

_ConfirmationService_ インスタンスの _confirm_ メソッドが使用され、トリガーがパラメーターとして渡されると、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アクションをトリガーし、ポップアップを閉じて、フォーカスをトリガーに移動します。