Frontend School

Build a Resilient & Accessible Popover Component

Machine Coding
hard

Build a Resilient & Accessible Popover Component

Hard
Frontend
React
UI
Tooltip
Popover
Machine Coding
Accessibility

Overview

Modern web applications demand highly interactive and accessible UI components. The Popover is a fundamental building block for many such interactions, including tooltips, dropdowns, and contextual menus. This problem challenges you to build a versatile Popover component that not only handles complex positioning logic to avoid viewport clipping but also ensures a robust and accessible user experience.

What you need to do / Task

Your primary task is to implement a reusable Popover React component with the following capabilities:

  1. Core Component: Create a Popover component that accepts trigger (a ReactNode) and content (a ReactNode) as props.
  2. Placement & Dynamic Positioning: Implement a placement prop ('top', 'bottom', 'left', 'right'). The popover should initially attempt to position itself according to this preference. If the preferred placement would cause the popover to extend beyond the viewport boundaries, it must dynamically adjust to an alternative, valid placement (e.g., if 'bottom' clips, try 'top'; if 'right' clips, try 'left').
  3. Interaction: The popover should open when the trigger element is clicked and close on a subsequent click of the trigger.
  4. Dismissal: The popover must close when:
    • The user clicks anywhere outside the trigger and the popover content itself.
    • The Escape key is pressed.
  5. Reusability & Composition: Demonstrate the versatility of your Popover component by creating a simple Tooltip component that leverages your Popover for hover-based interactions.

UI/UX expectations

  • The popover content should visually appear directly adjacent to its trigger element.
  • The popover and tooltip content should be clearly distinguishable from the background and other UI elements.
  • Accessibility: Ensure the component is accessible. This includes:
    • Correctly using ARIA attributes (e.g., aria-expanded, aria-haspopup, aria-controls, role) on the trigger and popover content.
    • Keyboard interaction for dismissal (Escape key).

Acceptance criteria

  • The Popover component is correctly implemented and renders its trigger and content.
  • The popover appears at the preferred placement (top, bottom, left, right) relative to the trigger.
  • The popover automatically repositions itself to stay within the viewport if the preferred placement would cause clipping.
  • Clicking the trigger toggles the popover's visibility (open/close).
  • Clicking anywhere outside the popover content or its trigger closes the popover.
  • Pressing the Escape key closes the popover.
  • The Tooltip component, built using Popover, correctly displays its content on hover and hides on hover out.
  • Appropriate ARIA attributes are used on both the trigger and the popover content to enhance accessibility.

Constraints

Constraints & assumptions

  • You are working within a React environment. You may use standard React hooks and features.
  • No external UI libraries (e.g., Material UI, Ant Design, Headless UI) are allowed for the core Popover logic. You can use basic CSS for styling.
  • The popover content can contain interactive elements (e.g., buttons, input fields).
  • Assume a standard browser environment. You do not need to support legacy browsers.
  • The Popover component should be designed for reusability.

Out of scope

  • Complex animations or transitions (a simple opacity transition is provided).
  • Deeply nested popovers or complex focus management within the popover content itself (beyond the Escape key handler).
  • Rendering the popover using ReactDOM.createPortal (position the popover as an absolutely positioned element relative to its container).
  • Adding a visual 'arrow' or 'caret' to the popover.

What interviewers evaluate

  • Component Design & Reusability: How well the Popover component is structured, its API, and its ability to be reused (e.g., for Tooltip).
  • Dynamic Positioning Logic: The accuracy and robustness of the calculations to position the popover and handle viewport clipping.
  • Event Handling: Correct and efficient implementation of click-outside and keyboard event listeners.
  • Accessibility: Thoughtful application of ARIA attributes and keyboard interactions for an inclusive user experience.
  • Code Quality: Readability, maintainability, and effective use of React patterns and hooks.

Loading editor...