Skip to content

Commit

Permalink
More complete DOMRect impl
Browse files Browse the repository at this point in the history
  • Loading branch information
TooTallNate committed Oct 31, 2023
1 parent fad5b59 commit ce32a6f
Showing 1 changed file with 44 additions and 16 deletions.
60 changes: 44 additions & 16 deletions packages/runtime/src/domrect.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { def } from './utils';

export interface DOMRectInit {
height?: number;
width?: number;
Expand All @@ -7,20 +9,15 @@ export interface DOMRectInit {

/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/DOMRectReadOnly) */
export class DOMRectReadOnly {
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/DOMRectReadOnly/bottom) */
//readonly bottom: number;
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/DOMRectReadOnly/height) */
readonly height: number;
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/DOMRectReadOnly/left) */
//readonly left: number;
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/DOMRectReadOnly/right) */
//readonly right: number;
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/DOMRectReadOnly/top) */
//readonly top: number;
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/DOMRectReadOnly/width) */
readonly width: number;

/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/DOMRectReadOnly/height) */
readonly height: number;

/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/DOMRectReadOnly/x) */
readonly x: number;

/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/DOMRectReadOnly/y) */
readonly y: number;

Expand All @@ -31,20 +28,50 @@ export class DOMRectReadOnly {
this.height = height ?? 0;
}

/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/DOMRectReadOnly/bottom) */
get bottom() {
return this.y + this.height;
}

/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/DOMRectReadOnly/left) */
get left() {
return this.x;
}

/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/DOMRectReadOnly/right) */
get right() {
return this.x + this.width;
}

/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/DOMRectReadOnly/top) */
get top() {
return this.y;
}

toJSON() {
return this;
return {
x: this.x,
y: this.y,
width: this.width,
height: this.height,
top: this.top,
right: this.right,
bottom: this.bottom,
left: this.left,
};
}

/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/DOMRectReadOnly/fromRect_static) */
static fromRect(other: DOMRectInit = {}): DOMRectReadOnly {
return new DOMRectReadOnly(other.x, other.y, other.width, other.height);
static fromRect(o: DOMRectInit = {}): DOMRectReadOnly {
return new DOMRectReadOnly(o.x, o.y, o.width, o.height);
}
}
def('DOMRectReadOnly', DOMRectReadOnly);

/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/DOMRect) */
export class DOMRect extends DOMRectReadOnly {
height: number;
width: number;
height: number;
x: number;
y: number;

Expand All @@ -56,7 +83,8 @@ export class DOMRect extends DOMRectReadOnly {
this.height = height ?? 0;
}

static fromRect(other: DOMRectInit = {}): DOMRect {
return new DOMRect(other.x, other.y, other.width, other.height);
static fromRect(o: DOMRectInit = {}): DOMRect {
return new DOMRect(o.x, o.y, o.width, o.height);
}
}
def('DOMRect', DOMRect);

0 comments on commit ce32a6f

Please sign in to comment.