Skip to content

Utilities

findEmptyPosition finds the first available root-grid location for a tile with the supplied size.

AddTileButton.tsx
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>;
}
Parameter Type Description
item Tile The tile to place. Its w and h determine the required space.
items Tile[] Existing tiles in the grid.

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.