Skip to content

Commit 14eb022

Browse files
brophdawg11rigor789
authored andcommitted
fix: force:false to correctly check element visibility in viewport (#150)
* Fix viewport issue for force:false * Defensively call onDone for force:false
1 parent 208ebf5 commit 14eb022

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

src/scrollTo.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,8 +187,15 @@ export const scroller = () => {
187187
diffX = targetX - initialX;
188188

189189
if (!force) {
190+
// When the container is the default (body) we need to use the viewport
191+
// height, not the entire body height
192+
const containerHeight =
193+
container.tagName.toLowerCase() === "body"
194+
? document.documentElement.clientHeight ||
195+
window.innerHeight
196+
: container.offsetHeight;
190197
const containerTop = initialY;
191-
const containerBottom = containerTop + container.offsetHeight;
198+
const containerBottom = containerTop + containerHeight;
192199
const elementTop = targetY;
193200
const elementBottom = elementTop + element.offsetHeight;
194201
if (
@@ -197,7 +204,7 @@ export const scroller = () => {
197204
) {
198205
// make sure to call the onDone callback even if there is no need to
199206
// scroll the container. Fixes #111 (ref #118)
200-
onDone(element);
207+
if (onDone) onDone(element);
201208
return;
202209
}
203210
}

0 commit comments

Comments
 (0)