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

Prevent unmounted EuiGlobalToastList from causing errors #1606

Merged
merged 6 commits into from
Feb 27, 2019
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -11,6 +11,7 @@

- `EuiBasicTable` select all shows up on mobile ([#1462](https://github.com/elastic/eui/pull/1462))
- Adds missing `hasActiveFilters` prop for `EuiFilterButton` type and fixes `onChange` signature for `EuiButtonGroup` ([#1603](https://github.com/elastic/eui/pull/1603))
- Prevent `EuiGlobalToastList` from attempting calculations on `null` DOM elements ([#1606](https://github.com/elastic/eui/pull/1606))

**Breaking changes**

Expand Down
19 changes: 17 additions & 2 deletions src/components/toast/global_toast_list.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ export class EuiGlobalToastList extends Component {

this.isScrollingToBottom = false;
this.isScrolledToBottom = true;

// See [Return Value](https://developer.mozilla.org/en-US/docs/Web/API/Window/requestAnimationFrame#Return_value)
// for information on initial value of 0
this.isScrollingAnimationFrame = 0;
this.startScrollingAnimationFrame = 0;
}

static propTypes = {
Expand All @@ -40,6 +45,10 @@ export class EuiGlobalToastList extends Component {
this.isScrollingToBottom = true;

const scrollToBottom = () => {
// Although we cancel the requestAnimationFrame in componentWillUnmount,
// it's possible for this.listElement to become null in the meantime
if (!this.listElement) return;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This check should resolve #1595


const position = this.listElement.scrollTop;
const destination = this.listElement.scrollHeight - this.listElement.clientHeight;
const distanceToDestination = destination - position;
Expand All @@ -54,11 +63,11 @@ export class EuiGlobalToastList extends Component {
this.listElement.scrollTop = position + distanceToDestination * 0.25;

if (this.isScrollingToBottom) {
window.requestAnimationFrame(scrollToBottom);
this.isScrollingAnimationFrame = window.requestAnimationFrame(scrollToBottom);
}
};

window.requestAnimationFrame(scrollToBottom);
this.startScrollingAnimationFrame = window.requestAnimationFrame(scrollToBottom);
}

onMouseEnter = () => {
Expand Down Expand Up @@ -162,6 +171,12 @@ export class EuiGlobalToastList extends Component {
}

componentWillUnmount() {
if (this.isScrollingAnimationFrame !== 0) {
window.cancelAnimationFrame(this.isScrollingAnimationFrame);
}
if (this.startScrollingAnimationFrame !== 0) {
window.cancelAnimationFrame(this.startScrollingAnimationFrame);
}
this.listElement.removeEventListener('scroll', this.onScroll);
this.listElement.removeEventListener('mouseenter', this.onMouseEnter);
this.listElement.removeEventListener('mouseleave', this.onMouseLeave);
Expand Down