0.3.0 • Published 23 days ago

baana-react v0.3.0

Weekly downloads
-
License
MIT
Repository
github
Last release
23 days ago

baana-react

baana-react demonstration picture

  • Zero-dependencies
  • 12 kB bundle size

Usage

const Diagram = () => {
    return (
        <>
            <div id="block1"></div>
            <div id="block2"></div>
            <Arrow start="block1" end="block2"/>
        </>
    );
}

const App = () => {
    return (
        <LineContextProvider>
            <Diagram />
        </LineContextProvider>
    )
}

API

Arrow PropertyTypeProperty in LineContextProvider
startReact.RefObject<HTMLElement> \| string (block id)-
endReact.RefObject<HTMLElement> \| string (block id)-
colorstringcolor
scalenumberscale
curvinessnumbercurviness
strokeWidthnumberstrokeWidth
onlyIntegerCoordsbooleanonlyIntegerCoords
useRegisterbooleanuseRegister
withHeadbooleanwithHead
headSizenumberheadSize
headColorstringheadColor
classNamestringarrowClassName
onClickMouseEventHandler-
onHoverMouseEventHandler-
labelJSX.Element-
MarkerMarkerPropsType-
offsetStartXnumberoffsetStartX
offsetStartYnumberoffsetStartY
offsetEndXnumberoffsetEndX
offsetEndYnumberoffsetEndY
type MarkerPropsType = {
    id: string;
    size?: number;
    color?: string;
};

LineContextProvider allows to set some properties for all arrows in it at once.

Optimization

Hook useReducedGraphics

  • It is needed to increase FPS due to a temporary decrease in rendering quality
  • Default delay: 400 ms
  • Based on shape-rendering: optimizeSpeed [MDN]
const { reducedClassName, reduceSVG } = useReducedGraphics();

const someAction = () => {
    ...
    reduceSVG();
}

return (
    <LineContextProvider arrowClassName={reducedClassName}>
        ... // arrows and other content
    </LineContextProvider>
);

Only integer coords

Some operations like scaling or moving whole space can cause a small recalculation of the coordinates of the arrows with an accuracy of up to one thousandth. And onlyIntegerCoords can reduce the load. But at high magnification, this can cause unpleasant twitching of the lines.

Update only changed arrows

This feature is hidden behind the property useRegister, as it requires a little extra memory.

After enabling, you can pass the HTML element corresponding to the start or end of the arrow to the update function.

Made with react-draggable.

const Diagram = () => {
    const { update } = useLineContext();

    const handleUpdate = (mouseEvent, dragEvent) => {
        update(dragEvent.node);
    };

    return (
        <>
            <Draggable
                onDrag={handleUpdate}
                onStart={handleUpdate}
                onStop={handleUpdate}
            >
                <div id="block1"></div>
            </Draggable>

            <Draggable
                onDrag={handleUpdate}
                onStart={handleUpdate}
                onStop={handleUpdate}
            >
                <div id="block2"></div>
            </Draggable>

            <Arrow start="block1" end="block2"/>
        </>
    );
}

const App = () => {
    return (
        <LineContextProvider>
            <Diagram />
        </LineContextProvider>
    )
}
0.3.0

23 days ago

0.2.2

30 days ago

0.2.1

1 month ago

0.2.0

3 months ago

0.0.16

4 months ago

0.0.14

5 months ago

0.0.15

5 months ago

0.0.13

5 months ago

0.0.12

6 months ago

0.0.11

6 months ago

0.0.10

6 months ago

0.0.10-0

6 months ago

0.0.9-0

6 months ago

0.0.8

6 months ago

0.0.8-0

6 months ago

0.0.6

7 months ago

0.0.5

7 months ago

0.0.4

7 months ago

0.0.2

7 months ago

0.0.1

7 months ago