DDBS UI
effect

Route Transition

Smooth page changes with the View Transitions API: a same-origin link click captures the page and the new one fades in when the pathname changes. Router injected through a prop.

npx shadcn@latest add https://ui.develop-better-solutions.com/r/route-transition.json
Fullscreen

Renders nothing: it listens for link clicks and runs a View Transition when the pathname changes. This browser ... the View Transitions API.

// app/template.tsx (Next.js)
"use client";
import { usePathname } from "next/navigation";
import { RouteTransition } from "@dbs/ui";

export default function Template({ children }) {
  const pathname = usePathname();
  return (<><RouteTransition pathname={pathname} basePath="/shop" />{children}</>);
}

/* globals.css */
html.dbs-vt::view-transition-old(root) { animation: 200ms ease-out both fade-out; }
html.dbs-vt::view-transition-new(root) { animation: 300ms ease-in both fade-in; }

Usage

route-transition-example.tsx
"use client";

import * as React from "react";
import { RouteTransition } from "@dbs/ui";

const USAGE = `// app/template.tsx (Next.js)
"use client";
import { usePathname } from "next/navigation";
import { RouteTransition } from "@dbs/ui";

export default function Template({ children }) {
  const pathname = usePathname();
  return (<><RouteTransition pathname={pathname} basePath="/shop" />{children}</>);
}

/* globals.css */
html.dbs-vt::view-transition-old(root) { animation: 200ms ease-out both fade-out; }
html.dbs-vt::view-transition-new(root) { animation: 300ms ease-in both fade-in; }`;

export default function RouteTransitionExample() {
  const [supported, setSupported] = React.useState<boolean | null>(null);
  React.useEffect(() => setSupported(typeof (document as Document & { startViewTransition?: unknown }).startViewTransition === "function"), []);
  return (
    <div className="mx-auto w-full min-w-0 max-w-xl space-y-4 py-6">
      <p className="text-sm text-ink-muted">
        Renders nothing: it listens for link clicks and runs a View Transition when the pathname changes. This browser{" "}
        <strong className="text-ink">{supported === null ? "..." : supported ? "supports" : "does not support"}</strong> the View Transitions API.
      </p>
      <pre className="max-w-full overflow-x-auto whitespace-pre rounded-lg bg-surface p-4 text-xs leading-5 text-ink">{USAGE}</pre>
    </div>
  );
}