refactor(examples): switch examples from cra to nextjs

This commit is contained in:
Christopher Möller
2022-07-19 17:23:26 +02:00
parent 62c417263c
commit be4dd6f1fa
86 changed files with 755 additions and 45859 deletions
@@ -0,0 +1,42 @@
import React from 'react';
import { useStore, useStoreApi } from '@react-flow/core';
import styles from './provider.module.css';
const Sidebar = () => {
const store = useStoreApi();
const nodeInternals = useStore((store) => store.nodeInternals);
const transform = useStore((store) => store.transform);
const selectAll = () => {
nodeInternals.forEach((node) => (node.selected = true));
store.setState({ nodeInternals: new Map(nodeInternals) });
};
return (
<aside className={styles.aside}>
<div className={styles.description}>
This is an example of how you can access the internal state outside of
the ReactFlow component.
</div>
<div className={styles.title}>Zoom & pan transform</div>
<div className={styles.transform}>
[{transform[0].toFixed(2)}, {transform[1].toFixed(2)},{' '}
{transform[2].toFixed(2)}]
</div>
<div className={styles.title}>Nodes</div>
{Array.from(nodeInternals).map(([, node]) => (
<div key={node.id}>
Node {node.id} - x: {node.position.x.toFixed(2)}, y:{' '}
{node.position.y.toFixed(2)}
</div>
))}
<div className={styles.selectall}>
<button onClick={selectAll}>select all nodes</button>
</div>
</aside>
);
};
export default Sidebar;
+69
View File
@@ -0,0 +1,69 @@
import React, { MouseEvent } from 'react';
import ReactFlow, {
ReactFlowProvider,
addEdge,
Node,
Controls,
Connection,
Edge,
ConnectionMode,
useNodesState,
useEdgesState,
ReactFlowInstance,
} from '@react-flow/core';
import Sidebar from './Sidebar';
import styles from './provider.module.css';
const onNodeClick = (_: MouseEvent, node: Node) => console.log('click', node);
const onInit = (reactFlowInstance: ReactFlowInstance) =>
console.log('pane ready:', reactFlowInstance);
const initialNodes: Node[] = [
{
id: '1',
type: 'input',
data: { label: 'Node 1' },
position: { x: 250, y: 5 },
},
{ id: '2', data: { label: 'Node 2' }, position: { x: 100, y: 100 } },
{ id: '3', data: { label: 'Node 3' }, position: { x: 400, y: 100 } },
{ id: '4', data: { label: 'Node 4' }, position: { x: 400, y: 200 } },
];
const initialEdges: Edge[] = [
{ id: 'e1-2', source: '1', target: '2', animated: true },
{ id: 'e1-3', source: '1', target: '3' },
];
const ProviderFlow = () => {
const [nodes, , onNodesChange] = useNodesState(initialNodes);
const [edges, setEdges, onEdgesChange] = useEdgesState(initialEdges);
const onConnect = (params: Edge | Connection) =>
setEdges((els) => addEdge(params, els));
return (
<div className={styles.providerflow}>
<ReactFlowProvider>
<Sidebar />
<div className={styles.wrapper}>
<ReactFlow
nodes={nodes}
edges={edges}
onNodesChange={onNodesChange}
onEdgesChange={onEdgesChange}
onNodeClick={onNodeClick}
onConnect={onConnect}
onInit={onInit}
connectionMode={ConnectionMode.Loose}
>
<Controls />
</ReactFlow>
</div>
</ReactFlowProvider>
</div>
);
};
export default ProviderFlow;
@@ -0,0 +1,45 @@
.providerflow {
flex-direction: column;
display: flex;
height: 100%;
}
.aside {
border-right: 1px solid #eee;
padding: 15px 10px;
font-size: 12px;
background: #fcfcfc;
}
.description {
margin-bottom: 10px;
}
.title {
font-weight: 700;
margin-bottom: 5px;
}
.transform {
margin-bottom: 20px;
}
.wrapper {
flex-grow: 1;
height: 100%;
}
.selectall {
margin-top: 10px;
}
@media screen and (min-width: 768px) {
.providerflow {
flex-direction: row;
}
.aside {
width: 20%;
max-width: 250px;
}
}