Created
June 21, 2024 05:39
-
-
Save alexreardon/cd2cff69b7e8e6197f6118ac5ea105df to your computer and use it in GitHub Desktop.
DOMRect polyfill
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 polyfills DOMRect | |
// DOMRect is currently not polyfilled by jsdom | |
(() => { | |
if (typeof window === 'undefined') { | |
return; | |
} | |
if (window.DOMRect) { | |
return; | |
} | |
class DOMRect { | |
constructor(x = 0, y = 0, width = 0, height = 0) { | |
this.x = x; | |
this.y = y; | |
this.width = width; | |
this.height = height; | |
// Computed values. | |
// See https://developer.mozilla.org/en-US/docs/Web/API/DOMRect | |
this.top = height < 0 ? y + height : y; | |
this.right = width < 0 ? x : x + width; | |
this.bottom = height < 0 ? y : y + height; | |
this.left = width < 0 ? x + width : x; | |
} | |
static fromRect(rectangle) { | |
return new DOMRect(rectangle?.x, rectangle?.y, rectangle?.width, rectangle?.height); | |
} | |
toJSON() { | |
return JSON.stringify(this); | |
} | |
} | |
window.DOMRect = DOMRect; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment