Skip to content

Commit b489a52

Browse files
committed
feat: allow to set className for BlockList node. Allow to reach instance of react layer via react ref.
1 parent 18af0e7 commit b489a52

File tree

11 files changed

+509
-20
lines changed

11 files changed

+509
-20
lines changed

docs/react/usage.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,20 @@ import { GraphCanvas } from "@gravity-ui/graph/react";
3434
graph={graph}
3535
renderBlock={renderBlock}
3636
className="my-graph"
37+
blockListClassName="custom-blocks-container"
38+
reactLayerRef={reactLayerRef}
3739
/>
3840
```
3941

42+
#### Props
43+
44+
- **`graph`** (required): The Graph instance to render
45+
- **`renderBlock`** (optional): Function to render custom block components
46+
- **`className`** (optional): CSS class for the main container
47+
- **`blockListClassName`** (optional): CSS class applied to the blocks container layer
48+
- **`reactLayerRef`** (optional): Ref to access the ReactLayer instance directly
49+
- **Event callbacks**: All graph event callbacks can be passed as props
50+
4051
### GraphBlock
4152

4253
The `GraphBlock` component is a crucial wrapper that handles the complex interaction between HTML elements and the canvas layer. It's responsible for:

src/components/canvas/layers/graphLayer/GraphLayer.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { effect } from "@preact/signals-core";
2-
31
import { Graph } from "../../../../graph";
42
import { GraphMouseEventNames, isNativeGraphEventName } from "../../../../graphEvents";
53
import { Component } from "../../../../lib/Component";

src/react-components/BlocksList.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { Graph, GraphState } from "../graph";
77
import { ESchedulerPriority } from "../lib";
88
import { ECameraScaleLevel, TCameraState } from "../services/camera/CameraService";
99
import { BlockState } from "../store/block/Block";
10-
import { debounce, throttle } from "../utils/functions";
10+
import { debounce } from "../utils/functions";
1111

1212
import { useSignal } from "./hooks";
1313
import { useGraphEvent } from "./hooks/useGraphEvents";

src/react-components/GraphCanvas.tsx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,21 @@ import { useFn } from "./utils/hooks/useFn";
1414
export type GraphProps = Pick<Partial<TBlockListProps>, "renderBlock"> &
1515
Partial<TGraphEventCallbacks> & {
1616
className?: string;
17+
blockListClassName?: string;
1718
graph: Graph;
19+
reactLayerRef?: React.MutableRefObject<ReactLayer | null>;
1820
};
1921

20-
export function GraphCanvas({ graph, className, renderBlock, ...cbs }: GraphProps) {
22+
export function GraphCanvas({ graph, className, blockListClassName, renderBlock, reactLayerRef, ...cbs }: GraphProps) {
2123
const containerRef = useRef<HTMLDivElement>();
2224

23-
const reactLayer = useLayer(graph, ReactLayer, {});
25+
const reactLayer = useLayer(graph, ReactLayer, {
26+
blockListClassName,
27+
});
28+
29+
if (reactLayerRef) {
30+
reactLayerRef.current = reactLayer;
31+
}
2432

2533
useEffect(() => {
2634
if (containerRef.current) {

src/react-components/hooks/useLayer.test.ts

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -127,20 +127,6 @@ describe("useLayer hook", () => {
127127
});
128128

129129
describe("Props management", () => {
130-
it("should call setProps on initial render", () => {
131-
// Setup
132-
const props = createValidLayerProps();
133-
134-
// Execute
135-
const { result } = renderHook(() => useLayer(graph, TestLayer, props));
136-
137-
// Get the layer instance
138-
const layer = result.current;
139-
140-
// Verify that setProps was called on the created layer
141-
expect(layer.setProps).toHaveBeenCalledWith(props);
142-
});
143-
144130
it("should pass initial props to layer via addLayer", () => {
145131
// Setup
146132
const initialProps = createValidLayerProps();

src/react-components/hooks/useLayer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useEffect, useLayoutEffect, useMemo, useRef, useState } from "react";
1+
import { useEffect, useState } from "react";
22

33
import isEqual from "lodash/isEqual";
44

0 commit comments

Comments
 (0)