Cookie Banner
A customizable cookie consent banner that handles privacy compliance with zero configuration required.
The Cookie Banner component provides an elegant way to obtain and manage cookie consent from your users. It handles all the complexity of privacy regulations while providing a smooth user experience that integrates naturally with your application.
Understanding Cookie Consent
Before diving into implementation, let's understand what makes a cookie banner effective. A good cookie banner needs to:
- Inform users about data collection clearly and concisely
- Obtain explicit consent before setting non-essential cookies
- Provide easy ways to accept, reject, or customize preferences
- Remember user choices across sessions
- Meet legal requirements across different jurisdictions
Our Cookie Banner component handles all of these requirements automatically, while remaining fully customizable to match your application's design.
Quick Start
Let's add a basic cookie banner to your application. First, install the package:
Then add the banner to your root component:
That's it! The banner will automatically:
- Appear on first visit
- Remember user choices
- Handle consent management
- Stay compliant with privacy laws
Component Architecture
The Cookie Banner uses a compound component pattern, giving you complete control over its structure when needed. Think of it like building blocks – you can use the pre-assembled version, or arrange the pieces yourself for custom layouts.
Customization
The Cookie Banner is designed to adapt to your application's visual style. Here are the main ways to customize its appearance:
Using Themes
Here are the available theme variables:
Prop | Type | Default |
---|---|---|
banner.root? | ThemeValue | - |
banner.card? | ThemeValue<CardCSSVariables> | - |
banner.header.root? | ThemeValue<HeaderCSSVariables> | - |
banner.header.title? | ThemeValue<TitleCSSVariables> | - |
banner.header.description? | ThemeValue<DescriptionCSSVariables> | - |
banner.footer? | ThemeValue<FooterCSSVariables> | - |
banner.footer.sub-group? | ThemeValue | - |
banner.footer.reject-button? | ThemeValue<ButtonCSSVariables> | - |
banner.footer.customize-button? | ThemeValue<ButtonCSSVariables> | - |
banner.footer.accept-button? | ThemeValue<ButtonCSSVariables> | - |
banner.overlay? | ThemeValue<OverlayCSSVariables> | - |
The theme prop provides a straightforward way to customize the banner's appearance:
Using CSS Modules
For more traditional styling approaches, you can use CSS modules:
Using Existing Components
You can integrate your own component library using the asChild
prop:
Scroll Locking
The Cookie Banner supports scroll locking, a technique that prevents users from interacting with your website until they've made a cookie consent choice.
When enabled, scroll locking:
- Prevents page scrolling and interaction
- Displays a background overlay
- Ensures users must make a privacy choice before accessing content
For best results, use scroll locking together with focus trapping to ensure complete keyboard accessibility.
For detailed implementation guides, best practices, and compliance considerations, see our Scroll Locking Guide.
Accessibility
The Cookie Banner is built with accessibility in mind:
- Proper ARIA roles and labels (role="dialog", aria-modal="true")
- Keyboard navigation and interaction support
- Focus management and trapping
- Screen reader announcements
- Semantic HTML structure
These features work automatically, ensuring all users can interact with your privacy controls effectively.
Focus Trapping
The Cookie Banner implements focus trapping when it's displayed, which is an essential accessibility feature that prevents keyboard focus from moving outside the banner. This behavior:
- Ensures users complete the consent flow before interacting with other page elements
- Prevents accidental interaction with content that shouldn't be accessible yet
- Helps compliance with accessibility guidelines like WCAG 2.4.3 (Focus Order)
Implementation Details
Focus trapping in the Cookie Banner works through the CookieBannerCard
component, which:
- Uses the
useFocusTrap
hook internally - Automatically sets
tabIndex={0}
on the container - Applies proper ARIA attributes (
role="dialog"
andaria-modal="true"
) - Remembers the previously focused element and restores it when closed
- Cycles focus between interactive elements when Tab is pressed
You can control focus trapping with the trapFocus
prop:
Focus trapping is enabled by default and recommended for accessibility compliance. For more details on implementation and best practices, see our useFocusTrap hook documentation.
Best Practices
Follow these guidelines for optimal implementation:
- Place the banner at the root level of your application
- Keep the title and description clear and concise
- Use the pre-assembled version unless you need custom layouts
- Test the banner across different screen sizes
- Ensure your theme maintains sufficient contrast ratios
- Consider scroll locking for strict compliance scenarios
- Test with keyboard navigation to ensure accessibility
Common Pitfalls
API Reference
CookieBanner
The main component accepts these props:
Prop | Type | Default |
---|---|---|
theme? | Partial<{ 'banner.root': ThemeValue; 'banner.card': ThemeValue<CardCSSVariables>; 'banner.header.root': ThemeValue<HeaderCSSVariables>; ... 7 more ...; 'banner.overlay': ThemeValue<...>; }> | undefined |
noStyle? | boolean | false |
title? | ReactNode | undefined |
description? | ReactNode | undefined |
rejectButtonText? | ReactNode | undefined |
customizeButtonText? | ReactNode | undefined |
acceptButtonText? | ReactNode | undefined |
scrollLock? | boolean | false |
trapFocus? | true | true |
Compound Components
Each compound component is designed for specific functionality:
Component | Description |
---|---|
CookieBanner.Root | The container component |
CookieBanner.Card | Wrapper for banner content |
CookieBanner.Header | Groups title and description |
CookieBanner.Footer | Contains action buttons |
CookieBanner.AcceptButton | Accepts all cookies |
CookieBanner.RejectButton | Rejects optional cookies |
CookieBanner.CustomizeButton | Opens preference dialog |