Quick Start
Core (vanilla JS)
Section titled “Core (vanilla JS)”import { createConsent } from "@uabcodus/consent/core";
const consent = createConsent({ categories: { necessary: { readOnly: true // Always accepted — user can't toggle it off }, analytics: { services: { googleAnalytics: { onAccept: () => console.log("GA enabled"), onReject: () => console.log("GA disabled") } } }, marketing: {} } as const});
// Check if consent was already given on a previous visitif (consent.state.valid) { console.log("Consent already given");}
// Accept everythingconsent.accept("all");
// Or accept selectivelyconsent.accept(["analytics"]);consent.reject("marketing");
// Check what's acceptedconsent.acceptedCategory("analytics"); // trueconsent.acceptedCategory("marketing"); // falseStick that inside a click handler on your cookie banner and you’re done.
Wrap your app with ConsentProvider and use the useConsent hook wherever you need consent state:
import { ConsentProvider, useConsent } from "@uabcodus/consent/react";
function App() { return ( <ConsentProvider options={{ categories: { necessary: { readOnly: true }, analytics: {}, marketing: {} } }} > <ConsentBanner /> </ConsentProvider> );}
function ConsentBanner() { const consent = useConsent();
// Your component re-renders automatically when consent state changes if (consent.state.valid) return null;
return ( <div> <p>We use cookies for analytics and marketing.</p> <button onClick={() => consent.accept("all")}>Accept All</button> <button onClick={() => consent.accept("necessary")}>Necessary Only</button> </div> );}The useConsent hook auto-subscribes to state changes — your component re-renders whenever consent is accepted, rejected, or modified.
Read the state
Section titled “Read the state”The consent.state object gives you everything you need to know:
consent.state.valid; // boolean — is consent given?consent.state.skipped; // boolean — was consent skipped (bot)?consent.state.mode; // "opt-in" | "opt-out"consent.state.acceptType; // "all" | "necessary" | "custom"consent.state.categories; // { [name]: { accepted: boolean; readOnly: boolean } }consent.state.services; // { [category]: { [service]: boolean } }consent.state.cookie; // CookieValue | nullNext steps
Section titled “Next steps”- How It Works → — deep dive into categories, services, and consent modes
- React Integration → — patterns and best practices for React apps
- Managing Scripts → — block scripts until consent is given
- Playground → — try it live with live state inspection