import { useEffect, useRef } from 'react'; import { Viewer, DocumentGate } 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 { interactionPlugin, useInteraction, useInteractionState, } from '@embedpdf/react/interaction'; import { SelectionLayer, selectionPlugin } from '@embedpdf/react/selection'; import { shellPlugin, useShell, useShellState } from '@embedpdf/react/shell'; import { localEngine } from '@embedpdf/engine'; import './menus.css'; const engine = localEngine(); const plugins = [ stagePlugin(), renderPlugin(), interactionPlugin(), selectionPlugin(), shellPlugin(), ]; // [!doc-source welcome] const welcome = async (): Promise => { const response = await fetch('https://www.embedpdf.com/pdfs/welcome.pdf'); return { kind: 'bytes', id: 'welcome', bytes: new Uint8Array(await response.arrayBuffer()) }; }; // [!/doc-source] const TOOLS = [ { id: 'pointer', label: 'Select text' }, { id: 'pan', label: 'Scroll with the hand' }, ]; // A menu with a submenu: opening the submenu leaves its menu open. function ViewMenu() { const shell = useShell(); const interaction = useInteraction(); const { openMenus } = useShellState(); const { activeToolId } = useInteractionState(); const bar = useRef(null); const anyOpen = openMenus.length > 0; // A press outside the menus, or Escape, closes all of them. useEffect(() => { if (!anyOpen) return; const outside = (event: PointerEvent) => { if (!bar.current?.contains(event.target as Node)) shell.closeAllMenus(); }; const escape = (event: KeyboardEvent) => { if (event.key === 'Escape') shell.closeAllMenus(); }; document.addEventListener('pointerdown', outside, true); document.addEventListener('keydown', escape); return () => { document.removeEventListener('pointerdown', outside, true); document.removeEventListener('keydown', escape); }; }, [shell, anyOpen]); return (
{openMenus.includes('view') && (
{openMenus.includes('view-tool') && (
{TOOLS.map((tool) => ( ))}
)}
)}
Open menus: {anyOpen ? openMenus.join(' › ') : 'none'}
); } function Workspace() { const shell = useShell(); // The menu and its submenu are open when the document is. useEffect(() => { shell.openMenu('view'); shell.openMenu('view-tool'); }, [shell]); return ( <> {() => ( <> )} ); } export default function App() { return ( Loading…

}>
); }