import { useState } from 'react'; import { Viewer, DocumentGate, useDocuments, useDocumentsState, type DocumentInfo, type FailedDocumentInfo, } from '@embedpdf/react/runtime'; import type { OpenInput } from '@embedpdf/react/runtime'; import { Stage, stagePlugin } from '@embedpdf/react/stage'; import { RenderLayer, renderPlugin } from '@embedpdf/react/render'; import { localEngine } from '@embedpdf/engine'; import './states.css'; const engine = localEngine(); const plugins = [stagePlugin(), renderPlugin()]; // [!doc-source welcome] const welcome = async (): Promise => { const response = await fetch('https://www.embedpdf.com/pdfs/welcome.pdf'); return { kind: 'bytes', bytes: new Uint8Array(await response.arrayBuffer()) }; }; // [!/doc-source] // A file that isn't a PDF, so its document can't be opened. const broken: OpenInput = { kind: 'bytes', bytes: new TextEncoder().encode('Not a PDF') }; function Tabs() { const documents = useDocuments(); const { documents: open, activeId } = useDocumentsState(); return (
{open.map((document) => ( ))}
); } function PasswordForm({ document }: { document: DocumentInfo }) { const documents = useDocuments(); const [password, setPassword] = useState(''); const [wrong, setWrong] = useState(document.passwordProvided ?? false); const unlock = async () => { try { await documents.unlock(document.id, { password }); } catch { setWrong(true); // it stays locked; ask again } }; return (

{document.name} needs a password

{wrong &&

That password isn’t right. Try again.

}
setPassword(event.target.value)} />
); } function OpenError({ document }: { document: FailedDocumentInfo }) { const documents = useDocuments(); return (

Couldn’t open {document.name}

{document.error.message}

); } export default function App() { return ( Opening…} locked={(document) => } error={(document) => } > {() => } ); }