Skip to content

Commit

Permalink
fix fonts not loading in pdf (#928)
Browse files Browse the repository at this point in the history
  • Loading branch information
bjosttveit authored Feb 15, 2023
1 parent 5a061a4 commit da8906e
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
4 changes: 4 additions & 0 deletions src/setupTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ Object.defineProperty(window, 'matchMedia', {
})),
});

Object.defineProperty(document, 'fonts', {
value: { ready: Promise.resolve({}) },
});

// org and app is assigned to window object, so to avoid 'undefined' in tests, they need to be set
const altinnWindow = window as Window as IAltinnWindow;
altinnWindow.org = 'ttd';
Expand Down
8 changes: 5 additions & 3 deletions src/shared/components/ReadyForPrint.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ import React from 'react';
* loading indicators to the user while waiting for content to get ready.
*/
export function ReadyForPrint() {
const [imagesLoaded, setImagesLoaded] = React.useState(false);
const [assetsLoaded, setAssetsLoaded] = React.useState(false);

React.useLayoutEffect(() => {
const promises: Promise<unknown>[] = [];

promises.push(document.fonts.ready);

const imageLoadPromise = (img: HTMLImageElement) => {
return new Promise((res) => {
img.addEventListener('load', res);
Expand All @@ -29,13 +31,13 @@ export function ReadyForPrint() {
// element after all images have been rendered.
requestAnimationFrame(() => {
requestAnimationFrame(() => {
setImagesLoaded(true);
setAssetsLoaded(true);
});
});
});
}, []);

if (!imagesLoaded) {
if (!assetsLoaded) {
return null;
}

Expand Down

0 comments on commit da8906e

Please sign in to comment.