The major architectural change in v1.x is the extraction of React from the core library. This creates a cleaner separation between:
- Core Library (
@gravity-ui/graph) - Canvas rendering, graph logic, no framework dependencies - React Components (
@gravity-ui/graph/react) - React-specific hooks and components - Optional Layers - Some advanced layers are now separate optional imports
- Update imports to separate core and React components
- Ensure React is in your project's dependencies
- Migrate from
GraphComponenttoGraphCanvas+useGraph - Update layer imports (ConnectionLayer, NewBlockLayer, useLayer hook)
import {
Graph,
GraphCanvas,
useGraph,
useGraphEvent,
TGraphConfig
} from "@gravity-ui/graph";// Core functionality (no React dependency)
import {
Graph,
TGraphConfig,
EAnchorType,
Layer
} from "@gravity-ui/graph";
// React components (requires React)
import {
GraphCanvas,
useGraph,
useGraphEvent
} from "@gravity-ui/graph/react";
// Optional layers (import only if needed)
import { ConnectionLayer, NewBlockLayer } from "@gravity-ui/graph";In v1.x, the layer system has been optimized:
- Built-in Optional Layers:
ConnectionLayerandNewBlockLayerare now optional imports instead of being automatically included - React Layer Hooks:
useLayerhook moved to@gravity-ui/graph/react - Benefits: Smaller core bundle size, tree-shaking friendly
| Layer | Purpose | Import Path |
|---|---|---|
ConnectionLayer |
Drag-to-connect functionality between blocks/anchors | @gravity-ui/graph |
NewBlockLayer |
Alt+drag block duplication | @gravity-ui/graph |
| Custom layers | User-defined layers with useLayer hook |
N/A |
What moves to /react:
- All React components:
GraphCanvas,GraphBlock,GraphBlockAnchor - All React hooks:
useGraph,useGraphEvent,useGraphEvents,useLayer,useElk - React-specific types:
TRenderBlockFn,GraphProps,TGraphEventCallbacks
What stays in core:
- Graph class and configuration:
Graph,TGraphConfig - Enums:
EAnchorType,EBlockType, etc. - Canvas components:
Block,Connection,Layer - Store types:
BlockState,ConnectionState
Ensure React is in your package.json:
{
"dependencies": {
"react": "^18.0.0",
"react-dom": "^18.0.0",
"@gravity-ui/graph": "^1.0.0"
}
}const graphRef = useRef<Graph>();
<GraphComponent
config={config}
graphRef={graphRef}
renderBlockFn={renderBlock}
onStateChanged={onStateChanged}
/>const { graph, setEntities, start } = useGraph(config);
useEffect(() => {
setEntities({ blocks, connections });
start();
}, [setEntities, start, blocks, connections]);
<GraphCanvas
graph={graph}
renderBlock={renderBlock}
onStateChanged={onStateChanged}
/>Some advanced layers are now optional imports to keep the core library lightweight:
// Before (automatically included)
// No explicit import needed
// After (explicit import required)
import { ConnectionLayer } from "@gravity-ui/graph";
const { graph } = useGraph();
// Add layer with configuration
graph.addLayer(ConnectionLayer, {
createIcon: {
path: "M7 0.75C7.41421...",
width: 14,
height: 14,
viewWidth: 14,
viewHeight: 14,
},
isConnectionAllowed: (sourceComponent) => {
return sourceComponent instanceof AnchorState;
}
});// Before (automatically included)
// No explicit import needed
// After (explicit import required)
import { NewBlockLayer } from "@gravity-ui/graph";
const { graph } = useGraph();
// Add layer with configuration
graph.addLayer(NewBlockLayer, {
ghostBackground: "rgba(255, 255, 255, 0.3)",
isDuplicateAllowed: (block) => {
return block.type !== "special-block";
}
});import { Graph, GraphComponent, useRef } from "@gravity-ui/graph";
function MyApp() {
const graphRef = useRef<Graph>();
return (
<GraphComponent
config={{
blocks: [...],
connections: [...]
}}
graphRef={graphRef}
renderBlockFn={renderBlock}
/>
);
}import { Graph } from "@gravity-ui/graph";
import { GraphCanvas, useGraph } from "@gravity-ui/graph/react";
function MyApp() {
const { graph, setEntities, start } = useGraph({
settings: { /* graph settings */ }
});
useEffect(() => {
setEntities({
blocks: [...],
connections: [...]
});
start();
}, [setEntities, start]);
return (
<GraphCanvas
graph={graph}
renderBlock={renderBlock}
/>
);
}import { useGraphEvent } from "@gravity-ui/graph";
// Usage remains the same
useGraphEvent(graph, "block-drag-end", (detail, event) => {
console.log("Block moved:", detail);
});import { useGraphEvent } from "@gravity-ui/graph/react";
// Usage remains exactly the same
useGraphEvent(graph, "block-drag-end", (detail, event) => {
console.log("Block moved:", detail);
});import { useLayer } from "@gravity-ui/graph";
const customLayer = useLayer(graph, CustomLayer, {
prop1: "value1"
});import { useLayer } from "@gravity-ui/graph/react";
const customLayer = useLayer(graph, CustomLayer, {
prop1: "value1"
});// ConnectionLayer and NewBlockLayer were automatically available
// No imports needed - they were part of the core// Import specific layers you need
import { ConnectionLayer, NewBlockLayer } from "@gravity-ui/graph";
// Add layers explicitly when needed
graph.addLayer(ConnectionLayer, {
// Connection creation configuration
});
graph.addLayer(NewBlockLayer, {
// Block duplication configuration
});| What Changed | Old Code | New Code |
|---|---|---|
| React imports | import { useGraph } from "@gravity-ui/graph" |
import { useGraph } from "@gravity-ui/graph/react" |
| GraphComponent | <GraphComponent /> |
<GraphCanvas /> + useGraph hook |
| Built-in layers | Automatically included | Import explicitly from @gravity-ui/graph |
| useLayer hook | @gravity-ui/graph |
@gravity-ui/graph/react |