Build a Resilient & Accessible Popover Component
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.
Your primary task is to implement a reusable Popover React component with the following capabilities:
Popover component that accepts trigger (a ReactNode) and content (a ReactNode) as props.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').trigger element is clicked and close on a subsequent click of the trigger.trigger and the popover content itself.Escape key is pressed.Popover component by creating a simple Tooltip component that leverages your Popover for hover-based interactions.popover content should visually appear directly adjacent to its trigger element.popover and tooltip content should be clearly distinguishable from the background and other UI elements.aria-expanded, aria-haspopup, aria-controls, role) on the trigger and popover content.Escape key).Popover component is correctly implemented and renders its trigger and content.placement (top, bottom, left, right) relative to the trigger.placement would cause clipping.Escape key closes the popover.Tooltip component, built using Popover, correctly displays its content on hover and hides on hover out.trigger and the popover content to enhance accessibility.Constraints
Popover logic. You can use basic CSS for styling.popover content can contain interactive elements (e.g., buttons, input fields).Popover component should be designed for reusability.opacity transition is provided).Escape key handler).ReactDOM.createPortal (position the popover as an absolutely positioned element relative to its container).Popover component is structured, its API, and its ability to be reused (e.g., for Tooltip).Loading editor...