Skip to content

Commit 2294fbc

Browse files
committed
fix(core dom): Don't break when elements given to get_relative_position and scroll_to_element are window or document and as such no real DOM elements. Use document.body instead.
1 parent 38afda4 commit 2294fbc

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

src/core/dom.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,9 +364,12 @@ const get_relative_position = (el, reference_el = document.body) => {
364364
// the relative position of the target.
365365
// In case of a scroll container of window, we do not have
366366
// getBoundingClientRect method, so get the body instead.
367-
if (reference_el === window) {
367+
if (reference_el === window || document) {
368368
reference_el = document.body;
369369
}
370+
if (el === window || document) {
371+
el = document.body;
372+
}
370373

371374
// Calculate absolute [¹] position difference between
372375
// scroll_container and scroll_target.
@@ -407,6 +410,14 @@ const get_relative_position = (el, reference_el = document.body) => {
407410
* @param {string} [direction="top"] - The direction to scroll to. Can be either "top", "left" or "both".
408411
*/
409412
const scroll_to_element = (el, scroll_container, offset = 0, direction = "top") => {
413+
// Normalize el and scroll_container
414+
if (el === window || document) {
415+
el = document.body;
416+
}
417+
if (scroll_container === window || document) {
418+
scroll_container = document.body;
419+
}
420+
410421
// Get the position of the element relative to the scroll container.
411422
const position = get_relative_position(el, scroll_container);
412423

0 commit comments

Comments
 (0)