{"version":3,"file":"index.cjs","sources":["../src/index.tsx"],"sourcesContent":["import {\n  useRef,\n  useEffect,\n  useImperativeHandle,\n  forwardRef,\n  type ForwardedRef,\n  type CSSProperties,\n} from 'react';\nimport EmbedPDF, {\n  type EmbedPdfContainer,\n  type PDFViewerConfig,\n  type PluginRegistry,\n} from '@embedpdf/snippet';\n\n// Re-export everything from snippet for convenience\nexport * from '@embedpdf/snippet';\n\nexport interface PDFViewerProps {\n  /** Full configuration for the PDF viewer */\n  config?: PDFViewerConfig;\n  /** CSS class name for the container div */\n  className?: string;\n  /** Inline styles for the container div */\n  style?: CSSProperties;\n  /** Callback when the viewer is initialized */\n  onInit?: (container: EmbedPdfContainer) => void;\n  /** Callback when the registry is ready */\n  onReady?: (registry: PluginRegistry) => void;\n}\n\nexport interface PDFViewerRef {\n  /** The EmbedPdfContainer element */\n  container: EmbedPdfContainer | null;\n  /** Promise that resolves to the PluginRegistry */\n  registry: Promise<PluginRegistry> | null;\n}\n\n/**\n * React component for embedding PDF documents\n *\n * @example\n * ```tsx\n * import { PDFViewer } from '@embedpdf/react-pdf-viewer';\n *\n * function App() {\n *   return (\n *     <PDFViewer\n *       config={{\n *         src: '/document.pdf',\n *         theme: { preference: 'system' },\n *       }}\n *       style={{ width: '100%', height: '100vh' }}\n *       onReady={(registry) => {\n *         console.log('PDF viewer ready', registry);\n *       }}\n *     />\n *   );\n * }\n * ```\n */\nexport const PDFViewer = forwardRef(function PDFViewer(\n  props: PDFViewerProps,\n  ref: ForwardedRef<PDFViewerRef>,\n) {\n  const { config = {}, className, style, onInit, onReady } = props;\n  const containerRef = useRef<HTMLDivElement>(null);\n  const viewerRef = useRef<EmbedPdfContainer | null>(null);\n\n  useImperativeHandle(ref, () => ({\n    get container() {\n      return viewerRef.current;\n    },\n    get registry() {\n      return viewerRef.current?.registry ?? null;\n    },\n  }));\n\n  useEffect(() => {\n    if (!containerRef.current) return;\n\n    // Track if this effect instance is still active (for Strict Mode)\n    let isActive = true;\n\n    // IMPORTANT: Capture the container reference in a closure\n    // This ensures we can clean up even after React unmounts the component\n    const containerElement = containerRef.current;\n\n    // Initialize the viewer with the config prop\n    const viewer = EmbedPDF.init({\n      type: 'container',\n      target: containerElement,\n      ...config,\n    });\n\n    if (viewer) {\n      viewerRef.current = viewer;\n      onInit?.(viewer);\n\n      // Call onReady when registry is available, but only if still active\n      if (onReady) {\n        viewer.registry.then((registry) => {\n          // Only call onReady if this effect instance is still active\n          // This prevents stale callbacks in React Strict Mode\n          if (isActive) {\n            onReady(registry);\n          }\n        });\n      }\n    }\n\n    return () => {\n      // Mark this effect instance as inactive\n      isActive = false;\n\n      // Cleanup: remove the viewer element using captured reference\n      // We use the captured containerElement instead of containerRef.current\n      // because React may have already unmounted and cleared the ref\n      containerElement.innerHTML = '';\n      viewerRef.current = null;\n    };\n    // eslint-disable-next-line react-hooks/exhaustive-deps\n  }, []);\n\n  return <div ref={containerRef} className={className} style={style} />;\n});\n\nexport default PDFViewer;\n"],"names":["PDFViewer","forwardRef","props","ref","config","className","style","onInit","onReady","containerRef","useRef","viewerRef","useImperativeHandle","container","current","registry","_a","useEffect","isActive","containerElement","viewer","EmbedPDF","init","type","target","then","innerHTML","jsx"],"mappings":"mMA4DaA,EAAYC,EAAAA,WAAW,SAClCC,EACAC,GAEA,MAAMC,OAAEA,EAAS,aAAIC,QAAWC,EAAAC,OAAOA,EAAAC,QAAQA,GAAYN,EACrDO,EAAeC,EAAAA,OAAuB,MACtCC,EAAYD,EAAAA,OAAiC,MAyDnD,OAvDAE,EAAAA,oBAAoBT,EAAK,KAAA,CACvB,aAAIU,GACF,OAAOF,EAAUG,OACnB,EACA,YAAIC,SACF,OAAO,OAAAC,EAAAL,EAAUG,cAAV,EAAAE,EAAmBD,WAAY,IACxC,KAGFE,EAAAA,UAAU,KACR,IAAKR,EAAaK,QAAS,OAG3B,IAAII,GAAW,EAIf,MAAMC,EAAmBV,EAAaK,QAGhCM,EAASC,EAASC,KAAK,CAC3BC,KAAM,YACNC,OAAQL,KACLf,IAmBL,OAhBIgB,IACFT,EAAUG,QAAUM,EACpB,MAAAb,GAAAA,EAASa,GAGLZ,GACFY,EAAOL,SAASU,KAAMV,IAGhBG,GACFV,EAAQO,MAMT,KAELG,GAAW,EAKXC,EAAiBO,UAAY,GAC7Bf,EAAUG,QAAU,OAGrB,IAEIa,EAAAA,IAAC,MAAA,CAAIxB,IAAKM,EAAcJ,YAAsBC,SACvD"}