feat(joint-react): React package for JointJS #2522
Draft
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
An early stage React package for working with JointJS.
Demo: https://codesandbox.io/s/jointjs-react-vldmzw?file=/src/App.js
This is a copy of #2391 recreated for
v4
.JointJS React
React core components for working with JointJS.
Note
The goal of this project is to define a minimal basis for the various React components for JointJS. Please help us shape the package by reporting issues or proposing API changes.
Important
This is an early stage product. The package may contain bugs and security issues. The API is subject to change.
Installation
API
Components
<Paper />
The main component that allows you to draw nodes and edges on the canvas. In JointJS terminology, you draw
cells
(elements
andlinks
) onpaper
. The component requires the<GraphProvider />
to be its ancestor.Props
dia.Paper.Options
paper
. It'sasync
by default.(dia.Element) => React.JSX.Element | null
React.createPortal()
.(dia.Paper) => void
paper
is mounted and ready (cells may not be rendered).(dia.Paper, eventName, ...eventArgs) => void
string[]
renderElement
function to be triggered. The default is["data"]
.Element | string | (dia.ElementView) => string | Element
portal
is selector"portal"
.Rendering React component inside an Element view
By default, the content of the element view is rendered using JointJS. However, the content (SVG or HTML) can also be rendered using React.
Currently, the following drawbacks are known with this approach:
portal
node).<Paper />
currently replaces the defaultdia.Paper.prototype.options.elementView
with a customElementView
that triggers theportal:ready
event ((elementView: dia.ElementView, portalEl: SVGElement | HTMLElement) => void
) when theportal
node is rendered (onRender()
method). If you want to use custom views and you want to userenderElement
with them, make sure you trigger theportal:ready
event manually.Paper provides context implicitly.
The
<Paper />
context provides thepaper
context to its descendants implicitly. If you need to use thepaper
context outside of the<Paper />
, use the<PaperProvider />
.<PaperProvider />
The
<PaperProvider />
component is a context provider that makes it possible to access the JointJS paper outside of the<Paper />
component. Unlike theGraphProvider
, thePaperProvider
is not mandatory.<GraphProvider />
The
<GraphProvider />
component is a context provider that provides JointJS graph to<Paper />
components. You need use the<GraphProvider />
in order to render the<Paper />
component.Here's an example of a
GraphProvider
providing agraph
to twopapers
.Props
dia.Graph
graph
instance to be provided to the descendantsHooks
The package exposed several custom hooks.
usePaper
The
usePaper
is a hook that let you use the JointJSpaper
from your component.useGraph
The
useGraph
is a hook that let you use the JointJSgraph
from your component.What's next
The possible tasks ahead of us.
JointJS+
Define React Components for JointJS+.
Higher-level Components
Define user-friendly higher-level components.