Focused hooks for Nuxt
Ten small hooks that each answer one question: is this category granted, which law applies, is the banner showing, and more.
What they are
useConsent() gives you everything and updates on every change. The composables on this page each answer one question and change only when that answer changes. They return computed refs, so read .value in script and use them directly in templates; the Returns column is the type of that value. useLanguage() and useReloadNotice() return an object whose fields are refs, plus functions. Nuxt imports every one of them for you; call them inside setup() or <script setup>.
| You want to know | Hook | Returns |
|---|---|---|
| Is one category granted? | useConsentCategory("analytics") | boolean, the saved value |
| Which privacy law applies? | useRegulation() | "GDPR" or "CCPA" |
| Why that law? | useRegion() | { region, regulation, source, confidence } |
| Which categories are configured? | useCategories() | { list, ids, requiredIds, isDefault } |
| Is the banner on screen? | useBannerVisibility() | boolean |
| Is a dialog open? | usePreferencesOpen(), useOptOutOpen() | boolean |
| What is the text in the active language? | useTranslations() | the translation map |
| Which language is active, and how do I switch it? | useLanguage() | { language, direction, languages, setLanguage } |
| Does a change need a page reload? | useReloadNotice() | { required, reasons, dismiss } |
Show something only with consent
<script setup lang="ts">
const allowed = useConsentCategory("analytics");
</script>
<template>
<iframe v-if="allowed" title="Dashboard" src="https://analytics.example.com/embed" />
<p v-else>Enable analytics cookies to see your dashboard.</p>
</template>The category can also be a ref or a getter, such as useConsentCategory(() => props.category).
useConsentCategory reads the saved value, so a switch flipped in the dialog but not saved changes nothing here. For an <iframe> with a ready-made placeholder, GatedFrame does this for you.
Which law applies, and why
useRegulation() returns the law in effect: "GDPR" or "CCPA".
useRegion() tells you how that was decided, which is the tool when a visitor sees an unexpected banner. source is "manual" when you set regulation yourself, "detected" when region.detect matched your region.map, and "strictest" when detection found nothing and region.strictest applied. confidence is "low" when the region was guessed.
Build your own category list
useCategories() returns your configured categories in order (list), their ids, the ids that are always on (requiredIds), and isDefault, which is true when the built-in five are in effect. Render from list rather than writing the five names by hand, so custom categories appear automatically. The list is fixed when the runtime starts, so it never changes.
The banner and the dialogs
useBannerVisibility() is true while the banner is on screen: the visitor has not chosen, has not closed it, and no dialog is open. Use it to move your own floating elements out of the way. It is not "has the visitor decided": for that, read useConsent().hasActed.
usePreferencesOpen() and useOptOutOpen() report the two dialogs.
Language and text
useTranslations() returns the text for the active language, the same strings the components show, and updates when the language changes. useLanguage() returns the active language, its direction for right-to-left layouts and the loaded languages as computed refs, plus setLanguage(tag), which returns a promise. Translations has a picker built on it.
A custom reload notice
useReloadNotice() returns required, the tool ids in reasons, and dismiss(). Most sites should render ReloadNotice instead; use the hook only for your own markup.
Common mistakes
"Ask for consent" shows while the visitor is inside the dialog.
You used useBannerVisibility() for "has not decided yet". Use useConsent().hasActed.
A custom category is missing from your list.
You wrote the five built-in names by hand. Render from useCategories().list.
Next steps
- useConsent: the whole state in one hook
- Translations: languages and text
- Configuration: the
regionoption behinduseRegion()