diff --git a/packages/react-pdf/src/Document.tsx b/packages/react-pdf/src/Document.tsx index bac901484..c3182e0a7 100644 --- a/packages/react-pdf/src/Document.tsx +++ b/packages/react-pdf/src/Document.tsx @@ -232,7 +232,7 @@ function isParameterObject(file: File): file is Source { /** * Loads a document passed using `file` prop. */ -const Document = forwardRef(function Document( +const Document: React.ForwardRefExoticComponent = forwardRef(function Document( { children, className, @@ -255,7 +255,7 @@ const Document = forwardRef(function Document( renderMode, rotate, ...otherProps - }: DocumentProps, + }, ref, ) { const [sourceState, sourceDispatch] = useResolver(); diff --git a/packages/react-pdf/src/DocumentContext.tsx b/packages/react-pdf/src/DocumentContext.tsx index 2cc037e82..c78ba1175 100644 --- a/packages/react-pdf/src/DocumentContext.tsx +++ b/packages/react-pdf/src/DocumentContext.tsx @@ -4,4 +4,7 @@ import { createContext } from 'react'; import type { DocumentContextType } from './shared/types.js'; -export default createContext(null); +const documentContext: React.Context = + createContext(null); + +export default documentContext; diff --git a/packages/react-pdf/src/LinkService.ts b/packages/react-pdf/src/LinkService.ts index 5da83874a..edf7ea316 100644 --- a/packages/react-pdf/src/LinkService.ts +++ b/packages/react-pdf/src/LinkService.ts @@ -49,31 +49,31 @@ export default class LinkService implements IPDFLinkService { this.pdfViewer = undefined; } - setDocument(pdfDocument: PDFDocumentProxy) { + setDocument(pdfDocument: PDFDocumentProxy): void { this.pdfDocument = pdfDocument; } - setViewer(pdfViewer: PDFViewer) { + setViewer(pdfViewer: PDFViewer): void { this.pdfViewer = pdfViewer; } - setExternalLinkRel(externalLinkRel?: ExternalLinkRel) { + setExternalLinkRel(externalLinkRel?: ExternalLinkRel): void { this.externalLinkRel = externalLinkRel; } - setExternalLinkTarget(externalLinkTarget?: ExternalLinkTarget) { + setExternalLinkTarget(externalLinkTarget?: ExternalLinkTarget): void { this.externalLinkTarget = externalLinkTarget; } - setHistory() { + setHistory(): void { // Intentionally empty } - get pagesCount() { + get pagesCount(): number { return this.pdfDocument ? this.pdfDocument.numPages : 0; } - get page() { + get page(): number { invariant(this.pdfViewer, 'PDF viewer is not initialized.'); return this.pdfViewer.currentPageNumber || 0; @@ -85,7 +85,7 @@ export default class LinkService implements IPDFLinkService { this.pdfViewer.currentPageNumber = value; } - get rotation() { + get rotation(): number { return 0; } @@ -147,11 +147,11 @@ export default class LinkService implements IPDFLinkService { }); } - navigateTo(dest: Dest) { + navigateTo(dest: Dest): void { this.goToDestination(dest); } - goToPage(pageNumber: number) { + goToPage(pageNumber: number): void { const pageIndex = pageNumber - 1; invariant(this.pdfViewer, 'PDF viewer is not initialized.'); @@ -167,41 +167,41 @@ export default class LinkService implements IPDFLinkService { }); } - addLinkAttributes(link: HTMLAnchorElement, url: string, newWindow: boolean) { + addLinkAttributes(link: HTMLAnchorElement, url: string, newWindow: boolean): void { link.href = url; link.rel = this.externalLinkRel || DEFAULT_LINK_REL; link.target = newWindow ? '_blank' : this.externalLinkTarget || ''; } - getDestinationHash() { + getDestinationHash(): string { return '#'; } - getAnchorUrl() { + getAnchorUrl(): string { return '#'; } - setHash() { + setHash(): void { // Intentionally empty } - executeNamedAction() { + executeNamedAction(): void { // Intentionally empty } - cachePageRef() { + cachePageRef(): void { // Intentionally empty } - isPageVisible() { + isPageVisible(): boolean { return true; } - isPageCached() { + isPageCached(): boolean { return true; } - executeSetOCGState() { + executeSetOCGState(): void { // Intentionally empty } } diff --git a/packages/react-pdf/src/Message.tsx b/packages/react-pdf/src/Message.tsx index 8f735d53f..1973b6e15 100644 --- a/packages/react-pdf/src/Message.tsx +++ b/packages/react-pdf/src/Message.tsx @@ -3,6 +3,6 @@ type MessageProps = { type: 'error' | 'loading' | 'no-data'; }; -export default function Message({ children, type }: MessageProps) { +export default function Message({ children, type }: MessageProps): React.ReactElement { return
{children}
; } diff --git a/packages/react-pdf/src/Outline.tsx b/packages/react-pdf/src/Outline.tsx index 5f7fcca3c..f03d52d69 100644 --- a/packages/react-pdf/src/Outline.tsx +++ b/packages/react-pdf/src/Outline.tsx @@ -64,7 +64,7 @@ export type OutlineProps = { * * Should be placed inside ``. Alternatively, it can have `pdf` prop passed, which can be obtained from ``'s `onLoadSuccess` callback function. */ -export default function Outline(props: OutlineProps) { +export default function Outline(props: OutlineProps): React.ReactNode { const documentContext = useDocumentContext(); const mergedProps = { ...documentContext, ...props }; diff --git a/packages/react-pdf/src/OutlineContext.tsx b/packages/react-pdf/src/OutlineContext.tsx index b290706ca..016b44e43 100644 --- a/packages/react-pdf/src/OutlineContext.tsx +++ b/packages/react-pdf/src/OutlineContext.tsx @@ -4,4 +4,6 @@ import { createContext } from 'react'; import type { OutlineContextType } from './shared/types.js'; -export default createContext(null); +const outlineContext: React.Context = createContext(null); + +export default outlineContext; diff --git a/packages/react-pdf/src/OutlineItem.tsx b/packages/react-pdf/src/OutlineItem.tsx index 25041c6f2..897aded7f 100644 --- a/packages/react-pdf/src/OutlineItem.tsx +++ b/packages/react-pdf/src/OutlineItem.tsx @@ -18,7 +18,7 @@ type OutlineItemProps = { pdf?: PDFDocumentProxy | false; }; -export default function OutlineItem(props: OutlineItemProps) { +export default function OutlineItem(props: OutlineItemProps): React.ReactElement { const documentContext = useDocumentContext(); const outlineContext = useOutlineContext(); diff --git a/packages/react-pdf/src/Page.tsx b/packages/react-pdf/src/Page.tsx index 79bfd7317..32ed1b244 100644 --- a/packages/react-pdf/src/Page.tsx +++ b/packages/react-pdf/src/Page.tsx @@ -300,7 +300,7 @@ export type PageProps = { * * Should be placed inside ``. Alternatively, it can have `pdf` prop passed, which can be obtained from ``'s `onLoadSuccess` callback function, however some advanced functions like linking between pages inside a document may not be working correctly. */ -export default function Page(props: PageProps) { +export default function Page(props: PageProps): React.ReactElement { const documentContext = useDocumentContext(); const mergedProps = { ...documentContext, ...props }; diff --git a/packages/react-pdf/src/Page/AnnotationLayer.tsx b/packages/react-pdf/src/Page/AnnotationLayer.tsx index dd665c394..9e28e8136 100644 --- a/packages/react-pdf/src/Page/AnnotationLayer.tsx +++ b/packages/react-pdf/src/Page/AnnotationLayer.tsx @@ -14,7 +14,7 @@ import { cancelRunningTask } from '../shared/utils.js'; import type { Annotations } from '../shared/types.js'; -export default function AnnotationLayer() { +export default function AnnotationLayer(): React.ReactElement { const documentContext = useDocumentContext(); const pageContext = usePageContext(); diff --git a/packages/react-pdf/src/Page/Canvas.tsx b/packages/react-pdf/src/Page/Canvas.tsx index 6e4652d59..3f7f61d58 100644 --- a/packages/react-pdf/src/Page/Canvas.tsx +++ b/packages/react-pdf/src/Page/Canvas.tsx @@ -24,7 +24,7 @@ type CanvasProps = { canvasRef?: React.Ref; }; -export default function Canvas(props: CanvasProps) { +export default function Canvas(props: CanvasProps): React.ReactElement { const pageContext = usePageContext(); invariant(pageContext, 'Unable to find Page context.'); diff --git a/packages/react-pdf/src/Page/TextLayer.tsx b/packages/react-pdf/src/Page/TextLayer.tsx index dee9d2135..ca9006c06 100644 --- a/packages/react-pdf/src/Page/TextLayer.tsx +++ b/packages/react-pdf/src/Page/TextLayer.tsx @@ -17,7 +17,7 @@ function isTextItem(item: TextItem | TextMarkedContent): item is TextItem { return 'str' in item; } -export default function TextLayer() { +export default function TextLayer(): React.ReactElement { const pageContext = usePageContext(); invariant(pageContext, 'Unable to find Page context.'); diff --git a/packages/react-pdf/src/PageContext.tsx b/packages/react-pdf/src/PageContext.tsx index 4bdf4d896..28a73b5ae 100644 --- a/packages/react-pdf/src/PageContext.tsx +++ b/packages/react-pdf/src/PageContext.tsx @@ -4,4 +4,6 @@ import { createContext } from 'react'; import type { PageContextType } from './shared/types.js'; -export default createContext(null); +const pageContext: React.Context = createContext(null); + +export default pageContext; diff --git a/packages/react-pdf/src/Ref.ts b/packages/react-pdf/src/Ref.ts index c54110277..876630167 100644 --- a/packages/react-pdf/src/Ref.ts +++ b/packages/react-pdf/src/Ref.ts @@ -7,7 +7,7 @@ export default class Ref { this.gen = gen; } - toString() { + toString(): string { let str = `${this.num}R`; if (this.gen !== 0) { str += this.gen; diff --git a/packages/react-pdf/src/StructTree.tsx b/packages/react-pdf/src/StructTree.tsx index 82a0e9754..fd27c343d 100644 --- a/packages/react-pdf/src/StructTree.tsx +++ b/packages/react-pdf/src/StructTree.tsx @@ -11,7 +11,7 @@ import { cancelRunningTask } from './shared/utils.js'; import type { StructTreeNodeWithExtraAttributes } from './shared/types.js'; -export default function StructTree() { +export default function StructTree(): React.ReactElement | null { const pageContext = usePageContext(); invariant(pageContext, 'Unable to find Page context.'); diff --git a/packages/react-pdf/src/StructTreeItem.tsx b/packages/react-pdf/src/StructTreeItem.tsx index c32e731b8..361d83566 100644 --- a/packages/react-pdf/src/StructTreeItem.tsx +++ b/packages/react-pdf/src/StructTreeItem.tsx @@ -14,7 +14,10 @@ type StructTreeItemProps = { node: StructTreeNodeWithExtraAttributes | StructTreeContent; }; -export default function StructTreeItem({ className, node }: StructTreeItemProps) { +export default function StructTreeItem({ + className, + node, +}: StructTreeItemProps): React.ReactElement { const attributes = useMemo(() => getAttributes(node), [node]); // biome-ignore lint/correctness/useExhaustiveDependencies: Looks like a Biome error diff --git a/packages/react-pdf/src/Thumbnail.tsx b/packages/react-pdf/src/Thumbnail.tsx index 88fc33d02..76e7c6b25 100644 --- a/packages/react-pdf/src/Thumbnail.tsx +++ b/packages/react-pdf/src/Thumbnail.tsx @@ -48,7 +48,7 @@ export type ThumbnailProps = Omit< * * Should be placed inside ``. Alternatively, it can have `pdf` prop passed, which can be obtained from ``'s `onLoadSuccess` callback function. */ -export default function Thumbnail(props: ThumbnailProps) { +export default function Thumbnail(props: ThumbnailProps): React.ReactElement { const documentContext = useDocumentContext(); const mergedProps = { ...documentContext, ...props }; diff --git a/packages/react-pdf/src/shared/constants.ts b/packages/react-pdf/src/shared/constants.ts index ba4810d95..54640494a 100644 --- a/packages/react-pdf/src/shared/constants.ts +++ b/packages/react-pdf/src/shared/constants.ts @@ -56,4 +56,4 @@ export const PDF_ROLE_TO_HTML_ROLE = { Artifact: null, }; -export const HEADING_PATTERN = /^H(\d+)$/; +export const HEADING_PATTERN: RegExp = /^H(\d+)$/; diff --git a/packages/react-pdf/src/shared/hooks/useDocumentContext.ts b/packages/react-pdf/src/shared/hooks/useDocumentContext.ts index 428ea9ba0..c180fd69b 100644 --- a/packages/react-pdf/src/shared/hooks/useDocumentContext.ts +++ b/packages/react-pdf/src/shared/hooks/useDocumentContext.ts @@ -2,6 +2,8 @@ import { useContext } from 'react'; import DocumentContext from '../../DocumentContext.js'; -export default function useDocumentContext() { +import type { DocumentContextType } from '../types.js'; + +export default function useDocumentContext(): DocumentContextType { return useContext(DocumentContext); } diff --git a/packages/react-pdf/src/shared/hooks/useOutlineContext.ts b/packages/react-pdf/src/shared/hooks/useOutlineContext.ts index 8ea2877fb..ea30d1d86 100644 --- a/packages/react-pdf/src/shared/hooks/useOutlineContext.ts +++ b/packages/react-pdf/src/shared/hooks/useOutlineContext.ts @@ -2,6 +2,8 @@ import { useContext } from 'react'; import OutlineContext from '../../OutlineContext.js'; -export default function useOutlineContext() { +import type { OutlineContextType } from '../types.js'; + +export default function useOutlineContext(): OutlineContextType { return useContext(OutlineContext); } diff --git a/packages/react-pdf/src/shared/hooks/usePageContext.ts b/packages/react-pdf/src/shared/hooks/usePageContext.ts index dc7e86087..22578488e 100644 --- a/packages/react-pdf/src/shared/hooks/usePageContext.ts +++ b/packages/react-pdf/src/shared/hooks/usePageContext.ts @@ -2,6 +2,8 @@ import { useContext } from 'react'; import PageContext from '../../PageContext.js'; -export default function usePageContext() { +import type { PageContextType } from '../types.js'; + +export default function usePageContext(): PageContextType { return useContext(PageContext); } diff --git a/packages/react-pdf/src/shared/hooks/useResolver.ts b/packages/react-pdf/src/shared/hooks/useResolver.ts index c556b53e1..65c9432f0 100644 --- a/packages/react-pdf/src/shared/hooks/useResolver.ts +++ b/packages/react-pdf/src/shared/hooks/useResolver.ts @@ -23,6 +23,6 @@ function reducer(state: State, action: Action): State { } } -export default function useResolver() { +export default function useResolver(): [State, React.Dispatch>] { return useReducer(reducer, { value: undefined, error: undefined }); } diff --git a/packages/react-pdf/src/shared/utils.ts b/packages/react-pdf/src/shared/utils.ts index db3a4bf04..5abf8dfcf 100644 --- a/packages/react-pdf/src/shared/utils.ts +++ b/packages/react-pdf/src/shared/utils.ts @@ -7,12 +7,12 @@ import type { PageCallback } from './types.js'; /** * Checks if we're running in a browser environment. */ -export const isBrowser = typeof document !== 'undefined'; +export const isBrowser: boolean = typeof document !== 'undefined'; /** * Checks whether we're running from a local file system. */ -export const isLocalFileSystem = isBrowser && window.location.protocol === 'file:'; +export const isLocalFileSystem: boolean = isBrowser && window.location.protocol === 'file:'; /** * Checks whether a variable is defined. @@ -83,28 +83,28 @@ export function dataURItoByteString(dataURI: unknown): string { return unescape(dataString); } -export function getDevicePixelRatio() { +export function getDevicePixelRatio(): number { return (isBrowser && window.devicePixelRatio) || 1; } const allowFileAccessFromFilesTip = 'On Chromium based browsers, you can use --allow-file-access-from-files flag for debugging purposes.'; -export function displayCORSWarning() { +export function displayCORSWarning(): void { warning( !isLocalFileSystem, `Loading PDF as base64 strings/URLs may not work on protocols other than HTTP/HTTPS. ${allowFileAccessFromFilesTip}`, ); } -export function displayWorkerWarning() { +export function displayWorkerWarning(): void { warning( !isLocalFileSystem, `Loading PDF.js worker may not work on protocols other than HTTP/HTTPS. ${allowFileAccessFromFilesTip}`, ); } -export function cancelRunningTask(runningTask?: { cancel?: () => void } | null) { +export function cancelRunningTask(runningTask?: { cancel?: () => void } | null): void { if (runningTask?.cancel) runningTask.cancel(); }