-
-
Notifications
You must be signed in to change notification settings - Fork 225
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update
devDependencies
, break up and organize code (#683)
- Loading branch information
1 parent
3cc32aa
commit 756888b
Showing
12 changed files
with
1,051 additions
and
734 deletions.
There are no files selected for viewing
This file contains 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 characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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 characters
This file contains 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 characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import * as React from "react"; | ||
|
||
const contextEnabled = Object.prototype.hasOwnProperty.call(React, "createContext"); | ||
|
||
export interface IPrintContextProps { | ||
handlePrint: (event: unknown, content?: (() => React.ReactInstance | null)) => void, | ||
} | ||
|
||
export const PrintContext = contextEnabled ? React.createContext({} as IPrintContextProps) : null; | ||
export const PrintContextConsumer = PrintContext ? PrintContext.Consumer : () => null; |
This file contains 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 characters
This file contains 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 characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// NOTE: https://github.com/Microsoft/TypeScript/issues/23812 | ||
export const defaultProps = { | ||
copyStyles: true, | ||
pageStyle: ` | ||
@page { | ||
/* Remove browser default header (title) and footer (url) */ | ||
margin: 0; | ||
} | ||
@media print { | ||
body { | ||
/* Tell browsers to print background colors */ | ||
-webkit-print-color-adjust: exact; /* Chrome/Safari/Edge/Opera */ | ||
color-adjust: exact; /* Firefox */ | ||
} | ||
} | ||
`, | ||
removeAfterPrint: false, | ||
suppressErrors: false, | ||
}; |
This file contains 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 characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import * as React from "react"; | ||
|
||
import { ReactToPrint } from "../components/ReactToPrint"; | ||
import { defaultProps } from "../consts/defaultProps"; | ||
import type { IReactToPrintProps } from "../types/reactToPrintProps"; | ||
import { wrapCallbackWithArgs } from "../utils/wrapCallbackWithArgs"; | ||
|
||
type UseReactToPrintHookReturn = ( | ||
event: unknown, | ||
content?: (() => React.ReactInstance | null) | ||
) => void; | ||
|
||
const hooksEnabled = Object.prototype.hasOwnProperty.call(React, "useMemo") && Object.prototype.hasOwnProperty.call(React, "useCallback"); | ||
|
||
export const useReactToPrint = (props: IReactToPrintProps): UseReactToPrintHookReturn => { | ||
if (!hooksEnabled) { | ||
if (!props.suppressErrors) { | ||
console.error('"react-to-print" requires React ^16.8.0 to be able to use "useReactToPrint"'); // eslint-disable-line no-console | ||
} | ||
|
||
return () => { | ||
throw new Error('"react-to-print" requires React ^16.8.0 to be able to use "useReactToPrint"'); | ||
}; | ||
} | ||
|
||
const reactToPrint = React.useMemo( | ||
// TODO: is there a better way of applying the defaultProps? | ||
() => new ReactToPrint({ ...defaultProps, ...props }), | ||
[props] | ||
); | ||
|
||
return React.useCallback( | ||
(event: unknown, content?: (() => React.ReactInstance | null)) => { | ||
/* eslint-disable-next-line @typescript-eslint/unbound-method */ | ||
const triggerPrint = wrapCallbackWithArgs(reactToPrint, reactToPrint.handleClick, content); | ||
// NOTE: `event` is a no-use argument | ||
// (necessary for backward compatibility with older versions) | ||
return triggerPrint(event); | ||
}, [reactToPrint]); | ||
}; |
This file contains 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 characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
export type { IPrintContextProps } from "./components/PrintContext"; | ||
export { PrintContextConsumer } from "./components/PrintContext"; | ||
export { ReactToPrint } from "./components/ReactToPrint"; | ||
export { useReactToPrint } from "./hooks/useReactToPrint"; | ||
export type { IReactToPrintProps, ITriggerProps } from "./types/reactToPrintProps"; | ||
|
||
import { ReactToPrint } from "./components/ReactToPrint"; | ||
export default ReactToPrint; | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
This file contains 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 characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
// https://developer.mozilla.org/en-US/docs/Web/API/FontFace/FontFace | ||
export type Font = { | ||
family: string; | ||
source: string; | ||
weight?: string; | ||
style?: string; | ||
}; |
Oops, something went wrong.