Skip to content

Commit e2ba8fe

Browse files
fix(unity-bootstrap-theme): replace custom throttle with @asu/shared
1 parent 53cd5b3 commit e2ba8fe

File tree

1 file changed

+2
-36
lines changed

1 file changed

+2
-36
lines changed

packages/unity-bootstrap-theme/src/js/anchor-menu.js

Lines changed: 2 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,5 @@
11
import { EventHandler } from "./bootstrap-helper";
2-
3-
/**
4-
* Throttles a function so it's called at most once during a specified delay.
5-
*
6-
* @param {function} func The function to throttle.
7-
* @param {number} delay The delay in milliseconds.
8-
* @return {function} The throttled function.
9-
*/
10-
function throttle(func, delay) {
11-
let timeoutId;
12-
let lastArgs;
13-
let lastThis;
14-
let calledDuringDelay = false;
15-
16-
return function (...args) {
17-
lastArgs = args;
18-
lastThis = this;
19-
20-
if (!timeoutId) {
21-
func.apply(lastThis, lastArgs);
22-
calledDuringDelay = false;
23-
timeoutId = setTimeout(() => {
24-
timeoutId = null;
25-
if (calledDuringDelay) {
26-
func.apply(lastThis, lastArgs);
27-
calledDuringDelay = false;
28-
}
29-
}, delay);
30-
} else {
31-
calledDuringDelay = true;
32-
}
33-
};
34-
}
2+
import { throttle } from "@asu/shared";
353

364
/**
375
* Initializes the anchor menu functionality.
@@ -179,9 +147,7 @@ function initAnchorMenu() {
179147
previousScrollPosition = window.scrollY;
180148
};
181149

182-
const throttledScrollHandler = throttle(scrollHandlerLogic, SCROLL_DELAY);
183-
184-
window.addEventListener("scroll", throttledScrollHandler, { passive: true });
150+
window.addEventListener("scroll", () => throttle(scrollHandlerLogic, SCROLL_DELAY), { passive: true });
185151

186152
// Set click event of anchors
187153
for (let [anchor, anchorTarget] of anchorTargets) {

0 commit comments

Comments
 (0)