Skip to content

Commit 437490e

Browse files
authored
Scrolling Change - PR#7 from glenn2223
This package is simple, no need to overcomplicate with returning scrollable parents. Some users may want to attach to every parent up the chain. Because of this, I have rolled back the previous update - Also did some very minor code tidying
2 parents 0f2f143 + 90ddaed commit 437490e

File tree

4 files changed

+14
-30
lines changed

4 files changed

+14
-30
lines changed

CHANGELOG.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
3232

3333
- Fixed the position data when a parent element was scrolled
3434

35-
### Added
36-
37-
- New `scrollableParents` field is now returned with `PositionData`
38-
3935
## [1.0.1] - 2023-11-14
4036

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

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Position (A JavaScript package)
22

3-
A small package to help position a floating element. This can be positioned relative to another element or to a mouse event.
3+
A small package to help position a floating element. This can be positioned relative to another elements' current screen position, or to a mouse event.
44

55
### Links
66

src/Types/PositionData.ts

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

src/index.ts

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,8 @@ 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, scrollableParents } = initialisePrivateFields();
22-
23-
const myPos = Helpers.parse(
21+
const { _bodyRect, _anchorRect, _targetRect } = initialisePrivateFields(),
22+
myPos = Helpers.parse(
2423
options.my,
2524
options.defaults
2625
? Helpers.parse(options.defaults.my)
@@ -44,7 +43,6 @@ function position(options: IOptions): PositionData {
4443
return {
4544
left: calculateLeft(myPos, atPos).value.toString() + 'px',
4645
top: calculateTop(myPos, atPos).value.toString() + 'px',
47-
scrollableParents,
4846
// @ts-ignore
4947
...(options.debug === true
5048
? { _bodyRect, _anchorRect, _targetRect }
@@ -57,7 +55,6 @@ function position(options: IOptions): PositionData {
5755
return {
5856
top: pos.top.toString() + 'px',
5957
left: pos.left.toString() + 'px',
60-
scrollableParents,
6158
// @ts-ignore
6259
...(options.debug === true
6360
? { _bodyRect, _anchorRect, _targetRect }
@@ -81,41 +78,33 @@ function position(options: IOptions): PositionData {
8178
return '{}';
8279
},
8380
}
84-
: options.anchor.getBoundingClientRect();
81+
: options.anchor.getBoundingClientRect(),
82+
originalDisplay = options.target.style.display,
83+
_targetRect = options.target.getBoundingClientRect();
8584

86-
const originalDisplay = options.target.style.display;
8785
options.target.style.display = 'block';
88-
89-
const _targetRect = options.target.getBoundingClientRect(),
90-
scrollableParents : HTMLElement[] = [];
91-
9286
options.target.style.display = originalDisplay;
9387

9488
// Adjust to scrollable regions
9589
if (options.anchor instanceof HTMLElement) {
96-
let parent = options.anchor.parentElement;
97-
98-
while (parent !== null && parent.tagName !== 'HTML') {
99-
// Check if scrollable
100-
if (parent.scrollHeight > parent.clientHeight)
101-
scrollableParents.push(parent)
102-
103-
parent = parent.parentElement;
104-
}
105-
10690
// Finally, adjust for window scroll position
10791
const doc = document.documentElement;
10892
_anchorRect.y +=
109-
(window.scrollY || doc.scrollTop) - (doc.clientTop || 0);
93+
(window.scrollY ||
94+
document.documentElement.scrollTop ||
95+
document.body.scrollTop ||
96+
0) - (doc.clientTop || 0);
11097
_anchorRect.x +=
111-
(window.scrollX || doc.scrollLeft) - (doc.clientLeft || 0);
98+
(window.scrollX ||
99+
document.documentElement.scrollLeft ||
100+
document.body.scrollLeft ||
101+
0) - (doc.clientLeft || 0);
112102
}
113103

114104
return {
115105
_bodyRect,
116106
_anchorRect,
117107
_targetRect,
118-
scrollableParents
119108
};
120109
}
121110

0 commit comments

Comments
 (0)