Back

Tooltip

easy

Streak

0 days

Progress

0%

Submitted

0

Tooltip

React45 mineasyFree

Prompt

Build Tooltip({ label, text, position = "top" }) in 45 minutes. The component owns one native button labelled by label and shows short, noninteractive help text. Support top, bottom, left, and right placement.

  • Open on trigger hover or keyboard focus. Stay open while the trigger is focused or the pointer is over either trigger or tooltip.
  • Allow a short 120ms close delay so the pointer can cross the gap. Cancel that delay on re-entry.
  • Escape dismisses without moving focus. Do not reopen during the same focus/hover engagement.
  • Connect aria-describedby on the actual button to a unique tooltip ID.
  • Portal to document.body, position from viewport rectangles, flip to the opposite side when needed, and clamp inside the viewport. Reposition on scroll and resize.
  • Remove listeners and pending timers on close/unmount.

The demo includes an overflow-hidden ancestor. Keep help text short enough to fit the viewport. Interactive popovers, disabled triggers, arbitrary child elements, animation, and top-layer dialogs are follow-ups.

Learn and continue

Prerequisite: Layout measurement and effects. Work through the React Machine Coding Foundations path to practice related skills in order.

Requirements

  • →Hover and keyboard focus independently open the same tooltip
  • →Mouse leaving a focused trigger does not close it
  • →Crossing the gap to the tooltip within 120ms cancels delayed close
  • →Escape closes without moving focus and prevents reopening until engagement ends
  • →The actual button describes the visible role=tooltip element; two instances have unique IDs
  • →Each placement flips/clamps at viewport edges and follows scroll/resize
  • →Unmount removes listeners and any pending close timer

Starter packet

What the interviewer handed you

Complete the supplied tooltip component using the prompt contract. The shell and fixtures are provided; TODOs identify the behavior to implement.

Manual acceptance checks

  • Hover and keyboard focus independently open the same tooltip
  • Mouse leaving a focused trigger does not close it
  • Crossing the gap to the tooltip within 120ms cancels delayed close
  • Escape closes without moving focus and prevents reopening until engagement ends
  • The actual button describes the visible role=tooltip element; two instances have unique IDs
  • Each placement flips/clamps at viewport edges and follows scroll/resize
  • Unmount removes listeners and any pending close timer

Likely follow-ups

  • • When is a dialog better than a tooltip?
  • • How do you avoid clipping inside an overflow container?
Example
Loading preview...
For the best coding experience, we recommend using a desktop device.
Preparing Sandbox...
Premium interview report

What interviewers score in this build

Use this before reading the code. It tells you what to say, what to test, and where machine-coding candidates usually lose points.

Interview signals

  • Core behavior: Hover and keyboard focus independently open the same tooltip
  • State and identity: Open state follows hover or focus; Escape dismisses the current engagement, and geometry needs its own lifecycle.
  • Accessibility: Use the documented native keyboard behavior, meaningful names, and visible focus.
  • Edge cases: Demonstrate: Hover plus focus, Rapid leave/re-entry, Escape while focused, Two instances, Overflow-hidden ancestor, Nested scroll and viewport edges, Unmount during delayed close.

Time checkpoints

  1. 1

    0:00: Read the contract and walk through the fixture.

  2. 2

    5:00: Define state, identity, and keyboard behavior.

  3. 3

    35:00: Finish core interactions and lifecycle cleanup.

  4. 4

    40:00: Run the acceptance checks and edge cases.

Edge-case checklist

Hover plus focus
Rapid leave/re-entry
Escape while focused
Two instances
Overflow-hidden ancestor
Nested scroll and viewport edges
Unmount during delayed close

Common mistakes

  • Putting aria-describedby only on a wrapper
  • Closing on mouseleave while focus remains
  • Using pointer-events:none on a hoverable popup
  • Assuming a portal solves every stacking/geometry issue
  • Leaking delayed timers or scroll listeners
SolutionRead-only · Live Preview

Technical Explanation

Use an explicit trigger API

The component owns a native button, so aria-describedby and focus events belong to the element receiving focus. A generic wrapper around arbitrary children does not ensure that relationship. Supporting arbitrary triggers later needs a deliberate ref and event-composition API.

Track reasons to remain open

Trigger hover, tooltip hover, and trigger focus are separate engagement reasons. Leaving the button with the mouse must not hide help while the button is still focused. Close only after all reasons end, using a 120ms delay to bridge the pointer gap; entering either surface cancels that delay.

Make Escape persistent

Listen for Escape only while open. Dismiss the tooltip, retain button focus, and remember that the current engagement was dismissed. Reset that guard only once hover and focus have ended. Otherwise a fresh mouse-enter event can reopen help that the user just closed.

Portal and position are separate jobs

Portalling to document.body escapes ordinary clipping by the demo ancestor. It does not solve every stacking context or place content above a modal top layer. Fixed positioning uses getBoundingClientRect coordinates directly. Measure both rectangles, try the preferred side, use its opposite if it does not fit, and clamp the final coordinates. Constrain text width so it can wrap.

Keep geometry current and clean up

A layout effect measures before painting. Scroll events from nested containers are observed with capture; resize also recalculates placement. Remove both listeners when closed and clear delayed-close work on unmount. Each instance uses useId so descriptions do not collide.

Check combined interactions

Focus the button, move the mouse away, and confirm help remains. Move from trigger to popup before the delay expires. Press Escape: focus stays and the popup disappears. Fully leave and blur, then return to reopen. Test all four placements near viewport edges and scroll an ancestor while open.

Interview Criteria

Core behavior

Hover and keyboard focus independently open the same tooltip

State and identity

Open state follows hover or focus; Escape dismisses the current engagement, and geometry needs its own lifecycle.

Accessibility

Use the documented native keyboard behavior, meaningful names, and visible focus.

Edge cases

Demonstrate: Hover plus focus, Rapid leave/re-entry, Escape while focused, Two instances, Overflow-hidden ancestor, Nested scroll and viewport edges, Unmount during delayed close.

Reasoning

Explain the state model and the tradeoff behind the interaction contract.

Time Checkpoints

0:00

0:00: Read the contract and walk through the fixture.

5:00

5:00: Define state, identity, and keyboard behavior.

35:00

35:00: Finish core interactions and lifecycle cleanup.

40:00

40:00: Run the acceptance checks and edge cases.

45:00

45:00: Explain tradeoffs and follow-up scope.

Streak

0 days

Last active: Sign in to track

Progress

0%

0/0 solved

Submitted

0

Solutions pushed to review history.

More Machine Coding

View all Machine Coding →
Machine CodingMedium

React Tic-Tac-Toe Engine

Machine CodingMedium

Performant Image Slider

Machine CodingMedium

API Search and Render Image

Machine CodingEasy

Accessible React Tabs

Machine CodingMedium

Recursive File Explorer

Machine CodingEasy

Stopwatch

Related hubs

  • All machine coding
  • Frontend machine coding guide
  • React machine coding questions
  • UI coding interview questions
  • React UI interview questions
  • Airbnb interview
  • Atlassian interview
  • Stripe interview

FAQ

What does a Tooltip question evaluate?

Trigger behavior (hover/focus), positioning, accessible naming, and not trapping keyboard users inside ephemeral UI.

Common pitfalls?

Relying only on hover, missing focus/blur handling, and poor contrast or clipped overflow positioning.

Related practice?

Modal system, accordion, and tabs.