Skip to content

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

TypeScript

perf(OverflowList): only the multi-row form tracks rows and rowHeight (#8523)

Christopher Chedeauf9281ef1cfe9
diff --git a/.changeset/overflow-rows-multirow-only.md b/.changeset/overflow-rows-multirow-only.md
new file mode 100644
index 0000000000..4669ca9b3d
--- /dev/null
+++ b/.changeset/overflow-rows-multirow-only.md
@@ -0,0 +1,7 @@
+---
+'@astryxdesign/core': patch
+---
+
+[perf] A single-line `OverflowList` no longer renders again after mount only to record an item height it never shows: it does not track the row count and item height that only its multi-row form (`maxRows` above 1) renders from. One whose items all fit now renders once on mount; one that collapses items still renders again for the new visible count. The public `useOverflow` hook is unchanged and still reports both on every path.
+
+@vjeux
diff --git a/packages/core/src/OverflowList/OverflowList.tsx b/packages/core/src/OverflowList/OverflowList.tsx
index fed9a652dc..79a6885b9f 100644
--- a/packages/core/src/OverflowList/OverflowList.tsx
+++ b/packages/core/src/OverflowList/OverflowList.tsx
@@ -31,7 +31,7 @@ import {isRenderable} from '../utils/isRenderable';
import type {SpacingStep} from '../utils/types';
import * as stylex from '@stylexjs/stylex';
import {mergeProps} from '../utils';
-import {useOverflow} from '../hooks/useOverflow';
+import {useOverflowInternal} from '../hooks/useOverflow';
import {useIsomorphicLayoutEffect} from '../hooks/useIsomorphicLayoutEffect';
import {spacingVars} from '../theme/tokens.stylex';
import {themeProps} from '../utils/themeProps';
@@ -284,15 +284,21 @@ export function OverflowList({
const observeParent = behavior === 'observeParent';
const isMultiRow = maxRows != null && maxRows > 1;
+ // Only the multi-row path renders from `rowHeight` (it caps the
+ // container's height), so only that path tracks it.
const {containerRef, measureRef, visibleCount, hasOverflow, rowHeight} =
- useOverflow(itemCount, {
- gap: gapPx,
- minVisibleItems,
- maxVisibleItems,
- maxRows,
- collapseFrom,
- behavior,
- });
+ useOverflowInternal(
+ itemCount,
+ {
+ gap: gapPx,
+ minVisibleItems,
+ maxVisibleItems,
+ maxRows,
+ collapseFrom,
+ behavior,
+ },
+ {tracksRows: isMultiRow},
+ );
const allItems: OverflowItem[] = childArray.map((child, index) => ({
child,
diff --git a/packages/core/src/hooks/useOverflow.test.ts b/packages/core/src/hooks/useOverflow.test.ts
index 069b6284fb..a0596d2697 100644
--- a/packages/core/src/hooks/useOverflow.test.ts
+++ b/packages/core/src/hooks/useOverflow.test.ts
@@ -2,7 +2,7 @@
import {describe, it, expect, vi, beforeEach} from 'vitest';
import {renderHook, act} from '@testing-library/react';
-import {useOverflow} from './useOverflow';
+import {useOverflow, useOverflowInternal} from './useOverflow';
// Builds a mock container element with a given offsetWidth
function mockContainer(width: number): HTMLElement {
@@ -386,6 +386,72 @@ describe('useOverflow with maxVisibleItems (cap)', () => {
});
});
+describe('useOverflowInternal without row tracking', () => {
+ it('renders the host once per measurement that changes nothing it shows: rows and rowHeight are not tracked', () => {
+ let renders = 0;
+ const {result} = renderHook(() => {
+ renders += 1;
+ return useOverflowInternal(3, {gap: 10}, {tracksRows: false});
+ });
+
+ act(() => {
+ // 3 items of 50px, 24px tall, gap 10 → 170 fits in 200: the count stays.
+ result.current.measureRef(mockMeasure([50, 50, 50], 40, 24));
+ result.current.containerRef(mockContainer(200));
+ });
+
+ // The measurement found the same count the hook started with; the item
+ // height it also read is nobody's to render here, so nothing renders.
+ expect(renders).toBe(1);
+ expect(result.current.visibleCount).toBe(3);
+ expect(result.current.rows).toBe(1);
+ expect(result.current.rowHeight).toBe(0);
+ });
+
+ it('reports rows 1 and rowHeight 0 once a multi-row host stops tracking', () => {
+ const initialProps: {maxRows: number | undefined} = {maxRows: 2};
+ const {result, rerender} = renderHook(
+ ({maxRows}: {maxRows: number | undefined}) =>
+ useOverflowInternal(
+ 6,
+ {gap: 10, maxRows},
+ {tracksRows: maxRows != null && maxRows > 1},
+ ),
+ {initialProps},
+ );
+
+ act(() => {
+ result.current.measureRef(mockMeasure([50, 50, 50, 50, 50, 50], 40, 24));
+ result.current.containerRef(mockContainer(170));
+ });
+ expect(result.current.visibleCount).toBe(6);
+ expect(result.current.rows).toBe(2);
+ expect(result.current.rowHeight).toBe(24);
+
+ rerender({maxRows: undefined});
+ // The untracked values, not the state the multi-row measurements left
+ // behind; the count is re-measured for one row.
+ expect(result.current.rows).toBe(1);
+ expect(result.current.rowHeight).toBe(0);
+ expect(result.current.visibleCount).toBeLessThan(6);
+ });
+});
+
+describe('useOverflow on the single-line path', () => {
+ it('still measures rowHeight, as its return type documents', () => {
+ const {result} = renderHook(() => useOverflow(3, {gap: 10}));
+
+ act(() => {
+ result.current.measureRef(mockMeasure([50, 50, 50], 40, 24));
+ result.current.containerRef(mockContainer(200));
+ });
+
+ expect(result.current.visibleCount).toBe(3);
+ expect(result.current.rows).toBe(1);
+ expect(result.current.rowHeight).toBe(24);
+ });
+});
+
describe('useOverflow with maxRows (multi-row)', () => {
it('exposes rows and rowHeight', () => {
const {result} = renderHook(() => useOverflow(6, {gap: 10, maxRows: 2}));
diff --git a/packages/core/src/hooks/useOverflow.ts b/packages/core/src/hooks/useOverflow.ts
index 67f68bbbc0..36390eb609 100644
--- a/packages/core/src/hooks/useOverflow.ts
+++ b/packages/core/src/hooks/useOverflow.ts
@@ -263,6 +263,21 @@ function observedSizeFollowsItems(
export function useOverflow(
itemCount: number,
options: UseOverflowOptions = {},
+): UseOverflowReturn {
+ return useOverflowInternal(itemCount, options, {tracksRows: true});
+}
+
+/**
+ * `useOverflow` for a host inside Core that states whether it renders from
+ * `rows` and `rowHeight`. With `tracksRows` false, the hook does not track
+ * them and reports `rows: 1` and `rowHeight: 0`. A state write that nobody
+ * reads still re-renders the host. Internal: the public hook always tracks
+ * them.
+ */
+export function useOverflowInternal(
+ itemCount: number,
+ options: UseOverflowOptions,
+ {tracksRows}: {tracksRows: boolean},
): UseOverflowReturn {
const {
gap = 0,
@@ -303,6 +318,11 @@ export function useOverflow(
applied.visibleCount = nextVisible;
setVisibleCount(nextVisible);
}
+ // A host that does not track rows renders from neither value, so they
+ // are not written.
+ if (!tracksRows) {
+ return;
+ }
if (applied.rows !== nextRows) {
applied.rows = nextRows;
setRows(nextRows);
@@ -312,7 +332,7 @@ export function useOverflow(
setRowHeight(nextRowHeight);
}
},
- [],
+ [tracksRows],
);
const calculate: Measure = useCallback(
@@ -488,7 +508,9 @@ export function useOverflow(
measureRef,
visibleCount,
hasOverflow,
- rows,
- rowHeight,
+ // A host that stops tracking keeps state from earlier measurements, so
+ // the return uses the fixed values.
+ rows: tracksRows ? rows : 1,
+ rowHeight: tracksRows ? rowHeight : 0,
};
}