Skip to content

Commit

Permalink
fix(Tile): recalculate tile rect height on component did mount (#9593)
Browse files Browse the repository at this point in the history
* fix(Tile): recalculate tile rect height on component did mount

* Update packages/react/src/components/Tile/Tile.js

Co-authored-by: Josh Black <[email protected]>

* fix(Tile): remove unused variable

* chore(Tile): update tests with mock ResizeObserver

* chore(Tile): format file

* fix(Tile-test): wrap resizeObserver in before/aftereach

* fix(Tile-test): add asynchronous afterAll call

* fix(jest): add ResizeObserver to setup

Co-authored-by: Josh Black <[email protected]>
  • Loading branch information
dakahn and joshblack authored Sep 24, 2021
1 parent f6b6290 commit d6884e0
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
10 changes: 10 additions & 0 deletions config/jest-config-carbon/setup/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,13 @@ if (global.window) {
const { getComputedStyle } = window;
window.getComputedStyle = (element) => getComputedStyle(element);
}

if (global.window) {
window.ResizeObserver = jest.fn(() => {
return {
observe: jest.fn(),
unobserve: jest.fn(),
disconnect: jest.fn(),
};
});
}
17 changes: 17 additions & 0 deletions packages/react/src/components/Tile/Tile.js
Original file line number Diff line number Diff line change
Expand Up @@ -543,11 +543,22 @@ export class ExpandableTile extends Component {
};
}

resizeObserver = null;

componentDidMount = () => {
this.resizeObserver = new ResizeObserver((entries) => {
const [aboveTheFold] = entries;
this.setState({
tileMaxHeight: aboveTheFold.contentRect.height,
});
});

if (this.tile) {
const getStyle = window.getComputedStyle(this.tile, null);

if (this.aboveTheFold) {
this.resizeObserver.observe(this.aboveTheFold);

this.setState({
tileMaxHeight: this.aboveTheFold.getBoundingClientRect().height,
tilePadding:
Expand All @@ -558,6 +569,12 @@ export class ExpandableTile extends Component {
}
};

componentWillUnmount() {
if (this.resizeObserver) {
this.resizeObserver.disconnect();
}
}

componentDidUpdate = (prevProps) => {
if (prevProps.expanded !== this.props.expanded) {
this.setMaxHeight();
Expand Down

0 comments on commit d6884e0

Please sign in to comment.