Skip to content

Rollback scrollable parent change #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Fixed the position data when a parent element was scrolled

### Added

- New `scrollableParents` field is now returned with `PositionData`

## [1.0.1] - 2023-11-14

<small>[Compare to previous release][comp:1.0.1]</small>
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Position (A JavaScript package)

A small package to help position a floating element. This can be positioned relative to another element or to a mouse event.
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.

### Links

Expand Down
1 change: 0 additions & 1 deletion src/Types/PositionData.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
export type PositionData = {
top: string;
left: string;
scrollableParents: HTMLElement[];
};
37 changes: 13 additions & 24 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@ import { PositionData } from './Types/PositionData';
* @returns object with a top and left value in the form `{number}px`
*/
function position(options: IOptions): PositionData {
const { _bodyRect, _anchorRect, _targetRect, scrollableParents } = initialisePrivateFields();

const myPos = Helpers.parse(
const { _bodyRect, _anchorRect, _targetRect } = initialisePrivateFields(),
myPos = Helpers.parse(
options.my,
options.defaults
? Helpers.parse(options.defaults.my)
Expand All @@ -44,7 +43,6 @@ function position(options: IOptions): PositionData {
return {
left: calculateLeft(myPos, atPos).value.toString() + 'px',
top: calculateTop(myPos, atPos).value.toString() + 'px',
scrollableParents,
// @ts-ignore
...(options.debug === true
? { _bodyRect, _anchorRect, _targetRect }
Expand All @@ -57,7 +55,6 @@ function position(options: IOptions): PositionData {
return {
top: pos.top.toString() + 'px',
left: pos.left.toString() + 'px',
scrollableParents,
// @ts-ignore
...(options.debug === true
? { _bodyRect, _anchorRect, _targetRect }
Expand All @@ -81,41 +78,33 @@ function position(options: IOptions): PositionData {
return '{}';
},
}
: options.anchor.getBoundingClientRect();
: options.anchor.getBoundingClientRect(),
originalDisplay = options.target.style.display,
_targetRect = options.target.getBoundingClientRect();

const originalDisplay = options.target.style.display;
options.target.style.display = 'block';

const _targetRect = options.target.getBoundingClientRect(),
scrollableParents : HTMLElement[] = [];

options.target.style.display = originalDisplay;

// Adjust to scrollable regions
if (options.anchor instanceof HTMLElement) {
let parent = options.anchor.parentElement;

while (parent !== null && parent.tagName !== 'HTML') {
// Check if scrollable
if (parent.scrollHeight > parent.clientHeight)
scrollableParents.push(parent)

parent = parent.parentElement;
}

// Finally, adjust for window scroll position
const doc = document.documentElement;
_anchorRect.y +=
(window.scrollY || doc.scrollTop) - (doc.clientTop || 0);
(window.scrollY ||
document.documentElement.scrollTop ||
document.body.scrollTop ||
0) - (doc.clientTop || 0);
_anchorRect.x +=
(window.scrollX || doc.scrollLeft) - (doc.clientLeft || 0);
(window.scrollX ||
document.documentElement.scrollLeft ||
document.body.scrollLeft ||
0) - (doc.clientLeft || 0);
}

return {
_bodyRect,
_anchorRect,
_targetRect,
scrollableParents
};
}

Expand Down