Skip to content

Commit e971bb1

Browse files
author
Muhammed Teuvazhukov
committed
Add AbortController to clean up event listeners
- Change other addEventListeners to AbortController way for same code style across listeners
1 parent 9bcb665 commit e971bb1

File tree

1 file changed

+30
-12
lines changed

1 file changed

+30
-12
lines changed

src/stickyScrollBar.tsx

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -141,18 +141,25 @@ const StickyScrollBar: React.ForwardRefRenderFunction<unknown, StickyScrollBarPr
141141
}));
142142

143143
React.useEffect(() => {
144-
document.body.addEventListener(MOUSEUP_EVENT, onMouseUp, false);
145-
document.body.addEventListener(MOUSEMOVE_EVENT, onMouseMove, false);
144+
const abortController = new AbortController();
145+
document.body.addEventListener(MOUSEUP_EVENT, onMouseUp, {
146+
capture: false,
147+
signal: abortController.signal,
148+
});
149+
document.body.addEventListener(MOUSEMOVE_EVENT, onMouseMove, {
150+
capture: false,
151+
signal: abortController.signal,
152+
});
146153
checkScrollBarVisible();
147154
return () => {
148-
document.body.removeEventListener(MOUSEUP_EVENT, onMouseUp);
149-
document.body.removeEventListener(MOUSEMOVE_EVENT, onMouseMove);
155+
abortController.abort();
150156
};
151157
}, [scrollBarWidth, isActive]);
152158

153159
// Loop for scroll event check
154160
React.useEffect(() => {
155161
if (!scrollBodyRef.current) return;
162+
const abortController = new AbortController();
156163

157164
const scrollParents: (HTMLElement | SVGElement)[] = [];
158165
let parent = getDOM(scrollBodyRef.current);
@@ -161,16 +168,27 @@ const StickyScrollBar: React.ForwardRefRenderFunction<unknown, StickyScrollBarPr
161168
parent = parent.parentElement;
162169
}
163170

164-
scrollParents.forEach(p => p.addEventListener(SCROLL_EVENT, checkScrollBarVisible, false));
165-
window.addEventListener(RESIZE_EVENT, checkScrollBarVisible, false);
166-
window.addEventListener(SCROLL_EVENT, checkScrollBarVisible, false);
167-
container.addEventListener(SCROLL_EVENT, checkScrollBarVisible, false);
171+
scrollParents.forEach(p =>
172+
p.addEventListener(SCROLL_EVENT, checkScrollBarVisible, {
173+
capture: false,
174+
signal: abortController.signal,
175+
}),
176+
);
177+
window.addEventListener(RESIZE_EVENT, checkScrollBarVisible, {
178+
capture: false,
179+
signal: abortController.signal,
180+
});
181+
window.addEventListener(SCROLL_EVENT, checkScrollBarVisible, {
182+
capture: false,
183+
signal: abortController.signal,
184+
});
185+
container.addEventListener(SCROLL_EVENT, checkScrollBarVisible, {
186+
capture: false,
187+
signal: abortController.signal,
188+
});
168189

169190
return () => {
170-
scrollParents.forEach(p => p.removeEventListener(SCROLL_EVENT, checkScrollBarVisible));
171-
window.removeEventListener(RESIZE_EVENT, checkScrollBarVisible);
172-
window.removeEventListener(SCROLL_EVENT, checkScrollBarVisible);
173-
container.removeEventListener(SCROLL_EVENT, checkScrollBarVisible);
191+
abortController.abort();
174192
};
175193
}, [container]);
176194

0 commit comments

Comments
 (0)