Skip to content

Commit

Permalink
fix: ensure to cancel requestAnimationFrame on EuiSelectableList on u…
Browse files Browse the repository at this point in the history
…nmount
  • Loading branch information
mgadewoll committed Apr 29, 2024
1 parent 1929ac4 commit 79e4776
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/components/selectable/selectable_list/selectable_list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,8 @@ export class EuiSelectableList<T> extends Component<
isVirtualized: true,
};

private animationFrameId: number | undefined;

constructor(props: EuiSelectableListProps<T>) {
super(props);

Expand All @@ -196,6 +198,15 @@ export class EuiSelectableList<T> extends Component<
listRef: FixedSizeList | null = null;
listBoxRef: HTMLUListElement | null = null;

componentWillUnmount(): void {
// ensure requestAnimationFrame is canceled on unmount as
// it could potentially run on a next tick otherwise
if (this.animationFrameId !== undefined) {
cancelAnimationFrame(this.animationFrameId);
this.animationFrameId = undefined;
}
}

setListRef = (ref: FixedSizeList | null) => {
this.listRef = ref;

Expand Down Expand Up @@ -508,7 +519,7 @@ export class EuiSelectableList<T> extends Component<
this.focusBadgeOffset = this.props.onFocusBadge === false ? 0 : 46;

// Wait a tick for the listbox ref to update before proceeding
requestAnimationFrame(() => {
this.animationFrameId = requestAnimationFrame(() => {
const scrollbarOffset = this.listBoxRef
? containerWidth - this.listBoxRef.offsetWidth
: 0;
Expand Down

0 comments on commit 79e4776

Please sign in to comment.