Skip to content

Commit 222ea31

Browse files
jasonhuang-skyJason Huang
and
Jason Huang
authored
[BD-9310][BpkMobileScrollContainer] Optimize the use of debounce for compatibility (#3750)
* Safe debounce bpk-component-mobile-scroll-container * fix lint * remove compatibility code for debounce * put back requestAnimationFrame --------- Co-authored-by: Jason Huang <[email protected]>
1 parent 7e71b87 commit 222ea31

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

packages/bpk-component-mobile-scroll-container/src/BpkMobileScrollContainer.tsx

+10-4
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ import debounce from 'lodash/debounce';
2323

2424
import { cssModules, isRTL } from '../../bpk-react-utils';
2525

26+
import type { DebouncedFunc } from 'lodash';
27+
2628
import STYLES from './BpkMobileScrollContainer.module.scss';
2729

2830
const getClassName = cssModules(STYLES);
@@ -84,6 +86,8 @@ type State = {
8486
};
8587

8688
class BpkMobileScrollContainer extends Component<Props, State> {
89+
debouncedResize: DebouncedFunc<() => void>
90+
8791
static defaultProps: Partial<Props> = {
8892
innerContainerTagName: 'div',
8993
showScrollbar: false,
@@ -96,24 +100,26 @@ class BpkMobileScrollContainer extends Component<Props, State> {
96100
computedHeight: 'auto',
97101
scrollIndicatorClassName: null,
98102
};
103+
104+
this.debouncedResize = debounce(this.onWindowResize, 100);
99105
}
100106

101107
componentDidMount() {
102108
requestAnimationFrame(() => {
103109
this.setScrollBarAwareHeight();
104110
this.setScrollIndicatorClassName();
105111
});
106-
window.addEventListener('resize', this.onWindowResize);
112+
window.addEventListener('resize', this.debouncedResize);
107113
}
108114

109115
componentWillUnmount() {
110-
window.removeEventListener('resize', this.onWindowResize);
116+
window.removeEventListener('resize', this.debouncedResize);
111117
}
112118

113-
onWindowResize = debounce(() => {
119+
onWindowResize = () => {
114120
this.setScrollBarAwareHeight();
115121
this.setScrollIndicatorClassName();
116-
}, 100);
122+
};
117123

118124
setScrollIndicatorClassName = () => {
119125
const classNames = computeScrollIndicatorClassName(

0 commit comments

Comments
 (0)