From 18b27d96332fc1c72ab246cece0909ec77b24eed Mon Sep 17 00:00:00 2001 From: Matthew Herbst Date: Mon, 15 Jul 2024 01:42:43 -0700 Subject: [PATCH] React 19 Support (#717) Co-authored-by: Matt Herbst --- . browserslistrc | 4 - README.md | 250 ++---- examples/BasicComponent/index.tsx | 40 + examples/ClassComponent/index.tsx | 83 -- .../ClassComponentContextConsumer/index.tsx | 79 -- examples/ClassComponentText/index.tsx | 64 -- examples/ComponentToPrint/index.tsx | 334 ++++---- examples/CustomPrint/index.tsx | 98 +-- examples/FunctionalComponent/index.tsx | 76 -- .../index.tsx | 76 -- .../FunctionalComponentWithHook/index.tsx | 68 -- .../index.tsx | 67 -- examples/LazyContent/index.tsx | 27 + examples/OnBeforePrint/index.tsx | 66 ++ examples/{index.d.ts => extensions.d.ts} | 0 examples/fonts.d.ts | 6 + examples/index.tsx | 89 +-- package-lock.json | 716 +++++++++++------- package.json | 13 +- src/components/PrintContext.tsx | 10 - src/components/ReactToPrint.tsx | 522 ------------- src/consts/defaultProps.ts | 19 - src/hooks/useReactToPrint.ts | 145 +++- src/index.ts | 17 +- src/types/ContentNode.ts | 1 + src/types/UseReactToPrintFn.ts | 3 + src/types/UseReactToPrintHookContent.ts | 3 + src/types/UseReactToPrintOptions.ts | 57 ++ src/types/reactToPrintProps.ts | 46 -- src/utils/generatePrintWindow.ts | 14 + src/utils/getContentNode.ts | 33 + src/utils/handleOnBeforePrint.ts | 28 + src/utils/handlePrintWindowOnLoad.ts | 293 +++++++ src/utils/logMessage.ts | 20 + src/utils/removePrintIframe.ts | 11 + src/utils/startPrint.ts | 88 +++ src/utils/wrapCallbackWithArgs.ts | 13 - tsconfig.json | 32 +- 38 files changed, 1574 insertions(+), 1937 deletions(-) delete mode 100644 . browserslistrc create mode 100644 examples/BasicComponent/index.tsx delete mode 100644 examples/ClassComponent/index.tsx delete mode 100644 examples/ClassComponentContextConsumer/index.tsx delete mode 100644 examples/ClassComponentText/index.tsx delete mode 100644 examples/FunctionalComponent/index.tsx delete mode 100644 examples/FunctionalComponentWithFunctionalComponentToPrint/index.tsx delete mode 100644 examples/FunctionalComponentWithHook/index.tsx delete mode 100644 examples/FunctionalComponentWithHookAndLazyContent/index.tsx create mode 100644 examples/LazyContent/index.tsx create mode 100644 examples/OnBeforePrint/index.tsx rename examples/{index.d.ts => extensions.d.ts} (100%) create mode 100644 examples/fonts.d.ts delete mode 100644 src/components/PrintContext.tsx delete mode 100644 src/components/ReactToPrint.tsx delete mode 100644 src/consts/defaultProps.ts create mode 100644 src/types/ContentNode.ts create mode 100644 src/types/UseReactToPrintFn.ts create mode 100644 src/types/UseReactToPrintHookContent.ts create mode 100644 src/types/UseReactToPrintOptions.ts delete mode 100644 src/types/reactToPrintProps.ts create mode 100644 src/utils/generatePrintWindow.ts create mode 100644 src/utils/getContentNode.ts create mode 100644 src/utils/handleOnBeforePrint.ts create mode 100644 src/utils/handlePrintWindowOnLoad.ts create mode 100644 src/utils/logMessage.ts create mode 100644 src/utils/removePrintIframe.ts create mode 100644 src/utils/startPrint.ts delete mode 100644 src/utils/wrapCallbackWithArgs.ts diff --git a/. browserslistrc b/. browserslistrc deleted file mode 100644 index b01ef37..0000000 --- a/. browserslistrc +++ /dev/null @@ -1,4 +0,0 @@ -# Browsers that we support - -defaults -IE11 diff --git a/README.md b/README.md index 42ba43d..7491ae1 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ [![NPM Downloads](https://img.shields.io/npm/dt/react-to-print.svg?style=flat)](https://npmcharts.com/compare/react-to-print?minimal=true) [![npm version](https://badge.fury.io/js/react-to-print.svg)](https://badge.fury.io/js/react-to-print) -So you've created a React component and would love to give end users the ability to print out the contents of that component. This package aims to solve that by popping up a print window with CSS styles copied over as well. +Print the content of a React component. ## Demo @@ -19,193 +19,65 @@ So you've created a React component and would love to give end users the ability ## API -### <ReactToPrint /> +### Usage -The component accepts the following props: +```tsx +const contentRef = useRef(null); +const handlePrint = useReactToPrint({ contentRef }); + +return ( +
+ +
Content to print
+
+); +``` + +### Options -| Name | Type | Description | +| Option | Type | Description | | :-------------------: | :------- | :---------------------------------------------------------------------------------------------------------------------------------- | | **`bodyClass?`** | `string` | One or more class names to pass to the print window, separated by spaces | -| **`content?`** | `function` | A function that returns a component reference value. The content of this reference value is then used for print. Alternatively, pass the content directly to the callback returned by `useReactToPrint` | -| **`copyStyles?`** | `boolean` | Copy all ` - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Test NameTest
- Test loading {""}s with multi-value `rel` - -
Purple Background
-
- Test loading {""}s with `as="style"` - -
Blue Background
-
Canvass - - Your browser does not support the HTML5 canvas tag. - -
Dynamic Content From Prop{text ?? 'Custom Text Here'}
Fonts -
Some Cool Font Text
-
123456789
-
- Some Cool Font Text -
-
Image: Local ImportA test image
Image: URL - Google logo -
Input
Input: Checkbox - -
Input: Date
Input: Radio - Blue - Red -
Select - -
TextArea -