Vize

use-permission

Observe a browser permission reactively.

Package @vizejs/composable/use-permission
Own the source vize lib pull composable:use-permission
Runtime exports usePermission
Gzip budget 2048 B

Minimal setup

Use a Vue 3.5+ project with Vue SFC compilation configured (see Vite+ integration).

vp install @vizejs/composable

Import the entry directly. Call lifecycle-bound utilities inside <script setup> so cleanup follows the component scope; consult the runtime contract for this entry's SSR and hydration behavior.

import { usePermission } from "@vizejs/composable/use-permission";

Try it

Adapt to a permission state

Read a permission state without prompting and adapt the interface to it.

Open the interactive example

What to observe: The message reflects whether location is allowed, blocked or not decided yet, and updates if the reader changes it in site settings.

usePermission only reads the state; the API that needs the permission is what prompts. Every example renders a neutral message until it mounts, so server and client markup match, and then explains what to do when the browser lacks the API.

Compare the initial and interacted states
Adapt to a permission state: initial state
Initial state
Adapt to a permission state: state after the verified interactions
After the verified interactions

Copy the example

Save this complete SFC in your Vue project. Its public imports and source are the same ones executed in the preview.

<script setup lang="ts">
import { useMounted } from "@vizejs/composable/use-mounted";
import { usePermission } from "@vizejs/composable/use-permission";

const mounted = useMounted();
const { state, supported } = usePermission("geolocation");
</script>

<template>
  <div class="composable-example">
    <p>
      Explain a location feature differently depending on whether the reader already allowed it.
    </p>
    <output aria-live="polite">{{
      !mounted
        ? "Checking permissions…"
        : !supported
          ? "This browser cannot report permission states."
          : state === "granted"
            ? "Location is allowed; nearby venues load automatically."
            : state === "denied"
              ? "Location is blocked; search for a venue instead."
              : "Location: we will ask when you look for nearby venues."
    }}</output>
  </div>
</template>

Runtime contract

Utility Category Stability SSR Hydration Cleanup Targets Host globals Uses
usePermission capability experimental deterministic-fallback caller-managed reactive-scope web, desktop window tryOnScopeDispose

API

usePermission

Observe a browser permission reactively. The permission is queried when the composable is created and whenever the reactive name/descriptor or host changes; the change event of the returned PermissionStatus keeps state current (for example after the user revokes camera access in the site settings). The subscription is removed when the name changes and when the owning reactive scope stops. Server rendering: no browser window exists, so nothing is queried and state stays at initialState ("unknown") with supported false. Unknown names (the browser rejects the query) report "unsupported". Inside a component the first query runs after mounting, so hydration renders initialState exactly like the server did.

function usePermission( name: MaybeRefOrGetter<WellKnownPermissionName | PermissionDescriptorLike>, options: UsePermissionOptions = {}, ): PermissionControls
const { state } = usePermission("camera");
const canUseCamera = computed(() => state.value === "granted");

Types

PermissionDescriptorLike

Descriptor passed to navigator.permissions.query.

Member Type Description
name WellKnownPermissionName Permission name.
userVisibleOnly? boolean Push: only user-visible notifications (required true by most browsers).
sysex? boolean MIDI: request system-exclusive message access.
panTiltZoom? boolean Camera: request pan-tilt-zoom control.
allowWithoutGesture? boolean Clipboard (Chromium): allow access without a user gesture.

PermissionStatusLike

Minimal PermissionStatus shape consumed by usePermission.

Member Type Description
state string Current state reported by the browser.

PermissionsHost

Minimal Permissions shape consumed by usePermission.

Member Type Description
query (descriptor: PermissionDescriptorLike) => Promise<PermissionStatusLike> Query one permission. Rejects (TypeError) for names the browser does not know. Declared with method syntax so the DOM Permissions object, whose descriptor names are a narrower union, satisfies this interface.

UsePermissionOptions

Options for usePermission.

Member Type Description
host? MaybeRefOrGetter<PermissionsHost | null | undefined> Permissions capability for alternate runtimes and tests.
initialState? PermissionQueryState State exposed before the first query settles and during server rendering.

PermissionControls

Reactive state returned by usePermission.

Member Type Description
state Readonly<Ref<PermissionQueryState>> Current permission state. "unsupported" means the Permissions API (or this permission name) is unavailable; "unknown" means no answer yet.
supported Readonly<Ref<boolean>> Whether a Permissions capability is attached.
query () => Promise<PermissionQueryState> Query the permission again and resubscribe to its changes.