UNPKG

nextjs-cookie-consent

Version:

A GDPR/DSGVO-compliant cookie consent banner for Next.js

172 lines (135 loc) โ€ข 4.74 kB
# ๐Ÿช nextjs-cookie-consent A ๐Ÿ›ก๏ธ GDPR / DSGVO-compliant cookie consent banner for Next.js with fully customizable categories like analytics, marketing, or preferences. Built for modern apps with easy styling, flexible API, and localStorage- or cookie-based consent handling. ## โœจ Features โœ… **GDPR / DSGVO-compliant**: Only essential cookies are enabled by default ๐Ÿง  **Fully configurable categories** (e.g. Necessary, Analytics, Marketing, ...) ๐Ÿ’ฌ **Custom text** with links to your privacy policy ๐Ÿ’พ **Consent stored** in localStorage (or cookies โ€“ configurable!) ๐Ÿ”„ **Dynamic access** to user consent via `useConsent()` hook ๐Ÿงฑ **Compatible** with both App Router and Pages Router ๐ŸŽจ **Minimal styling** (fully customizable) ## ๐Ÿš€ Installation ```bash npm install nextjs-cookie-consent ``` or with Yarn: ```bash yarn add nextjs-cookie-consent ``` ## โš™๏ธ Usage ### App Router (app/layout.tsx): ```tsx import { ConsentProvider, CookieBanner } from 'nextjs-cookie-consent'; import 'nextjs-cookie-consent/dist/styles/styles.css'; export default function RootLayout({ children }: { children: React.ReactNode }) { return ( <ConsentProvider> <html lang="en"> <body> {children} <CookieBanner categories={[ { key: 'analytics', label: 'Analytics' }, { key: 'marketing', label: 'Marketing' }, ]} storageStrategy="localStorage" // optional: "cookie" or "localStorage" (default) content={ <p> We use cookies to improve your experience.{' '} <a href="/privacy-policy" target="_blank" rel="noopener noreferrer"> Learn more </a> . </p> } /> </body> </html> </ConsentProvider> ); } ``` ### Pages Router (_app.tsx): ```tsx import { ConsentProvider, CookieBanner } from 'nextjs-cookie-consent'; import 'nextjs-cookie-consent/dist/styles/styles.css'; import type { AppProps } from 'next/app'; export default function App({ Component, pageProps }: AppProps) { return ( <ConsentProvider> <Component {...pageProps} /> <CookieBanner categories={[ { key: 'analytics', label: 'Analytics' }, { key: 'marketing', label: 'Marketing' }, ]} /> </ConsentProvider> ); } ``` ## ๐Ÿ“Š Accessing Consent State Use the `useConsent()` hook anywhere in your app: ```tsx 'use client'; import { useConsent } from 'nextjs-cookie-consent'; export default function AnalyticsLoader() { const { consent } = useConsent(['analytics', 'marketing']); return ( <> {consent.analytics && <p>๐Ÿ“ˆ Analytics enabled</p>} {!consent.analytics && <p>โŒ Analytics disabled</p>} </> ); } ``` ## ๐Ÿงช Example: Load Google Analytics only after consent ```tsx 'use client'; import Script from 'next/script'; import { useConsent } from 'nextjs-cookie-consent'; export default function GoogleAnalytics() { const { consent } = useConsent(['analytics']); if (!consent.analytics) return null; return ( <> <Script src="https://www.googletagmanager.com/gtag/js?id=G-XXXXXXX" strategy="afterInteractive" /> <Script id="ga-init" strategy="afterInteractive"> {`window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag('js', new Date()); gtag('config', 'G-XXXXXXX');`} </Script> </> ); } ``` ## ๐Ÿงฐ Props & API | Prop | Type | Description | |------|------|-------------| | `categories` | `CookieCategory[]` | An array of consent categories to configure | | `content` | `React.ReactNode` | (optional) Custom JSX/HTML content shown above the buttons | | `storageStrategy` | `'localStorage' \| 'cookie'` | (optional) Where to store consent. Default: `'localStorage'` | ```tsx interface CookieCategory { key: string; // e.g. "analytics" label: string; // e.g. "Analytics" } ``` ## ๐ŸŽจ Custom Styling Import the CSS file and override the classes: ```tsx import 'nextjs-cookie-consent/dist/styles/styles.css'; ``` Available CSS classes: - `.njs-cookie-banner-container` - Whole Banner - `.njs-cookie-banner-default` - Default Banner view - `.njs-cookie-banner-settings` - Settings Banner view - `.njs-cookie-banner-buttons` - Button group - `.njs-cookie-banner-button-necessary` - "Only necessary" button - `.njs-cookie-banner-button-accept-all` - "Accept all" button - `.njs-cookie-banner-button-settings` - "Settings" button - And many more... ## โš–๏ธ License MIT โ€“ free to use for personal and commercial projects. Built with โค๏ธ for modern Next.js apps.