Skip to content
This repository has been archived by the owner on Jun 5, 2023. It is now read-only.

Commit

Permalink
fix(utils): Improve getScrollParent
Browse files Browse the repository at this point in the history
  • Loading branch information
diondiondion committed May 5, 2021
1 parent 2014106 commit 944fa64
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 16 deletions.
21 changes: 21 additions & 0 deletions src/utils/getScrollParent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
function getScrollParent(node) {
const isElement = node instanceof HTMLElement;
const overflowY = isElement && window.getComputedStyle(node).overflowY;
const isScrollable = !(
overflowY.includes('hidden') || overflowY.includes('visible')
);

if (!node) {
return null;
} else if (isScrollable && node.scrollHeight >= node.clientHeight) {
return node;
}

return (
getScrollParent(node.parentNode) ||
document.scrollingElement ||
document.body
);
}

export default getScrollParent;
6 changes: 2 additions & 4 deletions src/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@ export * from './colors';
export {default as mergeRefs} from './mergeRefs';
export {default as mergeCallbacks} from './mergeCallbacks';
export {default as getLength} from './getLength';
export {default as getScrollParent} from './getScrollParent';
export {default as getSpacing} from './getSpacing';
export {
default as scrollIntoViewIfNeeded,
getScrollParent,
} from './scrollIntoViewIfNeeded';
export {default as scrollIntoViewIfNeeded} from './scrollIntoViewIfNeeded';
export * from './spacing';
export {default as truncateText} from './truncateText';
export * from './units';
Expand Down
13 changes: 1 addition & 12 deletions src/utils/scrollIntoViewIfNeeded.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,4 @@
function getScrollParent(element) {
if (!element) {
return null;
}
if (element.scrollHeight > element.clientHeight) {
return element;
} else {
return getScrollParent(element.parentNode);
}
}
import getScrollParent from './getScrollParent';

/**
* Vertically Scrolls an element into view if it's not visible yet.
Expand All @@ -32,6 +23,4 @@ function scrollIntoViewIfNeeded(element) {
}
}

export {getScrollParent};

export default scrollIntoViewIfNeeded;

0 comments on commit 944fa64

Please sign in to comment.