-
-
Notifications
You must be signed in to change notification settings - Fork 903
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
How to prevent the white 'flash' while changing slides? #418
Comments
You could render the new Page hidden using CSS, wait for |
Thank you. |
Can you please tell me how to do this ? |
So, in their Page component I have added the onRenderSuccess prop that sets a loading boolean state that I have created in my PDF component that uses their components: Also, I added my custom default class to their page component: Then.. on their Document component I have added the native className prop: My custom CSS to remove the white flash:
Summary: Start the Page component as display none until the onRenderSuccess is executed and sets the loading to false, then the pdf-loaded class is added in the page component which sets the display to inline-block, avoiding the whole flash thing. |
Kinda similar so I wanted to avoid opening a new issue, is there a way to prevent the white flash when changing the I'm not sure what would be the best solution to handle this, maybe a similar solution to switching pages (keeping the old document rendered until the new one has finished loading) but im not sure if that's possible. |
@saadq i follow the method mentioned for generating pdf where u generate a pdf file in backend server and render it my ui side. Actually if u notice u will get the white flash for the initial rendering of each pages & subsequent changes wont give u the flicker. I dont think there is a workable solution on this at the moment |
@igoroctaviano can you share the codes regarding this issue? I follow your instruction still getting the flash when slide changes. |
The best way to avoid flashing is to keep the previous page rendered on top of current page, until the next one is fully rendered: function Test() {
const [numPages, setNumPages] = useState(null);
const [pageNumber, setPageNumber] = useState(1);
const [renderedPageNumber, setRenderedPageNumber] = useState(null);
function onDocumentLoadSuccess({ numPages }) {
setNumPages(numPages);
}
function changePage(offset) {
setPageNumber(prevPageNumber => prevPageNumber + offset);
}
function previousPage() {
changePage(-1);
}
function nextPage() {
changePage(1);
}
const isLoading = renderedPageNumber !== pageNumber;
return (
<>
<Document file={samplePDF} onLoadSuccess={onDocumentLoadSuccess}>
{isLoading && renderedPageNumber ? (
<Page
{/**
* IMPORTANT: Keys are necessary so that React will know which Page component
* instances to use.
* Without keys, on page number update, React would replace the page number
* in 1st and 2nd page components. This may cause previously rendered page
* to render again, thus causing a flash.
* With keys, React, will add prevPage className to previously rendered page,
* and mount new Page component instance for the new page.
*/}
key={renderedPageNumber}
className="prevPage"
pageNumber={renderedPageNumber}
width={400}
/>
) : null}
<Page
key={pageNumber}
pageNumber={pageNumber}
onRenderSuccess={() => setRenderedPageNumber(pageNumber)}
width={400}
/>
</Document>
<div>
<p>
Page {pageNumber || (numPages ? 1 : "--")} of {numPages || "--"}
</p>
<button type="button" disabled={pageNumber <= 1} onClick={previousPage}>
Previous
</button>
<button
type="button"
disabled={pageNumber >= numPages}
onClick={nextPage}
>
Next
</button>
</div>
</>
);
} .react-pdf__Page.prevPage {
position: absolute !important;
z-index: 1;
} |
@wojtekmaj awesome! thank you. |
Still works! Thanks @wojtekmaj |
Instead of positioning the {isLoading && renderedPageNumber ? (
<Page
key={renderedPageNumber}
pageNumber={renderedPageNumber}
/>
) : null}
<Page
className={`${isLoading ? 'loadingPage' : ''}`}
key={pageNumber}
pageNumber={pageNumber}
onRenderSuccess={() => setRenderedPageNumber(pageNumber)}
/> .react-pdf__Page.loadingPage {
display: none;
} BTW, awesome library. I'm loving it! |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
I have tried this solution but it will not work if you render more than one page. The second page will still be flicked. This is the sandbox that I re-produce bug |
I handled it as follows: const [numPages, setNumPages] = useState<number>()
const [pageNumber, setPageNumber] = useState<number>(1)
const [previewPageNumber, setPreviewPageNumber] = useState<number>(1)
const [renderPageNumber, setRenderPageNumber] = useState<number>(1)
const [isRendering, setIsRendering] = useState<boolean>(false)
const handleChangePage = (pageNumber: number) => {
setIsRendering(true)
setRenderPageNumber(pageNumber)
}
const handleRenderPageLoadedSuccess = () => {
setIsRendering(false)
setPreviewPageNumber(renderPageNumber)
} <Document
file={file}
onLoadSuccess={onDocumentLoadSuccess}
>
<Page
className={`${!isRendering ? 'hidden' : ''}`}
key={`preview-${previewPageNumber}`}
pageNumber={previewPageNumber}
width={width}
/>
<Page
className={`${isRendering ? 'hidden' : ''}`}
key={`render-${renderPageNumber}`}
pageNumber={renderPageNumber}
width={width}
onLoadSuccess={handleRenderPageLoadedSuccess}
/>
</Document> |
I'd like to display multiple pages of my PDF and move through them without white flash (dark background).
I tried changing the CSS.
Environment
The text was updated successfully, but these errors were encountered: