'use client'; import { CodeExampleCard, ExamplePoster, type ExampleFile, type ExampleFrame, type ExampleMode, } from '@embedpdf/docs-kit'; import Link from 'next/link'; import { usePathname } from 'next/navigation'; import { useEffect, useRef, useState } from 'react'; import { DEFAULT_PRODUCT_INTEGRATION, DOCS_INTEGRATION_LABELS, docsIntegrationFromPath, docsIntegrationHref, isDocsIntegration, } from '@/lib/docs-integrations'; import { docsProductFromPath } from '@/lib/docs-products'; import { exampleScreenshot, examplePageHref, exampleSlug } from '@/lib/examples'; /** Mounts a built demo module (public/demos/…) via a NATIVE dynamic import — * the module carries its own framework runtime, so Vue/Svelte/Angular demos * run inside the Next site with no bundler integration at all. Import cost is * deferred until the preview is open AND near the viewport; once mounted, a * collapse keeps the instance alive (state survives toggling). `data-demo` says how far it got. */ export function DemoMount({ url, active }: { url: string; active: boolean }) { const ref = useRef(null); const [inView, setInView] = useState(false); const [started, setStarted] = useState(false); const [loaded, setLoaded] = useState(false); const [error, setError] = useState(false); useEffect(() => { const el = ref.current; if (!el || inView) return; const observer = new IntersectionObserver( (entries) => { if (entries.some((entry) => entry.isIntersecting)) { setInView(true); observer.disconnect(); } }, { rootMargin: '256px' }, ); observer.observe(el); return () => observer.disconnect(); }, [inView]); useEffect(() => { if (active && inView) setStarted(true); }, [active, inView]); useEffect(() => { if (!started) return; let cancelled = false; let cleanup: (() => void) | undefined; import(/* webpackIgnore: true */ url) .then((mod: { mount: (el: HTMLElement) => () => void }) => { if (!cancelled && ref.current) { cleanup = mod.mount(ref.current); setLoaded(true); } }) .catch(() => setError(true)); return () => { cancelled = true; cleanup?.(); }; }, [started, url]); return (
{!loaded && !error ? (

Loading live preview…

) : null} {error ? (

The live preview failed to load — run pnpm build:demos and reload.

) : null}
); } /** * A bigger preview than an example gets by default: `tall`, or `page`, an app that needs the * whole window: it opens on a page of its own (`/examples/`), and here its screenshot links * there (`lib/examples.ts`). */ export type ExampleSize = 'tall' | 'page'; /** * The preview's frame: a ready-made viewer example (`viewer/…`) fills a frame that suits the docs * column, which `size` makes bigger; other examples, and a screenshot, are as tall as they are. */ function frameOf(name: string | undefined, size: ExampleSize | undefined): ExampleFrame { if (size === 'tall') return 'tall'; if (size === 'page') return 'content'; return name?.startsWith('viewer/') ? 'viewer' : 'content'; } /** * The route-variant-resolved sample display. This site's job is RESOLUTION — * which framework's files, which built demo — and the honest not-yet-ported * fallback; the kit's CodeExampleCard owns every pixel of the shell, so the * "View code" chrome is identical on both docs sites. */ export function Example({ filesByFramework, demosByFramework, mode = 'default', kind = 'example', available, name, size, }: { filesByFramework?: string; demosByFramework?: string; mode?: ExampleMode; /** A `` is code only; its gap note says so. */ kind?: 'example' | 'snippet'; /** * The frameworks that have a version, when the server only sent the route's framework's files * (`RouteExample`); the "not available yet" note picks its link from it. */ available?: readonly string[]; /** The sample's name, as the page writes it (`viewer/quick-start/customized`). */ name?: string; /** A bigger preview (``). */ size?: ExampleSize; }) { const pathname = usePathname(); const product = docsProductFromPath(pathname); const integration = docsIntegrationFromPath(pathname); const defaultIntegration = product === 'viewer' ? DEFAULT_PRODUCT_INTEGRATION.viewer : DEFAULT_PRODUCT_INTEGRATION.headless; const variant = integration ?? defaultIntegration; const label = DOCS_INTEGRATION_LABELS[variant]; const byFramework: Record = filesByFramework ? JSON.parse(filesByFramework) : {}; const files = byFramework[variant]; const demos: Record = demosByFramework ? JSON.parse(demosByFramework) : {}; const demoUrl = demos[variant]; if (!files || files.length === 0) { const versions = available ?? Object.keys(byFramework).filter((fw) => byFramework[fw]?.length); const fallback = versions.includes(defaultIntegration) ? defaultIntegration : versions[0]; const fallbackIntegration = isDocsIntegration(fallback) ? fallback : null; const fallbackLabel = fallbackIntegration ? DOCS_INTEGRATION_LABELS[fallbackIntegration] : null; const fallbackHref = fallbackIntegration ? docsIntegrationHref(pathname, fallbackIntegration) : null; return (
{kind === 'snippet' ? 'This code isn’t written for ' : 'This example isn’t available for '} {label} yet. {fallback && fallbackHref && fallbackLabel ? ( <> {' '} You can read the{' '} {fallbackLabel} version {' '} in the meantime. ) : null}
); } const slug = size === 'page' && name ? exampleSlug(name) : null; const demo = slug ? () => : demoUrl ? (active: boolean) => : undefined; return ; }