Skip to content

Commit 31a941c

Browse files
committed
handle missing template row/column to the grid
1 parent a6b3032 commit 31a941c

6 files changed

Lines changed: 214 additions & 2 deletions

File tree

lib/CssGrid/CssGrid_layout.ts

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -434,8 +434,36 @@ export const CssGrid_layout = (
434434
if (cell.column + cell.columnSpan > maxCol)
435435
maxCol = cell.column + cell.columnSpan
436436
}
437-
while (rowSizes.length < maxRow) rowSizes.push(0)
438-
while (columnSizes.length < maxCol) columnSizes.push(0)
437+
438+
// Handle implicit row sizing when no gridTemplateRows is specified
439+
if (rowSizes.length === 0 && maxRow > 0 && opts.containerHeight) {
440+
// If no explicit rows but we have content and a container height,
441+
// distribute the height equally among the implicit rows
442+
const implicitRowCount = maxRow
443+
const availableHeight = opts.containerHeight - rowGap * (implicitRowCount - 1)
444+
const implicitRowHeight = availableHeight / implicitRowCount
445+
for (let i = 0; i < implicitRowCount; i++) {
446+
rowSizes.push(implicitRowHeight)
447+
}
448+
} else {
449+
// Default behavior: add zero-height rows
450+
while (rowSizes.length < maxRow) rowSizes.push(0)
451+
}
452+
453+
// Handle implicit column sizing when no gridTemplateColumns is specified
454+
if (columnSizes.length === 0 && maxCol > 0 && opts.containerWidth) {
455+
// If no explicit columns but we have content and a container width,
456+
// distribute the width equally among the implicit columns
457+
const implicitColCount = maxCol
458+
const availableWidth = opts.containerWidth - columnGap * (implicitColCount - 1)
459+
const implicitColWidth = availableWidth / implicitColCount
460+
for (let i = 0; i < implicitColCount; i++) {
461+
columnSizes.push(implicitColWidth)
462+
}
463+
} else {
464+
// Default behavior: add zero-width columns
465+
while (columnSizes.length < maxCol) columnSizes.push(0)
466+
}
439467

440468
// --- 6. Calculate exact coordinates for each cell ---
441469

site/level18.page.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import level18 from "testcases/level18"
2+
import { LevelDisplay } from "./LevelDisplay"
3+
4+
export default () => {
5+
return <LevelDisplay level={level18} />
6+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"water": {
3+
"x": 40,
4+
"y": 0,
5+
"width": 20,
6+
"height": 100
7+
}
8+
}

testcases/level18.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import type { CssGridOptions } from "lib/types"
2+
3+
export default {
4+
children: [{ key: "water", columnStart: 3 }],
5+
containerWidth: 100,
6+
containerHeight: 100,
7+
gridTemplateColumns: "20% 20% 20% 20% 20%",
8+
} satisfies CssGridOptions
Lines changed: 90 additions & 0 deletions
Loading

tests/level18.test.ts

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import { expect, test } from "bun:test"
2+
import level18 from "testcases/level18"
3+
import browserResult from "testcases/level18.browser-result.json"
4+
import { testGrid } from "./fixtures/testGrid"
5+
6+
test("level18", () => {
7+
const { laidOutResult, outputViz, layout } = testGrid(level18, browserResult)
8+
9+
expect(browserResult).toMatchInlineSnapshot(`
10+
{
11+
"water": {
12+
"height": 100,
13+
"width": 20,
14+
"x": 40,
15+
"y": 0,
16+
},
17+
}
18+
`)
19+
expect(laidOutResult).toMatchInlineSnapshot(`
20+
{
21+
"water": {
22+
"height": 100,
23+
"width": 20,
24+
"x": 40,
25+
"y": 0,
26+
},
27+
}
28+
`)
29+
30+
expect(layout).toMatchInlineSnapshot(`
31+
{
32+
"cells": [
33+
{
34+
"column": 2,
35+
"columnSpan": 1,
36+
"height": 100,
37+
"key": "water",
38+
"row": 0,
39+
"rowSpan": 1,
40+
"width": 20,
41+
"x": 40,
42+
"y": 0,
43+
},
44+
],
45+
"columnGap": 0,
46+
"columnSizes": [
47+
20,
48+
20,
49+
20,
50+
20,
51+
20,
52+
],
53+
"itemCoordinates": {
54+
"water": {
55+
"height": 100,
56+
"width": 20,
57+
"x": 40,
58+
"y": 0,
59+
},
60+
},
61+
"rowGap": 0,
62+
"rowSizes": [
63+
100,
64+
],
65+
}
66+
`)
67+
expect(outputViz).toMatchSvgSnapshot(import.meta.path)
68+
69+
if (!process.env.BUN_UPDATE_SNAPSHOTS) {
70+
expect(laidOutResult).toEqual(browserResult)
71+
}
72+
})

0 commit comments

Comments
 (0)