react-archer

Draw arrows between React elements

README

react-archer


CircleCI

🏹 Draw arrows between DOM elements in React 🖋

Installation


npm install react-archer --save or yarn add react-archer

Example



Example

  1. ```javascript
  2. import { ArcherContainer, ArcherElement } from 'react-archer';

  3. const rootStyle = { display: 'flex', justifyContent: 'center' };
  4. const rowStyle = { margin: '200px 0', display: 'flex', justifyContent: 'space-between' };
  5. const boxStyle = { padding: '10px', border: '1px solid black' };

  6. const App = () => {
  7.   return (
  8.     <div style={{ height: '500px', margin: '50px' }}>
  9.       <ArcherContainer strokeColor="red">
  10.         <div style={rootStyle}>
  11.           <ArcherElement
  12.             id="root"
  13.             relations={[
  14.               {
  15.                 targetId: 'element2',
  16.                 targetAnchor: 'top',
  17.                 sourceAnchor: 'bottom',
  18.                 style: { strokeDasharray: '5,5' },
  19.               },
  20.             ]}
  21.           >
  22.             <div style={boxStyle}>Root</div>
  23.           </ArcherElement>
  24.         </div>

  25.         <div style={rowStyle}>
  26.           <ArcherElement
  27.             id="element2"
  28.             relations={[
  29.               {
  30.                 targetId: 'element3',
  31.                 targetAnchor: 'left',
  32.                 sourceAnchor: 'right',
  33.                 style: { strokeColor: 'blue', strokeWidth: 1 },
  34.                 label: <div style={{ marginTop: '-20px' }}>Arrow 2</div>,
  35.               },
  36.             ]}
  37.           >
  38.             <div style={boxStyle}>Element 2</div>
  39.           </ArcherElement>

  40.           <ArcherElement id="element3">
  41.             <div style={boxStyle}>Element 3</div>
  42.           </ArcherElement>

  43.           <ArcherElement
  44.             id="element4"
  45.             relations={[
  46.               {
  47.                 targetId: 'root',
  48.                 targetAnchor: 'right',
  49.                 sourceAnchor: 'left',
  50.                 label: 'Arrow 3',
  51.               },
  52.             ]}
  53.           >
  54.             <div style={boxStyle}>Element 4</div>
  55.           </ArcherElement>
  56.         </div>
  57.       </ArcherContainer>
  58.     </div>
  59.   );
  60. };

  61. export default App;
  62. ```

API


ArcherContainer


Props


NameTypeDescription
---
`strokeColor``string`A
`strokeWidth``number`A
`strokeDasharray``string`Adds
`noCurves``boolean`Set
`lineStyle``string`Can
`offset``number`Optional
`svgContainerStyle``Style`Style
`children``React.Node`
`endShape``Object`An
`startMarker``boolean`Optional
`endMarker``boolean`Optional

Instance methods


If you access to the ref of your ArcherContainer, you will access the refreshScreen method.
This will allow you to have more control on when you want to re-draw the arrows.

Troubleshooting


My arrows don't re-render correctly...


Try using the refreshScreen instance method on your ArcherContainer element. You can access it through the ref of the component.

Call refreshScreen when the event that you need is triggered (onScroll etc.).