Skip to content

fix: make rootMargin in IO externally configurable #29

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 1 commit into
base: master
Choose a base branch
from
Open
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
7 changes: 5 additions & 2 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export type LazyProps = {
didHydrate?: VoidFunction;
promise?: Promise<any>;
on?: (keyof HTMLElementEventMap)[] | keyof HTMLElementEventMap;
rootMargin?: string;
};

type Props = Omit<React.HTMLProps<HTMLDivElement>, "dangerouslySetInnerHTML"> &
Expand All @@ -19,7 +20,7 @@ type VoidFunction = () => void;

const event = "hydrate";

const io =
const getIntersectionObserver = (rootMargin: string) =>
isBrowser && typeof IntersectionObserver !== "undefined"
? new IntersectionObserver(
entries => {
Expand All @@ -30,7 +31,7 @@ const io =
});
},
{
rootMargin: "250px"
rootMargin
}
)
: null;
Expand All @@ -55,6 +56,7 @@ function LazyHydrate(props: Props) {
on = [],
children,
didHydrate, // callback for hydration
rootMargin = "250px",
...rest
} = props;

Expand Down Expand Up @@ -116,6 +118,7 @@ function LazyHydrate(props: Props) {
let events = Array.isArray(on) ? on.slice() : [on];

if (whenVisible) {
const io = getIntersectionObserver(rootMargin);
if (io && childRef.current.childElementCount) {
// As root node does not have any box model, it cannot intersect.
const el = childRef.current.children[0];
Expand Down