* Compute grid render bounds *
*********************************************************/
-/* The grid starts at 10x10 and only grows when a tile is placed in
- * one of the outermost rows/columns, ensuring there is always at
- * least 1 empty row/column at each edge for placing new tiles. */
-const GRID_MIN_SIZE = 10;
+/* Read the cell size from the CSS --cell-size custom property so
+ * there's a single source of truth for the grid cell dimensions. */
+function get_cell_size() {
+ const board = document.querySelector(".board-container");
+ if (board) {
+ return parseFloat(getComputedStyle(board).getPropertyValue("--cell-size"));
+ }
+ return 48; /* fallback before board is rendered */
+}
+
+/* The grid starts as the largest square that fits the viewport width
+ * (capped at 10x10) and only grows when a tile is placed in one of
+ * the outermost rows/columns. */
+function initial_grid_size() {
+ const cell_size = get_cell_size() + 2; /* +2 for 1px border each side */
+ const available = window.innerWidth - 32; /* rough page padding */
+ const fit = Math.floor(available / cell_size);
+ return Math.max(4, Math.min(10, fit));
+}
function grid_bounds(grid, prev_bounds) {
const keys = Object.keys(grid);
if (keys.length === 0) {
- const half = Math.floor(GRID_MIN_SIZE / 2);
+ const half = Math.floor(initial_grid_size() / 2);
return { minR: -half, maxR: half - 1, minC: -half, maxC: half - 1 };
}
minC = prev_bounds.minC;
maxC = prev_bounds.maxC;
} else {
- const half = Math.floor(GRID_MIN_SIZE / 2);
+ const half = Math.floor(initial_grid_size() / 2);
minR = Math.min(-half, tileMinR - 1);
maxR = Math.max(half - 1, tileMaxR + 1);
minC = Math.min(-half, tileMinC - 1);
return (
<div key="board" className="board-container">
<div className="board"
- style={{ gridTemplateColumns: `repeat(${cols}, 48px)` }}>
+ style={{ gridTemplateColumns: `repeat(${cols}, var(--cell-size))` }}>
{rows.flat()}
</div>
</div>