Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Add custom id to Iframe body #785

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ It is also possible to lazy set the ref if your content being printed is dynamic
| **`preserveAfterPrint`** | `boolean` | Preserve the print iframe after printing. This can be useful for debugging by inspecting the print iframe |
| **`print`** | `(iframe: HTMLIFrameElement) => Promise<void>` | If passed, this function will be used instead of `window.print` to print the content. Use this to print in non-browser environments such as Electron |
| **`suppressErrors`** | `boolean` | When passed, prevents `console` logging of errors |
| **`iframeBodyId`** | `string` | Set custom id to iframe body |

## Compatibility

Expand Down
2 changes: 2 additions & 0 deletions src/types/UseReactToPrintOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,6 @@ export interface UseReactToPrintOptions {
suppressErrors?: boolean;
/** When passed, shadow root content will be copied */
copyShadowRoots?: boolean;
/** Add custom id to iframe body id */
iframeBodyId?: string;
}
18 changes: 11 additions & 7 deletions src/utils/handlePrintWindowOnLoad.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {logMessages} from "./logMessage";
import {startPrint} from "./startPrint";
import {Font} from "../types/font";
import type {UseReactToPrintOptions} from "../types/UseReactToPrintOptions";
import { logMessages } from "./logMessage";
import { startPrint } from "./startPrint";
import { Font } from "../types/font";
import type { UseReactToPrintOptions } from "../types/UseReactToPrintOptions";
import { cloneShadowRoots } from "./clone";
import { getErrorFromUnknown } from "./getErrorMessage";

Expand Down Expand Up @@ -62,7 +62,8 @@ export function handlePrintWindowOnLoad(
pageStyle,
nonce,
suppressErrors,
copyShadowRoots
copyShadowRoots,
iframeBodyId
} = options;

// Some agents, such as IE11 and Enzyme (as of 2 Jun 2020) continuously call the
Expand All @@ -72,6 +73,9 @@ export function handlePrintWindowOnLoad(
const domDoc = printWindow.contentDocument ?? printWindow.contentWindow?.document;

if (domDoc) {
if (iframeBodyId) {
domDoc.body.id = iframeBodyId;
}
const appendedContentNode = domDoc.body.appendChild(clonedContentNode);
if (copyShadowRoots) {
cloneShadowRoots(contentNode, appendedContentNode, !!suppressErrors);
Expand All @@ -83,7 +87,7 @@ export function handlePrintWindowOnLoad(
const fontFace = new FontFace(
font.family,
font.source,
{weight: font.weight, style: font.style}
{ weight: font.weight, style: font.style }
);
printWindow.contentDocument!.fonts.add(fontFace);
fontFace.loaded
Expand Down Expand Up @@ -191,7 +195,7 @@ export function handlePrintWindowOnLoad(
} else if (!videoNode.src) {
// There are scenarios where `src` can be empty, for example with lazy loading.
markLoaded(videoNode, ["Error loading video, `src` is empty", videoNode]);
} else{
} else {
videoNode.onloadeddata = () => {
markLoaded(videoNode)
};
Expand Down