Hooks
Mosyn exposes hooks for selection and live grid state. Both hooks must run below MosynManager.
useMosynManager
Section titled “useMosynManager”Use useMosynManager to read the selected tile, set selection, and respond to selection changes.
import { useMosynManager } from 'mosyn';
export function SelectionToolbar() { const { selected, setSelected } = useMosynManager();
return ( <button type="button" disabled={!selected} onClick={() => setSelected(undefined)}> Clear selection </button> );}Options
Section titled “Options”| Option | Type | Description |
|---|---|---|
onSelect |
(tile: Tile) => void |
Runs when a tile becomes selected. |
onUnSelect |
(tile: Tile) => void |
Runs when selection is cleared. |
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.
useGridStore
Section titled “useGridStore”Use useGridStore to subscribe to a value from the current grid’s live state.
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.
const addTile = useGridStore((state) => state.addTile);
addTile({ id: crypto.randomUUID(), x: 1, y: 1, w: 30, h: 16, renderer: <NewTile />,});Read a nested grid
Section titled “Read a nested grid”Pass the container tile id to read that nested grid instead of the root grid.
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
Section titled “MosynManagerContextType”MosynManagerContextType is exported for advanced TypeScript integrations. Prefer the hooks for application code; they provide the reactive values most integrations need.