Skip to content

Instantly share code, notes, and snippets.

@alexreardon
Created June 21, 2024 05:39
Show Gist options
  • Save alexreardon/cd2cff69b7e8e6197f6118ac5ea105df to your computer and use it in GitHub Desktop.
Save alexreardon/cd2cff69b7e8e6197f6118ac5ea105df to your computer and use it in GitHub Desktop.
DOMRect polyfill
// 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