Skip to content

Commit 69ccc65

Browse files
committed
Scrolling Changes
- Applied a fix to the body scrolling _`getBoundingClientRect` is relative to the screen so it should never need to be adjusted (unless the user scrolls)_ - Store the scrollable parent elements To help respond to the user's scrolling
1 parent 9c244ca commit 69ccc65

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

src/Types/PositionData.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export type PositionData = {
22
top: string;
33
left: string;
4+
scrollableParents: HTMLElement[];
45
};

src/index.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import { PositionData } from './Types/PositionData';
1818
* @returns object with a top and left value in the form `{number}px`
1919
*/
2020
function position(options: IOptions): PositionData {
21-
const { _bodyRect, _anchorRect, _targetRect } = initialisePrivateFields();
21+
const { _bodyRect, _anchorRect, _targetRect, scrollableParents } = initialisePrivateFields();
2222

2323
const myPos = Helpers.parse(
2424
options.my,
@@ -44,6 +44,7 @@ function position(options: IOptions): PositionData {
4444
return {
4545
left: calculateLeft(myPos, atPos).value.toString() + 'px',
4646
top: calculateTop(myPos, atPos).value.toString() + 'px',
47+
scrollableParents,
4748
// @ts-ignore
4849
...(options.debug === true
4950
? { _bodyRect, _anchorRect, _targetRect }
@@ -56,6 +57,7 @@ function position(options: IOptions): PositionData {
5657
return {
5758
top: pos.top.toString() + 'px',
5859
left: pos.left.toString() + 'px',
60+
scrollableParents,
5961
// @ts-ignore
6062
...(options.debug === true
6163
? { _bodyRect, _anchorRect, _targetRect }
@@ -84,7 +86,8 @@ function position(options: IOptions): PositionData {
8486
const originalDisplay = options.target.style.display;
8587
options.target.style.display = 'block';
8688

87-
const _targetRect = options.target.getBoundingClientRect();
89+
const _targetRect = options.target.getBoundingClientRect(),
90+
scrollableParents : HTMLElement[] = [];
8891

8992
options.target.style.display = originalDisplay;
9093

@@ -93,8 +96,9 @@ function position(options: IOptions): PositionData {
9396
let parent = options.anchor.parentElement;
9497

9598
while (parent !== null && parent.tagName !== 'HTML') {
96-
_anchorRect.y += parent.scrollTop;
97-
_anchorRect.x += parent.scrollLeft;
99+
// Check if scrollable
100+
if (parent.scrollHeight > parent.clientHeight)
101+
scrollableParents.push(parent)
98102

99103
parent = parent.parentElement;
100104
}
@@ -111,6 +115,7 @@ function position(options: IOptions): PositionData {
111115
_bodyRect,
112116
_anchorRect,
113117
_targetRect,
118+
scrollableParents
114119
};
115120
}
116121

0 commit comments

Comments
 (0)