Updated
Cookie Consent
NewCustomize
Bottom left
Duration0.45s
Installation
npx shadcn@latest add https://uibrik.com/r/cookie-consent.jsonInstall the packages
npm install motion react-use-measureAdd the cn helper
Imports
cnfrom@/lib/utils, whichshadcn initcreates. Add it by hand without shadcn.Copy the source into your project
components/uibrik/cookie-consent/cookie-consent.tsx"use client"; import { useEffect, useId, useRef, useState, type ReactNode } from "react"; import { AnimatePresence, MotionConfig, motion } from "motion/react"; import useMeasure from "react-use-measure"; import { cn } from "@/lib/utils"; // Cookie consent. The card slides up a moment after the page loads, unless // the visitor has already chosen. "Customize" grows the card in place to show // each category with its own switch, rows easing in one after another, and // the buttons change to fit. The choice is saved to localStorage and passed // to `onConsent`; reopening the card later (open it yourself, e.g. from a // footer link) goes straight to the categories with the saved choice. export type CookieCategory = { id: string; label: string; description?: string; /** Always on and can't be switched off, e.g. strictly necessary cookies. */ required?: boolean; /** Switched on before the visitor changes anything. Default false. */ defaultOn?: boolean; };
Adds the files to
components/uibrik/cookie-consent and installs motion and react-use-measure. With the @uibrik namespace set up: npx shadcn@latest add @uibrik/cookie-consent.Usage
import { CookieConsent } from "@/components/uibrik/cookie-consent/cookie-consent";const message = "We use cookies to keep the site working and, with your consent, to understand how it's used.";const categories = [ { id: "necessary", label: "Necessary", description: "Sign-in, security and your cookie choice.", required: true }, { id: "preferences", label: "Preferences", description: "Language, theme and other settings." }, // …2 more];<CookieConsent position="bottom-left" title="We use cookies" message={message} policyHref="#" duration={0.45} categories={categories} storageKey={null} delay={600} strategy="absolute" policyLabel="Cookie policy" onConsent={(preferences) => {}} // open={false} // onOpenChange={(open) => {}} // getCookieConsent={(storageKey) => {}}/>Where to use Cookie Consent
Cookie banners
A banner sliding up after load and growing into category switches covers GDPR consent.
Pairs with Checkbox
Privacy settings
Remembering the visitor's choice and reopening to saved settings saves users time.
Marketing opt-ins
Per-category switches let visitors allow analytics but block ads.
Pairs with Tactile Toggles
Props
CookieConsent- position
- Type "bottom-left" | "bottom" | "bottom-right"
- "bottom-left"
- Corner of the window the card sits in.
- title
- Type string
- "We use cookies"
- Heading of the card.
- message
- Type string
- "We use cookies to keep the site working and, with your consent, to understand how it's used."
- Text under the heading.
- policyHref
- Type string
- "#"
- Link to your cookie or privacy policy. Empty to leave it out.
- duration
- Type number
- 0.45
- How long the card takes to move and grow.
- open / onOpenChange
- Type boolean / (open) => void
- –
- Controlled visibility. Uncontrolled, the card shows itself after `delay` when no choice is saved.
- onConsent
- Type (preferences: Record<string, boolean>) => void
- –
- Called with every category's state once the visitor chooses.
- categories
- Type { id; label; description?; required?; defaultOn? }[]
- Necessary, Preferences, Analytics, Marketing
- Categories in the customize view. Required ones are always on.
- storageKey
- Type string | null
- "cookie-consent"
- localStorage key for the choice. null keeps nothing.
- delay
- Type number
- 600
- Milliseconds after mount before the card shows itself.
- strategy
- Type "fixed" | "absolute"
- "fixed"
- Pin it to the window, or to the nearest positioned parent.
- policyLabel
- Type string
- "Cookie policy"
- Text of the policy link.
- getCookieConsent
- Type (storageKey?) => preferences | null
- –
- Reads the saved choice anywhere in your app, e.g. before loading analytics.
- className
- Type string
- –
- Classes for the card.