Popover
Contextual information popup
A flexible Popover component that creates a floating content box triggered by a button, with support for various positions and accessibility features.
Overview
This documentation provides instructions on how to use the Popover component in your React projects. The Popover component is part of the abaabil.popover
library.
import Popover from 'abaabil.popover';
Key Features
- Customizable trigger button with label
- Support for multiple popover positions (top, bottom, left, right)
- Dynamic positioning based on available screen space
- Accessibility features including keyboard navigation and ARIA attributes
- Responsive design with automatic repositioning on window resize and scroll
- Click outside and escape key functionality for closing the popover
Usage Example
The Popover component can be used to add popovers to any element in your JSX. Wrap the element you want to add a popover to with the Popover component and provide the popover content.
Positions
The Popover component supports four positions: top (default), bottom, left, right
Properties
The Popover component accepts the following properties:
- label
Type:
string
Description: Text to display on the trigger button.
- children
Type:
ReactNode
Description: Content to be displayed inside the popover.
- buttonClassName
Type:
string
Description: Additional CSS classes to apply to the trigger button.
- position
Type:
'top' | 'bottom' | 'left' | 'right'
Description: Preferred position of the popover relative to the trigger button.
Default:
'top'
Accessibility
The Popover component is designed with accessibility in mind:
- Uses proper ARIA attributes (aria-expanded, aria-controls) for the trigger button
- Implements keyboard navigation support (Escape key to close)
- Ensures proper focus management
- Uses semantic HTML with appropriate roles (dialog for the popover content)
Styling
The Popover component uses Tailwind CSS classes for styling:
- Customizable trigger button appearance through buttonClassName prop
- Consistent styling for the popover content box
- Smooth opacity transition for showing/hiding the popover
- Customizable arrow indicator based on popover position
Best Practices
Follow these best practices when using the Popover component:
- Use clear and concise labels for the trigger button
- Keep popover content focused and relevant
- Consider the appropriate position based on the context and available space
Dynamic Positioning
The Popover component includes dynamic positioning features:
- Automatically adjusts position based on available screen space
- Recalculates position on window resize and scroll events
- Ensures popover remains visible within the viewport
Performance Considerations
The Popover component is optimized for performance:
- Uses React portals for efficient rendering outside the main DOM hierarchy
- Implements proper cleanup of event listeners
- Utilizes React.useCallback for optimized function references