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

Flashing on resize #1705

Closed
4 tasks done
jonahallibone opened this issue Jan 30, 2024 · 3 comments
Closed
4 tasks done

Flashing on resize #1705

jonahallibone opened this issue Jan 30, 2024 · 3 comments
Labels
question Further information is requested

Comments

@jonahallibone
Copy link

Before you start - checklist

  • I followed instructions in documentation written for my React-PDF version
  • I have checked if this bug is not already reported
  • I have checked if an issue is not listed in Known issues
  • If I have a problem with PDF rendering, I checked if my PDF renders properly in PDF.js demo

Description

I am trying to make my PDF responsive, however, I continually run into the same issue where the PDF pages disappear and reappear during the render phase. This causes my container to lose height and thus resets the scroll, which is a 2nd order problem caused by this behavior. In the other threads I've seen about responsive width, I haven't seen anyone document this issue.. Is there any solution? I have already implemented the suggested fix for scaling (having two pages render depending on if the scale has changed or not). I was wondering if there was some similar work around for sizing, as I have not been able to figure it out.

Kapture.2024-01-29.at.22.48.11.mp4

Steps to reproduce

This is roughly what my code looks like:

<PdfViewerContainer>
  <ChakraReactPdfDocument
    file={pdfUrl}
    onLoadSuccess={onDocumentLoadSuccess}
    loading={<Spinner />}
    height="min-content"
    minH="100%"
    margin="auto"
  >
    {display === "scroll" ? (
      pageArray.map((page) => (
        <Box
          boxShadow="xl"
          w="min-content"
          key={page}
          _notLast={{ mb: 3 }}
          marginLeft="auto"
          width={`${containerWidth - 32}px`}
        >
          {isPageRendering(page) ? (
            <Page
              key={`${page}@${getPageScale(page)}`}
              className="prevPage"
              pageNumber={page}
              scale={renderedPageScale[page]}
              width={containerWidth - 32}
            />
          ) : null}
          <Page
            key={`${page}@${scaleAsNumber}`}
            pageNumber={page}
            scale={scaleAsNumber}
            inputRef={(node) => setPageRef(node, page)}
            width={containerWidth - 32}
            onRenderSuccess={onRenderSuccess}
          />
        </Box>
      ))
    ) : (
      <Page pageNumber={Number(pageNumber)} />
    )}
  </ChakraReactPdfDocument>
</PdfViewerContainer>

Expected behavior

I expect the PDF to not flash when resizing it's parent container.

Actual behavior

The PDF disappears and reappears.

Additional information

No response

Environment

  • Browser (if applicable): Chrome 120.0.6099.234
  • React-PDF version: 7.7.0
  • React version: 18.2.0
  • Webpack version (if applicable):
@jonahallibone jonahallibone added the bug Something isn't working label Jan 30, 2024
@wojtekmaj wojtekmaj added question Further information is requested and removed bug Something isn't working labels Feb 1, 2024
@wojtekmaj
Copy link
Owner

Duplicate of #418

@wojtekmaj wojtekmaj marked this as a duplicate of #418 Feb 1, 2024
@wojtekmaj wojtekmaj closed this as not planned Won't fix, can't repro, duplicate, stale Feb 1, 2024
@trillium
Copy link

trillium commented Apr 4, 2024

Hey there,

Thanks for putting this project together, I'm using it to display a resume pdf on my portfolio site.

I'm having this problem as well where the container loses height on resize. It's a behaviour that exists in the next-app example as well. It's not a huge deal for me, but if the user changes their window width I'd like that to be as seamless as possible.

Screen.Recording.2024-04-04.at.1.58.54.PM.mp4

In my usecase all the pages are rendered in sequence just like in the next-app example, so when the canvas loses height I get a series of flickering boxes for each page before rerender.

I tried to implement the solution from #418 but couldn't get it figured out.

Steps to reproduce

  • install/run example from sample/next-app
  • open dev tools
  • resize window to below maxiumum width

@kauly
Copy link

kauly commented Jun 21, 2024

doing something like this fixed the problem here

  const onResize = useCallback<ResizeObserverCallback>((entries) => {
    const [entry] = entries

    if (entry) {
      setContainerWidth((p = 1) => {
        const newWidth = entry.contentRect.width
        return Math.abs(p - newWidth) > 50 ? newWidth : p
      })
    }
  }, [])

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

4 participants