MatthewHerbst / react-to-print

Print React components in the browser. Supports Chrome, Safari, Firefox and EDGE
MIT License
2.06k stars 221 forks source link

How to check that the user click on Save in `onAfterPrint`? #693

Closed alamenai closed 6 months ago

alamenai commented 6 months ago

Hello,

I have this code using useReactToPrint:


  const { toast } = useToast()

  const print = useReactToPrint({
    content: () => componentRef.current,
    pageStyle: "padding: 2px",
    fonts: [
      {
        family: "Inter",
        source: "https://fonts.googleapis.com/css2?family=Inter&display=swap",
      },
    ],
    documentTitle,
    onAfterPrint() {
      toast({ title: "Gerettet", description: "Ihr Standard-Installationsbericht wurde erfolgreich heruntergeladen." })
    },
  })

I'm using Taost from Shadcn library and I used it to display success when the use save the PDF.

However, I want to display it only when the use click Save but not Cancel.

Thank you.

MatthewHerbst commented 6 months ago

Hello. Unfortunately there isn't a good way to do this today, the browser does not distinguish between these two actions. As you can see in the documentation for the afterprint event, all we know is that the print preview was closed; we don't know why it was closed.

If you don't mind an opinion on this: the user knows when they click Save or Cancel, and the user trusts the browser; you don't need to show a success toast for this.

Closing just because there is no bug here, but happy to keep discussing, I've always wanted to solve this, but I believe it will take a browser standards change to do it properly.

alamenai commented 6 months ago

Thank you @MatthewHerbst . Yes, as you mentioned, 'The user trusts the Browser'.