Animated

Animated: Next.js

A client component in the root layout.

npm install glint-cursors

App Router

The loader is safe to import during server rendering, but the cursor must start in the browser. Make a small client component:

// app/cursor.tsx
'use client';
import { useEffect } from 'react';
import { loadCursor } from 'glint-cursors/animated';

// Serialize loads so a cancelled effect cannot destroy a newer instance.
let pending = Promise.resolve();

export default function Cursor() {
  useEffect(() => {
    let cursor: Awaited<ReturnType<typeof loadCursor>> = null;
    let cancelled = false;
    pending = pending.then(async () => {
      if (cancelled) return;
      const c = await loadCursor({ family: 'dot-ring', variant: 'light', scope: '.marketing' });
      if (cancelled) c?.destroy();
      else cursor = c;
    }).catch((error) => console.error('Cursor could not load:', error));
    return () => {
      cancelled = true;
      cursor?.destroy();
    };
  }, []);
  return null;
}

Render it once in the marketing layout, where the scoped element exists:

// app/(marketing)/layout.tsx
import Cursor from '../cursor';

export default function MarketingLayout({ children }: { children: React.ReactNode }) {
  return (
    <div className="marketing">
      {children}
      <Cursor />
    </div>
  );
}

Scope it to marketing routes

The example scopes the cursor to the first .marketing element and mounts it only on those routes. A scope selector that matches no element causes loadCursor to reject. The queue handles effect replay in this module; coordinate ownership if other code also loads a cursor. See the React guide for details.

Pages Router

Use the same component inside a marketing page layout that includes a .marketing wrapper. Mount it only while that wrapper is present.