import { useEffect, useState } 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 { SearchLayer, searchPlugin, useSearch, useSearchEvent, useSearchSettings, useSearchState, } from '@embedpdf/react/search'; import { localEngine } from '@embedpdf/engine'; import './hooks.css'; const engine = localEngine(); const plugins = [stagePlugin(), renderPlugin(), searchPlugin()]; // [!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] const colors = ['#ffd500', '#7dd3fc', '#86efac']; function SearchBox() { // The API: what search can do. const search = useSearch(); // The data to show: re-renders when the count or the active match changes. const { hitCount, activeHitIndex } = useSearchState(); // The settings: re-renders when the highlight color changes. const color = useSearchSettings((settings) => settings.highlight.color); const [text, setText] = useState('PDF'); const [announcement, setAnnouncement] = useState(''); // A call to your function when something happens. useSearchEvent( (search) => search.onCompleted, ({ hitCount }) => setAnnouncement(`The search finished with ${hitCount} matches.`), ); useEffect(() => { void search.search({ text }); }, [search, text]); return ( <>
setText(event.target.value)} /> {hitCount > 0 ? `${activeHitIndex + 1} of ${hitCount}` : 'No matches'} {colors.map((swatch) => (

{announcement}

); } export default function App() { return ( Opening…

}> {() => ( <> )}
); }