Popconfirm
Click an element to open a small card beside it with Cancel and Confirm buttons. The operation runs only after confirmation. Use it for a secondary confirmation, such as deleting a row, when a full dialog is unnecessary. For longer explanations or forms, use Modal.
Import
import { HuiPopconfirm } from '@nscode/handy-ui'
Basic Usage
Place the trigger element in the default slot. title provides the confirmation question. Confirm emits confirm, Cancel emits cancel, and both buttons close the popup.
<script setup lang="ts">
import { HuiPopconfirm, HuiButton, HuiMessage } from '@nscode/handy-ui'
const handleConfirm = () => {
// Perform the deletion here
HuiMessage.success('Confirm clicked; confirm emitted')
}
const handleCancel = () => {
HuiMessage('Cancel clicked; cancel emitted')
}
</script>
<template>
<HuiPopconfirm title="Delete this record?" @confirm="handleConfirm" @cancel="handleCancel">
<HuiButton type="danger">Delete</HuiButton>
</HuiPopconfirm>
</template>
Button Text
Use confirm-button-text and cancel-button-text to change the button labels. Their defaults are the localized Confirm and Cancel labels.
<template>
<HuiPopconfirm
title="This deletion cannot be undone. Continue?"
confirm-button-text="Delete"
cancel-button-text="Keep it"
confirm-button-type="danger"
>
<HuiButton type="danger">Delete</HuiButton>
</HuiPopconfirm>
</template>
Button Types
confirm-button-type and cancel-button-type accept the same seven values as HuiButton's type. The confirm button defaults to primary; the cancel button defaults to default. The first row changes the confirm button, and the second changes the cancel button. Open each popup to compare:
<template>
<HuiSpace direction="vertical" align="start">
<HuiSpace wrap>
<span class="row-label">Confirm button</span>
<HuiPopconfirm title="Confirm button: default" confirm-button-type="default">
<HuiButton>default</HuiButton>
</HuiPopconfirm>
<HuiPopconfirm title="Confirm button: primary (default)">
<HuiButton>primary</HuiButton>
</HuiPopconfirm>
<HuiPopconfirm title="Confirm button: success" confirm-button-type="success">
<HuiButton>success</HuiButton>
</HuiPopconfirm>
<HuiPopconfirm title="Confirm button: warning" confirm-button-type="warning">
<HuiButton>warning</HuiButton>
</HuiPopconfirm>
<HuiPopconfirm title="Confirm button: danger" confirm-button-type="danger">
<HuiButton>danger</HuiButton>
</HuiPopconfirm>
<HuiPopconfirm title="Confirm button: info" confirm-button-type="info">
<HuiButton>info</HuiButton>
</HuiPopconfirm>
<HuiPopconfirm title="Confirm button: help" confirm-button-type="help">
<HuiButton>help</HuiButton>
</HuiPopconfirm>
</HuiSpace>
<HuiSpace wrap>
<span class="row-label">Cancel button</span>
<HuiPopconfirm title="Cancel button: default (default)">
<HuiButton>default</HuiButton>
</HuiPopconfirm>
<HuiPopconfirm title="Cancel button: primary" cancel-button-type="primary">
<HuiButton>primary</HuiButton>
</HuiPopconfirm>
<HuiPopconfirm title="Cancel button: success" cancel-button-type="success">
<HuiButton>success</HuiButton>
</HuiPopconfirm>
<HuiPopconfirm title="Cancel button: warning" cancel-button-type="warning">
<HuiButton>warning</HuiButton>
</HuiPopconfirm>
<HuiPopconfirm title="Cancel button: danger" cancel-button-type="danger">
<HuiButton>danger</HuiButton>
</HuiPopconfirm>
<HuiPopconfirm title="Cancel button: info" cancel-button-type="info">
<HuiButton>info</HuiButton>
</HuiPopconfirm>
<HuiPopconfirm title="Cancel button: help" cancel-button-type="help">
<HuiButton>help</HuiButton>
</HuiPopconfirm>
</HuiSpace>
</HuiSpace>
</template>
<style scoped>
.row-label {
display: inline-block;
width: 56px;
color: var(--hui-text-color-secondary);
font-size: var(--hui-font-size-sm);
}
</style>
Icons
icon is an icon name, matching HuiIcon's name, and defaults to warning. icon-color changes the icon color, using the warning color when omitted. hide-icon hides the icon.
<template>
<HuiSpace wrap>
<HuiPopconfirm title="Submit now?">
<HuiButton>Default (warning)</HuiButton>
</HuiPopconfirm>
<HuiPopconfirm title="Publish now?" icon="info" icon-color="var(--hui-color-primary)">
<HuiButton>Change icon and color</HuiButton>
</HuiPopconfirm>
<HuiPopconfirm title="Mark as resolved?" icon="help" icon-color="var(--hui-color-help)">
<HuiButton>help icon</HuiButton>
</HuiPopconfirm>
<HuiPopconfirm
title="Permanently clear all items from the recycle bin?"
icon="trash"
icon-color="var(--hui-color-danger)"
>
<HuiButton>trash icon</HuiButton>
</HuiPopconfirm>
<HuiPopconfirm title="Publish now?" hide-icon>
<HuiButton>Hide the icon</HuiButton>
</HuiPopconfirm>
</HuiSpace>
</template>
Slots
Use the title slot for a formatted question; it takes precedence over the title prop. The reference slot also accepts the trigger element. When both reference and the default slot are provided, only reference is rendered.
<template>
<HuiSpace wrap>
<HuiPopconfirm>
<HuiButton>title slot</HuiButton>
<template #title>Move <strong>3 files</strong> to the recycle bin?</template>
</HuiPopconfirm>
<HuiPopconfirm title="Remove this member?">
<template #reference>
<HuiButton type="danger" variant="link">reference slot</HuiButton>
</template>
</HuiPopconfirm>
</HuiSpace>
</template>
Placement
placement supports 12 positions and defaults to top. The popup automatically flips to the opposite side when space is insufficient. offset sets the gap between the popup and its trigger. Open the buttons below to compare:
<template>
<div class="placements">
<div class="placements__row placements__row--center">
<HuiPopconfirm placement="top-start" title="Delete this record?" :width="160">
<HuiButton>top-start</HuiButton>
</HuiPopconfirm>
<HuiPopconfirm placement="top" title="Delete this record?" :width="160">
<HuiButton>top</HuiButton>
</HuiPopconfirm>
<HuiPopconfirm placement="top-end" title="Delete this record?" :width="160">
<HuiButton>top-end</HuiButton>
</HuiPopconfirm>
</div>
<div class="placements__row placements__row--between">
<HuiSpace direction="vertical" align="start">
<HuiPopconfirm placement="left-start" title="Delete this record?" :width="160">
<HuiButton>left-start</HuiButton>
</HuiPopconfirm>
<HuiPopconfirm placement="left" title="Delete this record?" :width="160">
<HuiButton>left</HuiButton>
</HuiPopconfirm>
<HuiPopconfirm placement="left-end" title="Delete this record?" :width="160">
<HuiButton>left-end</HuiButton>
</HuiPopconfirm>
</HuiSpace>
<HuiSpace direction="vertical" align="end">
<HuiPopconfirm placement="right-start" title="Delete this record?" :width="160">
<HuiButton>right-start</HuiButton>
</HuiPopconfirm>
<HuiPopconfirm placement="right" title="Delete this record?" :width="160">
<HuiButton>right</HuiButton>
</HuiPopconfirm>
<HuiPopconfirm placement="right-end" title="Delete this record?" :width="160">
<HuiButton>right-end</HuiButton>
</HuiPopconfirm>
</HuiSpace>
</div>
<div class="placements__row placements__row--center">
<HuiPopconfirm placement="bottom-start" title="Delete this record?" :width="160">
<HuiButton>bottom-start</HuiButton>
</HuiPopconfirm>
<HuiPopconfirm placement="bottom" title="Delete this record?" :width="160">
<HuiButton>bottom</HuiButton>
</HuiPopconfirm>
<HuiPopconfirm placement="bottom-end" title="Delete this record?" :width="160">
<HuiButton>bottom-end</HuiButton>
</HuiPopconfirm>
</div>
</div>
</template>
<style scoped>
.placements {
max-width: 520px;
margin: 0 auto;
}
.placements__row {
display: flex;
gap: var(--hui-spacing-2);
}
.placements__row--center {
justify-content: center;
}
.placements__row--between {
justify-content: space-between;
margin: var(--hui-spacing-3) 0;
}
</style>
Width
width defaults to 300. Numbers use pixels; strings can include units. Long questions wrap to fit the popup.
<template>
<HuiSpace wrap>
<HuiPopconfirm
title="This question is long enough to wrap at the popup width. The buttons remain on the last row."
>
<HuiButton>Default width 300</HuiButton>
</HuiPopconfirm>
<HuiPopconfirm
:width="220"
title="This question is long enough to wrap at the popup width. The buttons remain on the last row."
>
<HuiButton>:width="220"</HuiButton>
</HuiPopconfirm>
<HuiPopconfirm width="24em" title="The width is 24em; string values are used as provided.">
<HuiButton>width="24em"</HuiButton>
</HuiPopconfirm>
</HuiSpace>
</template>
Disabled
When disabled is true, clicking the trigger does not open the popup.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiCheckbox, HuiPopconfirm, HuiSpace } from '@nscode/handy-ui'
const disabled = ref(true)
</script>
<template>
<HuiSpace wrap>
<HuiCheckbox v-model="disabled">Disable confirmation</HuiCheckbox>
<HuiPopconfirm title="Opens only when enabled" :disabled="disabled">
<HuiButton>Click me</HuiButton>
</HuiPopconfirm>
</HuiSpace>
</template>
Async Operations After Confirmation
The popup has already closed when confirm is emitted, so the request can run next. Show a loading state on the button and disable the confirmation while the request is pending to prevent duplicate submissions.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiMessage, HuiPopconfirm } from '@nscode/handy-ui'
const publishing = ref(false)
const publish = () => {
publishing.value = true
// Simulate a request with a timer
setTimeout(() => {
publishing.value = false
HuiMessage.success('Published successfully')
}, 1500)
}
const cancel = () => {
HuiMessage('Publishing cancelled')
}
</script>
<template>
<HuiPopconfirm
title="Everyone will be able to see this after publishing. Publish now?"
icon="info"
icon-color="var(--hui-color-primary)"
confirm-button-text="Publish"
:disabled="publishing"
@confirm="publish"
@cancel="cancel"
>
<HuiButton type="primary" :loading="publishing" loading-text="Publishing...">Publish</HuiButton>
</HuiPopconfirm>
</template>
Confirming Individual Rows
Place an HuiPopconfirm on each row. Opening another row's popup automatically closes the previous one.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiPopconfirm, HuiButton, HuiSpace } from '@nscode/handy-ui'
const initialRows = [
{ id: 1, name: '张三' },
{ id: 2, name: '李四' },
{ id: 3, name: '王五' }
]
const rows = ref([...initialRows])
const remove = (id: number) => {
rows.value = rows.value.filter((row) => row.id !== id)
}
const reset = () => {
rows.value = [...initialRows]
}
</script>
<template>
<HuiSpace direction="vertical" align="start">
<HuiSpace v-for="row in rows" :key="row.id">
<span>{{ row.name }}</span>
<HuiPopconfirm :title="`Delete ${row.name}?`" @confirm="remove(row.id)">
<HuiButton size="small" type="danger">Delete</HuiButton>
</HuiPopconfirm>
</HuiSpace>
<HuiButton v-if="rows.length < initialRows.length" size="small" @click="reset">
Restore data
</HuiButton>
</HuiSpace>
</template>
Teleport Target
The popup is attached under body by default. Pass a CSS selector to teleport to use another target, or false to keep it where the component is rendered. true is equivalent to 'body'.
<template>
<HuiPopconfirm title="Keep at the component's location" :teleport="false">
<HuiButton>:teleport="false"</HuiButton>
</HuiPopconfirm>
</template>
Controlling Visibility Externally
The parent can request opening or closing. Confirmation, cancellation, outside clicks and Esc report visibility through update:visible.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiPopconfirm, HuiButton, HuiSpace } from '@nscode/handy-ui'
const visible = ref(false)
const result = ref('')
</script>
<template>
<HuiSpace align="center">
<HuiButton @click="visible = !visible">Toggle externally</HuiButton>
<HuiPopconfirm
v-model:visible="visible"
title="Confirm the current operation?"
@confirm="result = 'Confirmed'"
@cancel="result = 'Cancelled'"
><HuiButton>Open confirmation</HuiButton></HuiPopconfirm
>
<span>visible:{{ visible }};{{ result }}</span>
</HuiSpace>
</template>
Internal and Controlled Visibility
Without a visible prop, or with only an update listener, the component keeps its own visibility and starts hidden. Providing the prop makes it controlled; an explicit undefined means hidden. If the parent rejects an open or close request, the actual parent state remains in effect. confirm and cancel still report the button action. Switching modes produces a development warning. Removing the prop restores the previous internal state.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiPopconfirm, HuiSpace } from '@nscode/handy-ui'
const popupBound = ref(false)
const popupRedraw = ref(0)
const popupLast = ref<boolean | string>('None yet')
const popupAction = ref('None yet')
const recordPopup = (value: boolean) => {
popupLast.value = value
}
</script>
<template>
<HuiSpace class="popconfirm-q1-demo" direction="vertical" fill>
<HuiButton class="popup-redraw" @click="popupRedraw++"
>Parent redraw {{ popupRedraw }}</HuiButton
>
<HuiSpace wrap>
<HuiSpace class="popup-local" direction="vertical">
<span>Visibility prop omitted</span>
<HuiPopconfirm
:data-parent="popupRedraw"
title="Confirm this demo operation?"
@confirm="popupAction = 'Visibility prop omitted: confirm'"
@cancel="popupAction = 'Visibility prop omitted: cancel'"
>
<template #title
><HuiSpace direction="vertical"
><span>Confirm this demo operation?</span
><HuiButton class="popup-inside-redraw" @click="popupRedraw++"
>Parent redraw {{ popupRedraw }}</HuiButton
></HuiSpace
></template
><HuiButton class="popup-trigger">Show confirmation</HuiButton>
</HuiPopconfirm>
</HuiSpace>
<HuiSpace class="popup-listener" direction="vertical">
<span>Update listener only</span>
<HuiPopconfirm
@update:visible="recordPopup"
:data-parent="popupRedraw"
title="Confirm this demo operation?"
@confirm="popupAction = 'Update listener only: confirm'"
@cancel="popupAction = 'Update listener only: cancel'"
>
<template #title
><HuiSpace direction="vertical"
><span>Confirm this demo operation?</span
><HuiButton class="popup-inside-redraw" @click="popupRedraw++"
>Parent redraw {{ popupRedraw }}</HuiButton
></HuiSpace
></template
><HuiButton class="popup-trigger">Show confirmation</HuiButton>
</HuiPopconfirm>
</HuiSpace>
<HuiSpace class="popup-empty" direction="vertical">
<span>Explicit undefined</span>
<HuiPopconfirm
:visible="undefined"
:data-parent="popupRedraw"
title="Confirm this demo operation?"
@confirm="popupAction = 'Explicit undefined: confirm'"
@cancel="popupAction = 'Explicit undefined: cancel'"
>
<template #title
><HuiSpace direction="vertical"
><span>Confirm this demo operation?</span
><HuiButton class="popup-inside-redraw" @click="popupRedraw++"
>Parent redraw {{ popupRedraw }}</HuiButton
></HuiSpace
></template
><HuiButton class="popup-trigger">Show confirmation</HuiButton>
</HuiPopconfirm>
</HuiSpace>
<HuiSpace class="popup-fixed" direction="vertical">
<span>Always hidden; parent rejects opening</span>
<HuiPopconfirm
:visible="false"
:data-parent="popupRedraw"
title="Confirm this demo operation?"
@confirm="popupAction = 'Always hidden; parent rejects opening: confirm'"
@cancel="popupAction = 'Always hidden; parent rejects opening: cancel'"
>
<template #title
><HuiSpace direction="vertical"
><span>Confirm this demo operation?</span
><HuiButton class="popup-inside-redraw" @click="popupRedraw++"
>Parent redraw {{ popupRedraw }}</HuiButton
></HuiSpace
></template
><HuiButton class="popup-trigger">Show confirmation</HuiButton>
</HuiPopconfirm>
</HuiSpace>
<HuiSpace class="popup-bound" direction="vertical">
<span>Two-way binding</span>
<HuiPopconfirm
v-model:visible="popupBound"
:data-parent="popupRedraw"
title="Confirm this demo operation?"
@confirm="popupAction = 'Two-way binding: confirm'"
@cancel="popupAction = 'Two-way binding: cancel'"
>
<template #title
><HuiSpace direction="vertical"
><span>Confirm this demo operation?</span
><HuiButton class="popup-inside-redraw" @click="popupRedraw++"
>Parent redraw {{ popupRedraw }}</HuiButton
></HuiSpace
></template
><HuiButton class="popup-trigger">Show confirmation</HuiButton>
</HuiPopconfirm>
</HuiSpace>
</HuiSpace>
<span class="popup-status"
>Observed request: {{ popupLast }}; bound visibility: {{ popupBound }}; action:
{{ popupAction }}</span
>
</HuiSpace>
</template>
Props
| Prop | Type | Default | Description |
|---|---|---|---|
| title | string | - | Confirmation question |
| confirmButtonText | string | Locale-dependent (Confirm in English) | Confirm button label |
| cancelButtonText | string | Locale-dependent (Cancel in English) | Cancel button label |
| confirmButtonType | ButtonType | 'primary' | Confirm button type; see Types |
| cancelButtonType | ButtonType | 'default' | Cancel button type |
| icon | string | 'warning' | Icon name, matching HuiIcon's name |
| iconColor | string | - | Icon color; defaults to the warning color |
| hideIcon | boolean | false | Whether to hide the icon |
| disabled | boolean | false | Whether to disable the popup |
| width | number | string | 300 | Popup width; numbers use pixels |
| teleport | string | boolean | 'body' | CSS selector for the teleport target. false keeps the popup at the component's location |
| placement | PopperPlacement | 'top' | Popup position, using the same 12 values as HuiPopover. Automatically flips when space is insufficient |
| offset | number | - | Gap between the popup and its trigger in pixels. Defaults to HuiPopover's offset |
| visible | boolean | — | Controlled when provided, including undefined as hidden. Otherwise keeps internal state, even with an update listener. Supports v-model:visible |
Events
| Event | Callback Arguments | Description |
|---|---|---|
| confirm | - | Emitted when the Confirm button is clicked |
| cancel | - | Emitted when the Cancel button is clicked |
| update:visible | (value: boolean) | Requests opening or closing, including outside clicks, Esc, confirmation and cancellation |
Slots
| Slot | Scope Arguments | Description |
|---|---|---|
| default | - | Trigger element |
| reference | - | Trigger element, taking precedence over the default slot |
| title | - | Confirmation question, taking precedence over the title prop |
Types
import type { PopconfirmProps, ButtonType, PopperPlacement } from '@nscode/handy-ui'
type ButtonType = 'primary' | 'success' | 'warning' | 'danger' | 'info' | 'help' | 'default'
type PopperPlacement =
| 'top'
| 'top-start'
| 'top-end'
| 'bottom'
| 'bottom-start'
| 'bottom-end'
| 'left'
| 'left-start'
| 'left-end'
| 'right'
| 'right-start'
| 'right-end'