Skip to content

Refactor/class to func component #44

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

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 29 additions & 29 deletions src/PrintProvider.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,32 @@ export default function PrintProvider (props) {
const printableRegistry = useRef({});
const hasSingle = useRef(false);

const loose = props.loose || props.invert;
const loose_ = loose ? s.loose : '';
const invert_ = props.invert ? s.invert : '';

useEffect(() => {
window.matchMedia('print').onchange = () => {
debug('toggle print mode', window.matchMedia('print').matches);
setIsInPrintPreview(window.matchMedia('print').matches);
};
}, []);

useEffect(() => {
if (isInPrintPreview && printableNodes.length && !loose && !hasSingle.current) {
debug('render printable only', printableNodes);
const children = React.Children.map(printableNodes, (child, key) => {
return React.cloneElement(child, { key });
});
createRender(<div>{children}</div>);
}
setTimeout(() => deleteRender(), 0);
});

useEffect(() => {
debug('render everything', isInPrintPreview, printableNodes.length, !loose);
}, [isInPrintPreview, printableNodes.length, loose]);

// hideAll - is being used to cover all of React Portals, popups and modals and etc.
const hideAll = () => {
document.body.classList.add(s.hiddenAll);
Expand All @@ -58,7 +84,7 @@ export default function PrintProvider (props) {
);
printableRegistry.current[key] = printableNodes.length;

if (isSingle && !hasSingle) {
if (isSingle && !hasSingle.current) {
hideAll();
} else if (isSingle) {
console.warn(
Expand All @@ -83,40 +109,14 @@ export default function PrintProvider (props) {
printableRegistry.current[key]
)
);
printableRegistry.current = {
...printableRegistry.current,
printableRegistry.current = Object.assign({}, printableRegistry.current, {
[key]: undefined
};
});
if (isSingle) {
unhideAll();
}
};

useEffect(() => {
window.matchMedia('print').onchange = () => {
debug('toggle print mode', window.matchMedia('print').matches);
setIsInPrintPreview(window.matchMedia('print').matches);
};
});

useEffect(() => {
if (isInPrintPreview && printableNodes.length && !loose && !hasSingle) {
debug('render printable only', printableNodes);
const children = React.Children.map(printableNodes, (child, key) => {
return React.cloneElement(child, { key });
});
createRender(<div>{children}</div>);
}
setTimeout(() => deleteRender(), 0);
});

useEffect(() => {
debug('render everything', isInPrintPreview, printableNodes.length, !loose);
});

const loose = props.loose || props.invert;
const loose_ = loose ? s.loose : '';
const invert_ = props.invert ? s.invert : '';
return (
<PrintProviderContext.Provider unregPrintable={unregPrintable} regPrintable={regPrintable}>
<div className={`${s.wrap} ${loose_} ${invert_} `}>
Expand Down