Skip to content
easeweb
Search topics

Search panels that close on Tab and drop keyboard focus

The search panel closes as soon as its field loses focus, ignores Escape, and hides the control that had focus.

WCAG 2.2
SC 2.1.1ASC 2.4.3ASC 4.1.2A
Required by
Section 508 · EN 301 549 (EU)
Broken input.addEventListener('blur', close)
The accessibility problem illustrated: Motor:Where did the results go?
01

The problem

Keyboard users cannot reach the search results, cannot close the panel with Escape, and lose their place when it closes, and screen reader users aren't told whether the panel is open

02

Try it

Open Search, type, then press Tab to reach a result

Can you reach both lamps and the close button? Try Escape too, and watch where focus ends up. Does Search say whether the panel is open?

Broken Can you fix this?

BrokenA panel that closes whenever the field loses focus

Focus: –

A recreated demo. The broken version is intentionally inaccessible.

03

The fix

Close the panel when asked, and return focus to its button

Before
BrokenA panel that closes whenever the field loses focus
<button type="button" id="search-trigger">Search</button><div id="search-panel" hidden>  <label for="search-input">Search products</label>  <input id="search-input" type="search">  <ul>    <li>      <a href="/lamps/arc">Arc floor lamp</a>    </li>    <li>      <a href="/lamps/dome">Dome table lamp</a>    </li>  </ul>  <button type="button" id="search-close">Close</button></div><button type="button">Cart</button>
What is wrong5 points
  • Keyboard users can't pick a result: Tab from the field closes the panel before the result can be used.
  • The close button can't be reached from the keyboard at all: the panel closes before Tab gets that far.
  • Escape doesn't close the panel: nothing listens for it.
  • After it closes, a keyboard user can't tell where focus is: nothing returns it to Search.
  • A screen reader user isn't told whether the panel opened: the button has no aria-expanded.
After

A disclosure that closes when asked

<div id="search">  <button type="button" id="search-trigger" aria-expanded="false" aria-controls="search-panel">Search</button>  <div id="search-panel" tabindex="-1" hidden>    <label for="search-input">Search products</label>    <input id="search-input" type="search">    <ul>      <li>        <a href="/lamps/arc">Arc floor lamp</a>      </li>      <li>        <a href="/lamps/dome">Dome table lamp</a>      </li>    </ul>    <button type="button" id="search-close">Close search</button>  </div></div><button type="button">Cart</button>
How it is fixed5 steps
  1. Wrap the button and the panel in one #search, with the panel right after the button in the markup. Tab then goes from the button to the field, the results, the close button and then Cart.
  2. Give Search aria-expanded and aria-controls, and update aria-expanded as the panel opens and closes, so Search says whether its panel is open.
  3. Close the panel on Escape, from Close search and from Search, and put focus back on Search.
  4. Replace the field's blur timer with a focusout listener on the whole component. Moving focus inside it keeps the panel open, and Tab or Shift + Tab out of it closes the panel without moving focus anywhere else.
  5. Put tabindex="-1" on the panel, so a click on its blank space keeps focus inside and doesn't close it.

This fixes the Before and passes 2.1.1, 2.4.3 and 4.1.2.

04

Verify the fix

5 checks, no mouse.

Test with: Keyboard, Screen reader

0 of 5 checked

Record the OS, browser and assistive-technology versions, the build, the date, and the actual result of each step. Repeat after the shared header or search component changes.

How screen readers handle aria-expanded

Limits

This is a starting pattern for a non-modal search panel, not for comboboxes, menus or dialogs. A chat panel that opens over the page has the same closing to get right, and replies to announce as well: see chat widgets that never announce their messages. A panel placed at the end of the page, away from its button, is a different failure of 2.4.3: see floating buttons and panels that keyboard focus reaches last. The snippets omit fetching and announcing results. Test the complete journey, including your real results list, with your target assistive technology.

This is a recreated teaching example, not a finding about a named client. The code is a starting pattern: test it in your own product. No completed assistive-technology test is claimed here.

EaseWeb service

Fixed it? Keep it from coming back.

I turn guides like this one into rules your coding agents follow and checks your CI runs. Then I retest by hand with a keyboard and screen readers, periodically.

See the service