Skip to content

Commit 0f2f143

Browse files
authored
Scrolling fix - PR#6 from glenn2223
### Fixed - Fixed the position data when a parent element was scrolled
2 parents 9c244ca + a36a08f commit 0f2f143

File tree

3 files changed

+20
-4
lines changed

3 files changed

+20
-4
lines changed

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,16 @@ All notable changes to this project will be documented in this file.
2626
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
2727
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
2828

29+
## Unpublished
30+
31+
### Fixed
32+
33+
- Fixed the position data when a parent element was scrolled
34+
35+
### Added
36+
37+
- New `scrollableParents` field is now returned with `PositionData`
38+
2939
## [1.0.1] - 2023-11-14
3040

3141
<small>[Compare to previous release][comp:1.0.1]</small>

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)