@nexgraph/react
v0.1.4
Published
React bindings for Nexgraph (@nexgraph/core)
Maintainers
Readme
@nexgraph/react
Thin React bindings over @nexgraph/core: mounts a WebGL2 Renderer, keeps options in sync after mount, and offers declarative dataset / graph props plus optional automatic ForceLayout.
Demo: nexgraph-demos.vercel.app
Install
npm install @nexgraph/react react react-domPeer dependencies are react and react-dom (^18 or ^19). npm 7+ usually installs them with this package; pnpm and Yarn may want them listed in your package.json. @nexgraph/core is a dependency and is installed automatically. Import re-exported types from @nexgraph/react, or add @nexgraph/core yourself to pin the version.
Same requirements as core: WebGL2 and a bundler that resolves workers (Vite works out of the box).
Quick start
Give the parent a non-zero height so the canvas has space to draw.
import { NexgraphCanvas } from '@nexgraph/react';
const dataset = {
nodeCount: 4,
labels: ['a', 'b', 'c', 'd'],
edges: [
{ source: 0, target: 1 },
{ source: 1, target: 2 },
{ source: 2, target: 3 },
{ source: 3, target: 0 },
],
};
export function App() {
return (
<div style={{ width: '100vw', height: '100vh' }}>
<NexgraphCanvas dataset={dataset} autoForceLayout={false} />
</div>
);
}Topology-only datasets default to autoForceLayout (physics runs asynchronously). onReady can fire before data arrives, and the canvas does not call fitToData after a layout tick, so the example above turns auto-layout off and lets the parse path frame the first view. Set autoForceLayout back to true (the default) when you want the worker layout and are happy to orbit or call zoomToFit yourself.
When you already have typed buffers, pass graph instead (ignored while dataset is set) and optionally graphForceLayout:
import { useMemo } from 'react';
import { NexgraphCanvas } from '@nexgraph/react';
export function RingGraph() {
const graph = useMemo(() => {
const n = 5;
// Seed on a circle: nodes must not start on the same point, the layout refines them.
const positions = new Float32Array(n * 3);
const edges = new Uint32Array(n * 2);
for (let i = 0; i < n; i++) {
const angle = (i / n) * Math.PI * 2;
positions[i * 3] = Math.cos(angle) * 50;
positions[i * 3 + 1] = Math.sin(angle) * 50;
edges[i * 2] = i;
edges[i * 2 + 1] = (i + 1) % n;
}
return { positions, edges };
}, []);
return (
<div style={{ width: '100%', height: 480 }}>
<NexgraphCanvas
graph={graph}
graphForceLayout
fitGraph
onReady={(r) => r.fitToData()}
/>
</div>
);
}API
NexgraphCanvas mounts a full-size canvas inside a positioned container div. It accepts every RendererOptions field except parent (internal), with the same defaults: omitted props are passed through so the renderer decides. contextOptions apply only when the WebGL context is created — remount (e.g. a React key) to change them. Every other option, including labelFont and nodeDragMode, is applied again when it changes. With nodeDragMode="pin" or "snapBack", drags are already forwarded to the component's ForceLayout.
Imperative access: onReady(renderer) (once, after create / start when autoStart is on) or a ref of type NexgraphCanvasHandle — zoomToFit(), setZoomDistance(), getZoomDistance(), startLayout(), stopLayout(), resetPositions(), getGraphPositions().
For labelFont, see the core README. After mount, a change calls Renderer.setLabelFont; a new object with the same family, weight and url does not reload, so inline literals are fine.
Props
React-specific props (renderer options are in the core README):
| Prop | Type | Default | Purpose |
| ------------------------------------- | ------------------------------------------ | --------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------- |
| className, style | string, CSSProperties | — | Applied to the container div. |
| autoStart | boolean | true | Start the render loop on mount. |
| dataset | JSON string or document | — | See above; wins over graph. |
| graph | NexgraphCanvasGraphProps | — | positions, edges, optional colors, sizes, labels, edgeColors, edgeWeights. |
| autoForceLayout | boolean | true | Run ForceLayout for topology-only datasets. |
| graphForceLayout | boolean | false | Run ForceLayout after a graph upload. |
| forceLayoutPreset | ForceLayoutPresetId | 'interoperability' | Preset for either layout path. |
| forceConfig, edgeLengthMultiplier | Partial<ForceConfig>, number | — | Physics overrides on top of the preset; scale for link distance. |
| onDatasetLoaded, onDatasetError | callbacks | — | dataset parse result / failure. |
| onGraphLayoutTick | (positions) => void | — | After each layout tick while a layout drives the graph. |
| graphFilters | { minDegree?, minWeight?, edgeWeights? } | none (nothing hidden) | Hide weak nodes/edges; when the prop is set, minDegree and minWeight default to 1. |
| fitGraph | boolean | true | Call fitToData after each dataset / graph upload. |
| nodeColors, edgeColors | Uint8Array (RGBA) | — | Packed colours; override dataset/graph colours when long enough. |
| nodeColor, linkColor | colour callbacks (RGB 0–255) | — | Per-node / per-edge colour. Companions: nodeColorData / edgeColorData and nodeColorRevision / edgeColorRevision (default 0) to force a re-run. |
| onNodeClick | (node \| null) => void | — | Click on a node or empty canvas; also enables the pointer cursor. |
| nodeInteractionRevision | number | 0 | Re-registers picking callbacks so onNodeClick sees fresh nodeColorData. |
| zoomDistance | number | — | Orbit distance (clamped); omit to leave navigation alone. |
| autoRotate, autoRotateSpeed | boolean, number (rad/s) | false, 0.08 | Slow orbit around the target. |
| enableNavigationControls | boolean | true | false disables orbit, pan and zoom. |
| onDraw, onReady | callbacks | — | onDraw(gl, camera, dt) is registered with Renderer.setDrawCallback. onReady is described above. |
Exports: NexgraphCanvas, its prop / handle / dataset / colour-callback types, clampOrbitDistance, DEFAULT_AUTO_ROTATE_SPEED, plus selected types and constants re-exported from core. Full list: packages/react/src/index.ts.
For a Renderer mounted by hand inside useEffect, see the core README.
License
MIT © see LICENSE in the repository. The default label font bundled in @nexgraph/core, Ubuntu Medium (© 2011 Canonical Ltd.), is under the Ubuntu Font Licence 1.0.
