TypeScript
Add document viewer sidebar demo and fix registry deps
Andrew Luo06e26de8601b
diff --git a/apps/v4/components/document-viewer-sidebar-docs.tsx b/apps/v4/components/document-viewer-sidebar-docs.tsxnew file mode 100644index 0000000..d7bf1e9--- /dev/null+++ b/apps/v4/components/document-viewer-sidebar-docs.tsx@@ -0,0 +1,346 @@+"use client"++import * as React from "react"+import { SidebarLeftIcon } from "@hugeicons/core-free-icons"+import { HugeiconsIcon } from "@hugeicons/react"++import { cn } from "@/lib/utils"+import { Button } from "@/components/ui/button"+import {+ DocumentViewerSidebarSkeleton,+ DocumentViewerThumbnailSidebar,+ useElementWidth,+ useInlineThumbnailSidebar,+} from "@/components/ui/document-viewer-sidebar"+import { HighlightedCodeBlock } from "@/components/highlighted-code-block"++const demoPages = [+ { id: 1, label: "Invoice summary" },+ { id: 2, label: "Line items" },+ { id: 3, label: "Approval trail" },+ { id: 4, label: "Exception notes" },+] as const++function DemoThumbnail({+ active,+ page,+}: {+ active: boolean+ page: (typeof demoPages)[number]+}) {+ return (+ <div+ className={cn(+ "h-28 w-20 overflow-hidden rounded-md border bg-background p-2 shadow-xs transition-shadow",+ active && "shadow-sm ring-2 ring-ring/32"+ )}+ >+ <div className="mb-2 h-2 w-8 rounded-full bg-muted-foreground/24" />+ <div className="space-y-1.5">+ <div className="h-1.5 rounded-full bg-muted-foreground/20" />+ <div className="h-1.5 w-10 rounded-full bg-muted-foreground/20" />+ <div className="h-1.5 rounded-full bg-muted-foreground/20" />+ </div>+ <div className="mt-4 grid grid-cols-2 gap-1">+ <div className="h-5 rounded bg-muted" />+ <div className="h-5 rounded bg-muted" />+ <div className="h-5 rounded bg-muted" />+ <div className="h-5 rounded bg-muted" />+ </div>+ </div>+ )+}++function DemoDocumentPage({+ activePage,+}: {+ activePage: (typeof demoPages)[number]+}) {+ return (+ <div className="mx-auto flex min-h-full w-full max-w-[520px] items-start justify-center p-6">+ <div className="w-full rounded-sm border bg-background p-8 shadow-xs">+ <div className="h-2 w-20 rounded-full bg-muted-foreground/24" />+ <div className="mt-5 h-5 w-2/3 rounded-full bg-foreground/12" />+ <div className="mt-6 space-y-2">+ <div className="h-2 rounded-full bg-muted-foreground/16" />+ <div className="h-2 rounded-full bg-muted-foreground/16" />+ <div className="h-2 w-4/5 rounded-full bg-muted-foreground/16" />+ </div>+ <div className="mt-8 grid grid-cols-2 gap-3">+ <div className="h-20 rounded-md border bg-muted/40" />+ <div className="h-20 rounded-md border bg-muted/40" />+ <div className="h-20 rounded-md border bg-muted/40" />+ <div className="h-20 rounded-md border bg-muted/40" />+ </div>+ <div className="mt-6 text-sm font-medium text-muted-foreground">+ Page {activePage.id}: {activePage.label}+ </div>+ </div>+ </div>+ )+}++function DocumentViewerSidebarPreview() {+ const [viewerShellRef, viewerShellWidth] = useElementWidth<HTMLDivElement>()+ const sidebarInline = useInlineThumbnailSidebar(viewerShellWidth)+ const [sidebarOpen, setSidebarOpen] = React.useState(true)+ const [activePageId, setActivePageId] = React.useState(1)+ const activePage =+ demoPages.find((page) => page.id === activePageId) ?? demoPages[0]++ return (+ <div className="bg-background p-4 sm:p-6">+ <div className="mx-auto flex h-[460px] max-w-4xl flex-col overflow-hidden rounded-lg border bg-background">+ <div className="flex min-h-12 items-center justify-between gap-3 border-b px-3">+ <div className="flex items-center gap-2">+ <Button+ type="button"+ variant="ghost"+ size="icon-sm"+ aria-label="Toggle thumbnails"+ data-pressed={sidebarOpen ? "" : undefined}+ onClick={() => setSidebarOpen((open) => !open)}+ >+ <HugeiconsIcon icon={SidebarLeftIcon} className="size-4" />+ </Button>+ <div className="text-sm whitespace-nowrap text-primary">+ Page {activePage.id} of {demoPages.length}+ </div>+ </div>+ <div className="hidden text-xs text-muted-foreground sm:block">+ {sidebarInline ? "Inline thumbnails" : "Overlay thumbnails"}+ </div>+ </div>+ <div+ ref={viewerShellRef}+ className="relative flex min-h-0 flex-1 overflow-hidden bg-muted/30"+ >+ {sidebarOpen ? null : (+ <DocumentViewerSidebarSkeleton inline={sidebarInline} />+ )}+ <DocumentViewerThumbnailSidebar+ inline={sidebarInline}+ open={sidebarOpen}+ >+ <div className="h-full overflow-auto p-4">+ <div className="flex flex-col items-center gap-3">+ {demoPages.map((page) => (+ <Button+ key={page.id}+ type="button"+ variant="ghost"+ size="sm"+ className={cn(+ "!h-auto w-full flex-col items-center gap-2 p-2 text-xs shadow-none hover:bg-sidebar-accent",+ page.id === activePageId+ ? "bg-sidebar-accent text-foreground"+ : "text-muted-foreground"+ )}+ onClick={() => setActivePageId(page.id)}+ >+ <DemoThumbnail+ active={page.id === activePageId}+ page={page}+ />+ {page.id}+ </Button>+ ))}+ </div>+ </div>+ </DocumentViewerThumbnailSidebar>+ <div className="min-w-0 flex-1 overflow-auto">+ <DemoDocumentPage activePage={activePage} />+ </div>+ </div>+ </div>+ </div>+ )+}++export function DocumentViewerSidebarDemo() {+ const [isCodeVisible, setIsCodeVisible] = React.useState(false)++ return (+ <div+ data-slot="component-preview"+ className="group relative mt-4 mb-12 flex flex-col overflow-hidden rounded-xl border"+ >+ <DocumentViewerSidebarPreview />+ <div+ data-slot="code"+ data-mobile-code-visible={isCodeVisible}+ className="relative overflow-hidden **:data-[slot=copy-button]:right-4 **:data-[slot=copy-button]:hidden data-[mobile-code-visible=true]:**:data-[slot=copy-button]:flex [&_[data-rehype-pretty-code-figure]]:m-0! [&_[data-rehype-pretty-code-figure]]:rounded-t-none [&_[data-rehype-pretty-code-figure]]:border-t [&_pre]:max-h-72"+ >+ {isCodeVisible ? (+ <HighlightedCodeBlock+ code={documentViewerSidebarUsageCode}+ className="rounded-none border-x-0 border-b-0"+ />+ ) : (+ <div className="relative">+ <HighlightedCodeBlock+ code={documentViewerSidebarUsageCode}+ className="rounded-none border-x-0 border-b-0"+ maxHeightClassName="max-h-56"+ previewLines={10}+ showCopy={false}+ />+ <div className="absolute inset-0 flex items-center justify-center pb-4">+ <div+ className="absolute inset-0"+ style={{+ background:+ "linear-gradient(to top, var(--color-code), color-mix(in oklab, var(--color-code) 60%, transparent), transparent)",+ }}+ />+ <Button+ type="button"+ size="sm"+ variant="outline"+ className="docs-view-code-button relative z-10 rounded-lg"+ onClick={() => setIsCodeVisible(true)}+ >+ View Code+ </Button>+ </div>+ </div>+ )}+ </div>+ </div>+ )+}++const documentViewerSidebarUsageCode = `"use client"++import * as React from "react"++import {+ DocumentViewerThumbnailSidebar,+ useElementWidth,+ useInlineThumbnailSidebar,+} from "@/components/ui/document-viewer-sidebar"++const pages = [1, 2, 3, 4]++export function DocumentViewerShell() {+ const [viewerShellRef, viewerShellWidth] = useElementWidth<HTMLDivElement>()+ const sidebarInline = useInlineThumbnailSidebar(viewerShellWidth)+ const [sidebarOpen, setSidebarOpen] = React.useState(true)+ const [activePage, setActivePage] = React.useState(1)++ return (+ <div className="flex h-[560px] flex-col overflow-hidden border bg-background">+ <button type="button" onClick={() => setSidebarOpen((open) => !open)}>+ Toggle thumbnails+ </button>+ <div+ ref={viewerShellRef}+ className="relative flex min-h-0 flex-1 overflow-hidden bg-muted/30"+ >+ <DocumentViewerThumbnailSidebar+ inline={sidebarInline}+ open={sidebarOpen}+ >+ <div className="h-full overflow-auto p-4">+ {pages.map((page) => (+ <button+ key={page}+ type="button"+ onClick={() => setActivePage(page)}+ >+ Page {page}+ </button>+ ))}+ </div>+ </DocumentViewerThumbnailSidebar>+ <div className="min-w-0 flex-1 overflow-auto">+ Active page: {activePage}+ </div>+ </div>+ </div>+ )+}`++const documentViewerSidebarSourceCode = `"use client"++import * as React from "react"++import { cn } from "@/lib/utils"++const INLINE_THUMBNAIL_SIDEBAR_MIN_WIDTH = 768++export function useElementWidth<TElement extends HTMLElement>() {+ const ref = React.useRef<TElement | null>(null)+ const [width, setWidth] = React.useState(0)++ React.useLayoutEffect(() => {+ const element = ref.current+ if (!element) return++ const updateWidth = () => {+ setWidth(element.getBoundingClientRect().width)+ }++ updateWidth()++ const observer = new ResizeObserver(updateWidth)+ observer.observe(element)++ return () => observer.disconnect()+ }, [])++ return [ref, width] as const+}++export function useInlineThumbnailSidebar(width: number) {+ return width >= INLINE_THUMBNAIL_SIDEBAR_MIN_WIDTH+}++export function DocumentViewerThumbnailSidebar({+ children,+ className,+ inline,+ open,+}: {+ children: React.ReactNode+ className?: string+ inline: boolean+ open: boolean+}) {+ return (+ <aside+ data-document-thumbnail-sidebar=""+ data-sidebar-mode={inline ? "inline" : "overlay"}+ data-sidebar-open={open ? "true" : "false"}+ className={cn(+ "absolute inset-y-0 left-0 z-30 w-40 shrink-0 overflow-hidden border-r bg-sidebar shadow-lg transition-[translate,margin-left,border-color] duration-200 ease-out",+ inline && "relative z-auto translate-x-0 shadow-none",+ open+ ? "ml-0 translate-x-0"+ : inline+ ? "pointer-events-auto -ml-40 border-r-0"+ : "pointer-events-none -translate-x-full border-r-0",+ className+ )}+ >+ {children}+ </aside>+ )+}++export function DocumentViewerSidebarSkeleton({ inline }: { inline: boolean }) {+ if (!inline) return null++ return (+ <div className="w-40 shrink-0 border-r bg-sidebar p-4">+ <div className="mx-auto h-28 w-20 overflow-hidden rounded-md bg-background shadow-xs">+ <div className="h-full animate-pulse bg-muted" />+ </div>+ <div className="mx-auto mt-3 h-3 w-10 rounded-full bg-muted" />+ </div>+ )+}`++export function DocumentViewerSidebarSource() {+ return <HighlightedCodeBlock code={documentViewerSidebarSourceCode} />+}diff --git a/apps/v4/content/docs/components/document-viewer-sidebar.mdx b/apps/v4/content/docs/components/document-viewer-sidebar.mdxindex 7b5506d..79e6245 100644--- a/apps/v4/content/docs/components/document-viewer-sidebar.mdx+++ b/apps/v4/content/docs/components/document-viewer-sidebar.mdx@@ -6,6 +6,8 @@ component: trueDocument Viewer Sidebar provides the responsive shell used by document viewers to show thumbnail navigation inline on wider layouts and as an overlay on narrower layouts.+<DocumentViewerSidebarDemo />+## Installation<CodeTabs>@@ -26,7 +28,12 @@ npx shadcn@latest add https://ui.extend.ai/r/document-viewer-sidebar.json<Steps className="mb-0 pt-2">-<Step>Copy `components/ui/document-viewer-sidebar.tsx` from the registry item into your project.</Step>+<Step>+ Copy `components/ui/document-viewer-sidebar.tsx` from the registry item into+ your project.+</Step>++<DocumentViewerSidebarSource /><Step>Update the import paths to match your project setup.</Step>diff --git a/apps/v4/mdx-components.tsx b/apps/v4/mdx-components.tsxindex c2431e3..c5f86e2 100644--- a/apps/v4/mdx-components.tsx+++ b/apps/v4/mdx-components.tsx@@ -16,6 +16,10 @@ import {DocumentSplitsDemo,DocumentSplitsSource,} from "@/components/document-splitter-docs"+import {+ DocumentViewerSidebarDemo,+ DocumentViewerSidebarSource,+} from "@/components/document-viewer-sidebar-docs"import { DocxEditorDemo, DocxEditorSource } from "@/components/docx-editor-docs"import { DocxViewerDemo, DocxViewerSource } from "@/components/docx-viewer-docs"import { ESignatureDemo, ESignatureSource } from "@/components/e-signature-docs"@@ -374,6 +378,8 @@ export const mdxComponents = {ComponentsList: ComponentsListWrapper,CsvViewerDemo,CsvViewerSource,+ DocumentViewerSidebarDemo,+ DocumentViewerSidebarSource,DocumentSplitsDemo,DocumentSplitsSource,DocxEditorDemo,diff --git a/apps/v4/registry.json b/apps/v4/registry.jsonindex a006366..b430385 100644--- a/apps/v4/registry.json+++ b/apps/v4/registry.json@@ -527,7 +527,11 @@"@hugeicons/react@^1.1.6","border-beam@^1.0.1"],- "registryDependencies": ["card", "file-thumbnail", "utils"],+ "registryDependencies": [+ "card",+ "https://ui.extend.ai/r/file-thumbnail.json",+ "utils"+ ],"categories": ["documents"],"files": [{@@ -573,8 +577,8 @@],"registryDependencies": ["button",- "document-viewer-sidebar",- "file-thumbnail",+ "https://ui.extend.ai/r/document-viewer-sidebar.json",+ "https://ui.extend.ai/r/file-thumbnail.json","input","popover","scroll-area",@@ -604,8 +608,8 @@],"registryDependencies": ["button",- "document-viewer-sidebar",- "file-thumbnail",+ "https://ui.extend.ai/r/document-viewer-sidebar.json",+ "https://ui.extend.ai/r/file-thumbnail.json","scroll-area","select","separator",@@ -634,9 +638,9 @@],"registryDependencies": ["button",- "document-viewer-sidebar",+ "https://ui.extend.ai/r/document-viewer-sidebar.json","dropdown-menu",- "file-thumbnail",+ "https://ui.extend.ai/r/file-thumbnail.json","input","scroll-area","select",@@ -696,7 +700,7 @@],"registryDependencies": ["button",- "group",+ "https://ui.extend.ai/r/group.json","input","scroll-area","select",@@ -704,7 +708,7 @@"spinner","tooltip","utils",- "xlsx-viewer"+ "https://ui.extend.ai/r/xlsx-viewer.json"],"categories": ["documents"],"files": [@@ -750,7 +754,10 @@"title": "PDF Dropzone","description": "A PDF-only upload dropzone that opens the dropped file in the shared PDF viewer.","dependencies": ["@hugeicons/core-free-icons@^4.1.1"],- "registryDependencies": ["file-upload", "pdf-viewer"],+ "registryDependencies": [+ "https://ui.extend.ai/r/file-upload.json",+ "https://ui.extend.ai/r/pdf-viewer.json"+ ],"categories": ["documents", "blocks"],"files": [{@@ -771,8 +778,8 @@"react-markdown@^10.1.0"],"registryDependencies": [- "pdf-block-resizable-shell",- "pdf-viewer",+ "https://ui.extend.ai/r/pdf-block-resizable-shell.json",+ "https://ui.extend.ai/r/pdf-viewer.json","scroll-area","utils"],@@ -799,8 +806,8 @@"registryDependencies": ["button","dialog",- "pdf-block-resizable-shell",- "pdf-viewer",+ "https://ui.extend.ai/r/pdf-block-resizable-shell.json",+ "https://ui.extend.ai/r/pdf-viewer.json","scroll-area","utils"],@@ -827,8 +834,8 @@"registryDependencies": ["button","input",- "pdf-block-resizable-shell",- "pdf-viewer",+ "https://ui.extend.ai/r/pdf-block-resizable-shell.json",+ "https://ui.extend.ai/r/pdf-viewer.json","scroll-area","tabs","tooltip",@@ -857,9 +864,9 @@],"registryDependencies": ["button",- "file-thumbnail",- "pdf-block-resizable-shell",- "pdf-viewer",+ "https://ui.extend.ai/r/file-thumbnail.json",+ "https://ui.extend.ai/r/pdf-block-resizable-shell.json",+ "https://ui.extend.ai/r/pdf-viewer.json","scroll-area","utils"],@@ -878,11 +885,11 @@"title": "Excel Document Splits","description": "A workbook review block with sheet groups and the shared XLSX viewer.","registryDependencies": [- "file-thumbnail",- "pdf-block-resizable-shell",+ "https://ui.extend.ai/r/file-thumbnail.json",+ "https://ui.extend.ai/r/pdf-block-resizable-shell.json","scroll-area","utils",- "xlsx-viewer"+ "https://ui.extend.ai/r/xlsx-viewer.json"],"categories": ["documents", "blocks"],"files": [@@ -898,7 +905,7 @@"type": "registry:block","title": "DOCX Editor","description": "A full DOCX editor block with a Word-style toolbar, page thumbnails, and export controls.",- "registryDependencies": ["docx-editor"],+ "registryDependencies": ["https://ui.extend.ai/r/docx-editor.json"],"categories": ["documents", "blocks"],"files": [{