Cookie Consent
A cookie notice that floats over the page as a card or spans its foot as a bar: its title, the message with its link in the words, and small Accept all and Reject all buttons with an icon-only Customize after them, which turns the notice itself into its categories to switch on or off and Update, here with the first category always on and two more to choose.
Title
A short line about it goes here, with its link in the words.
import { Button } from "@produkto/ui/Button"; import { CookieConsent, type CookieCategory, } from "@produkto/ui/CookieConsent"; import { Link } from "@produkto/ui/Link"; import { useState } from "react"; const categories: CookieCategory[] = [ { id: "one", label: "Item 1", description: "A short line about it goes here.", required: true, }, { id: "two", label: "Item 2", description: "A short line about it goes here.", }, { id: "three", label: "Item 3", description: "A short line about it goes here.", }, ]; const Example = () => { const [open, setOpen] = useState(true); return ( <div className="relative isolate flex h-120 items-center justify-center overflow-hidden bg-background p-6"> <Button appearance="outline" onClick={() => setOpen(true)}> Open </Button> <CookieConsent categories={categories} className="absolute" message={ <> A short line about it goes here, with its{" "} <Link className="whitespace-nowrap" href="#" underline> link </Link>{" "} in the words. </> } onOpenChange={setOpen} open={open} title="Title" /> </div> ); }; export default Example;
Usage
import { CookieConsent, type CookieCategory, } from "@produkto/ui/CookieConsent"; import { Link } from "@produkto/ui/Link";
const categories: CookieCategory[] = [ { id: "one", label: "Item 1", description: "Description", required: true }, { id: "two", label: "Item 2", description: "Description" }, ]; <CookieConsent categories={categories} message={ <> A short line about it goes here, with its{" "} <Link href="#" underline> link </Link>{" "} in the words. </> } onAccept={(preferences) => {}} onReject={(preferences) => {}} onSave={(preferences) => {}} title="Title" />
Examples
Bar
layout="bar" spans the foot of the page, the message beside its buttons from md and above them on a phone, and its categories side by side once customized.
Title
A short line about it goes here, with its link in the words.
import { Button } from "@produkto/ui/Button"; import { CookieConsent, type CookieCategory, } from "@produkto/ui/CookieConsent"; import { Link } from "@produkto/ui/Link"; import { useState } from "react"; const categories: CookieCategory[] = [ { id: "one", label: "Item 1", description: "A short line about it goes here.", required: true, }, { id: "two", label: "Item 2", description: "A short line about it goes here.", }, ]; const Example = () => { const [open, setOpen] = useState(true); return ( <div className="relative isolate flex h-120 items-center justify-center overflow-hidden bg-background p-6"> <Button appearance="outline" onClick={() => setOpen(true)}> Open </Button> <CookieConsent categories={categories} className="absolute" layout="bar" message={ <> A short line about it goes here, with its{" "} <Link href="#" underline> link </Link>{" "} in the words. </> } onOpenChange={setOpen} open={open} title="Title" /> </div> ); }; export default Example;
End
align="end" floats the card in the far corner, and without categories there is nothing to customize, so it offers only Accept all and Reject all.
Title
A short line about it goes here, with its link in the words.
import { Button } from "@produkto/ui/Button"; import { CookieConsent } from "@produkto/ui/CookieConsent"; import { Link } from "@produkto/ui/Link"; import { useState } from "react"; const Example = () => { const [open, setOpen] = useState(true); return ( <div className="relative isolate flex h-120 items-center justify-center overflow-hidden bg-background p-6"> <Button appearance="outline" onClick={() => setOpen(true)}> Open </Button> <CookieConsent align="end" className="absolute" message={ <> A short line about it goes here, with its{" "} <Link href="#" underline> link </Link>{" "} in the words. </> } onOpenChange={setOpen} open={open} title="Title" /> </div> ); }; export default Example;
Preferences
onAccept, onReject and onSave each hand over the choice as a record of category ids, a required category always on, and defaultPreferences sets the switches Customize opens with.
Title
A short line about it goes here.
import { Badge } from "@produkto/ui/Badge"; import { Button } from "@produkto/ui/Button"; import { CookieConsent, type CookieCategory, type CookiePreferences, } from "@produkto/ui/CookieConsent"; import { useState } from "react"; const categories: CookieCategory[] = [ { id: "one", label: "Item 1", description: "A short line about it goes here.", required: true, }, { id: "two", label: "Item 2", description: "A short line about it goes here.", }, { id: "three", label: "Item 3", description: "A short line about it goes here.", }, ]; const Example = () => { const [open, setOpen] = useState(true); const [preferences, setPreferences] = useState<CookiePreferences>(); return ( <div className="relative isolate flex h-120 flex-col items-center justify-center gap-4 overflow-hidden bg-background p-6"> {preferences && ( <ul className="flex flex-wrap justify-center gap-2" role="list"> {categories.map((category) => ( <li key={category.id}> <Badge tone={preferences[category.id] ? "success" : "destructive"} > {category.label} </Badge> </li> ))} </ul> )} <Button appearance="outline" onClick={() => setOpen(true)}> Open </Button> <CookieConsent categories={categories} className="absolute" defaultPreferences={{ two: true }} message="A short line about it goes here." onAccept={setPreferences} onReject={setPreferences} onOpenChange={setOpen} onSave={setPreferences} open={open} title="Title" /> </div> ); }; export default Example;
Message only
Without a title the bar is one line and its buttons, named for screen readers by the kit's own words.
A short line about it goes here, with its link in the words.
import { Button } from "@produkto/ui/Button"; import { CookieConsent } from "@produkto/ui/CookieConsent"; import { Link } from "@produkto/ui/Link"; import { useState } from "react"; const Example = () => { const [open, setOpen] = useState(true); return ( <div className="relative isolate flex h-120 items-center justify-center overflow-hidden bg-background p-6"> <Button appearance="outline" onClick={() => setOpen(true)}> Open </Button> <CookieConsent className="absolute" layout="bar" message={ <> A short line about it goes here, with its{" "} <Link href="#" underline> link </Link>{" "} in the words. </> } onOpenChange={setOpen} open={open} /> </div> ); }; export default Example;
API Reference
CookieConsent
CookieConsentProps · blocks/CookieConsent/CookieConsent.types.ts| Prop | Type | Default |
|---|---|---|
| title | string | - |
| message | ReactNode | - |
| categories | CookieCategory[] | [] |
| defaultPreferences | CookiePreferences | - |
| open | boolean | - |
| defaultOpen | boolean | true |
| onOpenChange | (open: boolean) => void | - |
| onAccept | (preferences: CookiePreferences) => void | - |
| onReject | (preferences: CookiePreferences) => void | - |
| onSave | (preferences: CookiePreferences) => void | - |
| layout | CookieConsentLayout"card" | "bar" | "card" |
| align | CookieConsentAlign"start" | "center" | "end" | "start" |
| className | string | - |
CookieCategory
used by categories| Prop | Type | Default |
|---|---|---|
| idrequired | string | - |
| labelrequired | string | - |
| descriptionrequired | string | - |
| required | boolean | - |