Skip to content

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

TypeScript

fix(grid): lone column fills when fewer than max columns render (#3448)

Joey9daca871ab86
diff --git a/.changeset/grid-lone-column-fill.md b/.changeset/grid-lone-column-fill.md
new file mode 100644
index 0000000000..30f04d0caf
--- /dev/null
+++ b/.changeset/grid-lone-column-fill.md
@@ -0,0 +1,7 @@
+---
+'@astryxdesign/core': patch
+---
+
+[fix] Grid: with `columns={{minWidth, max}}`, columns that are present now fill the row when fewer than `max` fit. Previously the `max` cap was applied to each track's max size, so a layout collapsing to a single column (e.g. on mobile) left dead space on the right instead of stretching to full width. The cap now limits the column count while letting present columns reach `1fr`. (#3391)
+
+@josephfarina
diff --git a/apps/storybook/stories/Grid.stories.tsx b/apps/storybook/stories/Grid.stories.tsx
index 540ab91216..9975e878f5 100644
--- a/apps/storybook/stories/Grid.stories.tsx
+++ b/apps/storybook/stories/Grid.stories.tsx
@@ -238,7 +238,8 @@ export const CappedResponsive: Story = {
render: () => (
<div {...stylex.props(styles.container)}>
<Text type="supporting" xstyle={styles.sectionLabel}>
- Responsive with max 3 columns (min 250px per item, capped via track-max)
+ Responsive with max 3 columns (min 250px per item; column count is
+ capped but present columns always fill)
</Text>
<Grid columns={{minWidth: 250, max: 3}} gap={4}>
<GridItem>Item 1</GridItem>
@@ -252,6 +253,25 @@ export const CappedResponsive: Story = {
),
};
+export const CappedCollapsesToFullWidth: Story = {
+ name: 'Capped — fills when collapsed (#3391)',
+ render: () => (
+ <div {...stylex.props(styles.container)}>
+ <Text type="supporting" xstyle={styles.sectionLabel}>
+ columns={'{{minWidth: 360, max: 2}}'} — resize the viewport narrow
+ enough that only one column fits. The lone column stretches to full
+ width (no dead space on the right); on wider viewports it caps at 2.
+ </Text>
+ <div style={{maxWidth: 520, resize: 'horizontal', overflow: 'auto'}}>
+ <Grid columns={{minWidth: 360, max: 2}} gap={4}>
+ <GridItem>Left block</GridItem>
+ <GridItem>Right block</GridItem>
+ </Grid>
+ </div>
+ </div>
+ ),
+};
+
export const WithGridSpan: Story = {
render: () => (
<div {...stylex.props(styles.container)}>
diff --git a/packages/core/src/Grid/Grid.test.tsx b/packages/core/src/Grid/Grid.test.tsx
index 2b65835749..376e176436 100644
--- a/packages/core/src/Grid/Grid.test.tsx
+++ b/packages/core/src/Grid/Grid.test.tsx
@@ -65,7 +65,7 @@ describe('Grid', () => {
expect(templateColumns(grid)).toBe('repeat(auto-fill, minmax(250px, 1fr))');
});
- it('renders with columns object max (capped via track-max)', () => {
+ it('renders with columns object max (count capped, tracks still fill)', () => {
render(
<Grid columns={{minWidth: 250, max: 3}} gap={4} data-testid="grid">
<div>Item 1</div>
@@ -74,13 +74,37 @@ describe('Grid', () => {
</Grid>,
);
const grid = screen.getByTestId('grid');
- // Track max caps at (100% - 2 * gap) / 3 — no maxWidth on container
+ // Cap lives on the track MIN (min(100%, max(minWidth, perColumn))); the
+ // track MAX stays 1fr so present columns fill the row (a lone column on
+ // mobile stretches to 100% instead of leaving dead space).
expect(templateColumns(grid)).toBe(
- 'repeat(auto-fill, minmax(250px, calc((100% - 2 * var(--spacing-4)) / 3)))',
+ 'repeat(auto-fill, minmax(min(100%, max(250px, calc((100% - 2 * var(--spacing-4)) / 3))), 1fr))',
);
expect(grid.style.maxWidth).toBe('');
});
+ it('keeps track max at 1fr with a max cap so a lone column can fill (#3391)', () => {
+ // Regression: previously the cap was applied to the track MAX
+ // (minmax(minWidth, 100%/max)), so when fewer than `max` columns fit —
+ // e.g. a single column on mobile — the lone column was pinned to ~100%/max
+ // and left dead space on the right. The cap now lives on the track MIN and
+ // the track MAX stays 1fr, so present columns always stretch to fill.
+ render(
+ <Grid columns={{minWidth: 360, max: 2}} gap={4} data-testid="grid">
+ <div>Left</div>
+ <div>Right</div>
+ </Grid>,
+ );
+ const grid = screen.getByTestId('grid');
+ const template = templateColumns(grid);
+ // Track max must be 1fr (fills), not a fraction-of-container cap.
+ expect(template).toMatch(/, 1fr\)\)$/);
+ expect(template).not.toMatch(/, calc\([^)]*\/ 2\)\)\)$/);
+ expect(template).toBe(
+ 'repeat(auto-fill, minmax(min(100%, max(360px, calc((100% - 1 * var(--spacing-4)) / 2))), 1fr))',
+ );
+ });
+
it('renders with columns object max using columnGap', () => {
render(
<Grid columns={{minWidth: 200, max: 4}} columnGap={6} data-testid="grid">
@@ -89,9 +113,9 @@ describe('Grid', () => {
</Grid>,
);
const grid = screen.getByTestId('grid');
- // columnGap takes precedence for track-max calculation
+ // columnGap takes precedence in the perColumn floor calculation
expect(templateColumns(grid)).toBe(
- 'repeat(auto-fill, minmax(200px, calc((100% - 3 * var(--spacing-6)) / 4)))',
+ 'repeat(auto-fill, minmax(min(100%, max(200px, calc((100% - 3 * var(--spacing-6)) / 4))), 1fr))',
);
expect(grid.style.maxWidth).toBe('');
});
@@ -165,7 +189,7 @@ describe('Grid', () => {
expect(templateColumns(grid)).toBe('1fr');
});
- it('uses auto-fill without track-max cap when no max specified', () => {
+ it('uses auto-fill with a plain 1fr track when no max specified', () => {
render(
<Grid columns={{minWidth: 200}} data-testid="grid">
<div>Item</div>
@@ -220,7 +244,7 @@ describe('Grid', () => {
// --- P2: columns object + columnGap interaction (hardening #719) ---
- it('uses columnGap var in track-max when both columnGap and gap are set', () => {
+ it('uses columnGap var in the count-cap floor when both columnGap and gap are set', () => {
render(
<Grid
columns={{minWidth: 200, max: 3}}
@@ -231,14 +255,14 @@ describe('Grid', () => {
</Grid>,
);
const grid = screen.getByTestId('grid');
- // columnGap takes precedence over gap in track-max
+ // columnGap takes precedence over gap in the perColumn floor
expect(templateColumns(grid)).toBe(
- 'repeat(auto-fill, minmax(200px, calc((100% - 2 * var(--spacing-6)) / 3)))',
+ 'repeat(auto-fill, minmax(min(100%, max(200px, calc((100% - 2 * var(--spacing-6)) / 3))), 1fr))',
);
expect(grid.style.maxWidth).toBe('');
});
- it('uses gap var in track-max when columnGap is not set', () => {
+ it('uses gap var in the count-cap floor when columnGap is not set', () => {
render(
<Grid columns={{minWidth: 150, max: 2}} gap={3} data-testid="grid">
<div>Item</div>
@@ -246,12 +270,12 @@ describe('Grid', () => {
);
const grid = screen.getByTestId('grid');
expect(templateColumns(grid)).toBe(
- 'repeat(auto-fill, minmax(150px, calc((100% - 1 * var(--spacing-3)) / 2)))',
+ 'repeat(auto-fill, minmax(min(100%, max(150px, calc((100% - 1 * var(--spacing-3)) / 2))), 1fr))',
);
expect(grid.style.maxWidth).toBe('');
});
- it('uses simple fraction in track-max when no gap is set', () => {
+ it('uses simple fraction in the count-cap floor when no gap is set', () => {
render(
<Grid columns={{minWidth: 100, max: 3}} data-testid="grid">
<div>Item</div>
@@ -259,7 +283,7 @@ describe('Grid', () => {
);
const grid = screen.getByTestId('grid');
expect(templateColumns(grid)).toBe(
- 'repeat(auto-fill, minmax(100px, calc(100% / 3)))',
+ 'repeat(auto-fill, minmax(min(100%, max(100px, calc(100% / 3))), 1fr))',
);
expect(grid.style.maxWidth).toBe('');
});
@@ -332,7 +356,7 @@ describe('Grid', () => {
expect(templateColumns(grid)).toBe('repeat(auto-fill, minmax(280px, 1fr))');
});
- it('renders with columns={{minWidth, max}} capping via track-max', () => {
+ it('renders with columns={{minWidth, max}} capping the count while filling', () => {
render(
<Grid columns={{minWidth: 280, max: 3}} gap={4} data-testid="grid">
<div>Item 1</div>
@@ -340,14 +364,15 @@ describe('Grid', () => {
</Grid>,
);
const grid = screen.getByTestId('grid');
- // Track-max limits columns — grid stays full width
+ // Count is capped via the track MIN; track MAX stays 1fr so present
+ // columns fill the row (grid stays full width).
expect(templateColumns(grid)).toBe(
- 'repeat(auto-fill, minmax(280px, calc((100% - 2 * var(--spacing-4)) / 3)))',
+ 'repeat(auto-fill, minmax(min(100%, max(280px, calc((100% - 2 * var(--spacing-4)) / 3))), 1fr))',
);
expect(grid.style.maxWidth).toBe('');
});
- it('renders with columns={{minWidth, max, repeat: "fit"}} using auto-fit + track-max', () => {
+ it('renders with columns={{minWidth, max, repeat: "fit"}} using auto-fit + count cap', () => {
render(
<Grid
columns={{minWidth: 280, max: 3, repeat: 'fit'}}
@@ -359,7 +384,7 @@ describe('Grid', () => {
);
const grid = screen.getByTestId('grid');
expect(templateColumns(grid)).toBe(
- 'repeat(auto-fit, minmax(280px, calc((100% - 2 * var(--spacing-4)) / 3)))',
+ 'repeat(auto-fit, minmax(min(100%, max(280px, calc((100% - 2 * var(--spacing-4)) / 3))), 1fr))',
);
expect(grid.style.maxWidth).toBe('');
});
diff --git a/packages/core/src/Grid/Grid.tsx b/packages/core/src/Grid/Grid.tsx
index 0cd85c0a3c..4c67726e0e 100644
--- a/packages/core/src/Grid/Grid.tsx
+++ b/packages/core/src/Grid/Grid.tsx
@@ -38,9 +38,10 @@ export type GridAlignment = 'start' | 'center' | 'end' | 'stretch';
* - `minWidth` — minimum width (px) for each column track
* - `repeat` — `'fill'` (default) preserves empty tracks for consistent widths;
* `'fit'` collapses empty tracks so items stretch to fill
- * - `max` — caps the maximum number of columns by limiting track max size.
- * The grid stretches to 100% of its parent; `max` only limits how many
- * columns appear.
+ * - `max` — caps the maximum number of columns. The grid stretches to 100%
+ * of its parent and the columns that are present always fill the row, so a
+ * layout collapsing to a single column on mobile stretches to full width
+ * (no dead space on the right).
*/
export type GridColumns =
| number
@@ -321,9 +322,20 @@ const spacingVarNames: Record<SpacingStep, string> = {
};
/**
- * Build a grid-template-columns value that caps columns at `max` by
- * limiting each track's max size to `(100% - (max-1) * gap) / max`.
- * The grid stays 100% width; the track-max prevents more than `max` columns.
+ * Build a grid-template-columns value that caps the column *count* at `max`
+ * while still letting the columns that are actually present stretch to fill
+ * the row.
+ *
+ * The cap lives on the track's **min** size, not its max: each track is at
+ * least `perColumn = (100% - (max-1) * gap) / max`, so more than `max` columns
+ * can never fit. The track **max** stays `1fr`, so whenever fewer than `max`
+ * columns fit (most importantly a lone column on mobile) they still grow to
+ * fill the whole row — no dead space on the right.
+ *
+ * The track min is `max(minWidth, perColumn)` so an explicit `minWidth` is
+ * still honored, wrapped in `min(100%, …)` so a single column on a viewport
+ * narrower than `minWidth`/`perColumn` shrinks to the container instead of
+ * overflowing it.
*/
function buildCappedTemplate(
minWidth: number,
@@ -339,12 +351,17 @@ function buildCappedTemplate(
? spacingVarNames[gap]
: null;
- // trackMax = (100% - (maxCols - 1) * gap) / maxCols
- const trackMax = gapVar
+ // perColumn = (100% - (maxCols - 1) * gap) / maxCols — the width a track
+ // would have if exactly `maxCols` columns were present. Used as a floor so
+ // the grid never fits more than `maxCols` columns.
+ const perColumn = gapVar
? `calc((100% - ${maxCols - 1} * var(${gapVar})) / ${maxCols})`
: `calc(100% / ${maxCols})`;
- return `repeat(${repeatMode}, minmax(${minWidth}px, ${trackMax}))`;
+ // Track min caps the count; track max stays 1fr so present columns fill.
+ const trackMin = `min(100%, max(${minWidth}px, ${perColumn}))`;
+
+ return `repeat(${repeatMode}, minmax(${trackMin}, 1fr))`;
}
/**