Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

fix(useResizable): read the window size during render only, not again after each commit (#8857)

Cindy Zhang9b695006a6b3
diff --git a/.changeset/resizable-window-reads-once.md b/.changeset/resizable-window-reads-once.md
new file mode 100644
index 0000000000..6e0289b4a2
--- /dev/null
+++ b/.changeset/resizable-window-reads-once.md
@@ -0,0 +1,7 @@
+---
+'@astryxdesign/core': patch
+---
+
+[fix] `useResizable` (and every component that uses it, such as `SideNav`) no longer reads the window size again after each render it commits. That read forced a layout of the whole page inside every commit, so a tab switch under an `AppShell` with a `SideNav` paid for it on slow devices. The window is still read during render, and every size, bound and reported maximum is unchanged.
+
+@cixzhang
diff --git a/packages/core/src/Resizable/useResizable.test.ts b/packages/core/src/Resizable/useResizable.test.ts
index bed1bc8a41..72e5b2e773 100644
--- a/packages/core/src/Resizable/useResizable.test.ts
+++ b/packages/core/src/Resizable/useResizable.test.ts
@@ -1680,3 +1680,46 @@ describe('ResizableProps source compatibility (AST-010 API5)', () => {
expect(prePr._onResizeCancel).toBeUndefined();
});
});
+
+describe('useResizable window reads after a commit', () => {
+ // React re-reads a useSyncExternalStore snapshot after the commit whenever
+ // getSnapshot is a new function, and a window size read after the DOM has
+ // changed forces a layout of the whole page. A re-render with unchanged
+ // inputs reads the window during render only.
+ function readsAfterCommit(direction: 'horizontal' | 'vertical') {
+ const reads = {count: 0};
+ const width = vi.spyOn(window, 'innerWidth', 'get');
+ const height = vi.spyOn(window, 'innerHeight', 'get');
+ width.mockImplementation(() => {
+ reads.count += 1;
+ return 1024;
+ });
+ height.mockImplementation(() => {
+ reads.count += 1;
+ return 768;
+ });
+ function Region({tick}: {tick: number}) {
+ useResizable({defaultSize: 240, direction});
+ // Runs in the commit, before passive effects: every read counted
+ // after it happened after the DOM changed.
+ useLayoutEffect(() => {
+ reads.count = 0;
+ });
+ return createElement('div', {'data-tick': tick});
+ }
+ const {rerender} = render(createElement(Region, {tick: 0}));
+ act(() => rerender(createElement(Region, {tick: 1})));
+ const count = reads.count;
+ width.mockRestore();
+ height.mockRestore();
+ return count;
+ }
+
+ it('reads the window width again only during render, not after the commit', () => {
+ expect(readsAfterCommit('horizontal')).toBe(0);
+ });
+
+ it('reads the window height again only during render, not after the commit', () => {
+ expect(readsAfterCommit('vertical')).toBe(0);
+ });
+});
diff --git a/packages/core/src/Resizable/useResizable.ts b/packages/core/src/Resizable/useResizable.ts
index 7c011937b2..968ee46f8e 100644
--- a/packages/core/src/Resizable/useResizable.ts
+++ b/packages/core/src/Resizable/useResizable.ts
@@ -306,6 +306,34 @@ const SERVER_BASIS = 1200;
/** A store that never changes on its own; the snapshot is re-read on render. */
const noopSubscribe = () => () => {};
+
+// The window snapshots are module functions, so their identity is stable:
+// React re-reads a snapshot after the commit whenever getSnapshot is a new
+// function, and a window size read after the DOM changed forces a layout of
+// the whole page. With these, the window is read during render only.
+
+/** The viewport width, the basis without a container. */
+function readViewportBasis(): number {
+ return typeof window === 'undefined' ? SERVER_BASIS : window.innerWidth;
+}
+
+function readServerBasis(): number {
+ return SERVER_BASIS;
+}
+
+/** The window's extent along a horizontal hook's axis. */
+function readWindowWidth(): number | null {
+ return typeof window === 'undefined' ? null : window.innerWidth;
+}
+
+/** The window's extent along a vertical hook's axis. */
+function readWindowHeight(): number | null {
+ return typeof window === 'undefined' ? null : window.innerHeight;
+}
+
+function readNoWindowExtent(): null {
+ return null;
+}
const DEFAULT_SIZE_FALLBACK = 250;
const RESIZABLE_SIZE_GUIDANCE =
'Use a non-negative number of pixels, an exact "Npx" string, an exact ' +
@@ -698,8 +726,8 @@ function useSingleResizable(config: UseResizableSingleConfig): ResizableRegion {
);
const viewportBasis = useSyncExternalStore(
subscribeToViewport,
- () => (typeof window === 'undefined' ? SERVER_BASIS : window.innerWidth),
- () => SERVER_BASIS,
+ readViewportBasis,
+ readServerBasis,
);
// The window's extent along the hook's axis, for the REPORTED maximum of an
@@ -710,13 +738,8 @@ function useSingleResizable(config: UseResizableSingleConfig): ResizableRegion {
// (component:Resizable Performance and resources).
const windowExtent = useSyncExternalStore(
noopSubscribe,
- () =>
- typeof window === 'undefined'
- ? null
- : direction === 'vertical'
- ? window.innerHeight
- : window.innerWidth,
- () => null,
+ direction === 'vertical' ? readWindowHeight : readWindowWidth,
+ readNoWindowExtent,
);
// With a container, its content box is the basis; without one, the released