Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed issue with EuiResizeObserver fallback #3088

Merged
merged 7 commits into from
Mar 23, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
**Bug Fixes**

- Fixed race condition in `EuiIcon` when switching from dynamically fetched components ([#3118](https://github.com/elastic/eui/pull/3118))
- Fixed the issue that `EuiResizeObserver` fallback did not properly listen to pure window resizing ([#3088](https://github.com/elastic/eui/pull/3088)

## [`21.1.0`](https://github.com/elastic/eui/tree/v21.1.0)

Expand Down
20 changes: 18 additions & 2 deletions src/components/observer/resize_observer/resize_observer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,30 @@ export class EuiResizeObserver extends EuiObserver<Props> {
};
}

const makeCompatibleObserver = (node: Element, callback: () => void) => {
const observer = new MutationObserver(callback);
observer.observe(node, mutationObserverOptions);

window.addEventListener('resize', callback);
node.addEventListener('resize', callback);

observer.disconnect = () => {
observer.disconnect();

window.removeEventListener('resize', callback);
node.removeEventListener('resize', callback);
};

return observer;
};

const makeResizeObserver = (node: Element, callback: () => void) => {
let observer: Observer | undefined;
if (hasResizeObserver) {
observer = new window.ResizeObserver(callback);
observer.observe(node);
} else {
observer = new MutationObserver(callback);
observer.observe(node, mutationObserverOptions);
observer = makeCompatibleObserver(node, callback);
requestAnimationFrame(callback); // Mimic ResizeObserver behavior of triggering a resize event on init
}
return observer;
Expand Down