Skip to content

Commit 2ba183c

Browse files
committed
docs(migration): Add migration guide from 0.x to 1.x
1 parent ed035ab commit 2ba183c

File tree

1 file changed

+298
-0
lines changed

1 file changed

+298
-0
lines changed

docs/migration-guides/v0-to-v1.md

Lines changed: 298 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,298 @@
1+
# Migration Guide: v0.x → v1.x
2+
3+
## What's Changed
4+
5+
The major architectural change in v1.x is the **extraction of React from the core library**. This creates a cleaner separation between:
6+
7+
- **Core Library** (`@gravity-ui/graph`) - Canvas rendering, graph logic, no framework dependencies
8+
- **React Components** (`@gravity-ui/graph/react`) - React-specific hooks and components
9+
- **Optional Layers** - Some advanced layers are now separate optional imports
10+
11+
## Quick Migration Checklist
12+
13+
- [ ] Update imports to separate core and React components
14+
- [ ] Ensure React is in your project's dependencies
15+
- [ ] Migrate from `GraphComponent` to `GraphCanvas` + `useGraph`
16+
- [ ] Update layer imports (ConnectionLayer, NewBlockLayer, useLayer hook)
17+
18+
## New Import Structure
19+
20+
### Before (v0.x)
21+
```typescript
22+
import {
23+
Graph,
24+
GraphCanvas,
25+
useGraph,
26+
useGraphEvent,
27+
TGraphConfig
28+
} from "@gravity-ui/graph";
29+
```
30+
31+
### After (v1.x)
32+
```typescript
33+
// Core functionality (no React dependency)
34+
import {
35+
Graph,
36+
TGraphConfig,
37+
EAnchorType,
38+
Layer
39+
} from "@gravity-ui/graph";
40+
41+
// React components (requires React)
42+
import {
43+
GraphCanvas,
44+
useGraph,
45+
useGraphEvent
46+
} from "@gravity-ui/graph/react";
47+
48+
// Optional layers (import only if needed)
49+
import { ConnectionLayer, NewBlockLayer } from "@gravity-ui/graph";
50+
```
51+
52+
## 🎨 Layer System Changes
53+
54+
In v1.x, the layer system has been optimized:
55+
56+
- **Built-in Optional Layers**: `ConnectionLayer` and `NewBlockLayer` are now optional imports instead of being automatically included
57+
- **React Layer Hooks**: `useLayer` hook moved to `@gravity-ui/graph/react`
58+
- **Benefits**: Smaller core bundle size, tree-shaking friendly
59+
60+
### Available Layers
61+
62+
| Layer | Purpose | Import Path |
63+
|-------|---------|-------------|
64+
| `ConnectionLayer` | Drag-to-connect functionality between blocks/anchors | `@gravity-ui/graph` |
65+
| `NewBlockLayer` | Alt+drag block duplication | `@gravity-ui/graph` |
66+
| Custom layers | User-defined layers with `useLayer` hook | N/A |
67+
68+
## 🔧 Step-by-Step Migration
69+
70+
### Step 1: Update Your Imports
71+
72+
**What moves to `/react`:**
73+
- All React components: `GraphCanvas`, `GraphBlock`, `GraphBlockAnchor`
74+
- All React hooks: `useGraph`, `useGraphEvent`, `useGraphEvents`, `useLayer`, `useElk`
75+
- React-specific types: `TRenderBlockFn`, `GraphProps`, `TGraphEventCallbacks`
76+
77+
**What stays in core:**
78+
- Graph class and configuration: `Graph`, `TGraphConfig`
79+
- Enums: `EAnchorType`, `EBlockType`, etc.
80+
- Canvas components: `Block`, `Connection`, `Layer`
81+
- Store types: `BlockState`, `ConnectionState`
82+
83+
### Step 2: Check Dependencies
84+
85+
Ensure React is in your `package.json`:
86+
87+
```json
88+
{
89+
"dependencies": {
90+
"react": "^18.0.0",
91+
"react-dom": "^18.0.0",
92+
"@gravity-ui/graph": "^1.0.0"
93+
}
94+
}
95+
```
96+
97+
### Step 3: Migrate Component Usage
98+
99+
#### Old Pattern (Deprecated)
100+
```typescript
101+
const graphRef = useRef<Graph>();
102+
103+
<GraphComponent
104+
config={config}
105+
graphRef={graphRef}
106+
renderBlockFn={renderBlock}
107+
onStateChanged={onStateChanged}
108+
/>
109+
```
110+
111+
#### New Pattern (Recommended)
112+
```typescript
113+
const { graph, setEntities, start } = useGraph(config);
114+
115+
useEffect(() => {
116+
setEntities({ blocks, connections });
117+
start();
118+
}, [setEntities, start, blocks, connections]);
119+
120+
<GraphCanvas
121+
graph={graph}
122+
renderBlock={renderBlock}
123+
onStateChanged={onStateChanged}
124+
/>
125+
```
126+
127+
### Step 4: Import Optional Layers
128+
129+
Some advanced layers are now optional imports to keep the core library lightweight:
130+
131+
#### ConnectionLayer (for drag-to-connect functionality)
132+
```typescript
133+
// Before (automatically included)
134+
// No explicit import needed
135+
136+
// After (explicit import required)
137+
import { ConnectionLayer } from "@gravity-ui/graph";
138+
139+
const { graph } = useGraph();
140+
141+
// Add layer with configuration
142+
graph.addLayer(ConnectionLayer, {
143+
createIcon: {
144+
path: "M7 0.75C7.41421...",
145+
width: 14,
146+
height: 14,
147+
viewWidth: 14,
148+
viewHeight: 14,
149+
},
150+
isConnectionAllowed: (sourceComponent) => {
151+
return sourceComponent instanceof AnchorState;
152+
}
153+
});
154+
```
155+
156+
#### NewBlockLayer (for Alt+drag block duplication)
157+
```typescript
158+
// Before (automatically included)
159+
// No explicit import needed
160+
161+
// After (explicit import required)
162+
import { NewBlockLayer } from "@gravity-ui/graph";
163+
164+
const { graph } = useGraph();
165+
166+
// Add layer with configuration
167+
graph.addLayer(NewBlockLayer, {
168+
ghostBackground: "rgba(255, 255, 255, 0.3)",
169+
isDuplicateAllowed: (block) => {
170+
return block.type !== "special-block";
171+
}
172+
});
173+
```
174+
175+
## 📋 Migration Examples
176+
177+
### Basic Graph Setup
178+
179+
#### Before
180+
```typescript
181+
import { Graph, GraphComponent, useRef } from "@gravity-ui/graph";
182+
183+
function MyApp() {
184+
const graphRef = useRef<Graph>();
185+
186+
return (
187+
<GraphComponent
188+
config={{
189+
blocks: [...],
190+
connections: [...]
191+
}}
192+
graphRef={graphRef}
193+
renderBlockFn={renderBlock}
194+
/>
195+
);
196+
}
197+
```
198+
199+
#### After
200+
```typescript
201+
import { Graph } from "@gravity-ui/graph";
202+
import { GraphCanvas, useGraph } from "@gravity-ui/graph/react";
203+
204+
function MyApp() {
205+
const { graph, setEntities, start } = useGraph({
206+
settings: { /* graph settings */ }
207+
});
208+
209+
useEffect(() => {
210+
setEntities({
211+
blocks: [...],
212+
connections: [...]
213+
});
214+
start();
215+
}, [setEntities, start]);
216+
217+
return (
218+
<GraphCanvas
219+
graph={graph}
220+
renderBlock={renderBlock}
221+
/>
222+
);
223+
}
224+
```
225+
226+
### Event Handling
227+
228+
#### Before
229+
```typescript
230+
import { useGraphEvent } from "@gravity-ui/graph";
231+
232+
// Usage remains the same
233+
useGraphEvent(graph, "block-drag-end", (detail, event) => {
234+
console.log("Block moved:", detail);
235+
});
236+
```
237+
238+
#### After
239+
```typescript
240+
import { useGraphEvent } from "@gravity-ui/graph/react";
241+
242+
// Usage remains exactly the same
243+
useGraphEvent(graph, "block-drag-end", (detail, event) => {
244+
console.log("Block moved:", detail);
245+
});
246+
```
247+
248+
### Custom Layers (Using useLayer Hook)
249+
250+
#### Before
251+
```typescript
252+
import { useLayer } from "@gravity-ui/graph";
253+
254+
const customLayer = useLayer(graph, CustomLayer, {
255+
prop1: "value1"
256+
});
257+
```
258+
259+
#### After
260+
```typescript
261+
import { useLayer } from "@gravity-ui/graph/react";
262+
263+
const customLayer = useLayer(graph, CustomLayer, {
264+
prop1: "value1"
265+
});
266+
```
267+
268+
### Built-in Optional Layers
269+
270+
#### Before
271+
```typescript
272+
// ConnectionLayer and NewBlockLayer were automatically available
273+
// No imports needed - they were part of the core
274+
```
275+
276+
#### After
277+
```typescript
278+
// Import specific layers you need
279+
import { ConnectionLayer, NewBlockLayer } from "@gravity-ui/graph";
280+
281+
// Add layers explicitly when needed
282+
graph.addLayer(ConnectionLayer, {
283+
// Connection creation configuration
284+
});
285+
286+
graph.addLayer(NewBlockLayer, {
287+
// Block duplication configuration
288+
});
289+
```
290+
291+
## ⚠️ Breaking Changes Summary
292+
293+
| What Changed | Old Code | New Code |
294+
|--------------|----------|----------|
295+
| React imports | `import { useGraph } from "@gravity-ui/graph"` | `import { useGraph } from "@gravity-ui/graph/react"` |
296+
| GraphComponent | `<GraphComponent />` | `<GraphCanvas />` + `useGraph` hook |
297+
| Built-in layers | Automatically included | Import explicitly from `@gravity-ui/graph` |
298+
| useLayer hook | `@gravity-ui/graph` | `@gravity-ui/graph/react` |

0 commit comments

Comments
 (0)