Skip to content

Instantly share code, notes, and snippets.

@bryanmaina
Created August 29, 2024 16:33
Show Gist options
  • Save bryanmaina/5b5f72482d2b54446ee12de5f002050f to your computer and use it in GitHub Desktop.
Save bryanmaina/5b5f72482d2b54446ee12de5f002050f to your computer and use it in GitHub Desktop.
Run React and ReactFlow cdn
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="https://esm.sh/@xyflow/[email protected]/dist/style.css?dev">
<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
<script type="text/babel" data-type="module">
import React, { useCallback } from "https://esm.sh/[email protected]/?dev"
import ReactDOMClient from "https://esm.sh/[email protected]/client?dev"
import {
ReactFlow,
MiniMap,
Controls,
Background,
useNodesState,
useEdgesState,
addEdge,
} from "https://esm.sh/@xyflow/[email protected]/?dev"
const rootElement = ReactDOMClient.createRoot(document.getElementById('root'));
const initialNodes = [
{ id: '1', position: { x: 0, y: 0 }, data: { label: '1' } },
{ id: '2', position: { x: 0, y: 100 }, data: { label: '2' } },
];
const initialEdges = [{ id: 'e1-2', source: '1', target: '2' }];
function Graph() {
const [nodes, setNodes, onNodesChange] = useNodesState(initialNodes);
const [edges, setEdges, onEdgesChange] = useEdgesState(initialEdges);
const onConnect = useCallback(
(params) => setEdges((eds) => addEdge(params, eds)),
[setEdges],
);
return <ReactFlow
nodes={nodes}
edges={edges}
onNodesChange={onNodesChange}
onEdgesChange={onEdgesChange}
onConnect={onConnect}
>
<Controls />
<MiniMap />
<Background variant="dots" gap={12} size={1} />
</ReactFlow>
}
rootElement.render(<Graph />);
</script>
</head>
<body>
<div id="root"
style="width: 100vw; height: 100vh; display: flex; flex-direction: column; align-items: center; justify-content: center; text-align: center;">
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment