Skip to content
SiteEmail

Grid

The Grid component is a flexible, dynamic container that arranges content in a structured grid layout. It provides a mechanism for creating interactive grids where individual cells can be updated dynamically.

The Grid component must be used in tandem with the GridTile component, which allows for placement of content within specific grid cells.

The Grid component must be provided with number of rows and columns through the rows and cols props. You can dynamically add or remove items in specific cells using the provided methods.

import Grid from '@components/Layout/Grid/Grid';
import Block from '@components/Layout/Block/Block';
const App = () => {
return (
<Grid cols={3} rows={3} gap="1vmax" style={{ width: '30vmax', height: '30vmax' }} >
<Grid.Tile row={2} col={2}><Block>Placed in row 2 column 2</Block></Grid.Tile>
<Grid.Tile row={3} col={2}><Block>Placed in row 3 column 2</Block></Grid.Tile>
<Grid.Tile row={3} col={3}><Block>Placed in row 3 column 3</Block></Grid.Tile>
</Grid>
);
};
export default App;
Prop NameTypeDefaultDescription
styleJSX.CSSProperties{}Inline styles to apply directly to the component’s root element.
classstring""Additional CSS classes to apply to the component
refGridRefundefinedRetrieves the grid’s properties, methods and HTML element and assigns them to a local variable
rowsnumberRequiredThe number of rows in the grid.
colsnumberRequiredThe number of columns in the grid.
gapstringundefinedApplies spacing between the rows and columns. It’s a shorthand for both row and column gaps. Requires Gameface 2.2+.
row-gapstringundefinedVertical spacing between grid rows. Requires Gameface 2.2+.
column-gapstringundefinedHorizontal spacing between grid columns. Requires Gameface 2.2+.
row-classstring""Additional CSS classes for internal row wrappers.
column-classstring""Additional CSS classes for internal column/cell wrappers.
row-styleJSX.CSSProperties{}Inline styles applied to internal row wrappers.
column-styleJSX.CSSProperties{}Inline styles applied to internal column/cell wrappers.
onActionRecord<string, (scope?: string, ...args: any[]) => void>undefinedAllows you to add custom navigation action handlers to the component. See the Navigation component documentation for details.
anchorstring | HTMLElementundefinedLinks navigation to another element. When the anchor element is focused, this component’s actions will execute. Can be a CSS selector or HTMLElement.
MethodParametersReturn ValueDescription
addItemrow: number, col: number, item: JSX.ElementvoidAdds the provided element to the specified row and column. Will throw an error if the row and column don’t exist. Will overwrite any existing items to the specified row and column.
removeItemrow: number, col: numbervoidRemoves the content of the specified cell. Will throw an error if the row and column don’t exist.

The Tile slot is used to specify on which row and column the content inside it should be placed on the Grid.

Prop NameTypeDefaultDescription
styleJSX.CSSProperties{}Inline styles to apply directly to the component’s root element.
classstring""Additional CSS classes to apply to the component
refGridTileRefundefinedRetrieves the GridTile’s properties, methods and HTML element and assigns them to a local variable
rownumberRequiredThe row index where the tile will be placed.
colnumberRequiredThe column index where the tile will be placed.
onActionRecord<string, (scope?: string, ...args: any[]) => void>undefinedAllows you to add custom navigation action handlers to the component. See the Navigation component documentation for details.
anchorstring | HTMLElementundefinedLinks navigation to another element. When the anchor element is focused, this component’s actions will execute. Can be a CSS selector or HTMLElement.
MethodParametersReturn ValueDescription
moveTilenewRow: number, newCol: numbervoidMoves the tile to a different place on the grid. Will throw an error if the row and column don’t exist.
removeItemvoidvoidRemoves the tile from the grid. Will throw an error if the row and column don’t exist.
replaceTilenewTile: JSX.ELementvoidInserts new content on the place of the tile. Will throw an error if the row and column don’t exist.

The Grid component provides methods to dynamically add or remove items from specific grid cells. These methods can be accessed via the ref prop.

The steps to achieve it are as follows:

  1. Declare a variable to hold the ref but don’t initialize it with a value
  2. The declared value should have a type of GridRef, which you need to import
  3. Set the declared variable as the value of the ref prop of the Grid component
  4. Call the methods from the ref
import Grid { GridRef } from '@components/Layout/Grid/Grid';
import GridTile from '@components/Layout/GridTile/GridTile';
import Block from '@components/Layout/Block/Block';
const App = () => {
let gridRef!: GridRef;
const addNewGridItem = () => {
const newBlock = (<Block>New</Block>)
gridRef.addItem(1, 1, newBlock);
}
const removeGridItem = () => {
gridRef.removeItem(1, 1);
}
return (
<Grid ref={gridRef} cols={3} rows={3} >
<GridTile row={2} col={2}><Block>Placed in row 2 column 2</Block></GridTile>
<GridTile row={3} col={2}><Block>Placed in row 3 column 2</Block></GridTile>
<GridTile row={3} col={3}><Block>Placed in row 3 column 3</Block></GridTile>
</Grid>
<Block click={addNewGridItem}>Add Item</Block>
<Block click={removeGridItem}>Remove Item</Block>
);
};
export default App;