Skip to content
FunnelGuard

Fix guide by FunnelsGuard

Keyboard trap in modal or widget: how to find and fix it

PRF-312High impactCould addBy FunnelGuardPublished Updated
On this page

What this issue is

A keyboard trap means a person using only the keyboard moves focus into a component and cannot move it out again. It fails WCAG 2.1.2 (No Keyboard Trap), a Level A requirement. Focus is the highlighted element that receives your key presses.

Not every focus lock is a bug. A modal dialog should keep Tab inside the dialog while it is open. It becomes a trap when there is no way to close it with the keyboard, or when focus loops inside something that is not meant to hold it.

Intentional focus containment vs a keyboard trap
SituationTab stays inside?Way out by keyboard?Verdict
Modal with close button and Escape supportYesYesCorrect
Modal with no close button reachable by Tab, no EscapeYesNoTrap
Embedded iframe form or chat widget that swallows TabYesNoTrap
Date picker or video player that ignores Tab and EscapeYesNoTrap
Popup that closes visually but leaves an invisible focus loopYesNoTrap

Why it costs you leads or sales

People who use a keyboard, a switch device or a screen reader cannot get past the trap. They cannot submit your lead form, reach the checkout button or close the newsletter popup that covers the page. Many of them simply leave.

Power users who prefer the keyboard hit the same wall. Traps also tend to appear on your highest-value steps: exit-intent popups, cookie banners, booking widgets, embedded forms and live chat.

Where a keyboard trap stops the funnel
  1. Landing pageVisitor tabs through the page
  2. Popup opensOffer modal appears
  3. Focus trappedTab loops, no close by keyboard
  4. FormNever reached
  5. Lead or saleVisitor leaves

A visitor using only the keyboard gets stuck in a popup and never reaches the form.

Keyboard visitors reaching the formExample numbers
Keyboard visitors reaching the form (example)With a trap: 10 %; After the fix: 85 %033651085With a trapAfter the fix%

Example: of 100 keyboard-only visitors who open a popup, how many reach the form with and without a trap. Numbers are illustrative.

How to check if your site has it

Test for a keyboard trap by hand
  1. Put the mouse aside

    Open the page and use only the keyboard. Press Tab to move forward and Shift+Tab to move back.

  2. Open every popup and widget

    Trigger the modal, cookie banner, exit-intent offer, chat bubble and booking widget with Enter or Space on their buttons.

  3. Try to leave with Escape

    Press Escape. A modal should close. If it does not, press Tab repeatedly and look for a close button that receives a visible focus ring.

  4. Check where focus goes next

    After closing, focus should return to the button that opened the modal. If it jumps to the top of the page or disappears, see PRF-315.

  5. Test embedded iframes

    Tab into any embedded form, map, video or chat. Make sure Tab eventually moves focus out to the next element on the page.

  6. Repeat on mobile layout

    Narrow your browser window or use a device with a keyboard attached. Responsive popups often have different markup.

How to fix it

The goal is simple: whatever opens with the keyboard must close with the keyboard, and focus must be able to leave. For modals, keep focus inside while open, then release it on close.

Fix by platform
  1. Identify the source

    Disable popup, chat and cookie plugins one at a time on a staging copy and retest with Tab to find which one traps focus.

  2. Check plugin settings

    In the popup plugin, enable options such as close on Escape and show a close button. Update the plugin, as accessibility fixes are common in newer versions.

  3. Replace if needed

    If the plugin cannot be fixed, switch to one that uses a proper dialog and ask the vendor about WCAG 2.1.2.

Simplest fix: native dialog elementhtml
<button id="open-offer">Get the offer</button>

<dialog id="offer" aria-labelledby="offer-title">
  <h2 id="offer-title">Get 10% off</h2>
  <form method="dialog">
    <label for="email">Email</label>
    <input id="email" type="email" autocomplete="email">
    <button type="submit">Send</button>
    <button type="submit" value="cancel" formnovalidate>Close</button>
  </form>
</dialog>

<script>
  const dlg = document.getElementById('offer');
  document.getElementById('open-offer').addEventListener('click', () => dlg.showModal());
</script>
Custom modal: Escape to close and return focusjavascript
const modal = document.querySelector('#my-modal');
const closeBtn = modal.querySelector('.close');
let opener = null;

function openModal(trigger) {
  opener = trigger;
  modal.hidden = false;
  closeBtn.focus();
}

function closeModal() {
  modal.hidden = true;
  if (opener) opener.focus();
}

modal.addEventListener('keydown', (e) => {
  if (e.key === 'Escape') {
    closeModal();
    return;
  }
  if (e.key !== 'Tab') return;
  const items = modal.querySelectorAll('a[href], button:not([disabled]), input:not([disabled]), select, textarea, [tabindex]:not([tabindex="-1"])');
  const first = items[0];
  const last = items[items.length - 1];
  if (e.shiftKey && document.activeElement === first) {
    e.preventDefault();
    last.focus();
  } else if (!e.shiftKey && document.activeElement === last) {
    e.preventDefault();
    first.focus();
  }
});

closeBtn.addEventListener('click', closeModal);

How to confirm it's fixed

Retest with the keyboard only
  • Every popup, drawer and widget opens with Enter or Space.
  • Escape closes each modal.
  • A visible close button can be reached with Tab and activated with Enter.
  • Tab and Shift+Tab move through the modal in a logical order without leaving it while open.
  • After closing, focus returns to the element that opened the modal.
  • Embedded iframes release focus to the next element on the page.
  • A screen reader announces the dialog title when it opens (try NVDA, VoiceOver or TalkBack).
  • The form inside the modal can be submitted using only the keyboard.

How to prevent it happening again

Traps usually return when someone installs a new popup, chat or booking tool, or updates a theme. Add a keyboard test to your release routine.

Habits that keep traps out
  • Tab through the page and every popup after each plugin, app or theme update.
  • Prefer the native dialog element or a well-maintained, accessibility-tested component.
  • Ask vendors of chat and booking widgets whether they meet WCAG 2.1.2 before you install.
  • Run an automated accessibility scan such as axe DevTools or Lighthouse, then follow up by hand, since automated tools miss many traps.
  • Include keyboard-only checks in your QA list for every new campaign popup or landing page.

How FunnelGuard handles this

FunnelGuard does not check for keyboard traps yet. It is a possible future check, so you should not rely on it for this issue today.

What FunnelGuard does run is your real customer journeys on a schedule: it fills in lead forms, goes through checkouts and booking steps, and checks the tracking hits and whether test leads reach your inbox and CRM. If a broken popup or widget stops that robot from completing a journey, you will see the journey fail, with a screenshot and a likely cause. That is not the same as detecting a keyboard trap, because the robot is not testing keyboard navigation.

To watch for traps, use the manual Tab test above after every change, and run axe DevTools or Lighthouse in your browser. You can also start with the free FunnelGuard plan (1 site, 2 journeys, daily checks) to be told when a key journey stops working.

Questions people ask

What is a keyboard trap in accessibility?

A keyboard trap is when a person using only the keyboard can move focus into a component, such as a modal or widget, but cannot move it out again. It fails WCAG 2.1.2 (No Keyboard Trap), a Level A requirement. Users are stuck unless they use a mouse.

Why can't I tab out of a popup on my website?

The popup probably has no close button that can receive focus, ignores the Escape key, or runs a script that sends focus back inside whenever you press Tab. Check the popup plugin or custom code for Escape handling and a real button as the close control.

Is it a keyboard trap if Tab stays inside a modal?

Not if the modal can be closed with the keyboard. Keeping Tab inside an open modal dialog is expected behaviour. It is only a trap when there is no keyboard way out, such as Escape or a focusable close button.

How do I test for a keyboard trap?

Use only the keyboard. Press Tab and Shift+Tab through the page, open each popup, chat and embedded form, then try Escape and the close button. Confirm that focus can leave every component and returns to the opener when the modal closes.

How do I fix a chat widget that traps keyboard focus?

Check for an update or accessibility setting from the chat vendor, since you usually cannot edit their iframe. If it still traps focus, report it to the vendor or switch to a widget that lets Tab and Escape move focus out.

Do accessibility scanners find keyboard traps?

Only partly. Tools like axe and Lighthouse catch many issues but cannot reliably detect traps that depend on how a script handles keys. A manual Tab test is still needed.

Does FunnelGuard detect keyboard traps?

No. FunnelGuard does not check for keyboard traps yet, though it is a possible future check. It does run your real forms and checkouts on a schedule, so a popup that blocks the journey would show up as a failed journey.

Does FunnelGuard catch this?

Could add

Not checked yet: could be added to FunnelGuard.

Stop finding out from your customers

FunnelGuard runs your real forms and checkouts on a schedule, checks the tracking and the leads behind them, and alerts you once, with a screenshot. Try any paid plan free for 3 days.