Consent Manager Dialog
An accessible, animated modal interface that wraps the Consent Manager Widget for a focused privacy customization experience.
The Consent Manager Dialog provides a clean, focused way for users to customize their privacy preferences. Think of it as a spotlight that dims the rest of your application while users make important privacy decisions. When users click "Customize" on your cookie banner, this dialog smoothly appears with detailed privacy controls.
How It Works
The dialog acts as a wrapper around the Consent Manager Widget, adding several key features:
- A smooth fade-in animation that draws attention to privacy settings
- An overlay that dims the rest of the application
- Proper focus management for accessibility
- Automatic portal rendering to avoid layout issues
- Smart handling of mounting and unmounting
Let's explore how to implement this in your application.
Quick Start
First, install the package if you haven't already:
Then add the dialog to your application:
The dialog will automatically:
- Mount itself correctly in the DOM
- Handle animations smoothly
- Manage focus when opened
- Maintain accessibility standards
Understanding Animations
The dialog uses thoughtful animations to create a polished user experience. When opened, it:
- Fades in an overlay to dim the background
- Scales and fades in the dialog content
- Smoothly reverses these animations when closing
You can control this behavior with the disableAnimation
prop:
Customizing Appearance
The dialog supports two main approaches to styling:
Using Themes
Apply custom styles through the theme prop:
Removing Default Styles
For complete styling control, disable the default styles:
This gives you a blank canvas to build upon while maintaining the dialog's functionality.
Integration Examples
With Custom Trigger Button
Accessibility Features
The dialog implements several accessibility best practices:
Focus Management
When the dialog opens, it:
- Traps focus within the dialog
- Sets initial focus on the first interactive element
- Remembers and restores the previous focus position when closed
Focus Trapping
The dialog implements focus trapping to ensure keyboard navigation remains within the dialog while it's open. This is crucial for:
- Keyboard users: Prevents users from accidentally interacting with content hidden behind the modal
- Screen reader users: Maintains proper context and prevents confusion
- WCAG compliance: Supports 2.4.3 Focus Order and provides proper modal functionality
How Focus Trapping Works
The ConsentManagerDialog
uses the useFocusTrap
hook internally to:
- Capture the element that had focus before the dialog opened
- Set initial focus to the first interactive element inside the dialog
- Keep focus cycling within the dialog when users press Tab or Shift+Tab
- Restore focus to the original element when the dialog closes
You can control focus trapping with the trapFocus
prop:
Focus trapping is enabled by default and is recommended for WCAG compliance. Only disable it if you have a specific reason and are implementing alternative accessibility measures.
Keyboard Navigation
Users can:
- Close the dialog with the Escape key
- Navigate controls with Tab
- Interact with all elements using only the keyboard
Screen Readers
The dialog announces itself appropriately with:
- Proper ARIA roles and attributes
- Clear labeling of controls
- Status updates when opened/closed
Technical Details
Portal Rendering
The dialog uses React's createPortal to render outside your main application hierarchy. This ensures:
- No CSS specificity conflicts
- Proper stacking context
- Clean DOM structure
Client-side Only
To prevent hydration issues, the dialog only renders on the client side. This is handled automatically through the isMounted
state.
API Reference
Props
Prop | Type | Description | Default |
---|---|---|---|
theme | ConsentManagerWidgetTheme | Theme configuration | |
disableAnimation | boolean | Turns off animations | false |
noStyle | boolean | Removes default styles | false |
Theme Properties
Property | Purpose | Example Value |
---|---|---|
dialog | Main dialog container | "fixed inset-0" |
overlay | Background overlay | "bg-black/50" |
content | Dialog content wrapper | "bg-white p-4" |
Best Practices
-
Performance
- Only import the dialog where needed
- Consider lazy loading if not immediately required
- Use the disableAnimation prop on lower-end devices
-
User Experience
- Keep the dialog focused on privacy settings
- Provide clear save/cancel actions
- Maintain consistent styling with your application
-
Accessibility
- Test with keyboard navigation
- Verify screen reader announcements
- Ensure sufficient color contrast