Utilities
findEmptyPosition
Section titled “findEmptyPosition”findEmptyPosition finds the first available root-grid location for a tile with the supplied size.
import { findEmptyPosition, useGridStore } from 'mosyn';import type { Tile } from 'mosyn';
export function AddTileButton() { const tiles = useGridStore((state) => state.tiles); const addTile = useGridStore((state) => state.addTile);
const add = () => { const draft: Tile = { id: crypto.randomUUID(), x: 1, y: 1, w: 30, h: 16, renderer: <NewTile />, }; const position = findEmptyPosition(draft, tiles ?? []);
if (position) addTile(position); };
return <button type="button" onClick={add}>Add tile</button>;}Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
item |
Tile |
The tile to place. Its w and h determine the required space. |
items |
Tile[] |
Existing tiles in the grid. |
Return value
Section titled “Return value”The function returns a new tile object with a free x and y position, or undefined when it cannot find a location.
Use the returned object rather than mutating the draft tile. This keeps the initial tile definition reusable.