diff --git a/config/jest-config-carbon/setup/setup.js b/config/jest-config-carbon/setup/setup.js index c832e988aa3f..474e57786fa4 100644 --- a/config/jest-config-carbon/setup/setup.js +++ b/config/jest-config-carbon/setup/setup.js @@ -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(), + }; + }); +} diff --git a/packages/react/src/components/Tile/Tile.js b/packages/react/src/components/Tile/Tile.js index e40368855b87..d10221dbb688 100644 --- a/packages/react/src/components/Tile/Tile.js +++ b/packages/react/src/components/Tile/Tile.js @@ -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: @@ -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();