You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// Split Node - Divides space between children{type: 'split',direction: 'row'|'column',children: MosaicNode<T>[],splitPercentages?: number[]}// Tab Node - Stacks children as tabs{type: 'tabs',tabs: T[],activeTabIndex: number}// Leaf Node - Individual panelT// Just the panel ID (string or number)
Common Utilities
Type Guards
import{isSplitNode,isTabsNode}from'react-mosaic-component';if(isSplitNode(node)){// node is MosaicSplitNode<T>}if(isTabsNode(node)){// node is MosaicTabsNode<T>}
Tree Operations
import{getLeaves,getNodeAtPath,createBalancedTreeFromLeaves,convertLegacyToNary}from'react-mosaic-component';// Get all leaf panel IDsconstpanels=getLeaves(tree);// Get node at pathconstnode=getNodeAtPath(tree,[0,1,2]);// Create balanced layoutconsttree=createBalancedTreeFromLeaves(['a','b','c']);// Convert legacy treeconstmodernTree=convertLegacyToNary(legacyTree);
Tree Updates
import{updateTree,createRemoveUpdate,createExpandUpdate,createHideUpdate}from'react-mosaic-component';// Remove node at pathconstnewTree=updateTree(tree,[createRemoveUpdate([0,1])]);// Expand node to percentageconstnewTree=updateTree(tree,[createExpandUpdate([0],75)]);// Hide nodeconstnewTree=updateTree(tree,[createHideUpdate([1])]);// Multiple updatesconstnewTree=updateTree(tree,[removeUpdate,expandUpdate]);
Context Access
import{useContext}from'react';import{MosaicContext,MosaicWindowContext}from'react-mosaic-component';// In root descendantsconst{ mosaicActions }=useContext(MosaicContext);mosaicActions.remove(path);mosaicActions.expand(path);mosaicActions.hide(path);// In MosaicWindow descendantsconst{ mosaicWindowActions }=useContext(MosaicWindowContext);mosaicWindowActions.split();mosaicWindowActions.remove();constpath=mosaicWindowActions.getPath();
Component Props
Mosaic
<Mosaic<T>// RequiredrenderTile={(id,path)=>ReactElement}// State (use one)initialValue={MosaicNode<T>}// Uncontrolledvalue={MosaicNode<T>}// Controlled// HandlersonChange={(newNode)=>void}onRelease={(newNode)=>void}// ConfigclassName={string}blueprintNamespace={string}createNode={()=>T}resize={ResizeOptions}zeroStateView={ReactElement}/>
[]// Root node[0]// First child of root[1]// Second child of root[0,0]// First child of first child[1,2]// Third child of second child[0,1,0]// First child of second child of first child
import{describe,it,expect}from'vitest';describe('MyUtility',()=>{it('should handle normal case',()=>{constresult=myUtility(input);expect(result).toEqual(expected);});it('should throw on invalid input',()=>{expect(()=>myUtility(null)).toThrow();});});
Running Tests
# All tests
npm test# Specific file
npm test -- libs/react-mosaic-component/src/lib/util/mosaicUtilities.spec.ts
# Pattern matching
npm test -- --testNamePattern="Tree"# Watch mode
npm run test:watch
Troubleshooting
Issue
Solution
Build fails
Clear dist/ and node_modules/.cache/
Type errors
Run npm run build-lib:check for details
Tests fail
Run individually to isolate issue
Styles missing
Ensure CSS imported in app
Drag not working
Check drag-drop context setup
Performance Tips
Use React.memo for tile components
Memoize callbacks with useCallback
Avoid deep nesting (>10 levels)
Batch tree updates when possible
Use path operations (O(depth))
Accessibility
Keyboard navigation supported
Use semantic HTML
Add ARIA labels where needed
Test with screen readers
Ensure focus management
Migration (Legacy to Modern)
// OLD (v0.19){direction: 'row',first: 'left',second: 'right',splitPercentage: 40}// NEW (v1.0+){type: 'split',direction: 'row',children: ['left','right'],splitPercentages: [40,60]}// OR use conversion utilityconstmodernTree=convertLegacyToNary(legacyTree);