Skip to content

Commit

Permalink
feat: enable PDFDownloadLink to Receive Anchor Props (#2071)
Browse files Browse the repository at this point in the history
* feat: Enable `PDFDownloadLink` to Receive Anchor Props

Changes:
- Pass ALL anchor tag props to the rendered anchor
  element -- EXCLUDING the reserved `href` attribute.
- Improve the `onClick` type of `PDFDownloadLink`.
- Minor cleanup in related files.

* feat: preserve doc var

* fix: preserve blank line

---------

Co-authored-by: Diego Muracciole <[email protected]>
  • Loading branch information
ITenthusiasm and diegomura authored Jan 14, 2024
1 parent 93eafb0 commit 8b00577
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 11 deletions.
9 changes: 4 additions & 5 deletions packages/renderer/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -459,15 +459,14 @@ declare namespace ReactPDF {
*/
class PDFViewer extends React.Component<PDFViewerProps> {}

interface PDFDownloadLinkProps {
document: React.ReactElement<DocumentProps>;
interface PDFDownloadLinkProps extends Omit<React.AnchorHTMLAttributes<HTMLAnchorElement>, "href"> {
/** PDF filename. Alias for anchor tag `download` attribute. */
fileName?: string;
style?: Style | Style[];
className?: string;
document: React.ReactElement<DocumentProps>;
children?:
| React.ReactNode
| ((params: BlobProviderParams) => React.ReactNode);
onClick?: () => void;
onClick?(event: React.MouseEvent<HTMLAnchorElement, MouseEvent>, instance: UsePDFInstance): void;
}

/**
Expand Down
11 changes: 5 additions & 6 deletions packages/renderer/src/dom/PDFDownloadLink.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import { useEffect } from 'react';
import usePDF from './usePDF';

export const PDFDownloadLink = ({
style,
children,
className,
document: doc,
fileName = 'document.pdf',
document: doc,
children,
onClick,
href: _filteredOutHref,
...rest
}) => {
const [instance, updateInstance] = usePDF();

Expand All @@ -35,11 +35,10 @@ export const PDFDownloadLink = ({

return (
<a
style={style}
href={instance.url}
download={fileName}
className={className}
onClick={handleClick}
{...rest}
>
{typeof children === 'function' ? children(instance) : children}
</a>
Expand Down

0 comments on commit 8b00577

Please sign in to comment.