ConfirmDialog は、確認 API と統合されたダイアログ UI を使用します。
import ConfirmDialog from 'primevue/confirmdialog';
ConfirmDialog は、アプリケーションプラグインとしてインストールする必要がある *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();
ConfirmDialog は、ダイアログをカスタマイズするためのオプションを渡すことによって、 *$confirm* インスタンスの *require* メソッドを呼び出すことによって表示されます。ポップアップを参照元に合わせるには、*target* 属性が必須です。
<ConfirmDialog></ConfirmDialog>
<Button @click="confirm1()" label="Save" outlined></Button>
<Button @click="confirm2()" label="Delete" severity="danger" outlined></Button>
確認オプションの *position* プロパティは、ダイアログの位置を指定します。
<ConfirmDialog group="positioned"></ConfirmDialog>
<div class="flex flex-wrap justify-center gap-2 mb-4">
<Button @click="confirmPosition('left')" icon="pi pi-arrow-right" label="Left" severity="secondary" style="min-width: 10rem"></Button>
<Button @click="confirmPosition('right')" icon="pi pi-arrow-left" label="Right" severity="secondary" style="min-width: 10rem"></Button>
</div>
<div class="flex flex-wrap justify-center gap-2 mb-4">
<Button @click="confirmPosition('topleft')" icon="pi pi-arrow-down-right" label="TopLeft" severity="secondary" style="min-width: 10rem"></Button>
<Button @click="confirmPosition('top')" icon="pi pi-arrow-down" label="Top" severity="secondary" style="min-width: 10rem"></Button>
<Button @click="confirmPosition('topright')" icon="pi pi-arrow-down-left" label="TopRight" severity="secondary" style="min-width: 10rem"></Button>
</div>
<div class="flex flex-wrap justify-center gap-2">
<Button @click="confirmPosition('bottomleft')" icon="pi pi-arrow-up-right" label="BottomLeft" severity="secondary" style="min-width: 10rem"></Button>
<Button @click="confirmPosition('bottom')" icon="pi pi-arrow-up" label="Bottom" severity="secondary" style="min-width: 10rem"></Button>
<Button @click="confirmPosition('bottomright')" icon="pi pi-arrow-up-left" label="BottomRight" severity="secondary" style="min-width: 10rem"></Button>
</div>
テンプレートを使用すると、メッセージの内容をカスタマイズできます。
<ConfirmDialog 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">
<i :class="slotProps.message.icon" class="!text-6xl text-primary-500"></i>
<p>{{ slotProps.message.message }}</p>
</div>
</template>
</ConfirmDialog>
<Button @click="showTemplate()" label="Save"></Button>
ヘッドレスモードは、デフォルト要素の代わりに確認 UI 全体を実装できる *container* スロットを定義することによって有効になります。
<ConfirmDialog group="headless">
<template #container="{ message, acceptCallback, rejectCallback }">
<div class="flex flex-col items-center p-8 bg-surface-0 dark:bg-surface-900 rounded">
<div class="rounded-full bg-primary text-primary-contrast inline-flex justify-center items-center h-24 w-24 -mt-20">
<i class="pi pi-question text-5xl"></i>
</div>
<span class="font-bold text-2xl block mb-2 mt-6">{{ message.header }}</span>
<p class="mb-0">{{ message.message }}</p>
<div class="flex items-center gap-2 mt-6">
<Button label="Save" @click="acceptCallback" class="w-32"></Button>
<Button label="Cancel" outlined @click="rejectCallback" class="w-32"></Button>
</div>
</div>
</template>
</ConfirmDialog>
<Button @click="requireConfirmation()" label="Save"></Button>
ConfirmDialog コンポーネントは、ヘッダー要素を参照する *aria-labelledby* とともに *alertdialog* ロールを使用しますが、属性はルート要素に渡されるため、*aria-labelledby* を使用してこのデフォルトの動作をオーバーライドできます。さらに、フォーカスがポップアップ内に保持されるため、*aria-modal* が追加されます。
*$confirm* インスタンスの *require* メソッドが使用され、トリガーがパラメーターとして渡されると、ConfirmDialog はトリガーとダイアログの関係が定義されるように、トリガーに *aria-expanded* 状態属性と *aria-controls* を追加します。
<ConfirmDialog id="confirm" />
<Button @click="openDialog()" label="Confirm" :aria-expanded="visible" :aria-controls="visible ? 'confirm' : null"></Button>
<script setup>
const confirm = useConfirm();
const isVisible = ref(false);
const openDialog = () => {
confirm.require({
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 | ダイアログを閉じます。 |