Git Product home page Git Product logo

react-lineto's Introduction

react-lineto

Draw a line between two elements in React.

Build Status

Getting Started

yarn install
yarn run demo

Browse to localhost:4567.

Demo

Demo

Components

LineTo

Draw line between two DOM elements.

Example

import LineTo from 'react-lineto';

function render() {
    return (
        <div>
            <div className="A">Element A</div>
            <div className="B">Element B</div>
            <LineTo from="A" to="B" />
        </div>
    );
}

If using multiple instances of <LineTo /> inside separate components, you must provide a unique key for each of the container divs.

Properties

Name Type Description Example Values
borderColor string Border color #f00, red, etc.
borderStyle string Border style solid, dashed, etc.
borderWidth number Border width (px)
className string Desired CSS className for the rendered element
delay number or bool Force render after delay (ms) 0, 1, 100, true
fromAnchor string Anchor for starting point (Format: "x y") top right, bottom center, left, 100% 0
from* string CSS class name of the first element
toAnchor string Anchor for ending point (Format: "x y") top right, bottom center, left, 100% 0
to* string CSS class name of the second element
within string CSS class name of the desired container
zIndex number Z-index offset

* Required

SteppedLineTo

Draw stepped line between two DOM elements.

Example

import { SteppedLineTo } from 'react-lineto';

function render() {
    return (
        <div>
            <div className="A">Element A</div>
            <div className="B">Element B</div>
            <SteppedLineTo from="A" to="B" orientation="v" />
        </div>
    );
}

Properties

Name Type Description Example Values
borderColor string Border color #f00, red, etc.
borderStyle string Border style solid, dashed, etc.
borderWidth number Border width (px)
className string Desired CSS className for the rendered element
delay number or bool Force render after delay (ms) 0, 1, 100, true
fromAnchor string Anchor for starting point (Format: "x y") top right, bottom center, left, 100% 0
from* string CSS class name of the first element
orientation enum "h" for horizonal, "v" for vertical h or v
toAnchor string Anchor for ending point (Format: "x y") top right, bottom center, left, 100% 0
to* string CSS class name of the second element
within string CSS class name of the desired container
zIndex number Z-index offset

* Required

Line

Draw line using pixel coordinates (relative to viewport).

Example

import { Line } from 'react-lineto';

function render() {
    return (
        <Line x0={0} y0={0} x1={10} y1={10} />
    );
}

Properties

Name Type Description Example Values
borderColor string Border color #f00, red, etc.
borderStyle string Border style solid, dashed, etc.
borderWidth number Border width (px)
className string Desired CSS className for the rendered element
within string CSS class name of the desired container
x0* number First X coordinate
x1* number Second X coordinate
y0* number First Y coordinate
y1* number Second Y coordinate
zIndex number Z-index offset

* Required

Release Checklist

  1. Bump version in package.json
  2. Update CHANGELOG.md
  3. Run yarn build or ./scripts/update
  4. Create version commit (ex. "2.0.0")
  5. Create matching tag (ex. "2.0.0")
  6. Push master branch and tags to origin
  7. Verify Travis CI published NPM package

react-lineto's People

Contributors

andremerdan avatar bkw avatar carlopascual avatar carronmedia avatar dependabot[bot] avatar dongyuwei avatar kdeloach avatar ngs avatar robertgr991 avatar timcritt avatar vpzomtrrfrt avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

react-lineto's Issues

fix subpixel render issues

hi @kdeloach, @jazzyarchitects
First I want to say thanks for the great Stepped line component!

In my project, I fixed some subpixel render issues of SteppedLine, if you are happy to accept a PR , I can send a PR to fix them.

The following is some snapshots of the issues :
Screen Shot 2021-09-15 at 1 03 24 PM


Screen Shot 2021-09-15 at 1 05 30 PM

And in the original PR of #2 we can also see similar issue, like this:
Screen Shot 2021-09-15 at 1 14 42 PM

I only patched SteppedLine's renderHorizontal method, since I only used it to render a horizontal tree nodes.

The patch is the following :

renderHorizontal() {
    // begin patch
    const x0 = Math.round(this.props.x0);
    const y0 = Math.round(this.props.y0);
    const x1 = Math.round(this.props.x1);
    const y1 = Math.round(this.props.y1);

    const dy = y1 - y0;

    if (Math.abs(dy) <= 1) { // one key point 
      return <Line {...this.props} {...{ x0, y0, x1, y1: y0 }} />;
    }

    if (dy === 0) {
      return <Line {...this.props} />;
    }

    const borderWidth = this.props.borderWidth || defaultBorderWidth;
    const x2 = Math.round((x0 + x1) / 2);
    // end patch

    const yOffset = dy < 0 ? borderWidth : 0;
    const minY = Math.min(y0, y1) - yOffset;
    const maxY = Math.max(y0, y1);

    return (
      <div className="react-steppedlineto">
        <Line {...this.props} x0={x0} y0={y0} x1={x2} y1={y0} />
        <Line {...this.props} x0={x1} y0={y1} x1={x2} y1={y1} />
        <Line {...this.props} x0={x2} y0={minY} x1={x2} y1={maxY} />
      </div>
    );
  }

The key point is use Math.round to turn some subpixel(eg 100.125px) to integer number. I tested the patches on iPhone 12 pro max, it works as expected.

You may ask why we have subpixel render issues? The root cause is we use rem to do some responsive design. Eg:

html {
  /* the standard ux design is 812px width, the conversion between rem and px  is :1rem === 100px */
  font-size: calc(100 / 812 * 100vw);
}

So the UI in standard ux design is all good, but when switched to a different device size, the subpixel render issues will bite us.

Could not find a declaration file for module 'react-lineto'.

Hi,

I am trying to use this package in my ReactJS application. I took the following steps to install the package:

  1. Added "react-lineto": "3.1.3" to my package.json file
  2. Ran "yarn install"
  3. Added some code using the SteppedLineTo feature from the library.

No lines appear on my page.

My IDE highlights the import statement (import {SteppedLineTo} from "react-lineto";) with the following message:
Could not find a declaration file for module 'react-lineto'. '/Users/bob/Sites/audience-builder-frontend/node_modules/react-lineto/dist/react-lineto.js' implicitly has an 'any' type.
Try npm install @types/react-lineto if it exists or add a new declaration (.d.ts) file containing declare module 'react-lineto';

Lines only appear if in DOM on initial load, will not appear if added to DOM later

Example: https://codesandbox.io/s/heuristic-hooks-nhiic?file=/src/App.js

I have some points and I want to hide the line and the points initially when the page loads.
However, once the user does some action (in this simplified example, when they click the "Hello" button) then the line should appear.

However, only the dots appear (the ones that the line is supposed to connect). The line itself does not appear unless I cause some other state change to occur, like if I drag the dot so that the line moves.

Is this an issue with my configuration or is there a way around this issue?

Showing the line all the time https://codesandbox.io/s/stoic-smoke-w3zk6?file=/src/App.js works if the component is rendered from the beginning

Showing the line only after some state is updated does not show line unless I force another state update:
https://codesandbox.io/s/heuristic-hooks-nhiic?file=/src/App.js (click "Hello" - only dots appear, but not line - it doesn't seem to even be appearing in the DOM)

Remove lifecycle warnings (componentWillMount, componentDidMount)

Warning: componentWillMount has been renamed, and is not recommended for use. See https://fb.me/react-unsafe-component-lifecycles for details

It's the usual warning for lifecycle methods either needing to be renamed to UNSAFE_* or to use equivalent hooks. Cheers!

Forward props to underlying div element

I'm wanting to test that a line is drawn under a given scenario with React Testing Library and it seems "data-testid" is the only attribute I could use to select it. However, passing one to the <LineTo /> component doesn't seem to make it to the DOM.

It would be nice if we could forward and spread any additional props passed in to the underlying <div /> element that is the line.

Vertically oriented SteppedLineTo does not align correctly

Hi.

I'm building a folder structure using SteppedLineTo, and see that when I use orientation="v" the lines connected to the children (i.e. sub-folders) does not actually align with the children. Sample code:

return React.Children.map(children, (child, child_index) => {
      return (
        <SteppedLineTo
          from={parentClassName}
          to={child.props.className}
          delay={2000}
          fromAnchor="bottom right"
          toAnchor="center left"
          orientation="v"
        />
      )

However, if I run the exact same code using orientation="h" things align perfectly and the line goes from the parent to it's children as expected.
In case it's relevant, the parent is connecting to children inside a collapse component that is default expanded.

Are there any known issues with the vertical orientation that may explain this sort of issue?

zIndex and within not properly working

good day!

I have a react-leaflet-map and a react-bootstrap-table2 inside rc-tabs component. While I hover on the row in the table I want to draw a line from some point in the row to the point on map. That works fine BUT if map is zoomed too close and the point became to be in non-visible area the line goes through components and it looks bad. I tried to set zIndex of the line by 10 and other components by 11 but with no luck, it seems it doesn't affect. I also tried to use within: "class-name" but also with no luck, the behavior is strange and line hides somewhere.

Could you please provide
example
some tips on this?

Inverse

Is it possible to draw the whole tree inverted? From the right (the 0) to the left (all the childs). @kdeloach

LineTo only drawing a horizontal line

When I place LineTo into my React app and set the to and from it will only render a horizontal line rather than rendering it from a specified location to a specified location. In addition, I am unable to change the color of the line by saying borderColor="red" inside the LineTo tag. Code below

div className="a"> <p className="date">{x.date}</p> </div> <LineTo delay={0} from="a" to="b" fromAnchor="bottom center" toAnchor="center" borderColor="red" orientation="v" /> <div className="b"> <p className="time"> <span id="time" style={{ marginRight: "0.75rem" }}> {x.time} </span> </p> </div>

'react-lineto-placeholder' divs only, no visible lines

image

I'm seeing the divs created with the react-lineto-placeholder class, but there's no visible lines. Admittedly this is in a larger application, but there's nothing to crazy going on.

Any ideas why I don't see the lines? Using version 3.0.0.

Arrow

I've been playing around with react-lineto and I really enjoy working with it. Thanks for putting this together!

Is there a way to add an arrow to one or both ends of the line?

Change the break point in SteppedLine

There is a const x2 = (x0 + x1) / 2; and a const y2 = (y0 + y1) / 2;.
Maybe this / 2 could be an parameter (defaults to 0.5) so the user could change this to avoid overlaping with some other elements in screen.

Something like this:

const defaultPercentBreakPoint = 0.5;

...

 renderVertical() {
...
const y2 = (y0 + y1) * (percentBreak ?? this.defaultPercentBreakPoint);
...
}

renderHorizontal(){
...
const x2 = (x0 + x1) * (percentBreak ?? this.defaultPercentBreakPoint);
...
}

SteppedLine.propTypes = Object.assign({}, {
    x0: PropTypes.number.isRequired,
    y0: PropTypes.number.isRequired,
    x1: PropTypes.number.isRequired,
    y1: PropTypes.number.isRequired,
    orientation: PropTypes.oneOf(['h', 'v']),
   percentBreak: PropTypes.number,
}, optionalStyleProps);

Drawn lines not displaying correctly

@kdeloach

Hello, I'm using react-lineto in my app to draw lines between elements, but when a page is rendered for the first time, lines are not displaying correctly, for example, should be a vertical line between B&C, but when the page loads for the first time, this line doesn't go from "B" to "C", it goes way up from "C" and displays correctly only if I refresh the page or change screen size.
Could you please provide any tips on how to fix this?

Thank you.

Curved Lines

Is this library only for straight line segments or can you draw bezier or splines?

'LineTo' cannot be used as a JSX component.

Hello there, I'm encountering the following error when trying to use the component within my .tsx file:

'LineTo' cannot be used as a JSX component.
  Its instance type 'LineTo<LineToProps>' is not a valid JSX element.
    The types returned by 'render()' are incompatible between these types.
      Type 'React.ReactNode' is not assignable to type 'import("/myLocalPath").ReactNode'.
        Type '{}' is not assignable to type 'ReactNode'.

React v 17.0.2, Typescript 4.4.3

Lines in fixed scrolling container

This might be out of support request, so you can close it, if so. I have scrolling container and lines goes out of bounds. Because it's static, and not dynamic draw, but it could be static within container, right? Is it possible to solve it? Thanks.

Screen Shot 2021-03-23 at 2 08 13 PM

Not working in render

Hello, I am using react-lineto in my app, but when my component is rendered the first time it is not working, until I make an event and it is rendered again the connection I want to use appears.

Also when I add a new line it appears scattered, then when it returns to render the component takes its proper place.

`deferUpdate` calls `forceUpdate` even after unmount

If any of the components exported by react-lineto have a delay set, the component may schedule a call to forceUpdate after the component is unmounted:

// Forced update after delay (MS)
deferUpdate(delay) {
clearTimeout(this.t);
this.t = setTimeout(() => this.forceUpdate(), delay);
}

Fix is trivial and consists into clearing the timeout in componentWillUnmount(). I'll propose a fix shortly.

z-index with CSS/3D transform

The z-index works great with flat HTML, but if something like this: transform: rotateX(60deg) rotateZ(-45deg) scale(1.2); is applied, the z-index seems to not work. Not sure if this is an issue or if there's a recommended approach.

Thanks in advance.

Drawn lines not dynamic?

I'm using a couple lines similar to this
<LineTo from="img" to="img2" borderColor="white" borderWidth={4} />
When rendered initially, the lines show up great, but if I move the images around say with animation or just by moving their relative position on screen by changing the size of the window, the lines stay where they were when first rendered, rather than following the images around. Am I doin' something wrong?

Adding an arbitrary label or component alongside the line

I'm loving the package! It's been extremely useful. I was wondering if it was possible to render some text alongside/inside the lines. So that'd it looks roughly like this:

image

I think I know how to customize the line so that there's spacing within but not sure how to render something inside of it.
Thanks!

DOMException: Failed to execute 'removeChild' on 'Node': The node to be removed is not a child of this node.

Love the component by the way. It was exactly what I was looking for. I had a problem that I managed to solve, so thought I'd write a quick note here.

I was getting this error while using the <LineTo /> component. Here is the setup that produced it and how I fixed it.

I have two pages that make use of five <LineTo /> components, each one inside an instance of another component, and with that component inside a div with the same class name on both pages. This is how it's structured. <MyFirstPage> is never rendered at the same time as <MySecondPage>.

<MyFirstPage>
    <div className={"my-class"}>
//some other stuff here not found in second page
        <MyLineContainingComponent>
            <LineTo/>
            <LineTo/>
            <LineTo/>
            <LineTo/>
            <LineTo/>
          <MyLineContainingComponent />
     <div>
<MyFirstPage />

<MySecondPage>
    <div className={"my-class"}>
//some other stuff here not found in first page
        <MyLineContainingComponent>
            <LineTo/>
            <LineTo/>
            <LineTo/>
            <LineTo/>
            <LineTo/>
          <MyLineContainingComponent />
     <div>
<MySecondPage />

When I navigate to MyFirstPage, the lines display correctly. When I navigate directly to the second, the page renders but without the lines with no errors in the console. If I then navigate back to the first page directly from the second, I get the following error.

"DOMException: Failed to execute 'removeChild' on 'Node': The node to be removed is not a child of this node."

The error is produced on line 378 of react-lineto.js when this.within.removeChild(this.el) is called inside componentWillUnmount() when the second page unmounts.

Providing unique class names to the container divs doesn't solve the issue.

I fixed it by wrapping all the LineTo components in a div and providing the div with a key that is unique to each instance.

This isn't a problem unique to react-lineto, I think it's a problem that generally occurs when you manipulate the DOM directly instead of relying on React to do it. It took me a while to work out why this was happening, so I'm posting here in case someone has the same issue. Perhaps a warning about providing unique keys to the div that contains the <LineTo /> component could be added to the readme.

Live demo?

Any plans for a live demo? Github actions + pages would work great with that probably.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.