-
-
Notifications
You must be signed in to change notification settings - Fork 651
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
11 changed files
with
200 additions
and
86 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
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
63 changes: 63 additions & 0 deletions
63
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,63 @@ | ||
import type {BoundingRectangle} from '@dnd-kit/geometry'; | ||
|
||
import {isOverflowVisible} from './isOverflowVisible.ts'; | ||
|
||
/* | ||
* Get the currently visible bounding rectangle of an element | ||
* @param element | ||
* @param boundingClientRect | ||
* @returns Rect | ||
*/ | ||
export function getVisibleBoundingRectangle( | ||
element: Element, | ||
boundingClientRect = element.getBoundingClientRect() | ||
): BoundingRectangle { | ||
// Get the initial bounding client rect of the element | ||
let rect: BoundingRectangle = boundingClientRect; | ||
const {ownerDocument} = element; | ||
const ownerWindow = 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 !== ownerDocument.documentElement) { | ||
if (!isOverflowVisible(ancestor)) { | ||
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; | ||
} |
16 changes: 16 additions & 0 deletions
16
packages/dom/src/utilities/bounding-rectangle/isOverflowVisible.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,16 @@ | ||
/* | ||
* Check if an element has visible overflow. | ||
* @param element | ||
* @param style | ||
* @returns boolean | ||
*/ | ||
export function isOverflowVisible( | ||
element: Element, | ||
style = getComputedStyle(element) | ||
) { | ||
const {overflow, overflowX, overflowY} = style; | ||
|
||
return ( | ||
overflow === 'visible' && overflowX === 'visible' && overflowY === 'visible' | ||
); | ||
} |
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
Oops, something went wrong.