Skip to content
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
17 changes: 16 additions & 1 deletion src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,21 @@ function reducer() {
return true;
}

// find a visible node to observe
// NOTE: this just checks the first child at each level
function getFirstVisibleChild(el) {
const child = el.firstElementChild;
if (child) {
const style = getComputedStyle(child);
// element might contain visible children
if (!["contents"].includes(style.display)) {
return child;
}

return getFirstVisibleChild(child);
}
return child;
}
function LazyHydrate(props: Props) {
const childRef = React.useRef<HTMLElement>(null);

Expand Down Expand Up @@ -92,7 +107,7 @@ function LazyHydrate(props: Props) {
const element = noWrapper
? rootElement
: // As root node does not have any box model, it cannot intersect.
rootElement.firstElementChild;
getFirstVisibleChild(rootElement);

if (element && typeof IntersectionObserver !== "undefined") {
const observerOptions =
Expand Down