Skip to content

ConsentBanner

MD · 1950B gzip budget

A cookie banner where decline is exactly as easy to press as accept.

Category
Accessibility & preferences
Budget
md, several states or a live subscription
External runtime
React / React DOM only
axe fixtureSSR rendersource scanbundle budget

Preview

Live fixture
Preview locale

We use only the storage this site needs to work. No third-party tracking.

Try the component here. Locale-aware formatting and direction follow the selected language; example content and labels are not automatically translated. Overlay demos use interactive launchers.

Install

npx shadcn@latest add https://gear5-ui.vercel.app/r/consent-banner.json

Copies the source into your project. Pulls in 2 shared registry items: cn, use-stored-value.

New here? Set up Tailwind and import aliases first →

Source & props

Edit on GitHub ↗

Prop types and inline documentation are included below. This is repository source; the installer rewrites shared imports for your project.

"use client";

import { useStoredValue } from "../lib/use-stored-value";
import { cn } from "../lib/cn";

export interface ConsentBannerLabels {
  message: string;
  accept: string;
  decline: string;
}

const DEFAULT_LABELS: ConsentBannerLabels = {
  message: "We use only the storage this site needs to work. No third-party tracking.",
  accept: "Accept",
  decline: "Decline",
};

export interface ConsentBannerProps {
  storageKey?: string;
  labels?: Partial<ConsentBannerLabels>;
  onDecide?: (accepted: boolean) => void;
  className?: string;
}

/**
 * A cookie/consent banner scoped to what this library actually believes in:
 * decline is exactly as easy to press as accept — no dark-pattern size or
 * placement difference — and the choice is asked once, stored locally, and
 * never re-asked while it stands.
 */
export function ConsentBanner({ storageKey = "gear5-ui:consent", labels: labelOverrides, onDecide, className }: ConsentBannerProps) {
  const labels = { ...DEFAULT_LABELS, ...labelOverrides };
  const [decision, setDecision] = useStoredValue(storageKey);

  if (decision !== null) return null;

  const decide = (accepted: boolean) => {
    setDecision(accepted ? "accepted" : "declined");
    onDecide?.(accepted);
  };

  return (
    <div
      role="region"
      aria-label="Cookie consent"
      className={cn(
        "fixed inset-x-0 bottom-0 z-50 flex flex-col items-center gap-3 border-t border-neutral-200 bg-white p-4 text-sm shadow-lg sm:flex-row sm:justify-between dark:border-neutral-800 dark:bg-neutral-950",
        className,
      )}
    >
      <p>{labels.message}</p>
      <div className="flex shrink-0 gap-2">
        <button
          type="button"
          onClick={() => decide(false)}
          className="rounded-md border border-neutral-300 px-3 py-1.5 font-medium hover:bg-neutral-50 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-blue-600 dark:border-neutral-700 dark:hover:bg-neutral-800"
        >
          {labels.decline}
        </button>
        <button
          type="button"
          onClick={() => decide(true)}
          className="rounded-md bg-neutral-900 px-3 py-1.5 font-medium text-white hover:bg-neutral-800 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-blue-600 dark:bg-neutral-100 dark:text-neutral-900"
        >
          {labels.accept}
        </button>
      </div>
    </div>
  );
}

What CI checks

Quality contract
  • Bundled, minified and gzipped against its tier budget
  • Audited by axe in the state previewed above
  • Rendered through react-dom/server with no browser globals
  • Scanned for network calls, dangerous sinks, and unguarded animation