Skip to content

Early previewhamur is not on npm yet. The components are being built in the open, follow along on GitHub.

OverlaysReact

Popconfirm

A small popup anchored to a button that asks the person to confirm an action before it runs.

Rendered by React 19.3.1

Loading React…
import { Popconfirm } from '@hamur/react/popconfirm'

export default function PopconfirmExample() {
  return (
    <Popconfirm.Root>
      <Popconfirm.Trigger className="button danger">
        Delete
      </Popconfirm.Trigger>
      <Popconfirm.Portal>
        <Popconfirm.Positioner sideOffset={8}>
          <Popconfirm.Popup className="popover">
            <Popconfirm.Arrow className="arrow" />
            <Popconfirm.Title className="popover-title">
              Delete this item?
            </Popconfirm.Title>
            <Popconfirm.Description className="muted">
              This cannot be undone.
            </Popconfirm.Description>
            <div className="actions">
              <Popconfirm.Cancel className="button">
                Cancel
              </Popconfirm.Cancel>
              <Popconfirm.Confirm className="button danger">
                Delete
              </Popconfirm.Confirm>
            </div>
          </Popconfirm.Popup>
        </Popconfirm.Positioner>
      </Popconfirm.Portal>
    </Popconfirm.Root>
  )
}
Not measured yetTests passing in React
59.8 KiBSize
Not measured yetMemory
Not measured yetSpeed
[N] commits

Installation

npm i @hamur/react

Not on npm yet. The package goes out under the chosen name.

Anatomy

Import the component and assemble its parts:

<Popconfirm.Root>
  <Popconfirm.Trigger />
  <Popconfirm.Portal>
    <Popconfirm.Positioner>
      <Popconfirm.Popup>
        <Popconfirm.Arrow />
        <Popconfirm.Title />
        <Popconfirm.Description />
        <Popconfirm.Cancel />
        <Popconfirm.Confirm />
      </Popconfirm.Popup>
    </Popconfirm.Positioner>
  </Popconfirm.Portal>
</Popconfirm.Root>

Examples

Async

onConfirm returns a promise: Confirm shows data-pending until it settles, and Cancel and Escape still close the popup.

PopconfirmAsync.tsx
import * as React from 'react'
import { Popconfirm } from '@hamur/react/popconfirm'

export default function PopconfirmAsyncExample() {
  const [status, setStatus] = React.useState('Nothing deleted yet')
  const remove = () =>
    new Promise<void>((resolve) => {
      setTimeout(() => {
        setStatus('Deleted')
        resolve()
      }, 1200)
    })
  return (
    <div className="stack">
      <Popconfirm.Root onConfirm={remove}>
        <Popconfirm.Trigger className="button danger">
          Delete
        </Popconfirm.Trigger>
        <Popconfirm.Portal>
          <Popconfirm.Positioner sideOffset={8}>
            <Popconfirm.Popup className="popover">
              <Popconfirm.Arrow className="arrow" />
              <Popconfirm.Title className="popover-title">
                Delete this item?
              </Popconfirm.Title>
              <Popconfirm.Description className="muted">
                Deleting takes a moment. You can still cancel.
              </Popconfirm.Description>
              <div className="actions">
                <Popconfirm.Cancel className="button">
                  Cancel
                </Popconfirm.Cancel>
                <Popconfirm.Confirm className="button danger">
                  Delete
                </Popconfirm.Confirm>
              </div>
            </Popconfirm.Popup>
          </Popconfirm.Positioner>
        </Popconfirm.Portal>
      </Popconfirm.Root>
      <p className="muted">{status}</p>
    </div>
  )
}

API reference

35 props across 10 parts, in React syntax. className, style, render are listed once, under Shared props.

ArrowPopconfirm.Arrow

Displays an element positioned against the popconfirm anchor. Renders a <div>.

Arrow data attributes
Data attributeDescription
data-openPresent when the popup is open.
data-closedPresent when the popup is closed.
data-sideIndicates which side the popup is positioned relative to the trigger.
data-alignIndicates how the popup is aligned relative to specified side.
data-uncenteredPresent when the popconfirm arrow is uncentered.

CancelPopconfirm.Cancel

A button that closes the popconfirm without confirming. It is focused when the popup opens, and stays usable while a confirm is pending. Renders a <button>.

Cancel props
PropTypeDefault
nativeButtonWhether the component renders a native <button> element when replacing itbooleantrue
disabledWhether the button ignores presses.boolean—
Cancel data attributes
Data attributeDescription
data-disabledPresent when the button is disabled.
data-pendingPresent while the promise returned by onConfirm has not settled.

ConfirmPopconfirm.Confirm

A button that calls the root's onConfirm and closes the popconfirm. While the promise onConfirm returned is pending, presses are ignored and it has aria-disabled. Renders a <button>.

Confirm props
PropTypeDefault
nativeButtonWhether the component renders a native <button> element when replacing itbooleantrue
disabledWhether the button ignores presses.boolean—
Confirm data attributes
Data attributeDescription
data-disabledPresent when the button is disabled.
data-pendingPresent while the promise returned by onConfirm has not settled.

DescriptionPopconfirm.Description

A paragraph that describes what the confirmed action does. Renders a <p>.

PopupPopconfirm.Popup

A container for the popconfirm contents, rendered as an alertdialog. Renders a <div>.

Popup props
PropTypeDefault
initialFocusDetermines the element to focus when the popconfirm is opened.boolean | React.RefObject<HTMLElement | null> | ((openType: InteractionType) => boolean | void | HTMLElement | null)—
finalFocusDetermines the element to focus when the popconfirm is closed.boolean | React.RefObject<HTMLElement | null> | ((closeType: InteractionType) => boolean | void | HTMLElement | null)—
Popup data attributes
Data attributeDescription
data-openPresent when the popup is open.
data-closedPresent when the popup is closed.
data-starting-stylePresent when the popup begins animating in.
data-ending-stylePresent when the popup is animating out.
data-sideIndicates which side the popup is positioned relative to the trigger.
data-alignIndicates how the popup is aligned relative to specified side.
data-instantPresent if animations should be instant.
data-pendingPresent while the promise returned by onConfirm has not settled.
Popup CSS variables
CSS variableDescription
--popup-widthThe width of the popup.
--popup-heightThe height of the popup.

PortalPopconfirm.Portal

A portal element that moves the popup to a different part of the DOM. By default, the portal element is appended to <body>. Renders a <div>.

Portal props
PropTypeDefault
keepMountedWhether to keep the portal mounted in the DOM while the popup is hidden.booleanfalse
containerA parent element to render the portal element into.HTMLElement | ShadowRoot | React.RefObject<HTMLElement | ShadowRoot | null> | null—

PositionerPopconfirm.Positioner

Positions the popconfirm against the trigger. Renders a <div>.

Positioner props
PropTypeDefault
anchorAn element to position the popup against.Element | VirtualElement | React.RefObject<Element | null> | (() => Element | VirtualElement | null) | null—
positionMethodDetermines which CSS position property to use."absolute" | "fixed"'absolute'
sideWhich side of the anchor element to align the popup against.Side'bottom'
sideOffsetDistance between the anchor and the popup in pixels.number | OffsetFunction0
alignHow to align the popup relative to the specified side.Align'center'
alignOffsetAdditional offset along the alignment axis in pixels.number | OffsetFunction0
collisionBoundaryAn element or a rectangle that delimits the area that the popup is confined to.Boundary'clipping-ancestors'
collisionPaddingAdditional space to maintain from the edge of the collision boundary.Padding5
stickyWhether to maintain the popup in the viewport afterbooleanfalse
arrowPaddingMinimum distance to maintain between the arrow and the edges of the popup.number5
disableAnchorTrackingWhether to disable the popup from tracking any layout shift of its positioning anchor.booleanfalse
collisionAvoidanceDetermines how to handle collisions when positioning the popup.CollisionAvoidance—
Positioner data attributes
Data attributeDescription
data-openPresent when the popup is open.
data-closedPresent when the popup is closed.
data-anchor-hiddenPresent when the anchor is hidden.
data-sideIndicates which side the popup is positioned relative to the trigger.
data-alignIndicates how the popup is aligned relative to specified side.
Positioner CSS variables
CSS variableDescription
--available-widthThe available width between the trigger and the edge of the viewport.
--available-heightThe available height between the trigger and the edge of the viewport.
--anchor-widthThe anchor's width.
--anchor-heightThe anchor's height.
--transform-originThe coordinates that this element is anchored to. Used for animations and transitions.
--positioner-widthThe width of the popconfirm's positioner. It is important to set width to this value when using CSS to animate size changes.
--positioner-heightThe height of the popconfirm's positioner. It is important to set height to this value when using CSS to animate size changes.

RootPopconfirm.Root

Groups all parts of the popconfirm. Doesn’t render its own element.

Root props
PropTypeDefault
defaultOpenWhether the popconfirm is initially open.booleanfalse
openWhether the popconfirm is currently open.boolean—
onOpenChangeEvent handler called when the popconfirm is opened or closed.((open: boolean, eventDetails: PopconfirmRootChangeEventDetails) => void)—
onOpenChangeCompleteEvent handler called after any animations complete when the popconfirm is opened or closed.((open: boolean) => void)—
onConfirmCalled when Confirm is pressed.((details: PopconfirmRoot.ConfirmDetails<Payload>) => unknown)—
actionsRefA ref to imperative actions.React.RefObject<PopconfirmRootActions | null>—
modalDetermines if the popconfirm enters a modal state when open.boolean | "trap-focus"'trap-focus'
triggerIdID of the trigger that the popconfirm is associated with.string | null—
defaultTriggerIdID of the trigger that the popconfirm is associated with.string | null—
handleA handle to associate the popconfirm with a trigger.PopconfirmHandle<Payload>—
childrenThe content of the popconfirm.React.ReactNode | PayloadChildRenderFunction<Payload>—

TitlePopconfirm.Title

A heading that labels the popconfirm. Renders a <h2>.

TriggerPopconfirm.Trigger

A button that opens the popconfirm when pressed. Renders a <button>.

Trigger props
PropTypeDefault
nativeButtonWhether the component renders a native <button> element when replacing itbooleantrue
handleA handle to associate the trigger with a popconfirm.PopconfirmHandle<Payload>—
payloadA payload to pass to the popconfirm when it is opened.Payload—
idID of the trigger.string—
Trigger data attributes
Data attributeDescription
data-popup-openPresent when the corresponding popconfirm is open.
data-pressedPresent when the trigger is pressed.
data-disabledPresent when the trigger is disabled.

Shared props

Every part that renders an element takes these.

Shared props
PropType
classNameCSS class applied to the element, or a function that returns a class based on the component's state.string | ((state: State) => string | undefined)
styleStyle applied to the element, or a function that returns a style object based on the component's state.React.CSSProperties | ((state: State) => React.CSSProperties | undefined)
renderAllows you to replace the component's HTML element with a different tag, or compose it with another component.React.ReactElement<unknown, string | React.JSXElementConstructor<any>> | ComponentRenderFn<HTMLProps, State>

Keyboard and accessibility

Keyboard

Keyboard
KeysDoesPart
EnterSpaceOpen or close the popconfirm; press Cancel or Confirm when focusedTrigger, buttons
TabShiftTabMove between Cancel and Confirm; focus stays inside and wrapsPopup
EscapeCloses the popconfirm and returns focus to the Trigger, also while pendingPopup

Accessibility

What the parts set
PartSetsSo that
Triggeraria-haspopup="dialog", aria-expanded, aria-controlsa screen reader says the button opens a dialog and whether it is open
Popuprole="alertdialog", tabindex="-1"the popup is announced as an alert that needs an answer
Popuparia-labelledby to Title, aria-describedby to Descriptionthe question and its consequence are read when it opens
Confirmaria-disabled="true" and data-pending while pendingthe button keeps focus while it waits, which the native disabled would drop

Focus order

  1. Opening moves focus to Cancel, or to the Popup when there is no Cancel, or to initialFocus.
  2. Tab and Shift+Tab stay inside the popup and wrap at either end. Cancel comes first, then Confirm.
  3. Closing returns focus to the Trigger that opened it, or to finalFocus.