Skip to content

Hooks

Mosyn exposes hooks for selection and live grid state. Both hooks must run below MosynManager.

Use useMosynManager to read the selected tile, set selection, and respond to selection changes.

SelectionToolbar.tsx
import { useMosynManager } from 'mosyn';
export function SelectionToolbar() {
const { selected, setSelected } = useMosynManager();
return (
<button type="button" disabled={!selected} onClick={() => setSelected(undefined)}>
Clear selection
</button>
);
}
Option Type Description
onSelect (tile: Tile) => void Runs when a tile becomes selected.
onUnSelect (tile: Tile) => void Runs when selection is cleared.
SelectionStatus.tsx
const { selected } = useMosynManager({
onSelect: (tile) => analytics.track('tile_selected', { id: tile.id }),
onUnSelect: (tile) => analytics.track('tile_unselected', { id: tile.id }),
});

Keep callback references stable with useCallback if your component renders frequently.

Use useGridStore to subscribe to a value from the current grid’s live state.

TileCount.tsx
import { useGridStore } from 'mosyn';
export function TileCount() {
const tileCount = useGridStore((state) => state.tiles.length);
return <span>{tileCount} tiles</span>;
}

The selector runs against both grid state and grid actions. Select an existing value or action reference.

AddTileButton.tsx
const addTile = useGridStore((state) => state.addTile);
addTile({
id: crypto.randomUUID(),
x: 1,
y: 1,
w: 30,
h: 16,
renderer: <NewTile />,
});

Pass the container tile id to read that nested grid instead of the root grid.

WorkspaceCount.tsx
const workspaceTiles = useGridStore((state) => state.tiles, 'workspace');

The value can be unavailable before a grid has registered. Account for that in UI that renders before the grid.

MosynManagerContextType is exported for advanced TypeScript integrations. Prefer the hooks for application code; they provide the reactive values most integrations need.