Skip to content

Fix glitching behavior on scrolling up #21

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: main
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
57 changes: 44 additions & 13 deletions src/render-if-visible.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,45 @@ const RenderIfVisible = ({
}: Props) => {
const [isVisible, setIsVisible] = useState<boolean>(initialVisible)
const wasVisible = useRef<boolean>(initialVisible)
const placeholderHeight = useRef<number>(defaultHeight)
const intersectionRef = useRef<HTMLDivElement>(null)
const innerRef = useRef<HTMLDivElement>(null)

const [rootHeight, setRootHeight] = useState<number>(defaultHeight)

useEffect(() => {
if (innerRef.current) {
const localRef = innerRef.current
const resizeObserver = new ResizeObserver((entries) => {
const resizeEntry = entries[0]

/* Sets the height of the container if the previous value is the default one or if the current value is greater than its previous value */
setRootHeight((prev) => {
if (
(prev === defaultHeight && resizeEntry?.contentRect.height !== 0) ||
resizeEntry?.contentRect.height > prev
) {
return resizeEntry?.contentRect.height
}
return prev
})
})

resizeObserver.observe(localRef)
return () => {
if (localRef) {
resizeObserver.unobserve(localRef)
}
}
}
return () => {}
}, [innerRef, setRootHeight, defaultHeight])

// Set visibility with intersection observer
useEffect(() => {
if (intersectionRef.current) {
const localRef = intersectionRef.current
const observer = new IntersectionObserver(
(entries) => {
// Before switching off `isVisible`, set the height of the placeholder
if (!entries[0].isIntersecting) {
placeholderHeight.current = localRef!.offsetHeight
}
if (typeof window !== undefined && window.requestIdleCallback) {
window.requestIdleCallback(
() => setIsVisible(entries[0].isIntersecting),
Expand Down Expand Up @@ -80,7 +106,8 @@ const RenderIfVisible = ({
}
}, [isVisible])

const placeholderStyle = { height: placeholderHeight.current }
const rootStyle = useMemo(() => ({ height: `${rootHeight}px` }), [rootHeight])

const rootClasses = useMemo(
() => `renderIfVisible ${rootElementClass}`,
[rootElementClass]
Expand All @@ -91,16 +118,20 @@ const RenderIfVisible = ({
)

return React.createElement(rootElement, {
children: isVisible || (stayRendered && wasVisible.current) ? (
<>{children}</>
) : (
React.createElement(placeholderElement, {
className: placeholderClasses,
style: placeholderStyle,
})
children: (
<div ref={innerRef} style={{ overflow: 'auto' }}>
{isVisible || (stayRendered && wasVisible.current) ? (
<>{children}</>
) : (
React.createElement(placeholderElement, {
className: placeholderClasses,
})
)}
</div>
),
ref: intersectionRef,
className: rootClasses,
style: rootStyle,
})
}

Expand Down