GridBox
A box that lays its children on a CSS grid: its rows and columns as a count or as tracks, each item in its cell, or named areas.
import { GridBox } from "x4js"
Cells
Each item is put in its row and column (from 0); columns and rows: a count, or the CSS of the tracks.
export function cells( ) {
return new GridBox( {
cls: "cells",
columns: 3,
rows: [ "auto", "60px" ],
items: [
{ row: 0, col: 0, item: new SimpleText( { text: "row 0, col 0" } ) },
{ row: 0, col: 1, item: new SimpleText( { text: "row 0, col 1" } ) },
{ row: 0, col: 2, item: new SimpleText( { text: "row 0, col 2" } ) },
{ row: 1, col: 0, item: new SimpleText( { text: "row 1, col 0" } ) },
{ row: 1, col: 2, item: new SimpleText( { text: "row 1, col 2" } ) },
],
} );
}
.x4gridbox.cells {
gap: 6px;
> .x4simpletext {
display: flex;
align-items: center;
justify-content: center;
padding: 8px;
background: var( --background-secondary );
color: var( --text-primary );
}
}
Areas
setTemplate names the areas of the grid; each child says its area (gridArea).
export function areas( ) {
const grid = new GridBox( { cls: "cells", height: 200, columns: [ "140px", "1fr" ], rows: [ "auto", "1fr", "auto" ] } );
grid.setTemplate( [
"head head",
"side main",
"foot foot",
] );
grid.setContent( [
new SimpleText( { text: "head", style: { gridArea: "head" } } ),
new SimpleText( { text: "side", style: { gridArea: "side" } } ),
new SimpleText( { text: "main", style: { gridArea: "main" } } ),
new SimpleText( { text: "foot", style: { gridArea: "foot" } } ),
] );
return grid;
}
Columns changed
Children without a cell flow in the grid; setColCount lays them out on another count of columns.
export function columns( ) {
const grid = new GridBox( { cls: "cells", columns: 3 } );
grid.setContent( [
new SimpleText( { text: "A" } ),
new SimpleText( { text: "B" } ),
new SimpleText( { text: "C" } ),
new SimpleText( { text: "D" } ),
new SimpleText( { text: "E" } ),
new SimpleText( { text: "F" } ),
] );
return new VBox( { content: [
new HBox( { content: [
new Label( { text: "Columns:" } ),
new Button( { label: "2", cls: "outline", click: ( ) => grid.setColCount( 2 ) } ),
new Button( { label: "3", cls: "outline", click: ( ) => grid.setColCount( 3 ) } ),
new Button( { label: "6", cls: "outline", click: ( ) => grid.setColCount( 6 ) } ),
] } ),
grid,
] } );
}
For an assistant: llms.txt, llms-full.txt.