Skip to content

Instantly share code, notes, and snippets.

@souporserious
Created October 6, 2023 17:00
Show Gist options
  • Save souporserious/10c6c71c6a686f45f05e020e092b5025 to your computer and use it in GitHub Desktop.
Save souporserious/10c6c71c6a686f45f05e020e092b5025 to your computer and use it in GitHub Desktop.
/** 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