Skip to content

Commit 0d49b99

Browse files
authored
ensure detached nodes no-op (#467)
1 parent 7f2377f commit 0d49b99

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

src/index.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,21 @@ function scrollIntoView<T>(
6363
): T
6464
function scrollIntoView(target: Element, options?: Options | boolean): void
6565
function scrollIntoView<T>(target: Element, options?: Options<T> | boolean) {
66+
// Browsers treats targets that aren't in the dom as a no-op
67+
const scrollingElement = document.scrollingElement || document.documentElement
68+
const targetIsDetached =
69+
target !== scrollingElement && !scrollingElement.contains(target)
70+
6671
if (
6772
isOptionsObject<CustomBehaviorOptions<T>>(options) &&
6873
typeof options.behavior === 'function'
6974
) {
70-
return options.behavior(compute(target, options))
75+
return options.behavior(targetIsDetached ? [] : compute(target, options))
76+
}
77+
78+
// Don't do anything if using a standard behavior on an element that is not in the document
79+
if (targetIsDetached) {
80+
return
7181
}
7282

7383
// @TODO see if it's possible to avoid this assignment

0 commit comments

Comments
 (0)