Skip to content

Commit

Permalink
fix(componentpositions): fixed returning size for invisible element
Browse files Browse the repository at this point in the history
  • Loading branch information
mjancarik committed Apr 24, 2019
1 parent e868faf commit 3d00a9c
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 10 deletions.
4 changes: 2 additions & 2 deletions preprocess.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ module.exports = {
if (filename.endsWith('.js') || filename.endsWith('.jsx')) {
return babel.transform(src, {
filename,
presets: ['react'],
plugins: ['transform-es2015-modules-commonjs'],
presets: ['@babel/preset-react'],
plugins: ['@babel/plugin-transform-modules-commonjs'],
retainLines: true
}).code;
}
Expand Down
18 changes: 14 additions & 4 deletions src/ComponentPositions.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,14 +162,24 @@ export default class ComponentPositions {
);
}

let clientRect = element.getBoundingClientRect();
const clientRect = element.getBoundingClientRect();
const isInvisibleElement =
clientRect.top === 0 &&
clientRect.left === 0 &&
clientRect.width === 0 &&
clientRect.height === 0;

if (isInvisibleElement) {
const { top, left, width, height } = clientRect;

return { top, left, width, height };
}

let elmRectStyle = {
top: clientRect.top - extended,
left: clientRect.left - extended,
width: clientRect.width + 2 * extended,
height:
(clientRect.height || height || 0 / width || 0 * clientRect.width) +
2 * extended
height: (clientRect.height || height || 0 / width || 0) + 2 * extended
};

return elmRectStyle;
Expand Down
25 changes: 21 additions & 4 deletions src/__tests__/ComponentPositionsSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ describe('ComponentPositions', () => {
}).toThrow();
});

it('should returns expanded size for element', () => {
it('should return expanded size for element', () => {
let element = {
getBoundingClientRect: () => elmRect
};
Expand All @@ -100,10 +100,27 @@ describe('ComponentPositions', () => {
width: 700,
height: 700
});
});

expect(function() {
componentPositions.getBoundingClientRect({});
}).toThrow();
it('should return zero size for invisible element', () => {
let element = {
getBoundingClientRect: () => {
return { top: 0, left: 0, width: 0, height: 0 };
}
};

expect(
componentPositions.getBoundingClientRect(
element,
{ width: 100, height: 100 },
300
)
).toEqual({
top: 0,
left: 0,
width: 0,
height: 0
});
});
});
});

0 comments on commit 3d00a9c

Please sign in to comment.