Skip to content

Commit 3fb4281

Browse files
authored
refactor(OptimizedResize): rewrite in typescript (#18731)
1 parent 5a68309 commit 3fb4281

File tree

2 files changed

+25
-26
lines changed

2 files changed

+25
-26
lines changed

packages/react/src/internal/FloatingMenu.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import * as FeatureFlags from '@carbon/feature-flags';
2222
import ReactDOM from 'react-dom';
2323
import window from 'window-or-global';
2424
import { keys, match } from '../internal/keyboard';
25-
import OptimizedResize from './OptimizedResize';
25+
import { OptimizedResize } from './OptimizedResize';
2626
import { selectorFocusable, selectorTabbable } from './keyboard/navigation';
2727
import { PrefixContext } from './usePrefix';
2828
import { warning } from './warning';
@@ -399,7 +399,7 @@ export const FloatingMenu = ({
399399
});
400400

401401
return () => {
402-
resizeHandler.release();
402+
resizeHandler.remove();
403403
};
404404
}, [
405405
triggerRef,
Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright IBM Corp. 2016, 2023
2+
* Copyright IBM Corp. 2016, 2025
33
*
44
* This source code is licensed under the Apache-2.0 license found in the
55
* LICENSE file in the root directory of this source tree.
@@ -8,46 +8,47 @@
88
// mdn resize function
99
import window from 'window-or-global';
1010

11-
const OptimizedResize = (function optimizedResize() {
12-
const callbacks = [];
11+
/**
12+
* A callback function to be executed on `resize`.
13+
*/
14+
type Callback = () => void;
15+
16+
export const OptimizedResize = (() => {
17+
const callbacks: Callback[] = [];
1318
let running = false;
1419

15-
// run the actual callbacks
16-
function runCallbacks() {
20+
const runCallbacks = () => {
1721
callbacks.forEach((callback) => {
1822
callback();
1923
});
2024

2125
running = false;
22-
}
26+
};
2327

24-
// fired on resize event
25-
function resize() {
28+
const handleResize = () => {
2629
if (!running) {
2730
running = true;
2831
window.requestAnimationFrame(runCallbacks);
2932
}
30-
}
31-
32-
// adds callback to loop
33-
function addCallback(callback) {
34-
if (callback) {
35-
const index = callbacks.indexOf(callback);
36-
if (index < 0) {
37-
callbacks.push(callback);
38-
}
33+
};
34+
35+
const addCallback = (callback: Callback) => {
36+
const index = callbacks.indexOf(callback);
37+
if (index < 0) {
38+
callbacks.push(callback);
3939
}
40-
}
40+
};
4141

4242
return {
43-
// public method to add additional callback
44-
add: (callback) => {
43+
/** Adds a callback function to be executed on window `resize`. */
44+
add: (callback: Callback) => {
4545
if (!callbacks.length) {
46-
window.addEventListener('resize', resize);
46+
window.addEventListener('resize', handleResize);
4747
}
4848
addCallback(callback);
4949
return {
50-
release() {
50+
/** Removes the callback. */
51+
remove: () => {
5152
const index = callbacks.indexOf(callback);
5253
if (index >= 0) {
5354
callbacks.splice(index, 1);
@@ -57,5 +58,3 @@ const OptimizedResize = (function optimizedResize() {
5758
},
5859
};
5960
})();
60-
61-
export default OptimizedResize;

0 commit comments

Comments
 (0)