mirror of
https://github.com/suitenumerique/docs.git
synced 2026-09-12 12:47:55 +02:00
⚡️(frontend) responsive throttling instead of debouncing
We changed the responsive store to use throttling instead of debouncing for the window resize event. This change ensures that the store updates more consistently during rapid window resizing, providing a smoother user experience.
This commit is contained in:
@@ -42,6 +42,8 @@ export const useResponsiveStore = create<UseResponsiveStore>((set) => ({
|
||||
screenWidth: initialState.screenWidth,
|
||||
setScreenSize: (size: ScreenSize) => set(() => ({ screenSize: size })),
|
||||
initializeResizeListener: () => {
|
||||
let pendingResize = false;
|
||||
|
||||
const resizeHandler = () => {
|
||||
const width = window.innerWidth;
|
||||
if (width < BREAKPOINTS.SMALL_MOBILE) {
|
||||
@@ -89,10 +91,16 @@ export const useResponsiveStore = create<UseResponsiveStore>((set) => ({
|
||||
|
||||
let resizeTimeout: ReturnType<typeof setTimeout> | undefined;
|
||||
const debouncedResizeHandler = () => {
|
||||
if (pendingResize) {
|
||||
return;
|
||||
}
|
||||
|
||||
pendingResize = true;
|
||||
clearTimeout(resizeTimeout);
|
||||
resizeTimeout = setTimeout(() => {
|
||||
resizeHandler();
|
||||
}, 300);
|
||||
pendingResize = false;
|
||||
}, 200);
|
||||
};
|
||||
|
||||
window.addEventListener('resize', debouncedResizeHandler);
|
||||
|
||||
Reference in New Issue
Block a user