libconsent

API reference

Everything @libconsent/core exports.

<ConsentProvider>

PropTypeDefault
adapterConsentAdapterlocal()Which CMP to talk to.
consentModeboolean | "auto""auto"Mirror consent into Google Consent Mode.
servicesServiceSpec[]—Services to list before they mount.
onChange(state: ConsentState) => void—Called whenever consent changes.

useConsent()

const {
  ready,        // the CMP has loaded and answered
  hasDecision,  // the user actually chose
  view,         // "hidden" | "banner" | "settings"
  isOpen,       // view !== "hidden"
  categories,   // { essential: true, analytics: false, … }
  has,          // (category | serviceId | ServiceSpec) => boolean
  open,
  openSettings,
  close,
  acceptAll,
  rejectAll,
  reset,
  supports,     // which of the actions above this CMP can do
  services,     // registered services, for building a UI
  cmp,          // { id, name }
  state,        // the raw ConsentState
} = useConsent();

useHasConsent(target)

useHasConsent("analytics");
useHasConsent("hotjar");
useHasConsent({ id: "acme", category: "marketing", aliases: ["Acme Ltd."] });

A bare string is a category when the CMP knows one by that name, and a service otherwise.

useCookieService(spec)

Registers the service and returns whether it may run. <CookieService> is this plus conditional rendering.

useConsentForm()

const {
  categories, // [{ category, granted, locked, services, toggle }]
  dirty,      // the draft differs from what is saved
  save,
  acceptAll,
  rejectAll,
} = useConsentForm();

Needs a platform that accepts consent from your own UI (supports.setConsent). See The banner.

<CookieService>

PropType
idstringRequired. A catalog id fills in everything else.
namestringLabel in the preferences UI.
categoryCategoryNameWhat gates it. Defaults to functional.
descriptionstringOne line for the preferences UI.
aliasesstring[]Other names CMPs file it under.
consentModebooleanRender regardless and let Consent Mode gate it.
fallbackReactNodeShown while consent is missing.
childrenReactNode | (granted: boolean) => ReactNode

<ConsentScript>

Takes the same service props, plus:

PropType
srcstringExternal script URL.
childrenstringInline script body.
attributesRecord<string, string>Extra attributes for the tag.
onLoad() => void

Adapters

  • local(options?) — the built-in CMP
  • localStorageAdapter, cookieStorage(options?) — storage backends
  • defineAdapter(definition) — write your own
  • loadScript(src, attributes?), waitFor(getter, options?) — the helpers, standalone
  • consentModeSnippet(waitForUpdate?) — the consent default snippet, as a string
  • toConsentModeSignals(state) — categories → Google's four signals

Helpers

The same functions the hooks use, usable outside React:

  • hasConsent(state, target)
  • hasCategoryConsent(state, category)
  • hasServiceConsent(state, service)
  • listCategories(state)
  • resolveService(spec) — apply the catalog and the defaults
  • normalizeId(value) — the key-folding rule
  • catalog — the built-in service catalog
  • ConsentStore — the store the provider drives

Constants

  • CATEGORIES — ["essential", "functional", "analytics", "marketing"]
  • OPTIONAL_CATEGORIES — the three the user can turn off
  • INITIAL_STATE — nothing known, nothing granted
  • DEFAULT_STORAGE_KEY — "libconsent"

On this page