Question presented to candidate: "Build a reusable tooltip. It should appear when you hover over a button or focus it with the keyboard, accept a preferred side (top, right, bottom or left), and never end up off screen: if there is no room on the preferred side, flip it, and if it would overflow sideways, slide it back in."
Requirements:
- Show on hover after a short delay (300 ms), immediately on keyboard focus; hide on mouse leave after a short delay (100 ms), on blur and on Escape.
- The pointer can move from the trigger onto the tooltip without it disappearing.
- Placement prop:
top,right,bottom,left, with an 8 px gap from the trigger. - Flip to the opposite side when the preferred side does not fit; shift along the other axis to stay 8 px inside the viewport.
- Rendered in a portal with
position: fixed; follows the trigger on scroll and resize. role="tooltip"andaria-describedbyon the trigger.
What a strong answer should cover:
- Positioning as a pure function of the trigger rect, the tooltip size, the placement and the viewport: easy to reason about and to test.
- Measuring after render but before paint, so the tooltip never flashes at the wrong place.
- Why a portal and
position: fixed(clipping and stacking contexts), and how scroll in any container is caught (capture phase). - Hover and focus parity, delays with a single timer, and WCAG 1.4.13 (dismissible with Escape, hoverable, persistent).
- When to reach for a library such as Floating UI, and the CSS anchor positioning alternative.
Clarifying questions expected:
- "Plain text only, or rich interactive content? (Interactive content is a popover, not a tooltip.)"
- "Should touch devices show it, and how?"
- "Do we need an arrow pointing at the trigger?"
Code / implementation expected: Yes. A positioning function, a reusable tooltip and a demo with each placement plus a flip and a shift case.