Skip to content

Instantly share code, notes, and snippets.

@THEtheChad
Last active June 30, 2022 09:40
Show Gist options
  • Save THEtheChad/e1d349e4d4d1e0e833c62a275b11b76a to your computer and use it in GitHub Desktop.
Save THEtheChad/e1d349e4d4d1e0e833c62a275b11b76a to your computer and use it in GitHub Desktop.
export default function getScrollParent(node?: Node | null) {
if (!(node instanceof HTMLElement)) return null;
if (typeof window.getComputedStyle !== 'function') return null;
const overflowY = window.getComputedStyle(node).overflowY || 'visible';
const isScrollable = /^(visible|hidden)/.test(overflowY);
if (isScrollable && node.scrollHeight >= node.clientHeight) {
return node;
}
return getScrollParent(node.parentNode) || document.body;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment