GooseUI

Components

Cookie Consent

New

The banner is a non-modal <dialog open> with a <form method="dialog">: every button closes it without JavaScript, the settings panel is a <details>, the switches are checkboxes. Entry and exit are CSS transitions with @starting-style. The client part is small: it saves the choice to localStorage, reopens the banner after a reset and calls the callbacks. A one-line inline script hides the banner before the first paint if a choice is already stored, so it never flashes.

Browser Support

<dialog>

Displays the consent banner.

Your browser:
:has()

Styles the banner based on its state.

Your browser:
@starting-style

Animates the banner entrance.

Your browser:
transition-behavior

Animates the banner exit.

Your browser:

Preview

Here the banner is rendered inside the frame with position="absolute". Answer it, then press the button to bring it back.

We use cookies

Cookies help us keep the site working and understand how it is used. You can accept all of them or decline the optional ones.
Stored choice: no answer yet

Installation

npx shadcn@latest add @gooseui/cookie-consent
pnpm dlx shadcn@latest add @gooseui/cookie-consent
yarn dlx shadcn@latest add @gooseui/cookie-consent
bunx --bun shadcn@latest add @gooseui/cookie-consent

Usage

Put the banner once in the root layout. It is a server component: the HTML arrives with the banner, and the inline script removes it at once for people who already answered.

import { CookieConsent } from "@/components/ui/cookie-consent"

export default function RootLayout({ children }: { children: React.ReactNode }) {
  return (
    <html lang="en">
      <body>
        {children}
        <CookieConsent />
      </body>
    </html>
  )
}

Examples

Corner card

variant="card" takes less space. side picks the corner.

We use cookies

Cookies help us keep the site working and understand how it is used. You can accept all of them or decline the optional ones.
Stored choice: no answer yet
<CookieConsent variant="card" side="left" />

Categories

Pass categories to show a Customize button with switches. Required categories are locked on.

We use cookies

Cookies help us keep the site working and understand how it is used. You can accept all of them or decline the optional ones.
Customize
  • Sign-in, cart and security. Always on.

  • Anonymous visit statistics.

  • Personalized offers on other sites.

Stored choice: no answer yet
<CookieConsent
  variant="card"
  categories={[
    { id: "necessary", label: "Necessary", required: true },
    { id: "analytics", label: "Analytics", defaultEnabled: true },
    { id: "marketing", label: "Marketing" },
  ]}
/>

Reading the choice

useCookieConsent reads the same storage key and updates when the choice changes. This card follows the banner from the previous example.

Analytics scriptblocked
import { useCookieConsent } from "@/components/ui/cookie-consent"

function Analytics() {
  const { isAllowed } = useCookieConsent()
  if (!isAllowed("analytics")) return null
  return <script src="/analytics.js" async />
}

Callbacks and event

onAccept fires for Accept all and for saved preferences (status "custom"), onDecline for Decline and the close button. Functions can be passed only from a client component. From a server layout listen to the bubbling "cookieconsent" event, its detail is the stored value.

<CookieConsent
  storageKey="my-site-consent"
  delay={800}
  labels={{ title: "Cookies", accept: "OK" }}
  onAccept={(value) => console.log(value.status, value.categories)}
  onDecline={() => console.log("declined")}
/>

// In a server layout
// window.addEventListener("cookieconsent", (e) => console.log(e.detail))

Props

PropTypeDefaultDescription
storageKeystring"cookie-consent"localStorage key for the choice
variant"bar" | "card""bar"Bottom bar or corner card
position"fixed" | "absolute""fixed"fixed pins to the viewport, absolute stays inside the nearest positioned parent
side"left" | "right""right"Corner for the card variant
categoriesCookieCategory[]-Categories with switches, see the type below
labelsPartial<CookieConsentLabels>-Texts: title, description, accept, decline, customize, save, close
delaynumber0Delay before showing, ms
showIconbooleantrueShow the cookie icon
onAccept(value: CookieConsentValue) => void-Called on accept or saved preferences (client components only)
onDecline(value: CookieConsentValue) => void-Called on decline or close (client components only)
classNamestring-Additional CSS classes for the wrapper
type CookieCategory = {
  id: string
  label: string
  description?: string
  required?: boolean
  defaultEnabled?: boolean
}

type CookieConsentValue = {
  status: "accepted" | "declined" | "custom"
  categories: Record<string, boolean>
  timestamp: number
}

useCookieConsent

Exported from the same file. Takes the storage key ("cookie-consent" by default) and returns:

FieldTypeDescription
consentCookieConsentValue | nullStored value { status, categories, timestamp } or null if there is no answer
isLoadedbooleanFalse on the server and during hydration
isAllowed(category: string) => booleanChecks whether a category was allowed
reset() => voidClears the choice and shows the banner again

Features

  • No flash: an inline script removes the banner during HTML parsing if a choice is stored
  • Works without JavaScript: the buttons close the <dialog> through <form method="dialog">, Customize is a <details>
  • Synced: every banner and hook with the same key updates together, including other tabs
  • CSS animation: @starting-style and transition-behavior: allow-discrete, off for reduced motion
  • Accessible: a labelled dialog, real buttons and checkboxes with role="switch"; Enter in a switch does not submit the form