Last active
January 29, 2024 19:17
-
-
Save FredrikAugust/e2c5f7c5865be41fa52ab4fd9a5d2853 to your computer and use it in GitHub Desktop.
Revisions
-
FredrikAugust revised this gist
Jan 17, 2023 . 2 changed files with 43 additions and 18 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,29 +1,16 @@ /** * Allows you to subscribe to whether or not the window is currently printing (user invoked the system print dialog). * * @returns Whether or not the document is currently printing. */ export default function useIsPrinting(): boolean { const [printing, setIsPrinting] = useState( window.matchMedia('print').matches ); useEffect(() => { const beforeprint = () => setIsPrinting(true); const afterprint = () => setIsPrinting(false); window.addEventListener('beforeprint', beforeprint); window.addEventListener('afterprint', afterprint); @@ -32,7 +19,7 @@ export default function useIsPrinting(options?: UseIsPrintingOptions): boolean { window.removeEventListener('beforeprint', beforeprint); window.removeEventListener('afterprint', afterprint); }; }, []); return printing; } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,38 @@ type UseIsPrintingOptions = { beforePrintCallback: () => void; afterPrintCallback: () => void; }; /** * Allows you to subscribe to whether or not the window is currently printing (user invoked the system print dialog). * * @param options An optional object with callbacks to be invoked when the window goes into and/or out of printing mode. * @returns Whether or not the document is currently printing. */ export default function useIsPrinting(options?: UseIsPrintingOptions): boolean { const [printing, setIsPrinting] = useState( window.matchMedia('print').matches ); useEffect(() => { const beforeprint = () => { options?.beforePrintCallback?.call(null); setIsPrinting(true); }; const afterprint = () => { options?.afterPrintCallback?.call(null); setIsPrinting(false); }; window.addEventListener('beforeprint', beforeprint); window.addEventListener('afterprint', afterprint); return () => { window.removeEventListener('beforeprint', beforeprint); window.removeEventListener('afterprint', afterprint); }; }, [options?.afterPrintCallback, options?.beforePrintCallback]); return printing; } -
FredrikAugust created this gist
Jan 17, 2023 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,38 @@ type UseIsPrintingOptions = { beforePrintCallback: () => void; afterPrintCallback: () => void; }; /** * Allows you to subscribe to whether or not the window is currently printing (user invoked the system print dialog). * * @param options An optional object with callbacks to be invoked when the window goes into and/or out of printing mode. * @returns Whether or not the document is currently printing. */ export default function useIsPrinting(options?: UseIsPrintingOptions): boolean { const [printing, setIsPrinting] = useState( window.matchMedia('print').matches ); useEffect(() => { const beforeprint = () => { options?.beforePrintCallback?.call(null); setIsPrinting(true); }; const afterprint = () => { options?.afterPrintCallback?.call(null); setIsPrinting(false); }; window.addEventListener('beforeprint', beforeprint); window.addEventListener('afterprint', afterprint); return () => { window.removeEventListener('beforeprint', beforeprint); window.removeEventListener('afterprint', afterprint); }; }, [options?.afterPrintCallback, options?.beforePrintCallback]); return printing; }