Skip to content
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

fast path a bunch of code for top layer elements #234

Closed
Closed
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
54 changes: 32 additions & 22 deletions src/anchored-position.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@
anchorAlign: AnchorAlignment
}

interface BoxPosition extends Size, Position {}
interface BoxPosition extends Size, Position { }

Check failure on line 128 in src/anchored-position.ts

View workflow job for this annotation

GitHub Actions / lint

Delete `·`

Check failure on line 128 in src/anchored-position.ts

View workflow job for this annotation

GitHub Actions / lint

Delete `·`

/**
* Given a floating element and an anchor element, return coordinates for the top-left
Expand Down Expand Up @@ -180,31 +180,32 @@
return document.body
}

/**
* Returns true if the element is likely to be on the `top-layer`.
*/
function isOnTopLayer(element: Element) {
if (element.tagName === 'DIALOG') {
return true
}
function isModalDialog(element: Element) {
return element.matches('dialog:modal')
}

function isFullscreen(element: Element) {
return element.matches(':fullscreen')
}

function isPopover(element: Element) {
try {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
if (element.matches(':popover-open') && /native code/.test((document.body as any).showPopover?.toString())) {
return true
}
return (element.matches(':popover-open') && /native code/.test((document.body as any).showPopover?.toString()))

Check failure on line 193 in src/anchored-position.ts

View workflow job for this annotation

GitHub Actions / lint

Replace `(element.matches(':popover-open')·&&·/native·code/.test((document.body·as·any).showPopover?.toString()` with `element.matches(':popover-open')·&&·/native·code/.test((document.body·as·any).showPopover?.toString(`

Check failure on line 193 in src/anchored-position.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type

Check failure on line 193 in src/anchored-position.ts

View workflow job for this annotation

GitHub Actions / lint

Replace `(element.matches(':popover-open')·&&·/native·code/.test((document.body·as·any).showPopover?.toString()` with `element.matches(':popover-open')·&&·/native·code/.test((document.body·as·any).showPopover?.toString(`

Check failure on line 193 in src/anchored-position.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
} catch {
return false
}
return false
}

/**
* Returns the rectangle (relative to the window) that will clip the given element
* if it is rendered outside of its bounds.
* @param element
* @returns
* Returns true if the element is likely to be on the `top-layer`.
*/
function getClippingRect(element: Element): BoxPosition {
function isOnTopLayer(element: Element) {
return isModalDialog(element) || isFullscreen(element) || isPopover(element)
}

function getClippingParent(element: Element): Element {
if (element === document.body) return element;

Check failure on line 207 in src/anchored-position.ts

View workflow job for this annotation

GitHub Actions / lint

Delete `;`

Check failure on line 207 in src/anchored-position.ts

View workflow job for this annotation

GitHub Actions / lint

Delete `;`

let parentNode: typeof element.parentNode = element
while (parentNode !== null) {
if (!(parentNode instanceof Element)) {
Expand All @@ -216,8 +217,17 @@
}
parentNode = parentNode.parentNode
}
const clippingNode = parentNode === document.body || !(parentNode instanceof HTMLElement) ? document.body : parentNode
return parentNode === document.body || !(parentNode instanceof HTMLElement) ? document.body : parentNode
}

/**
* Returns the rectangle (relative to the window) that will clip the given element
* if it is rendered outside of its bounds.
* @param element
* @returns
*/
function getClippingRect(element: Element): BoxPosition {
const clippingNode = getClippingParent(element)
const elemRect = clippingNode.getBoundingClientRect()
const elemStyle = getComputedStyle(clippingNode)

Expand Down Expand Up @@ -292,7 +302,7 @@
relativePosition: Position,
floatingRect: Size,
anchorRect: BoxPosition,
{side, align, allowOutOfBounds, anchorOffset, alignmentOffset}: PositionSettings,
{ side, align, allowOutOfBounds, anchorOffset, alignmentOffset }: PositionSettings,

Check failure on line 305 in src/anchored-position.ts

View workflow job for this annotation

GitHub Actions / lint

Replace `·side,·align,·allowOutOfBounds,·anchorOffset,·alignmentOffset·` with `side,·align,·allowOutOfBounds,·anchorOffset,·alignmentOffset`

Check failure on line 305 in src/anchored-position.ts

View workflow job for this annotation

GitHub Actions / lint

Replace `·side,·align,·allowOutOfBounds,·anchorOffset,·alignmentOffset·` with `side,·align,·allowOutOfBounds,·anchorOffset,·alignmentOffset`
): AnchorPosition {
// Compute the relative viewport rect, to bring it into the same coordinate space as `pos`
const relativeViewportRect: BoxPosition = {
Expand Down Expand Up @@ -375,8 +385,8 @@
}
}

return {...pos, anchorSide, anchorAlign}
return { ...pos, anchorSide, anchorAlign }

Check failure on line 388 in src/anchored-position.ts

View workflow job for this annotation

GitHub Actions / lint

Replace `·...pos,·anchorSide,·anchorAlign·` with `...pos,·anchorSide,·anchorAlign`
}

Check failure on line 389 in src/anchored-position.ts

View workflow job for this annotation

GitHub Actions / lint

Replace `·...pos,·anchorSide,·anchorAlign·` with `...pos,·anchorSide,·anchorAlign`

/**
* Given a floating element and an anchor element, return coordinates for the
Expand Down Expand Up @@ -467,8 +477,8 @@
}
}

return {top, left}
return { top, left }

Check failure on line 480 in src/anchored-position.ts

View workflow job for this annotation

GitHub Actions / lint

Replace `·top,·left·` with `top,·left`
}

Check failure on line 481 in src/anchored-position.ts

View workflow job for this annotation

GitHub Actions / lint

Replace `·top,·left·` with `top,·left`

/**
* Determines if there is an overflow
Expand Down
Loading