Skip to content
Closed
Changes from all 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
21 changes: 11 additions & 10 deletions src/lib/handleViewport.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
// HOC for handleViewport
import React, { useRef, forwardRef } from 'react';
import hoistNonReactStatic from 'hoist-non-react-statics';
import useInViewport from './useInViewport';

const noop = () => {};

function handleViewport(TargetComponent, options, config = { disconnectOnLeave: false }) {
const ForwardedRefComponent = forwardRef((props, ref) => {
return <TargetComponent {...props} forwardedRef={ref} />;
});

const InViewport = ({ onEnterViewport = noop, onLeaveViewport = noop, ...restProps }) => {
const node = useRef();
const InViewport = ({
onEnterViewport = noop,
onLeaveViewport = noop,
...restProps
}, ref) => {
const node = useRef(ref);
const { inViewport, enterCount, leaveCount } = useInViewport(
node,
options,
Expand All @@ -21,9 +20,9 @@ function handleViewport(TargetComponent, options, config = { disconnectOnLeave:
onLeaveViewport
}
);

return (
<ForwardedRefComponent
<TargetComponent
{...restProps}
inViewport={inViewport}
enterCount={enterCount}
Expand All @@ -33,7 +32,9 @@ function handleViewport(TargetComponent, options, config = { disconnectOnLeave:
);
};

return hoistNonReactStatic(InViewport, ForwardedRefComponent);
const name = TargetComponent.displayName || TargetComponent.name || 'Component';
InViewport.displayName = `handleViewport(${name})`;
return forwardRef(InViewport);
}

export default handleViewport;