UNPKG

@pdf-viewer/react

Version:

A react-pdf-viewer component for React and Next.js. Suitable for react-pdf document.

40 lines (39 loc) 1.27 kB
import { jsxs as m, jsx as x } from "react/jsx-runtime"; import { createContext as C, useRef as c, useContext as F } from "react"; import { useDocumentContext as h } from "./RPDocumentContext.js"; import { useDocumentPasswordContext as P } from "./DocumentPasswordContext.js"; const l = C(null), g = ({ children: t }) => { const { setFilename: u, setPdfSrc: p } = h(), r = c(), o = c(null), { setPassword: a } = P(), f = () => { var e; (e = o.current) == null || e.click(); }, d = (e) => { var i; const n = (i = e.target.files) == null ? void 0 : i[0]; if (!n || n.type !== "application/pdf") return; const s = URL.createObjectURL(n); r.current && URL.revokeObjectURL(r.current), r.current = s, u(n.name), p(s), e.target.value = "", a(""); }; return /* @__PURE__ */ m(l.Provider, { value: { openFile: f }, children: [ t, /* @__PURE__ */ x( "input", { accept: "application/pdf", type: "file", ref: o, style: { display: "none" }, onChange: d } ) ] }); }, j = () => { const t = F(l); if (!t) throw new Error("useOpenFileContext must be used within FileInputProvider"); return t; }; export { g as FileInputProvider, j as useOpenFileContext };