Use the controlled pattern when you need to own the layout state. This is required for:
- Persisting the layout (e.g., to
localStorage, a server, or the URL). - Programmatic mutation (e.g., adding a panel via an external button or resetting to a preset).
- Reacting to changes (e.g., for analytics, undo/redo functionality, or derived UI).
Set value to null to represent an empty layout, which will render the zeroStateView.
Note: Do not provide both initialValue and value simultaneously; doing so will trigger a runtime warning.
import { useState } from 'react';
import { Mosaic, MosaicWindow, MosaicNode } from 'react-mosaic-component';
function ControlledExample() {
const [tree, setTree] = useState<MosaicNode<string> | null>({
type: 'split',
direction: 'row',
children: ['a', 'b'],
});
return (
<Mosaic<string>
value={tree}
onChange={setTree}
renderTile={(id, path) => (
<MosaicWindow path={path} title={`Panel ${id}`}>
<div>{id}</div>
</MosaicWindow>
)}
/>
);
}