Frontend School

Dynamic HoverCard Component with Adaptive Positioning

Machine Coding
hard

Dynamic HoverCard Component with Adaptive Positioning

Hard
Frontend
React
UI
Tooltip
Popover
Accessibility

Overview

Tesla's vehicle software and internal tools demand highly intuitive and responsive user interfaces. Components like tooltips and popovers are critical for conveying information efficiently without cluttering the main display. This problem challenges you to build a HoverCard component that intelligently adapts its display based on user interaction duration and available screen space, ensuring a seamless and accessible user experience.

What you need to do / Task

Your goal is to implement a reusable HoverCard React component that wraps any child element. This component should display contextual information in two distinct modes: a brief tooltip and a more detailed popover.

  1. Basic Interaction:

    • When the wrapped children element is hovered over, a simple textual tooltip should appear after a short delay (e.g., 100ms).
    • If the hover continues for a longer duration (e.g., 500ms total from initial hover), the tooltip should be replaced by a richer popover component.
    • The popover should also appear on focus (e.g., via keyboard navigation) or click of the children element.
    • Both the tooltip and popover should disappear when the mouse leaves the children element or the popover itself (if interactive), or when the children element loses focus.
  2. Adaptive Positioning:

    • The HoverCard should accept a position prop ('top', 'bottom', 'left', 'right') determining its preferred display location relative to the children.
    • Implement logic to detect if the chosen position would cause the tooltip/popover to go off-screen. If so, automatically adjust its position to the most suitable alternative (e.g., if 'top' is off-screen, try 'bottom', then 'left', etc.). Prioritize vertical over horizontal adjustments.
  3. Dismissal & Accessibility:

    • The popover must be dismissible by pressing the Escape key.
    • The popover should also dismiss when clicking outside of it.
    • Ensure the component is accessible:
      • Proper ARIA attributes for tooltips (aria-describedby, role="tooltip") and popovers (aria-haspopup, aria-expanded, role="dialog" with aria-labelledby).
      • Keyboard navigability (focus management).

UI/UX expectations

  • Visual Distinction: The tooltip should be visually distinct from the popover (e.g., simpler styling, less padding).
  • Transitions: While not strictly required, a subtle fade-in/fade-out transition for visibility changes would enhance the UX.
  • Hover Delays: Implement distinct delays for initial tooltip appearance and subsequent popover appearance.
  • Popover Interaction: The popover content itself can be interactive (e.g., contain buttons or links), and hovering over it should keep it open.
  • Z-index: Ensure the tooltip/popover appears above other content.

Acceptance criteria

  • A HoverCard component is implemented that wraps children.
  • Short hover on children displays a simple tooltip.
  • Longer hover (or click/focus) on children replaces the tooltip with a detailed popover.
  • Tooltip/popover hide on mouse leave/blur.
  • Popover automatically repositions itself if it would go off-screen based on the position prop.
  • Popover can be dismissed by pressing Escape.
  • Popover can be dismissed by clicking outside of it.
  • Appropriate ARIA attributes are used for accessibility.
  • Keyboard focus on the trigger activates the popover.

Constraints

Constraints & assumptions

  • Environment: React application (functional components, hooks).
  • Libraries: Do not use any third-party tooltip/popover libraries. Implement the core logic from scratch. You may use utility libraries like lodash if necessary for debouncing/throttling, but it's not required.
  • Styling: Use plain CSS (via /styles.css or inline styles) for styling.
  • Browser Support: Assume a modern browser environment (Chrome, Firefox, Safari latest versions).
  • Viewport: Assume a standard desktop viewport. Off-screen detection should consider window.innerWidth and window.innerHeight.
  • Data: Mock data for popover content can be hardcoded within /App.tsx or /HoverCard.tsx.

Out of scope

  • Complex animations beyond simple opacity changes.
  • Full design system integration or theming.
  • Support for nested HoverCard components (focus on a single instance).
  • Server-side rendering (SSR) considerations.

What interviewers evaluate

  • Component Design & Reusability: How well the HoverCard component is designed for reusability and clarity.
  • State Management: Effective use of React state and effects to manage visibility, delays, and positioning.
  • Positioning Logic: Correct and robust implementation of adaptive positioning based on viewport boundaries.
  • Accessibility: Proper implementation of ARIA attributes and keyboard interaction for inclusive UX.
  • Code Quality: Readability, maintainability, and adherence to React best practices.

Loading editor...