Skip to content

Commit

Permalink
fast path a bunch of code for top layer elements
Browse files Browse the repository at this point in the history
  • Loading branch information
keithamus committed Oct 17, 2023
1 parent e1a8175 commit 6fc7e3d
Showing 1 changed file with 32 additions and 22 deletions.
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 @@ export interface AnchorPosition {
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 @@ function getPositionedParent(element: Element) {
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 @@ function getClippingRect(element: Element): BoxPosition {
}
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 @@ function pureCalculateAnchoredPosition(
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,7 +385,7 @@ function pureCalculateAnchoredPosition(
}
}

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`

/**
Expand Down Expand Up @@ -467,7 +477,7 @@ function calculatePosition(
}
}

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`

/**
Expand Down

0 comments on commit 6fc7e3d

Please sign in to comment.