@sewdn/diagram-kit
v0.1.0
Published
SVG architecture diagrams in React: boxes, layers, layouts, edges, pan and zoom.
Readme
@sewdn/diagram-kit
React-first SVG diagram toolkit for documentation portals. It ships layout primitives, box templates, connection routing, a pan/zoom SVG viewer, and a lil-gui based controls panel with persisted settings + export helpers.
Features
- React SVG diagrams with layout primitives (horizontal/vertical/grid layouts + auto registration).
- DiagramRoot wrapper that bundles SVGContainer, layout provider, and default layers.
- Component boxes, subgraph boxes, layer groups, legends, info cards, and template icons.
- Layout wrappers for common boxes (ComponentBoxWithLayout, SubgraphBoxWithLayout).
- Connection routing: orthogonal, orthogonal-avoid, straight, and curved.
- Routing offload to a Web Worker (sync fallback) with deterministic config.
- Arrow styling per edge (filled, hollow, vee, circle, cross, diamond, diamond-hollow, bar, square, square-hollow).
- Edge labels + start/end cardinalities.
- Connection points rendered on the box edge or the connection path.
- Debug points + debug panel with live payload and copy-to-clipboard.
- Diagram settings panel (lil-gui) persisted to localStorage with JSON/snippet export.
- SVG viewer with pan/zoom, fit-on-load, and SVG file or React component sources.
- Example diagram included (ExampleDiagram).
- Storybook for browsing components and diagram features.
Installation (workspace)
This package is meant for local workspace use. Add the dependency:
{
"dependencies": {
"@sewdn/diagram-kit": "workspace:*"
}
}Styles
Import the diagram styles once in your app:
import "@sewdn/diagram-kit/styles.css";The settings panel imports lil-gui styles internally. The app is expected to provide Tailwind utilities and CSS variables for theme colors (e.g. --foreground, --muted-foreground, --destructive, --primary).
Quick Start
import {
DiagramSettingsProvider,
DiagramSettingsPanel,
SvgViewer,
ExampleDiagram,
} from "@sewdn/diagram-kit";
export function DiagramPage() {
return (
<DiagramSettingsProvider>
<DiagramSettingsPanel className="diagram-gui-container" />
<div id="diagram-debug-panel" className="diagram-debug-host" />
<div style={{ height: 720 }}>
<SvgViewer svgComponent={ExampleDiagram} fitOnLoad />
</div>
</DiagramSettingsProvider>
);
}Per-diagram settings
The controls panel can export the current configuration. Use the Export → Copy diagram snippet button or add settings manually to a diagram registry:
diagramSettings: {
routingMode: 'worker',
routingAlgorithm: 'orthogonal',
minStraightLength: 24,
showArrows: true,
arrowSize: 10,
showConnectionLabels: true,
},Storybook
From this package:
bun run storybookAPI Reference
SvgViewer
type SvgViewerProps = {
svgPath?: string;
svgComponent?: React.ComponentType;
viewBox?: string;
className?: string;
fitOnLoad?: boolean;
};svgPath: URL to an SVG asset.svgComponent: React diagram component (useSVGContainer contentOnly).viewBox: Override viewBox when the SVG has none.fitOnLoad: Auto fit diagram to viewer after load.
DiagramSettings
type DiagramSettings = {
showConnections: boolean;
routingMode: "worker" | "sync" | "off";
routingAlgorithm: "orthogonal" | "orthogonal-avoid" | "straight" | "curved";
shortDistanceThreshold: number;
gridSize: number; // legacy (unused by routing)
obstaclePadding: number;
portOffset: number;
minStraightLength: number;
turnPenalty: number;
cornerRadius: number;
snapToGrid: "axis" | "direct"; // legacy (unused by routing)
avoidObstacles: boolean;
avoidConnections: boolean;
debugPoints: boolean;
showConnectionPoints: boolean;
connectionPointSize: number;
connectionPointLocation: "box" | "edge";
showArrows: boolean;
arrowSize: number;
showConnectionLabels: boolean;
showDebugPanel: boolean;
fitOnLoad: boolean;
};DiagramSettingsProvider
type DiagramSettingsProviderProps = {
initialSettings?: Partial<DiagramSettings>;
children: React.ReactNode;
};DiagramSettingsPanel
type DiagramSettingsPanelProps = {
className?: string;
};The panel persists settings in localStorage and provides JSON/snippet export buttons.
Layout primitives
LayoutProvider
LayoutItem({ id, x, y, width, height, children })
HorizontalLayout({ x, y, spacing?, align?, children })
VerticalLayout({ x, y, spacing?, align?, children })
GridLayout({ x, y, columns, rowHeight, columnWidth, gapX?, gapY?, children })Use LayoutProvider to auto-register positions for connections.
Boxes & groups
ComponentBox
type ComponentBoxProps = {
x: number;
y: number;
width: number;
height: number;
title: string;
subtitle?: string;
items?: string[];
variant?:
| "component"
| "primary"
| "accent"
| "muted"
| "legacy"
| "new"
| "success"
| "migrated"
| "decommissioned"
| "placeholder"
| "gateway"
| "adapter";
badge?: { text: string; variant: "new" | "success" | "migrating" | "decom" };
template?: "frontend" | "service" | "api" | "storage";
};Template shortcuts:
FrontendComponentBox,ServiceComponentBox,ApiComponentBox,StorageComponentBox
SubgraphBox
type SubgraphBoxProps = {
x: number;
y: number;
width: number;
height: number;
title: string;
subtitle?: string;
entities?: Array<{ name: string; isKey?: boolean }>;
source?: string;
variant?: "subgraph" | "adapter" | "component" | "new";
badge?: { text: string; variant: "new" | "success" | "migrating" | "decom" };
};LayerGroup
type LayerGroupProps = {
x: number;
y: number;
width: number;
height: number;
title: string;
children: React.ReactNode;
};Legend
type LegendItemProps = {
variant:
| "component"
| "primary"
| "accent"
| "new"
| "legacy"
| "migrated"
| "decommissioned"
| "subgraph"
| "adapter";
label: string;
};
type LegendProps = { x: number; y: number; title?: string; items: LegendItemProps[] };DiagramTitle
type DiagramTitleProps = { x: number; y: number; title: string; subtitle?: string };InfoBox
type InfoBoxProps = {
x: number;
y: number;
width: number;
height: number;
title: string;
sections: Array<{ title: string; items: string[] }>;
variant?: "muted" | "component" | "accent";
};Connections
EdgeConnection
type EdgeConnectionProps = {
id?: string;
from: string;
to: string;
variant?: "default" | "data" | "sync" | "adapter" | "readonly";
edgeCode?: string; // e.g. "BT" (bottom -> top)
arrowStyle?:
| "filled"
| "hollow"
| "vee"
| "circle"
| "cross"
| "diamond"
| "diamond-hollow"
| "bar"
| "square"
| "square-hollow"
| "none";
arrowPlacement?: "none" | "start" | "end" | "both";
fromSide?: "top" | "bottom" | "left" | "right";
toSide?: "top" | "bottom" | "left" | "right";
fromOffset?: number;
toOffset?: number;
label?: string;
startCardinality?: string;
endCardinality?: string;
};ConnectionLayer
type ConnectionLayerProps = {
connections?: EdgeConnection[];
shortDistanceThreshold?: number;
routing?: { mode?: "worker" | "sync" | "off"; config?: RoutingConfig };
debug?: boolean;
};ConnectionPointsLayer
type ConnectionPointsLayerProps = {
renderIn: "connections" | "components";
connections?: EdgeConnection[];
config?: RoutingConfig;
};RoutingConfig
type RoutingConfig = {
gridSize?: number; // legacy (unused by routing)
obstaclePadding?: number;
portOffset?: number;
minStraightLength?: number;
turnPenalty?: number;
shortDistanceThreshold?: number;
algorithm?: "orthogonal" | "orthogonal-avoid" | "straight" | "curved";
cornerRadius?: number;
snapToGrid?: "axis" | "direct"; // legacy (unused by routing)
avoidObstacles?: boolean;
avoidConnections?: boolean;
};SVGContainer
type SVGContainerProps = {
viewBox: string;
width?: number;
height?: number;
children: React.ReactNode;
className?: string;
contentOnly?: boolean;
};When rendered inside SvgViewer, set contentOnly to true.
Example diagrams included
ExampleDiagram
