Created
October 6, 2023 17:00
-
-
Save souporserious/10c6c71c6a686f45f05e020e092b5025 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** Get the closest element that scrolls */ | |
function getClosestOverflowElement(node: HTMLElement, includeHidden?: boolean) { | |
if (node) { | |
const { overflow, overflowX, overflowY } = getComputedStyle(node) | |
const canScroll = ( | |
includeHidden ? /(auto|scroll|hidden)/ : /(auto|scroll)/ | |
).test(overflow + overflowX + overflowY) | |
if (node === document.body || canScroll) { | |
return node | |
} else { | |
return getClosestOverflowElement( | |
node.parentNode as HTMLElement, | |
includeHidden | |
) | |
} | |
} else { | |
return document.body | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment