typescript-utility-types
Use when typeScript utility types, mapped types, and advanced type manipulation. Use when creating flexible, type-safe TypeScript code.
svelteflow-fundamentals
Use when building node-based UIs, flow diagrams, workflow editors, or interactive graphs with Svelte Flow. Covers setup, nodes, edges, controls, and interactivity.
Full skill instructions
Build customizable node-based editors and interactive diagrams with Svelte Flow. This skill covers core concepts, setup, and common patterns for creating flow-based interfaces in Svelte applications.
# npm
npm install @xyflow/svelte
# pnpm
pnpm add @xyflow/svelte
# yarn
yarn add @xyflow/svelte
<script lang="ts">
import {
SvelteFlow,
Controls,
Background,
MiniMap,
type Node,
type Edge,
} from '@xyflow/svelte';
import { writable } from 'svelte/store';
import '@xyflow/svelte/dist/style.css';
const nodes = writable<Node[]>([
{
id: '1',
type: 'input',
data: { label: 'Start' },
position: { x: 250, y: 0 },
},
{
id: '2',
data: { label: 'Process' },
position: { x: 250, y: 100 },
},
{
id: '3',
type: 'output',
data: { label: 'End' },
position: { x: 250, y: 200 },
},
]);
const edges = writable<Edge[]>([
{ id: 'e1-2', source: '1', target: '2' },
{ id: 'e2-3', source: '2', target: '3' },
]);
</script>
<div style="height: 100vh; width: 100vw;">
<SvelteFlow {nodes} {edges} fitView>
<Controls />
<Background />
<MiniMap />
</SvelteFlow>
</div>
<script lang="ts">
import { SvelteFlow, type Node, type Edge } from '@xyflow/svelte';
import { writable, derived } from 'svelte/store';
// Writable stores for reactive state
const nodes = writable<Node[]>([
{ id: '1', data: { label: 'Node 1' }, position: { x: 0, y: 0 } },
{ id: '2', data: { label: 'Node 2' }, position: { x: 200, y: 100 } },
]);
const edges = writable<Edge[]>([
{ id: 'e1-2', source: '1', target: '2' },
]);
// Derived store for computed values
const nodeCount = derived(nodes, ($nodes) => $nodes.length);
const edgeCount = derived(edges, ($edges) => $edges.length);
// Add a new node
function addNode() {
const id = `node-${Date.now()}`;
nodes.update((n) => [
...n,
{
id,
data: { label: `Node ${$nodeCount + 1}` },
position: { x: Math.random() * 300, y: Math.random() * 300 },
},
]);
}
// Update node data
function updateNodeLabel(id: string, label: string) {
nodes.update((n) =>
n.map((node) =>
node.id === id ? { ...node, data: { ...node.data, label } } : node
)
);
}
// Delete a node
function deleteNode(id: string) {
nodes.update((n) => n.filter((node) => node.id !== id));
// Also remove connected edges
edges.update((e) => e.filter((edge) => edge.source !== id && edge.target !== id));
}
</script>
<div>
<p>Nodes: {$nodeCount} | Edges: {$edgeCount}</p>
<button on:click={addNode}>Add Node</button>
</div>
<SvelteFlow {nodes} {edges} fitView />
<script lang="ts">
import { SvelteFlow, type Node } from '@xyflow/svelte';
import { writable } from 'svelte/store';
const nodes = writable<Node[]>([
// Input node - only has source handles
{
id: '1',
type: 'input',
data: { label: 'Input Node' },
position: { x: 0, y: 0 },
},
// Default node - has both source and target handles
{
id: '2',
type: 'default',
data: { label: 'Default Node' },
position: { x: 0, y: 100 },
},
// Output node - only has target handles
{
id: '3',
type: 'output',
data: { label: 'Output Node' },
position: { x: 0, y: 200 },
},
]);
</script>
import { Position, type Node } from '@xyflow/svelte';
const node: Node = {
id: 'unique-id',
type: 'default',
position: { x: 100, y: 100 },
data: { label: 'My Node', customProp: 'value' },
// Optional properties
style: 'background-color: #f0f0f0;',
class: 'custom-node',
sourcePosition: Position.Right,
targetPosition: Position.Left,
draggable: true,
selectable: true,
connectable: true,
deletable: true,
hidden: false,
selected: false,
dragging: false,
zIndex: 0,
extent: 'parent',
parentId: 'parent-node-id',
expandParent: true,
};
<script lang="ts">
import { SvelteFlow, MarkerType, type Edge } from '@xyflow/svelte';
import { writable } from 'svelte/store';
const edges = writable<Edge[]>([
// Default edge (bezier curve)
{
id: 'e1',
source: '1',
target: '2',
type: 'default',
},
// Straight line
{
id: 'e2',
source: '2',
target: '3',
type: 'straight',
},
// Step edge (right angles)
{
id: 'e3',
source: '3',
target: '4',
type: 'step',
},
// Smoothstep edge (rounded corners)
{
id: 'e4',
source: '4',
target: '5',
type: 'smoothstep',
},
// Styled edge with arrow
{
id: 'e5',
source: '5',
target: '6',
label: 'Connection',
style: 'stroke: #333; stroke-width: 2px;',
animated: true,
markerEnd: {
type: MarkerType.ArrowClosed,
color: '#333',
},
},
]);
</script>
<script lang="ts">
import { SvelteFlow, type Node, type Edge } from '@xyflow/svelte';
import { writable } from 'svelte/store';
const nodes = writable<Node[]>([]);
const edges = writable<Edge[]>([]);
// Node events
function handleNodeClick(event: CustomEvent<{ node: Node }>) {
console.log('Node clicked:', event.detail.node);
}
function handleNodeDoubleClick(event: CustomEvent<{ node: Node }>) {
console.log('Node double clicked:', event.detail.node);
}
function handleNodeDragStart(event: CustomEvent<{ node: Node }>) {
console.log('Drag started:', event.detail.node.id);
}
function handleNodeDragStop(event: CustomEvent<{ node: Node }>) {
console.log('Drag stopped:', event.detail.node.position);
}
// Edge events
function handleEdgeClick(event: CustomEvent<{ edge: Edge }>) {
console.log('Edge clicked:', event.detail.edge);
}
// Connection events
function handleConnect(event: CustomEvent<{ connection: any }>) {
const { connection } = event.detail;
edges.update((e) => [
...e,
{
id: `${connection.source}-${connection.target}`,
source: connection.source,
target: connection.target,
},
]);
}
// Selection events
function handleSelectionChange(
event: CustomEvent<{ nodes: Node[]; edges: Edge[] }>
) {
console.log('Selected nodes:', event.detail.nodes);
console.log('Selected edges:', event.detail.edges);
}
</script>
<SvelteFlow
{nodes}
{edges}
on:nodeclick={handleNodeClick}
on:nodedoubleclick={handleNodeDoubleClick}
on:nodedragstart={handleNodeDragStart}
on:nodedragstop={handleNodeDragStop}
on:edgeclick={handleEdgeClick}
on:connect={handleConnect}
on:selectionchange={handleSelectionChange}
fitView
/>
<script>
import { SvelteFlow, Background, BackgroundVariant } from '@xyflow/svelte';
</script>
<SvelteFlow {nodes} {edges}>
<!-- Dots pattern -->
<Background variant={BackgroundVariant.Dots} gap={12} size={1} />
<!-- Lines pattern -->
<Background variant={BackgroundVariant.Lines} gap={20} />
<!-- Cross pattern -->
<Background variant={BackgroundVariant.Cross} gap={25} />
<!-- Custom color -->
<Background color="#aaa" gap={16} />
</SvelteFlow>
<script>
import { SvelteFlow, Controls } from '@xyflow/svelte';
</script>
<SvelteFlow {nodes} {edges}>
<Controls
showZoom={true}
showFitView={true}
showInteractive={true}
position="bottom-left"
/>
</SvelteFlow>
<script>
import { SvelteFlow, MiniMap } from '@xyflow/svelte';
</script>
<SvelteFlow {nodes} {edges}>
<MiniMap
nodeColor={(node) => {
switch (node.type) {
case 'input':
return '#0041d0';
case 'output':
return '#ff0072';
default:
return '#1a192b';
}
}}
nodeStrokeWidth={3}
zoomable
pannable
/>
</SvelteFlow>
<script>
import { SvelteFlow, Panel } from '@xyflow/svelte';
</script>
<SvelteFlow {nodes} {edges}>
<Panel position="top-left">
<button on:click={saveFlow}>Save</button>
<button on:click={restoreFlow}>Restore</button>
</Panel>
<Panel position="top-right">
<p>Node count: {$nodes.length}</p>
</Panel>
</SvelteFlow>
<script lang="ts">
import { SvelteFlow, useSvelteFlow, Panel } from '@xyflow/svelte';
const { zoomIn, zoomOut, fitView, setCenter, getViewport } = useSvelteFlow();
function handleFitView() {
fitView({ padding: 0.2 });
}
function handleCenter() {
setCenter(0, 0, { zoom: 1 });
}
function logViewport() {
const viewport = getViewport();
console.log('Current viewport:', viewport);
}
</script>
<SvelteFlow {nodes} {edges}>
<Panel position="top-left">
<button on:click={() => zoomIn()}>Zoom In</button>
<button on:click={() => zoomOut()}>Zoom Out</button>
<button on:click={handleFitView}>Fit View</button>
<button on:click={handleCenter}>Center</button>
<button on:click={logViewport}>Log Viewport</button>
</Panel>
</SvelteFlow>
<script lang="ts">
import { SvelteFlow, useSvelteFlow, type Node } from '@xyflow/svelte';
import { writable } from 'svelte/store';
const nodes = writable<Node[]>([]);
const edges = writable<Edge[]>([]);
const { getNodes, setNodes, addNodes, deleteElements } = useSvelteFlow();
function addNewNode() {
const id = `node-${Date.now()}`;
addNodes([
{
id,
data: { label: 'New Node' },
position: { x: Math.random() * 300, y: Math.random() * 300 },
},
]);
}
function deleteSelectedNodes() {
const selectedNodes = getNodes().filter((node) => node.selected);
deleteElements({ nodes: selectedNodes });
}
function updateAllLabels(prefix: string) {
setNodes((nodes) =>
nodes.map((node, index) => ({
...node,
data: { ...node.data, label: `${prefix} ${index + 1}` },
}))
);
}
</script>
<script lang="ts">
import { SvelteFlow, useSvelteFlow, Panel, type Viewport } from '@xyflow/svelte';
import { writable, get } from 'svelte/store';
const nodes = writable<Node[]>([]);
const edges = writable<Edge[]>([]);
const { toObject, setViewport } = useSvelteFlow();
function saveFlow() {
const flow = toObject();
localStorage.setItem('flow', JSON.stringify(flow));
console.log('Flow saved');
}
function restoreFlow() {
const saved = localStorage.getItem('flow');
if (saved) {
const flow = JSON.parse(saved);
nodes.set(flow.nodes || []);
edges.set(flow.edges || []);
if (flow.viewport) {
setViewport(flow.viewport);
}
console.log('Flow restored');
}
}
</script>
<SvelteFlow {nodes} {edges}>
<Panel position="top-right">
<button on:click={saveFlow}>Save</button>
<button on:click={restoreFlow}>Restore</button>
</Panel>
</SvelteFlow>
<script lang="ts">
import { SvelteFlow, type IsValidConnection } from '@xyflow/svelte';
const isValidConnection: IsValidConnection = (connection) => {
// Prevent self-connections
if (connection.source === connection.target) {
return false;
}
// Add custom validation logic
return true;
};
</script>
<SvelteFlow {nodes} {edges} {isValidConnection} />
Use svelteflow-fundamentals when you need to:
Use when typeScript utility types, mapped types, and advanced type manipulation. Use when creating flexible, type-safe TypeScript code.
Use when writing shell scripts that need to run across different systems, shells, or environments. Covers POSIX compatibility and platform differences.
Use when optimizing Tailwind CSS for production, reducing bundle size, and improving performance. Covers PurgeCSS, JIT mode, and build optimization.
Generate production-ready code from a Figma component or frame using the Figma MCP server
Use when integrating Dialyzer into development workflows and CI/CD pipelines for Erlang/Elixir projects.
Use when ruboCop configuration including YAML config, departments, inheritance, and customization.
Use when Elixir OTP patterns including GenServer, Supervisor, Agent, and Task. Use when building concurrent, fault-tolerant Elixir applications.
Use when creating custom Ameba rules for Crystal code analysis including rule development, AST traversal, issue reporting, and rule testing.
Use when swift's optional handling patterns including optional binding, chaining, nil coalescing, and modern approaches to safely working with optional values while avoiding common pitfalls and force unwrapping.
Use when annotating code with structured metadata, tags, and markers for AI-assisted development workflows. Covers annotation formats, semantic tags, and integration with development tools.
Use when scala collections including immutable/mutable variants, List, Vector, Set, Map operations, collection transformations, lazy evaluation with views, parallel collections, and custom collection builders for efficient data processing.
Use when creating custom React Flow nodes, edges, and handles. Covers custom node components, resizable nodes, toolbars, and advanced customization.
Generate, edit, and beat-sync AI video with leading models in one workspace.
The world's fastest calendar for remote work
Transform Your Design with AI Designer by ImgCreator.ai
Revolutionizing Video Production with AI-Powered Creativity
Extend an image past the frame and let AI fill the new aspect ratio.
Discover your celebrity doppelgänger with StarByFace!
ChainClarity explains 700+ crypto whitepapers in plain English, with layered summaries, comparisons, research tools, alerts, and a $4.99 Pro plan.
Opus.ai: Revolutionize Your Web Experience