Skip to content

shadcn/ui Dialog focus trap and auto-focus fixes

Stop shadcn/ui Dialog from auto-focusing the close button or mobile keyboard, fix focus return from DropdownMenu, and handle third-party portals.

Updated 3 December 2026Tested with
  • shadcn 4.23.0
  • Tailwind CSS 4.3.5
  • Vite 8
  • React 19.3

shadcn/ui Dialog traps keyboard focus and automatically focuses the first tabbable element when it opens. That is great for accessibility, until it auto-focuses the Close X button with a bright focus ring, opens the mobile keyboard on an input you didn't want focused, or blocks clicks on a third-party date picker or Stripe element.

Three common focus issues and exact fixes

  1. Step 1: Stop the Dialog from auto-focusing the first input or close button

    Use onOpenAutoFocus={(e) => e.preventDefault()} on DialogContent, or move focus explicitly to a heading or primary field:

    dialog-autofocus.tsxTSX
    <DialogContent onOpenAutoFocus={(e) => e.preventDefault()}>  <DialogHeader>    <DialogTitle>Confirm delete</DialogTitle>  </DialogHeader></DialogContent>
  2. Step 2: Control where focus returns on close

    When a dialog opens from a DropdownMenuItem, closing the dialog tries to return focus to the now-unmounted menu item, jumping focus back to <body>. Use onCloseAutoFocus to focus the dropdown trigger button instead:

    dialog-close-focus.tsxTSX
    <DialogContent  onCloseAutoFocus={(e) => {    e.preventDefault();    triggerRef.current?.focus();  }}>
  3. Step 3: Third-party portals (Stripe, Google Places, TinyMCE) can't be focused

    Radix's modal focus trap pulls focus back inside DialogContent whenever a click lands on a portal appended directly to <body>. Either mount the third-party popover inside the dialog container, or pass modal={false} on <Dialog modal={false}> if you render your own backdrop.

Questions

Why can't I scroll a Combobox inside a Dialog?

That is a related Radix scroll-lock + focus-trap conflict. Set modal={true} on the inner Popover; see shadcn/ui Combobox not scrolling inside a Dialog.

Is disabling onOpenAutoFocus bad for accessibility?

Only if keyboard users lose track of where they are. If you prevent auto-focus on an input, keep the dialog itself focusable (tabIndex={-1}) or move focus to the dialog's primary action so screen readers still announce the modal.

Was this page helpful?

Still stuck? We answer every message ourselves. Write to us

Building with shadcn/ui and Tailwind CSS v4?

Install 188 free blocks and components with the shadcn CLI (no account), preview them in your own brand in Studio, or get all 1,570 blocks and every theme in UI Pro (€199 once).

Use it with

This page is built from UI Pro parts: the docs article block (docs-quernly) with the code, terminal, install command, steps, contents and feedback components, included in UI Pro.