Skip to content

Commit

Permalink
fix: improper positioning in Safari et al.
Browse files Browse the repository at this point in the history
In several browsers, getBoundingClientRect returns an object that does
not have x/y properties (see
https://developer.mozilla.org/en-US/docs/Web/API/Element/getBoundingClientRect).

As of 0.1.1, passing the result of getBoundingClientRect to the
constructor would cause a type error.

Since it's very easy to extend the return value to have those
properties, we now do that in our constructor, and update our
constructor's typings to accept ClientRect instead of DOMRect.
  • Loading branch information
rylnd committed Dec 20, 2017
1 parent f7412fa commit a8708ae
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/ringside.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { DOMRect, RectProps } from './types';
import { ClientToDOM } from './utils';

export type Orientation = 'top' | 'left' | 'bottom' | 'right';
export type HAlignment = 'start' | 'center' | 'end';
Expand Down Expand Up @@ -45,17 +46,21 @@ class Ringside implements RingsideInterface {
public readonly rightLane: Lane;
public readonly bottomLane: Lane;
public readonly leftLane: Lane;
public readonly innerBounds: DOMRect;
public readonly outerBounds: DOMRect;

public orientations: Orientation[] = ['top', 'right', 'bottom', 'left'];
public hAlignments: HAlignment[] = ['start', 'center', 'end'];
public vAlignments: VAlignment[] = ['top', 'middle', 'bottom'];

constructor(
readonly innerBounds: DOMRect,
readonly outerBounds: DOMRect,
innerBounds: ClientRect,
outerBounds: ClientRect,
readonly height: number,
readonly width: number,
) {
this.innerBounds = ClientToDOM(innerBounds);
this.outerBounds = ClientToDOM(outerBounds);
this.topLane = {
name: 'top',
x: this.outerBounds.x,
Expand Down
4 changes: 4 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,7 @@ export const rectToDOMRect: (
right: x + width,
};
};

export const ClientToDOM: (rect: ClientRect) => DOMRect = rect => {
return rectToDOMRect(rect.left, rect.top, rect.height, rect.width);
};
30 changes: 30 additions & 0 deletions test/ringside.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,36 @@ describe('Ringside', () => {
y: 80,
});
});

it('positions given Safari ClientRect bounds', () => {
outer = {
top: 0,
left: 0,
right: 600,
bottom: 500,
height: 500,
width: 600,
};
inner = {
top: 200,
left: 150,
right: 350,
bottom: 300,
height: 100,
width: 200,
};

ringside = new Ringside(inner, outer, height, width);
const bottom = ringside.bottom();

expect(bottom.end.bottom).toEqual({
fits: true,
height,
width,
x: 150,
y: 300,
});
});
});

describe('fit', () => {
Expand Down

0 comments on commit a8708ae

Please sign in to comment.