KatInterview

Single-Open Async Tooltip Panels with Keyboard Support in React

Data Structure DesignModel answer
MediumModel answerData Structure Designreactaccessibilityrace-conditions

Last updated

Design a React popover that fetches data when opened, stays exclusive across many triggers, ignores stale responses, and handles focus, Esc, and outside clicks accessibly.

Build a Fetch-on-Open Popover

Your page has several buttons. Each one opens a small floating panel that loads its own data when the panel opens. Implement a reusable React component with this signature:

function Popover({ triggerId, fetchData }) {
  // triggerId: unique string identifying this popover and its button
  // fetchData: function that returns a Promise with the content to show
}

What it must do

  1. Accessibility. Use suitable ARIA attributes (for example aria-expanded, aria-describedby, aria-busy). Keyboard focus must be handled sensibly when the panel opens, either trapped inside it or moved in a deliberate way.
  2. Loading and result states. Show a loading indicator while the request is pending, then render the data on success.
  3. Stale-response protection. If the user opens one popover, quickly opens another, then returns to the first, the first must display the result of its most recent request. A slow earlier response must never overwrite it.
  4. Exclusive opening. Only one popover may be open at any moment, even though many triggers exist.
  5. Anchored placement. The panel appears next to the button that opened it. Simple absolute positioning is fine, or explain how you would use a positioning library such as Floating UI.
  6. Dismissal. Pressing Esc or clicking outside the panel closes it.
  7. Failures. If fetchData rejects (or throws), handle it gracefully.

Behavior examples

  • Clicking "Load User A" shows a panel at the button's position with a spinner, which is replaced by the user profile once data arrives.
  • Pressing Tab reaches the button and Enter opens the panel. Pressing Esc closes it and focus returns to the button.

What to deliver

Write the component (plus any helper hook or context you need) and explain your key decisions: where shared state lives, how you discard stale results, how focus is handled, and which edge cases you considered. State any assumptions, such as whether the component renders its own trigger button.

Hints

Hint 1

Which piece of state must be shared among all popovers to guarantee only one is open? Consider a single `openId` in a context, with each popover open when it equals its own `triggerId`.

Hint 2

Tag each request with an incrementing counter (and optionally an AbortController). Only commit a response if its tag still equals the latest counter value; bump the counter on close and unmount.

Hint 3

Think about the closing paths separately: Esc should return focus to the trigger, while an outside click should not steal focus. Make the outside-click handler ignore clicks on the trigger itself to avoid a close-then-reopen double toggle.