-
-
Notifications
You must be signed in to change notification settings - Fork 669
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
128 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
packages/dom/src/utilities/bounding-rectangle/getVisibleBoundingRectangle.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import type {Rect} from './types.ts'; | ||
|
||
export function getVisibleBoundingRectangle( | ||
element: Element, | ||
boundingClientRect = element.getBoundingClientRect() | ||
): Rect { | ||
// Get the initial bounding client rect of the element | ||
let rect: Rect = boundingClientRect; | ||
const ownerWindow = element.ownerDocument?.defaultView ?? window; | ||
|
||
// Traverse up the DOM tree to clip the rect based on ancestors' bounding rects | ||
let ancestor: HTMLElement | null = element.parentElement; | ||
|
||
while (ancestor && ancestor !== document.documentElement) { | ||
const ancestorRect = ancestor.getBoundingClientRect(); | ||
|
||
// Clip the rect based on the ancestor's bounding rect | ||
rect = { | ||
top: Math.max(rect.top, ancestorRect.top), | ||
right: Math.min(rect.right, ancestorRect.right), | ||
bottom: Math.min(rect.bottom, ancestorRect.bottom), | ||
left: Math.max(rect.left, ancestorRect.left), | ||
width: 0, // Will be calculated next | ||
height: 0, // Will be calculated next | ||
}; | ||
|
||
// Calculate the width and height after clipping | ||
rect.width = rect.right - rect.left; | ||
rect.height = rect.bottom - rect.top; | ||
|
||
// Move to the next ancestor | ||
ancestor = ancestor.parentElement; | ||
} | ||
|
||
// Clip the rect based on the viewport (window) | ||
const viewportWidth = ownerWindow.innerWidth; | ||
const viewportHeight = ownerWindow.innerHeight; | ||
|
||
rect = { | ||
top: Math.max(rect.top, 0), | ||
right: Math.min(rect.right, viewportWidth), | ||
bottom: Math.min(rect.bottom, viewportHeight), | ||
left: Math.max(rect.left, 0), | ||
width: 0, // Will be calculated next | ||
height: 0, // Will be calculated next | ||
}; | ||
|
||
// Calculate the width and height after clipping | ||
rect.width = rect.right - rect.left; | ||
rect.height = rect.bottom - rect.top; | ||
|
||
return rect; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export type Rect = Pick< | ||
DOMRect, | ||
'top' | 'left' | 'right' | 'bottom' | 'width' | 'height' | ||
>; |