Vize

use-document-title

Bind the document title to reactive state.

Package @vizejs/composable/use-document-title
Own the source vize lib pull composable:use-document-title
Runtime exports useDocumentTitle
Gzip budget 2304 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 { useDocumentTitle } from "@vizejs/composable/use-document-title";

Try it

Show an unread count in the tab title

Keep the document title in sync with application state.

Open the interactive example

What to observe: Receive message twice and the tab title becomes (2) Inbox. Mark all read restores Inbox.

The title accepts a ref, getter or computed. When the component unmounts, the previous title is restored by default.

Compare the initial and interacted states
Show an unread count in the tab title: initial state
Initial state
Show an unread count in the tab title: 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 { computed, ref } from "vue";
import { useDocumentTitle } from "@vizejs/composable/use-document-title";

const unread = ref(0);
useDocumentTitle(computed(() => (unread.value > 0 ? `(${unread.value}) Inbox` : "Inbox")));
</script>

<template>
  <div class="composable-example">
    <p>Show the unread count in the browser tab title.</p>
    <div class="example-actions">
      <button type="button" @click="() => (unread += 1)">Receive message</button>
      <button type="button" @click="() => (unread = 0)">Mark all read</button>
    </div>
    <output aria-live="polite">{{ unread }} unread</output>
  </div>
</template>

Runtime contract

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

API

useDocumentTitle

Bind the document title to reactive state. Accepts a writable ref (used as-is), a getter (followed), a plain string, or nothing (an owned ref seeded from the current title). Every non-nullish title is passed through template before it is written. With observe, external title changes flow back into title. Server rendering: the document is never touched and title keeps the value it was given (or undefined). Head management for server-rendered markup (<title> in the HTML) is the job of the application's head manager; this composable only updates the live document on the client. Watchers and the observer are released with the owning reactive scope, which also applies restoreOnDispose.

function useDocumentTitle( title?: MaybeRefOrGetter<string | null | undefined>, options: UseDocumentTitleOptions = {}, ): DocumentTitleControls
const { title } = useDocumentTitle(() => route.meta.title, { template: "%s | Vize" });

Types

DocumentTitleHost

Document subset used by useDocumentTitle.

Member Type Description
title string Current document title (readable and writable).
observeTitle? (callback: () => void) => () => void Observe external title changes (for example by other libraries). Returns a function that stops observing.

UseDocumentTitleOptions

Options for useDocumentTitle.

Member Type Description
template? DocumentTitleTemplate Template applied to every title written to the document.
restoreOnDispose? DocumentTitleRestore Title restored when the owning reactive scope stops: false leaves the current title, "previous" restores the title seen at creation, and a function computes it.
observe? boolean Follow title changes made outside this composable.
host? MaybeRefOrGetter<DocumentTitleHost | null | undefined> Document capability for alternate runtimes and tests.

DocumentTitleControls

Reactive state returned by useDocumentTitle.

Member Type Description
title Ref<string | null | undefined> Title before templating. Writable; null/undefined leaves the document title untouched. When a getter was passed, the next getter change overwrites manual assignments.
supported ComputedRef<boolean> Whether a document capability is attached. Inside a component it stays false until mounted, so hydration matches the server render.