├── example
├── public
│ ├── robots.txt
│ ├── favicon.ico
│ ├── logo192.png
│ ├── logo512.png
│ ├── manifest.json
│ └── index.html
├── src
│ ├── setupTests.js
│ ├── App.res
│ ├── index.css
│ ├── reportWebVitals.js
│ ├── NavButton.res
│ ├── index.js
│ ├── validation.css
│ ├── Validation.res
│ ├── logo.svg
│ └── Basic.res
├── .gitignore
├── bsconfig.json
├── package.json
└── README.md
├── .npmignore
├── .gitignore
├── bsconfig.json
├── package.json
├── yarn.lock
├── LICENSE
├── src
├── ReactFlow_Utils.res
├── ReactFlow.res
└── ReactFlow_Types.res
├── README.md
└── assets
└── logo.svg
/example/public/robots.txt:
--------------------------------------------------------------------------------
1 | # https://www.robotstxt.org/robotstxt.html
2 | User-agent: *
3 | Disallow:
4 |
--------------------------------------------------------------------------------
/example/public/favicon.ico:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/rescriptbr/react-flow/HEAD/example/public/favicon.ico
--------------------------------------------------------------------------------
/example/public/logo192.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/rescriptbr/react-flow/HEAD/example/public/logo192.png
--------------------------------------------------------------------------------
/example/public/logo512.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/rescriptbr/react-flow/HEAD/example/public/logo512.png
--------------------------------------------------------------------------------
/.npmignore:
--------------------------------------------------------------------------------
1 | .DS_Store
2 | /node_modules/
3 | .merlin
4 | *.log
5 | /tests/
6 | /static/
7 | /example/
8 | /assets/
9 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | .DS_Store
2 | .merlin
3 | .bsb.lock
4 | npm-debug.log
5 | /node_modules/
6 | src/*.js
7 | lib
8 | package-lock.json
9 | .bs.js
10 | /example/src/*.bs.js
11 | *.bs.js
--------------------------------------------------------------------------------
/bsconfig.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "@rescriptbr/react-flow",
3 | "bsc-flags": ["-bs-super-errors"],
4 | "version": "0.1.0",
5 | "sources": ["src"],
6 | "bs-dependencies": ["@rescript/react"],
7 | "reason": {
8 | "react-jsx": 3
9 | },
10 | "refmt": 3
11 | }
12 |
--------------------------------------------------------------------------------
/example/src/setupTests.js:
--------------------------------------------------------------------------------
1 | // jest-dom adds custom jest matchers for asserting on DOM nodes.
2 | // allows you to do things like:
3 | // expect(element).toHaveTextContent(/react/i)
4 | // learn more: https://github.com/testing-library/jest-dom
5 | import '@testing-library/jest-dom';
6 |
--------------------------------------------------------------------------------
/example/src/App.res:
--------------------------------------------------------------------------------
1 | @react.component
2 | let make = () => {
3 | let url = RescriptReactRouter.useUrl()
4 |
5 | let selected = switch url.path {
6 | | list{"Basic"} =>
7 | | _ =>
8 | }
9 |
10 |
11 |
12 |
13 | {selected}
14 |
15 | }
16 |
--------------------------------------------------------------------------------
/example/src/index.css:
--------------------------------------------------------------------------------
1 | body {
2 | margin: 0;
3 | font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen',
4 | 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue',
5 | sans-serif;
6 | -webkit-font-smoothing: antialiased;
7 | -moz-osx-font-smoothing: grayscale;
8 | }
9 |
10 | code {
11 | font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New',
12 | monospace;
13 | }
14 |
--------------------------------------------------------------------------------
/example/.gitignore:
--------------------------------------------------------------------------------
1 | # See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
2 |
3 | # dependencies
4 | /node_modules
5 | /.pnp
6 | .pnp.js
7 |
8 | # testing
9 | /coverage
10 |
11 | # production
12 | /build
13 |
14 | # misc
15 | .DS_Store
16 | .env.local
17 | .env.development.local
18 | .env.test.local
19 | .env.production.local
20 | *.bs.js
21 |
22 | npm-debug.log*
23 | yarn-debug.log*
24 | yarn-error.log*
25 |
--------------------------------------------------------------------------------
/example/src/reportWebVitals.js:
--------------------------------------------------------------------------------
1 | const reportWebVitals = onPerfEntry => {
2 | if (onPerfEntry && onPerfEntry instanceof Function) {
3 | import('web-vitals').then(({ getCLS, getFID, getFCP, getLCP, getTTFB }) => {
4 | getCLS(onPerfEntry);
5 | getFID(onPerfEntry);
6 | getFCP(onPerfEntry);
7 | getLCP(onPerfEntry);
8 | getTTFB(onPerfEntry);
9 | });
10 | }
11 | };
12 |
13 | export default reportWebVitals;
14 |
--------------------------------------------------------------------------------
/example/src/NavButton.res:
--------------------------------------------------------------------------------
1 | @react.component
2 | let make = (~name: string, ~linkTo: string) => {
3 | let url = RescriptReactRouter.useUrl()
4 | let path = List.length(url.path) > 0 ? List.hd(url.path) : ""
5 | let style = if path == name {
6 | ReactDOM.Style.make(~backgroundColor="#656565", ~padding="1ex", ~color="#fff", ())
7 | } else {
8 | ReactDOM.Style.make(~backgroundColor="#efefef", ~padding="1ex", ())
9 | }
10 |
11 | {React.string(name)}
12 | }
13 |
--------------------------------------------------------------------------------
/example/bsconfig.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "react-flow-test",
3 | "namespace": true,
4 | "reason": {
5 | "react-jsx": 3
6 | },
7 | "refmt": 3,
8 | "bs-dependencies": ["@rescript/react", "@rescriptbr/react-flow"],
9 | "ppx-flags": [],
10 | "sources": [
11 | {
12 | "dir": "src",
13 | "subdirs": true
14 | }
15 | ],
16 | "package-specs": {
17 | "module": "es6",
18 | "in-source": true
19 | },
20 | "suffix": ".bs.js",
21 | "bsc-flags": ["-bs-no-version-header", "-bs-super-errors", "-bs-g"]
22 | }
23 |
--------------------------------------------------------------------------------
/example/src/index.js:
--------------------------------------------------------------------------------
1 | import React from 'react';
2 | import ReactDOM from 'react-dom';
3 | import './index.css';
4 | import { make as App } from './App.bs';
5 | import reportWebVitals from './reportWebVitals';
6 |
7 | ReactDOM.render(
8 |
9 |
10 | ,
11 | document.getElementById('root')
12 | );
13 |
14 | // If you want to start measuring performance in your app, pass a function
15 | // to log results (for example: reportWebVitals(console.log))
16 | // or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals
17 | reportWebVitals();
18 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "@rescriptbr/react-flow",
3 | "version": "0.1.2",
4 | "description": "Easily use React Flow with ReScript",
5 | "repository": "git@github.com:rescriptbr/react-flow.git",
6 | "homepage": "https://github.com/rescriptbr/react-flow#readme",
7 | "bugs": "https://github.com/rescriptbr/react-flow/issues",
8 | "author": "Diogo Mafra ",
9 | "license": "MIT",
10 | "devDependencies": {
11 | "@rescript/react": "^0.10.1",
12 | "bs-platform": "^9.0.0"
13 | },
14 | "peerDependencies": {
15 | "@rescript/react": "^0.10.1"
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/example/public/manifest.json:
--------------------------------------------------------------------------------
1 | {
2 | "short_name": "React App",
3 | "name": "Create React App Sample",
4 | "icons": [
5 | {
6 | "src": "favicon.ico",
7 | "sizes": "64x64 32x32 24x24 16x16",
8 | "type": "image/x-icon"
9 | },
10 | {
11 | "src": "logo192.png",
12 | "type": "image/png",
13 | "sizes": "192x192"
14 | },
15 | {
16 | "src": "logo512.png",
17 | "type": "image/png",
18 | "sizes": "512x512"
19 | }
20 | ],
21 | "start_url": ".",
22 | "display": "standalone",
23 | "theme_color": "#000000",
24 | "background_color": "#ffffff"
25 | }
26 |
--------------------------------------------------------------------------------
/yarn.lock:
--------------------------------------------------------------------------------
1 | # THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
2 | # yarn lockfile v1
3 |
4 |
5 | "@rescript/react@^0.10.1":
6 | version "0.10.1"
7 | resolved "https://registry.yarnpkg.com/@rescript/react/-/react-0.10.1.tgz#ddce66ba664a104354d559c350ca4ebf17ab5a26"
8 | integrity sha512-5eIfGnV1yhjv03ktK6fQ6iEfsZKXKXXrq5hx4+ngEY4R/RU8o/oH9ne375m9RJMugV/jsE8hMoEeSSg2YQy3Ag==
9 |
10 | bs-platform@^9.0.0:
11 | version "9.0.0"
12 | resolved "https://registry.yarnpkg.com/bs-platform/-/bs-platform-9.0.0.tgz#873e9edcfe49a89b082e71c604d73bdb8276e2fb"
13 | integrity sha512-32gIXIUt1RO+OUWLrkhXFOuZKS5Cka6lEShXaVC/ohg5N8cUnoNZW9M/vW3CWnmgZkQ0Hp9g9vj5I5NfzHpy5A==
14 |
--------------------------------------------------------------------------------
/example/src/validation.css:
--------------------------------------------------------------------------------
1 | .validationflow .react-flow__node {
2 | width: 150px;
3 | border-radius: 5px;
4 | padding: 10px;
5 | color: #555;
6 | border: 1px solid #ddd;
7 | text-align: center;
8 | font-size: 12px;
9 | }
10 |
11 | .validationflow .react-flow__node-customnode {
12 | background: #e6e6e9;
13 | border: 1px solid #ddd;
14 | }
15 |
16 | .react-flow__node-custominput .react-flow__handle {
17 | background: #e6e6e9;
18 | }
19 |
20 | .validationflow .react-flow__node-custominput {
21 | background: #fff;
22 |
23 | }
24 |
25 | .validationflow .react-flow__handle-connecting {
26 | background: #ff6060;
27 | }
28 |
29 | .validationflow .react-flow__handle-valid {
30 | background: #55dd99;
31 | }
32 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2021 Diogo Mafra
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/example/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "example",
3 | "version": "0.1.0",
4 | "private": true,
5 | "dependencies": {
6 | "@rescript/react": "^0.10.1",
7 | "@rescriptbr/react-flow": "^0.1.2",
8 | "@testing-library/jest-dom": "^5.11.4",
9 | "@testing-library/react": "^11.1.0",
10 | "@testing-library/user-event": "^12.1.10",
11 | "react": "^17.0.1",
12 | "react-dom": "^17.0.1",
13 | "react-flow-renderer": "^8.8.0",
14 | "react-scripts": "4.0.2",
15 | "web-vitals": "^1.0.1"
16 | },
17 | "scripts": {
18 | "start": "react-scripts start",
19 | "re:build": "bsb -make-world -clean-world",
20 | "re:watch": "bsb -make-world -clean-world -w",
21 | "build": "react-scripts build",
22 | "test": "react-scripts test",
23 | "eject": "react-scripts eject"
24 | },
25 | "eslintConfig": {
26 | "extends": [
27 | "react-app",
28 | "react-app/jest"
29 | ]
30 | },
31 | "browserslist": {
32 | "production": [
33 | ">0.2%",
34 | "not dead",
35 | "not op_mini all"
36 | ],
37 | "development": [
38 | "last 1 chrome version",
39 | "last 1 firefox version",
40 | "last 1 safari version"
41 | ]
42 | },
43 | "devDependencies": {
44 | "bs-platform": "^9.0.1"
45 | }
46 | }
47 |
--------------------------------------------------------------------------------
/example/public/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
12 |
13 |
17 |
18 |
27 | Rescript React Flow Examples
28 |
29 |
30 | You need to enable JavaScript to run this app.
31 |
32 |
42 |
43 |
44 |
--------------------------------------------------------------------------------
/example/src/Validation.res:
--------------------------------------------------------------------------------
1 | %%raw(`import "./validation.css"`)
2 |
3 | let elements = [
4 | ReactFlow.Types.Node(
5 | ReactFlow.Node.makeNode(~id="0", ~type_="custominput", ~position={x: 0, y: 150}, ()),
6 | ),
7 | ReactFlow.Types.Node(
8 | ReactFlow.Node.makeNode(~id="A", ~type_="customnode", ~position={x: 250, y: 0}, ()),
9 | ),
10 | ReactFlow.Types.Node(
11 | ReactFlow.Node.makeNode(~id="B", ~type_="customnode", ~position={x: 250, y: 150}, ()),
12 | ),
13 | ReactFlow.Types.Node(
14 | ReactFlow.Node.makeNode(~id="C", ~type_="customnode", ~position={x: 250, y: 300}, ()),
15 | ),
16 | ]
17 |
18 | let isValidConnection = (connection: ReactFlow.Types.connection) => {
19 | Belt.Option.getExn(connection.target) === "B"
20 | }
21 |
22 | let onConnectStop = event => Js.log(event)
23 | let onConnectStart = (event, params) => Js.log2(event, params)
24 | let onConnectEnd = event => Js.log2("end: ", event)
25 |
26 | module CustomInput = {
27 | @react.component
28 | let make = () => {
29 | <>
30 | {React.string("Only connectable with B")}
31 |
32 | >
33 | }
34 | }
35 |
36 | module CustomNode = {
37 | @react.component
38 | let make = (~id) => {
39 | <>
40 |
41 | {React.string(id)}
42 |
43 | >
44 | }
45 | }
46 |
47 | let nodeTypes = {
48 | "custominput": CustomInput.make,
49 | "customnode": CustomNode.make,
50 | }
51 |
52 | let onLoad = (reactFlowInstance: ReactFlow.Types.onLoadParams) => {
53 | reactFlowInstance.fitView({padding: None, includeHiddenNodes: None})
54 | }
55 |
56 | @react.component
57 | let make = () => {
58 | let (elems, setElems) = React.useState(() => elements)
59 | let onConnect = newEdgeParams => {
60 | setElems(elems => ReactFlow.Utils.addEdge(newEdgeParams, elems))
61 | }
62 |
63 |
64 | ReactFlow.Utils.elementsToRaw}
66 | onConnect
67 | onConnectEnd
68 | onConnectStart
69 | onConnectStop
70 | selectNodesOnDrag=false
71 | onLoad
72 | className="validationflow"
73 | nodeTypes
74 | />
75 |
76 | }
77 |
--------------------------------------------------------------------------------
/example/src/logo.svg:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/example/src/Basic.res:
--------------------------------------------------------------------------------
1 | let elements = [
2 | ReactFlow.Types.Node(
3 | ReactFlow.Node.makeNode(
4 | ~id="1",
5 | ~position={x: 250, y: 0},
6 | ~data=ReactFlow.Node.toData({"label": React.string("test")}),
7 | ~type_="input",
8 | (),
9 | ),
10 | ),
11 | ReactFlow.Types.Node(
12 | ReactFlow.Node.makeNode(
13 | ~id="2",
14 | ~position={x: 100, y: 100},
15 | ~data=ReactFlow.Node.toData({"label": React.string("test2")}),
16 | ~type_="output",
17 | (),
18 | ),
19 | ),
20 | ReactFlow.Types.Node(
21 | ReactFlow.Node.makeNode(
22 | ~id="3",
23 | ~data=ReactFlow.Node.toData({"label": React.string("test3")}),
24 | ~position={x: 400, y: 100},
25 | ~type_="default",
26 | ~style=ReactDOM.Style.make(
27 | ~color="#333",
28 | ~background="#D6D5E6",
29 | ~border="1px solid #222138",
30 | ~width="180",
31 | (),
32 | ),
33 | (),
34 | ),
35 | ),
36 | ReactFlow.Types.Edge(
37 | ReactFlow.Edge.makeEdge(
38 | ~id="e1-2",
39 | ~source="1",
40 | ~target="2",
41 | ~label="this is an edge label",
42 | ~data=ReactFlow.Edge.toData("some other data"),
43 | (),
44 | ),
45 | ),
46 | ]
47 |
48 | let onLoad = (reactFlowInstance: ReactFlow.Types.onLoadParams) => {
49 | reactFlowInstance.fitView({padding: None, includeHiddenNodes: None})
50 | }
51 |
52 | @react.component
53 | let make = () => {
54 | let (elems, setElems) = React.useState(() => elements)
55 | let onElementsRemove = elementsToRemove => {
56 | setElems(elems => ReactFlow.Utils.removeElements(elementsToRemove, elems))
57 | }
58 |
59 | let onConnect = newEdgeParams => {
60 | setElems(elems => ReactFlow.Utils.addEdge(newEdgeParams, elems))
61 | }
62 |
63 |
64 | ReactFlow.Utils.elementsToRaw}
66 | onElementsRemove
67 | onConnect
68 | onLoad
69 | snapToGrid=true
70 | snapGrid=(15, 15)>
71 |
72 |
73 | {
75 | switch ReactFlow.Node.type_Get(n) {
76 | | Some("input") => "#0041d0"
77 | | Some("output") => "#ff0072"
78 | | Some("default") => "#1a192b"
79 | | _ => "#eee"
80 | }
81 | }}
82 | nodeStrokeColor={_ => "#fff"}
83 | nodeBorderRadius={2}
84 | />
85 |
86 |
87 | }
88 |
--------------------------------------------------------------------------------
/src/ReactFlow_Utils.res:
--------------------------------------------------------------------------------
1 | open ReactFlow_Types
2 |
3 | @module("react-flow-renderer")
4 | external isEdge: rawElement => bool = "isEdge"
5 |
6 | @module("react-flow-renderer")
7 | external isNode: rawElement => bool = "isNode"
8 |
9 | @module("react-flow-renderer")
10 | external jsRemoveElements: (rawElements, rawElements) => rawElements = "removeElements"
11 |
12 | @module("react-flow-renderer")
13 | external jsAddEdge: (rawElement, rawElements) => rawElements = "addEdge"
14 |
15 | @module("react-flow-renderer")
16 | external jsUpdateEdge: (rawElement, rawElement, rawElements) => rawElements = "updateEdge"
17 |
18 | @module("react-flow-renderer")
19 | external jsGetOutgoers: (rawElement, rawElements) => rawElements = "getOutgoers"
20 |
21 | @module("react-flow-renderer")
22 | external jsGetIncomers: (rawElement, rawElements) => rawElements = "getIncomers"
23 |
24 | @module("react-flow-renderer")
25 | external jsGetConnectedEdges: (rawElements, rawElements) => rawElements = "getConnectedEdges"
26 |
27 | @module("react-flow-renderer")
28 | external getTransformForBounds: (
29 | ~bounds: rect,
30 | ~width: float,
31 | ~height: float,
32 | ~minZoom: float,
33 | ~maxZoom: float,
34 | ~padding: float=?,
35 | ) => transform = "getTransformForBounds"
36 |
37 | external rawToNode: rawElement => Node.t = "%identity"
38 |
39 | external rawToEdge: rawElement => Edge.t = "%identity"
40 |
41 | external elemToRaw: 'a => rawElement = "%identity"
42 |
43 | let unwrapElem = elem => {
44 | switch elem {
45 | | Node(elem) => elemToRaw(elem)
46 | | Edge(elem) => elemToRaw(elem)
47 | }
48 | }
49 |
50 | let elementsToRaw = (elems): rawElements => {
51 | elems->Belt.Array.map(unwrapElem)
52 | }
53 |
54 | let rawToElements = rawElements => {
55 | rawElements->Belt.Array.map(elem => {
56 | if isNode(elem) {
57 | Node(rawToNode(elem))
58 | } else {
59 | Edge(rawToEdge(elem))
60 | }
61 | })
62 | }
63 |
64 | let addEdge = (rawElement, elements) => {
65 | rawToElements(jsAddEdge(rawElement, elementsToRaw(elements)))
66 | }
67 |
68 | let removeElements = (rawElements, elements) => {
69 | rawToElements(jsRemoveElements(rawElements, elementsToRaw(elements)))
70 | }
71 |
72 | let updateEdge = (~oldEdge: Edge.t, ~newConnection: connection, ~elems: elements) => {
73 | rawToElements(jsUpdateEdge(elemToRaw(oldEdge), elemToRaw(newConnection), elementsToRaw(elems)))
74 | }
75 |
76 | let getOutgoers = (~node: Node.t, ~elems: elements): array => {
77 | jsGetOutgoers(elemToRaw(node), elementsToRaw(elems))->Belt.Array.map(rawToNode)
78 | }
79 |
80 | let getIncomers = (~node: Node.t, ~elems: elements): array => {
81 | jsGetIncomers(elemToRaw(node), elementsToRaw(elems))->Belt.Array.map(rawToNode)
82 | }
83 |
84 | let getConnectedEdges = (~nodes: array, ~edges: array): array => {
85 | jsGetConnectedEdges(
86 | elementsToRaw(nodes->Belt.Array.map(n => Node(n))),
87 | elementsToRaw(edges->Belt.Array.map(e => Edge(e))),
88 | )->Belt.Array.map(rawToEdge)
89 | }
90 |
--------------------------------------------------------------------------------
/example/README.md:
--------------------------------------------------------------------------------
1 | # Getting Started with Create React App
2 |
3 | This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).
4 |
5 | ## Available Scripts
6 |
7 | In the project directory, you can run:
8 |
9 | ### `yarn start`
10 |
11 | Runs the app in the development mode.\
12 | Open [http://localhost:3000](http://localhost:3000) to view it in the browser.
13 |
14 | The page will reload if you make edits.\
15 | You will also see any lint errors in the console.
16 |
17 | ### `yarn test`
18 |
19 | Launches the test runner in the interactive watch mode.\
20 | See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information.
21 |
22 | ### `yarn build`
23 |
24 | Builds the app for production to the `build` folder.\
25 | It correctly bundles React in production mode and optimizes the build for the best performance.
26 |
27 | The build is minified and the filenames include the hashes.\
28 | Your app is ready to be deployed!
29 |
30 | See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information.
31 |
32 | ### `yarn eject`
33 |
34 | **Note: this is a one-way operation. Once you `eject`, you can’t go back!**
35 |
36 | If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project.
37 |
38 | Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own.
39 |
40 | You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it.
41 |
42 | ## Learn More
43 |
44 | You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started).
45 |
46 | To learn React, check out the [React documentation](https://reactjs.org/).
47 |
48 | ### Code Splitting
49 |
50 | This section has moved here: [https://facebook.github.io/create-react-app/docs/code-splitting](https://facebook.github.io/create-react-app/docs/code-splitting)
51 |
52 | ### Analyzing the Bundle Size
53 |
54 | This section has moved here: [https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size](https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size)
55 |
56 | ### Making a Progressive Web App
57 |
58 | This section has moved here: [https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app](https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app)
59 |
60 | ### Advanced Configuration
61 |
62 | This section has moved here: [https://facebook.github.io/create-react-app/docs/advanced-configuration](https://facebook.github.io/create-react-app/docs/advanced-configuration)
63 |
64 | ### Deployment
65 |
66 | This section has moved here: [https://facebook.github.io/create-react-app/docs/deployment](https://facebook.github.io/create-react-app/docs/deployment)
67 |
68 | ### `yarn build` fails to minify
69 |
70 | This section has moved here: [https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify](https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify)
71 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 | # ReScript React Flow
7 |
8 | [React Flow](https://reactflow.dev/) bindings for ReScript.
9 |
10 | ## Installation
11 |
12 | 1. Install `@rescriptbr/react-flow` using npm:
13 |
14 | ```
15 | npm install --save @rescriptbr/react-flow
16 | ```
17 |
18 | or yarn:
19 |
20 | ```
21 | yarn add @rescriptbr/react-flow
22 | ```
23 |
24 | 2. Add `@rescriptbr/react-flow` as dependency to your `bsconfig.json`:
25 |
26 | ```json
27 | {
28 | "name": "your-project",
29 | "bs-dependencies": ["@rescriptbr/react-flow"]
30 | }
31 | ```
32 |
33 | # Examples
34 |
35 | Check the code below for fast basic examples:
36 |
37 | #### Creating a simple Node
38 |
39 | ```rescript
40 | ReactFlow.Types.Node(
41 | ReactFlow.Node.makeNode(
42 | ~id="1",
43 | ~position={x: 250, y: 0},
44 | ~data=ReactFlow.Node.toData({"label": React.string("test")}),
45 | ~type_="input",
46 | (),
47 | ),
48 | ),
49 | ```
50 |
51 | #### Creating a simple Edge
52 |
53 | ```rescript
54 | ReactFlow.Types.Edge(
55 | ReactFlow.Edge.makeEdge(
56 | ~id="e1-2",
57 | ~source="1",
58 | ~target="2",
59 | ~label="this is an edge label",
60 | ~data=ReactFlow.Edge.toData("some other data"),
61 | (),
62 | ),
63 | ),
64 | ```
65 |
66 | #### Creating elements array
67 |
68 | ```rescript
69 | let elements = [
70 | ReactFlow.Types.Edge(
71 | ReactFlow.Edge.makeEdge(
72 | ~id="e1-2",
73 | ~source="1",
74 | ~target="2",
75 | ~label="this is an edge label",
76 | ~data=ReactFlow.Edge.toData("some other data"),
77 | (),
78 | ),
79 | ),
80 | ReactFlow.Types.Node(
81 | ReactFlow.Node.makeNode(
82 | ~id="1",
83 | ~position={x: 250, y: 0},
84 | ~data=ReactFlow.Node.toData({"label": React.string("test")}),
85 | ~type_="input",
86 | (),
87 | ),
88 | ),
89 | ]
90 | ```
91 |
92 | #### Rendering React Flow
93 |
94 | ```rescript
95 | @react.component
96 | let make = () => {
97 | let (elems, setElems) = React.useState(() => elements)
98 | let onElementsRemove = elementsToRemove => {
99 | setElems(elems => ReactFlow.Utils.removeElements(elementsToRemove, elems))
100 | }
101 |
102 | let onConnect = newEdgeParams => {
103 | setElems(elems => ReactFlow.Utils.addEdge(newEdgeParams, elems))
104 | }
105 |
106 |
107 | ReactFlow.Utils.elementsToRaw}
109 | onElementsRemove
110 | onConnect
111 | onLoad
112 | snapToGrid=true
113 | snapGrid=(15, 15)>
114 |
115 |
116 | {
118 | switch ReactFlow.Node.type_Get(n) {
119 | | Some("input") => "#0041d0"
120 | | Some("output") => "#ff0072"
121 | | Some("default") => "#1a192b"
122 | | _ => "#eee"
123 | }
124 | }}
125 | nodeStrokeColor={_ => "#fff"}
126 | nodeBorderRadius={2}
127 | />
128 |
129 |
130 | }
131 | ```
132 |
133 | Please look at the example folder for more advanced implementations.
134 |
135 | ## Contributing
136 |
137 | If you'd like to contribute, you can follow the instructions below to get things working locally.
138 |
139 | ### Getting Started
140 |
141 | 1. After cloning the repo, install the dependencies
142 |
143 | ```
144 | yarn install
145 | ```
146 |
147 | 2. Build:
148 |
149 | ```
150 | yarn re:build
151 | ```
152 |
153 | 3. If you're running the example, in other terminal run:
154 |
155 | ```
156 | yarn start
157 | ```
158 |
--------------------------------------------------------------------------------
/src/ReactFlow.res:
--------------------------------------------------------------------------------
1 | module Types = ReactFlow_Types
2 |
3 | module Utils = ReactFlow_Utils
4 |
5 | module Node = Types.Node
6 |
7 | module Edge = Types.Edge
8 |
9 | @module("react-flow-renderer") @react.component
10 | external make: (
11 | ~elements: Types.rawElements,
12 | ~children: React.element=?,
13 | ~onElementsClick: (~event: Dom.mouseEvent=?, ~element: Types.rawElement=?) => unit=?,
14 | ~snapToGrid: bool=?,
15 | ~onConnect: Types.rawElement => unit=?,
16 | ~onElementsRemove: Types.rawElements => unit=?,
17 | ~onLoad: Types.onLoadParams => unit=?,
18 | ~snapGrid: (int, int)=?,
19 | ~nodeTypes: 'weakNodeType=?,
20 | ~edgeTypes: 'weakEdgeType=?,
21 | ~connectionLineComponent: Types.connectionLineComponent=?,
22 | ~selectNodesOnDrag: bool=?,
23 | ~className: string=?,
24 | ~onConnectStart: Types.onConnectStartFunc=?,
25 | ~onConnectStop: Types.onConnectStopFunc=?,
26 | ~onConnectEnd: Types.onConnectEndFunc=?,
27 | ) => React.element = "default"
28 |
29 | module Handle = {
30 | @module("react-flow-renderer") @react.component
31 | external make: (
32 | @as("type") ~_type: Types.Handle.handleType,
33 | ~position: Types.position,
34 | ~isConnectable: bool=?,
35 | ~onConnect: Types.Handle.onConnectFunc=?,
36 | ~isValidConnection: Types.connection => bool=?,
37 | ~id: Types.elementId=?,
38 | ~className: string=?,
39 | ~style: ReactDOM.Style.t=?,
40 | ~children: React.element=?,
41 | ) => React.element = "Handle"
42 | }
43 |
44 | module MiniMap = {
45 | @module("react-flow-renderer") @react.component
46 | external make: (
47 | ~nodeColor: Types.MiniMap.stringFunc=?,
48 | ~nodeStrokeColor: Types.MiniMap.stringFunc=?,
49 | ~nodeClassName: Types.MiniMap.stringFunc=?,
50 | ~nodeBorderRadius: int=?,
51 | ~nodeStrokeWidth: int=?,
52 | ~maskColor: string=?,
53 | ) => React.element = "MiniMap"
54 | }
55 |
56 | module Controls = {
57 | @module("react-flow-renderer") @react.component
58 | external make: (
59 | ~showZoom: bool=?,
60 | ~showFitView: bool=?,
61 | ~showInteractive: bool=?,
62 | ~fitViewParams: Types.fitViewParams=?,
63 | ~onZoomIn: unit => unit=?,
64 | ~onZoomOut: unit => unit=?,
65 | ~onFitView: unit => unit=?,
66 | ~onInteractiveChange: (~interactiveStatus: bool) => unit=?,
67 | ) => React.element = "Controls"
68 | }
69 |
70 | module Background = {
71 | type backgroundVariant = [#lines | #dots]
72 |
73 | @module("react-flow-renderer") @react.component
74 | external make: (
75 | ~variant: backgroundVariant=?,
76 | ~gap: int=?,
77 | ~color: string=?,
78 | ~size: int=?,
79 | ) => React.element = "Background"
80 | }
81 |
82 | module Provider = {
83 | @module("react-flow-renderer") @react.component
84 | external make: (~children: React.element) => React.element = "ReactFlowProvider"
85 | }
86 |
87 | module EdgeText = {
88 | @module("react-flow-renderer") @react.component
89 | external make: (
90 | ~x: int,
91 | ~y: int,
92 | ~label: React.element=?,
93 | ~labelStyle: ReactDOM.Style.t=?,
94 | ~labelShowBg: bool=?,
95 | ~labelBgStyle: ReactDOM.Style.t=?,
96 | ~labelBgPadding: (int, int)=?,
97 | ~labelBgBorderRadius: int=?,
98 | ) => React.element = "EdgeText"
99 | }
100 |
101 | @module("react-flow-renderer")
102 | external useStoredAction: unit => Types.Action.t = "useStoredAction"
103 |
104 | @module("react-flow-renderer")
105 | external useStoreState: unit => Types.reactFlowState = "useStoreState"
106 |
107 | @module("react-flow-renderer")
108 | external useZoomPanHelper: unit => Types.zoomPanHelperFunctions = "useZoomPanHelper"
109 |
110 | @module("react-flow-renderer")
111 | external useUpdateNodeInternals: unit => Types.updateNodeInternals = "useUpdatenodeInternals"
112 |
113 | @module("react-flow-renderer")
114 | external getBezierPath: (
115 | ~sourceX: int,
116 | ~sourceY: int,
117 | ~sourcePosition: Types.position=?,
118 | ~targetX: int,
119 | ~targetY: int,
120 | ~targetPosition: Types.position=?,
121 | ~centerX: int=?,
122 | ~centerY: int=?,
123 | ) => React.element = "getBezierPath"
124 |
125 | @module("react-flow-renderer")
126 | external getSmoothStepPath: (
127 | ~sourceX: int,
128 | ~sourceY: int,
129 | ~sourcePosition: Types.position=?,
130 | ~targetX: int,
131 | ~targetY: int,
132 | ~targetPosition: Types.position=?,
133 | ~borderRadius: int=?,
134 | ~centerX: int=?,
135 | ~centerY: int=?,
136 | ) => string = "getSmoothStepPath"
137 |
138 | @module("react-flow-renderer")
139 | external getMarkerEnd: (~arrowHeadType: Types.arrowHeadType=?, ~markerEndId: string=?) => string =
140 | "getMarkerEnd"
141 |
142 | @module("react-flow-renderer")
143 | external getCenter: (
144 | ~sourceX: int,
145 | ~sourceY: int,
146 | ~targetX: int,
147 | ~targetY: int,
148 | ~sourcePosition: Types.position=?,
149 | ~targetPosition: Types.position=?,
150 | ) => (int, int, int, int) = "getCenter"
151 |
--------------------------------------------------------------------------------
/src/ReactFlow_Types.res:
--------------------------------------------------------------------------------
1 | type rawElement
2 |
3 | type rawElements = array
4 |
5 | type elementId = string
6 |
7 | type transform = (int, int, int)
8 |
9 | type position = [#left | #top | #right | #bottom]
10 |
11 | type arrowHeadType = [#Arrow | #ArrowClosed]
12 |
13 | type xyPosition = {x: int, y: int}
14 |
15 | type dimensions = {
16 | width: int,
17 | height: int,
18 | }
19 |
20 | type rect = {
21 | x: int,
22 | y: int,
23 | width: int,
24 | height: int,
25 | }
26 |
27 | module Node = {
28 | type data
29 |
30 | external toData: 'anything => data = "%identity"
31 |
32 | @deriving(abstract)
33 | type t = {
34 | id: elementId,
35 | position: xyPosition,
36 | @optional @as("type") type_: string,
37 | @optional
38 | data: data,
39 | @optional
40 | style: ReactDOM.Style.t,
41 | @optional
42 | className: string,
43 | @optional
44 | targetPosition: position,
45 | @optional
46 | sourcePosition: position,
47 | @optional
48 | isHidden: bool,
49 | @optional
50 | draggable: bool,
51 | @optional
52 | selectable: bool,
53 | @optional
54 | connectable: bool,
55 | }
56 |
57 | let makeNode = t
58 | }
59 |
60 | module Edge = {
61 | type data
62 |
63 | external toData: 'anything => data = "%identity"
64 |
65 | @deriving(abstract)
66 | type t = {
67 | id: elementId,
68 | source: elementId,
69 | target: elementId,
70 | @optional @as("type") type_: string,
71 | @optional
72 | sourceHandle: elementId,
73 | @optional
74 | targetHandle: elementId,
75 | @optional
76 | label: string,
77 | @optional
78 | labelStyle: ReactDOM.Style.t,
79 | @optional
80 | labelShowBg: bool,
81 | @optional
82 | labelBgStyle: ReactDOM.Style.t,
83 | @optional
84 | labelBdPadding: (int, int),
85 | @optional
86 | labelBgBorderRadius: int,
87 | @optional
88 | style: ReactDOM.Style.t,
89 | @optional
90 | animated: bool,
91 | @optional
92 | arrowHeadType: arrowHeadType,
93 | @optional
94 | isHidden: bool,
95 | @optional
96 | data: data,
97 | @optional
98 | className: string,
99 | }
100 |
101 | let makeEdge = t
102 | }
103 |
104 | type connection = {
105 | source: option,
106 | target: option,
107 | sourceHandle: option,
108 | targetHandle: option,
109 | }
110 |
111 | type connectionLineType = [#default | #straight | #step | #smoothstep]
112 |
113 | type connectionLineComponentProps = {
114 | sourceX: int,
115 | sourceY: int,
116 | sourcePosition: option,
117 | targetX: int,
118 | targetY: int,
119 | targetPosition: option,
120 | connectionLineStyle: option,
121 | connectionLineType: connectionLineType,
122 | }
123 |
124 | type connectionLineComponent = React.componentLike
125 |
126 | type edgeOrConnection = Connection(connection) | Edge(Edge.t)
127 |
128 | type flowElement = Node(Node.t) | Edge(Edge.t)
129 |
130 | type elements = array
131 |
132 | type fitViewParams = {
133 | padding: option,
134 | includeHiddenNodes: option,
135 | }
136 |
137 | type flowTransform = {
138 | x: int,
139 | y: int,
140 | zoom: int,
141 | }
142 |
143 | type fitViewFunc = fitViewParams => unit
144 |
145 | type projectFunc = (~position: xyPosition) => xyPosition
146 |
147 | type flowExportObj = {
148 | elements: elements,
149 | position: (int, int),
150 | zoom: int,
151 | }
152 |
153 | type toObjFunc = unit => flowExportObj
154 |
155 | type onLoadParams = {
156 | zoomIn: unit => unit,
157 | zoomOut: unit => unit,
158 | zoomTo: (~zoomLevel: int) => unit,
159 | fitView: fitViewFunc,
160 | project: projectFunc,
161 | getElements: unit => elements,
162 | setTransform: (~transform: flowTransform) => unit,
163 | toObject: toObjFunc,
164 | }
165 |
166 | type onConnectStartParams = {
167 | nodeId: option,
168 | handleId: option,
169 | handleType: [#source | #target],
170 | }
171 |
172 | type translateExtent = ((int, int), (int, int))
173 |
174 | type connectionMode = [#strict | #loose]
175 |
176 | type selectionRect = {
177 | x: int,
178 | y: int,
179 | width: int,
180 | height: int,
181 | startX: int,
182 | startY: int,
183 | draw: bool,
184 | }
185 |
186 | type onConnectFunc = connection => unit
187 | type onConnectStartFunc = (ReactEvent.Mouse.t, onConnectStartParams) => unit
188 | type onConnectStopFunc = ReactEvent.Mouse.t => unit
189 | type onConnectEndFunc = ReactEvent.Mouse.t => unit
190 |
191 | module Handle = {
192 | type handleType = [#source | #target]
193 |
194 | type onConnectFunc = connection => unit
195 |
196 | type isValidConnectionFunc = connection => bool
197 | }
198 |
199 | type reactFlowState = {
200 | width: int,
201 | height: int,
202 | transform: transform,
203 | nodes: array,
204 | edges: array,
205 | selectedElements: Js.nullable,
206 | selectedNodesBox: rect,
207 | minZoom: int,
208 | maxZoom: int,
209 | translateExtent: translateExtent,
210 | nodeExtent: translateExtent,
211 | nodesSelectionActive: bool,
212 | selectionActive: bool,
213 | userSelectionRect: selectionRect,
214 | connectionNodeId: Js.nullable,
215 | connectionHandleId: Js.nullable,
216 | connectionHandleType: Js.nullable,
217 | connectionPosition: xyPosition,
218 | connectionMode: connectionMode,
219 | snapToGrid: bool,
220 | snapGrid: (int, int),
221 | nodesDraggable: bool,
222 | nodesConnectable: bool,
223 | elementsSelectable: bool,
224 | multiSelectionActive: bool,
225 | reactFlowVersion: string,
226 | onConnect: option,
227 | onConnectStart: option,
228 | onConnectStop: option,
229 | onConnectEnd: option,
230 | }
231 |
232 | type updateNodeInternals = elementId => unit
233 |
234 | type nodePosUpdate = {
235 | id: elementId,
236 | pos: xyPosition,
237 | }
238 |
239 | type nodeDiffUpdate = {
240 | id: option,
241 | diff: option,
242 | isDragging: option,
243 | }
244 |
245 | type setConnectionId = {
246 | connectionNodeId: Js.nullable,
247 | connectionHandleid: Js.nullable,
248 | connectionHandleType: Js.nullable,
249 | }
250 |
251 | type zoomPanHelperFunctions = {
252 | zoomIn: unit => unit,
253 | zoomOut: unit => unit,
254 | zoomTo: int => unit,
255 | transform: flowTransform => unit,
256 | fitView: fitViewFunc,
257 | setCenter: (~x: int, ~y: int, ~zoom: int=?) => unit,
258 | fitBounds: (~bounds: rect, ~padding: int=?) => unit,
259 | project: xyPosition => xyPosition,
260 | initialized: bool,
261 | }
262 |
263 | module MiniMap = {
264 | type stringFunc = Node.t => string
265 | }
266 |
267 | module Action = {
268 | type setOnConnect = onConnectFunc => unit
269 |
270 | type setOnConnectStart = onConnectStartFunc => unit
271 |
272 | type setOnConnectStop = onConnectStopFunc => unit
273 |
274 | type setOnConnectEnd = onConnectEndFunc => unit
275 |
276 | type setElements = rawElements => unit
277 |
278 | type updateNodePos = nodePosUpdate => unit
279 |
280 | type updateNodePosDiff = nodeDiffUpdate => unit
281 |
282 | type setUserSelection = xyPosition => unit
283 |
284 | type updateUserSelection = xyPosition => unit
285 |
286 | type unsetUserSelection = unit => unit
287 |
288 | type setSelection = bool => unit
289 |
290 | type unsetNodesSelection = unit => unit
291 |
292 | type resetSelectedElements = unit => unit
293 |
294 | type setSelectedElements = rawElements => unit
295 |
296 | type addSelectedElements = rawElements => unit
297 |
298 | type updateTransform = transform => unit
299 |
300 | type updateSize = dimensions => unit
301 |
302 | type setMinZoom = int => unit
303 |
304 | type setMaxZoom = int => unit
305 |
306 | type setTranslateExtent = translateExtent => unit
307 |
308 | type setConnectionPosition = xyPosition => unit
309 |
310 | type setConnectionNodeId = setConnectionId => unit
311 |
312 | type setSnapToGrid = bool => unit
313 |
314 | type setSnapGrid = (int, int) => unit
315 |
316 | type setInteractive = bool => unit
317 |
318 | type setNodesDraggable = bool => unit
319 |
320 | type setNodesConnectable = bool => unit
321 |
322 | type setElementsSelectable = bool => unit
323 |
324 | type setMultiSelectionActive = bool => unit
325 |
326 | type setConnectionMode = connectionMode => unit
327 |
328 | type setNodeExtent = translateExtent => unit
329 |
330 | type t = {
331 | setOnConnect: setOnConnect,
332 | setOnConnectStart: setOnConnectStart,
333 | setOnConnectStop: setOnConnectStop,
334 | setOnConnectEnd: setOnConnectEnd,
335 | setElements: setElements,
336 | updateNodePos: updateNodePos,
337 | updateNodePosDiff: updateNodePosDiff,
338 | setuserSelection: setUserSelection,
339 | updateUserSelection: updateUserSelection,
340 | unsetUserSelection: unsetUserSelection,
341 | setSelection: setSelection,
342 | unsetNodesSelection: unsetNodesSelection,
343 | resetSelectedElements: resetSelectedElements,
344 | setSelectedElements: setSelectedElements,
345 | addSelectedElements: addSelectedElements,
346 | updateTransform: updateTransform,
347 | updateSize: updateSize,
348 | setMinZoom: setMinZoom,
349 | setMaxZoom: setMaxZoom,
350 | setTranslateExtent: setTranslateExtent,
351 | setConnectionPosition: setConnectionPosition,
352 | setConnectionNodeId: setConnectionNodeId,
353 | setSnapToGrid: setSnapToGrid,
354 | setSnapGrid: setSnapGrid,
355 | setInteractive: setInteractive,
356 | setNodesDraggable: setNodesDraggable,
357 | setNodesConnectable: setNodesConnectable,
358 | setElementsSelectable: setElementsSelectable,
359 | setMultiSelectionActive: setMultiSelectionActive,
360 | setConnectionMode: setConnectionMode,
361 | setNodeExtent: setNodeExtent,
362 | }
363 | }
364 |
--------------------------------------------------------------------------------
/assets/logo.svg:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
--------------------------------------------------------------------------------