react-xarrows icon indicating copy to clipboard operation
react-xarrows copied to clipboard

.getBoundingClientRect() of arrow

Open pmorim opened this issue 4 years ago • 10 comments

Is your feature request related to a problem? Please describe. I'm trying to get the .getBoundingClientRect() of the <XArrow /> component so that I can get its width, height, etc... The problem is that there is no way - that I know of - to get a ref of the Arrow.

Describe the solution you'd like ~~There should be a way to add a ref to the <XArrow /> component~~ ~~This can be achieved by defining the <XArrow /> component with forwardRef.~~

Edit: If you pass a ref to SVGcanvasProps or arrowBodyProps you get an error on the console.

Describe alternatives you've considered I tried applying the ref to a wrapping div (see code below) as a work-around, but it did not work.

const App = () => {
  const ref = useRef()
  // ...

  return (
    <div ref={ref}>
      <XArrow start={start} end={end} />
    </div>
  )
}

pmorim avatar Aug 07 '21 18:08 pmorim

You don't want to forward ref, you probably want to pass ref to the div container. Go ahead and try passing ref to divContainerProps. Please read advanced customization

Eliav2 avatar Aug 08 '21 05:08 Eliav2

@Eliav2, I already had tried doing that, but I always got an error on the console:

Uncaught TypeError: Cannot read property 'getBoundingClientRect' of null The above error occurred in the <Xarrow> component

Besides that, the rect would come with { ..., width: 0, height: 0 }.

Here is a CodeSandbox that shows what is happening: https://codesandbox.io/s/react-xarrows-bug-arrow-ref-z1tkr?file=/src/Connection.jsx.

Edit: I noticed that the error does not show because I call .getBoundingClientRect()... It happens just because I am passing a ref to divContainerProps or arrowBodyProps.

pmorim avatar Aug 09 '21 18:08 pmorim

To get the size of the arrow you should pass ref to svgprops Does you solved the issue?

Eliav2 avatar Aug 20 '21 15:08 Eliav2

Sorry for the long wait...

There isn't any prop named svgprops on XArrow. Could you provide the fix in a fork of the CodeSandbox that I specified above?

pmorim avatar Oct 19 '21 18:10 pmorim

SVGcanvasProps

Eliav2 avatar Oct 19 '21 19:10 Eliav2

In the CodeSandbox that I shared, I had already tried using that image

pmorim avatar Oct 19 '21 19:10 pmorim

oh yeah.. you can't pass this prop because you overwrite 'ref' which is used internally by react-xarrow ... this means that in the current version you can not access the ref. there might be a way around it and I will think about it for next release.

Eliav2 avatar Oct 19 '21 20:10 Eliav2

@Eliav2 is there any update you can give on the progress of v3? I'm also wanting to access the underlying <path> via a ref, as I'm needing to animate an element along the path that react-xarrows generates (so I need to be able to access its d value).

joealden avatar May 06 '22 15:05 joealden

@joealden you can access all properties of the Xarrow-SVG by giving a ref to a parent div. Then you will just have to get the child of the div, and you can access all dimensions, path, etc.

LucaH612 avatar Jan 02 '23 12:01 LucaH612

@LucaH612 indeed - and this is how I worked around this, but I think it'd be better if this was exposed by react-xarrows, as it'd avoid some of the logic I need to have in an effect handler (as the path will change on container resize etc.), and I think it'd mean less type casting in TypeScript!

joealden avatar Jan 02 '23 12:01 joealden