fix: repack the library grid only after resize settles
Continuous Integration / build_web (push) Failing after 0s
Continuous Integration / build (macos-latest) (push) Skipped
Continuous Integration / build (ubuntu-22.04) (push) Skipped
Continuous Integration / build (windows-11-arm) (push) Skipped
Continuous Integration / build (windows-latest) (push) Skipped
Continuous Integration / nightly_release (push) Skipped
Continuous Integration / build_web (push) Failing after 0s
Continuous Integration / build (macos-latest) (push) Skipped
Continuous Integration / build (ubuntu-22.04) (push) Skipped
Continuous Integration / build (windows-11-arm) (push) Skipped
Continuous Integration / build (windows-latest) (push) Skipped
Continuous Integration / nightly_release (push) Skipped
Recomputing columns on every pixel of a window drag made cards jump between layouts. Debounce width changes by 120ms.
This commit is contained in:
1 parent
08f0ca0df1
commit
2aa3e4106c
1 file changed
+9
-4
@@ -181,10 +181,11 @@ const LibraryItemsGrid = () => {
|
||||
const element = parentRef.current;
|
||||
if (!element) return;
|
||||
let frame = 0;
|
||||
let timer: ReturnType<typeof setTimeout> | undefined;
|
||||
let lastWidth = -1;
|
||||
|
||||
const measure = () => {
|
||||
frame = 0;
|
||||
const apply = () => {
|
||||
timer = undefined;
|
||||
const width = element.clientWidth;
|
||||
if (Math.abs(width - lastWidth) < 1) return;
|
||||
lastWidth = width;
|
||||
@@ -198,14 +199,18 @@ const LibraryItemsGrid = () => {
|
||||
};
|
||||
|
||||
const schedule = () => {
|
||||
if (!frame) frame = requestAnimationFrame(measure);
|
||||
// While the user drags the window edge, don't reflow the whole grid
|
||||
// on every pixel; repack once the width has been stable briefly.
|
||||
if (timer) clearTimeout(timer);
|
||||
timer = setTimeout(apply, 120);
|
||||
};
|
||||
|
||||
schedule();
|
||||
apply();
|
||||
const observer = new ResizeObserver(schedule);
|
||||
observer.observe(element);
|
||||
return () => {
|
||||
observer.disconnect();
|
||||
if (timer) clearTimeout(timer);
|
||||
if (frame) cancelAnimationFrame(frame);
|
||||
};
|
||||
}, []);
|
||||
|
||||
Reference in new issue
Block a user