Skip to content

Banner 2

Cookie consent card with accept, reject and preferences actions.

02 / 02

Installation

Run this in a project that already uses shadcn/ui. The CLI adds the block, its components and npm dependencies.

pnpm dlx shadcn@latest add https://ui.sumansharma.info.np/r/banner-2.json
Prefer to iterate with AI?Open in

Usage

The CLI writes the block to components/banner-2.tsx. Import it anywhere:

Loading...

Source

The CLI writes this component to components/banner-2.tsx.

'use client'

import { useState } from 'react'
import Link from 'next/link'
import { Cookie } from 'lucide-react'
import { Button } from '@/components/ui/button'

type Choice = 'accepted' | 'rejected' | null

export default function CookieBanner() {
    const [choice, setChoice] = useState<Choice>(null)

    return (
        <div className="relative min-h-[420px]">
            {choice && (
                <div className="flex min-h-[420px] flex-col items-center justify-center gap-3 text-sm">
                    <p className="text-muted-foreground">
                        Cookies {choice}. Wire this choice to your analytics or consent manager.
                    </p>
                    <button
                        type="button"
                        onClick={() => setChoice(null)}
                        className="underline underline-offset-4">
                        Reset
                    </button>
                </div>
            )}

            {!choice && (
                <div
                    role="dialog"
                    aria-modal="false"
                    aria-labelledby="cookie-title"
                    aria-describedby="cookie-body"
                    className="bg-background fixed inset-x-4 bottom-4 z-50 mx-auto max-w-xl rounded-2xl border p-5 shadow-2xl shadow-black/10 sm:left-6 sm:right-auto sm:mx-0">
                    <div className="flex gap-4">
                        <span className="bg-muted flex size-10 shrink-0 items-center justify-center rounded-full">
                            <Cookie className="size-5" />
                        </span>
                        <div>
                            <h2
                                id="cookie-title"
                                className="font-medium">
                                We value your privacy
                            </h2>
                            <p
                                id="cookie-body"
                                className="text-muted-foreground mt-1 text-sm">
                                We use cookies to keep you signed in and to understand how the site is used. Read our{' '}
                                <Link
                                    href="#"
                                    className="text-foreground underline underline-offset-4">
                                    cookie policy
                                </Link>
                                .
                            </p>
                        </div>
                    </div>
                    <div className="mt-5 flex flex-wrap justify-end gap-2">
                        <Button
                            variant="ghost"
                            size="sm"
                            asChild>
                            <Link href="#">Preferences</Link>
                        </Button>
                        <Button
                            variant="outline"
                            size="sm"
                            onClick={() => setChoice('rejected')}>
                            Reject all
                        </Button>
                        <Button
                            size="sm"
                            onClick={() => setChoice('accepted')}>
                            Accept all
                        </Button>
                    </div>
                </div>
            )}
        </div>
    )
}