@dxos/react-ui-graph 0.8.4-main.84f28bd → 0.8.4-main.937b3ca

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (104) hide show
  1. package/dist/lib/browser/chunk-LKC455GK.mjs +52 -0
  2. package/dist/lib/browser/chunk-LKC455GK.mjs.map +7 -0
  3. package/dist/lib/browser/index.mjs +1095 -244
  4. package/dist/lib/browser/index.mjs.map +4 -4
  5. package/dist/lib/browser/meta.json +1 -1
  6. package/dist/lib/browser/testing/index.mjs +3 -3
  7. package/dist/lib/browser/testing/index.mjs.map +3 -3
  8. package/dist/lib/node-esm/chunk-JE4KZCUF.mjs +54 -0
  9. package/dist/lib/node-esm/chunk-JE4KZCUF.mjs.map +7 -0
  10. package/dist/lib/node-esm/index.mjs +1095 -244
  11. package/dist/lib/node-esm/index.mjs.map +4 -4
  12. package/dist/lib/node-esm/meta.json +1 -1
  13. package/dist/lib/node-esm/testing/index.mjs +3 -3
  14. package/dist/lib/node-esm/testing/index.mjs.map +3 -3
  15. package/dist/types/src/components/Graph/Graph.d.ts +4 -5
  16. package/dist/types/src/components/Graph/Graph.d.ts.map +1 -1
  17. package/dist/types/src/components/Graph/Graph.stories.d.ts +16 -7
  18. package/dist/types/src/components/Graph/Graph.stories.d.ts.map +1 -1
  19. package/dist/types/src/components/Mesh/Mesh.stories.d.ts +9 -5
  20. package/dist/types/src/components/Mesh/Mesh.stories.d.ts.map +1 -1
  21. package/dist/types/src/components/SVG/Grid.d.ts.map +1 -1
  22. package/dist/types/src/components/SVG/Root.d.ts +1 -1
  23. package/dist/types/src/components/SVG/Root.d.ts.map +1 -1
  24. package/dist/types/src/components/SVG/SVG.d.ts +14 -17
  25. package/dist/types/src/components/SVG/SVG.d.ts.map +1 -1
  26. package/dist/types/src/components/SVG/Svg.stories.d.ts +10 -8
  27. package/dist/types/src/components/SVG/Svg.stories.d.ts.map +1 -1
  28. package/dist/types/src/components/SVG/Zoom.d.ts.map +1 -1
  29. package/dist/types/src/fx/fx.stories.d.ts +16 -15
  30. package/dist/types/src/fx/fx.stories.d.ts.map +1 -1
  31. package/dist/types/src/graph/projector/graph-drag.d.ts +1 -1
  32. package/dist/types/src/graph/projector/graph-drag.d.ts.map +1 -1
  33. package/dist/types/src/graph/projector/graph-force-projector.d.ts +2 -2
  34. package/dist/types/src/graph/projector/graph-force-projector.d.ts.map +1 -1
  35. package/dist/types/src/graph/projector/graph-forces.d.ts.map +1 -1
  36. package/dist/types/src/graph/projector/graph-hierarchical-projector.d.ts +2 -2
  37. package/dist/types/src/graph/projector/graph-hierarchical-projector.d.ts.map +1 -1
  38. package/dist/types/src/graph/projector/graph-projector.d.ts +4 -4
  39. package/dist/types/src/graph/projector/graph-projector.d.ts.map +1 -1
  40. package/dist/types/src/graph/projector/graph-radial-projector.d.ts +2 -2
  41. package/dist/types/src/graph/projector/graph-radial-projector.d.ts.map +1 -1
  42. package/dist/types/src/graph/projector/graph-relational-projector.d.ts +2 -2
  43. package/dist/types/src/graph/projector/graph-relational-projector.d.ts.map +1 -1
  44. package/dist/types/src/graph/projector/projector.d.ts.map +1 -1
  45. package/dist/types/src/graph/renderer/bullets.d.ts.map +1 -1
  46. package/dist/types/src/graph/renderer/graph-renderer.d.ts +1 -1
  47. package/dist/types/src/graph/renderer/graph-renderer.d.ts.map +1 -1
  48. package/dist/types/src/graph/renderer/renderer.d.ts.map +1 -1
  49. package/dist/types/src/graph/types.d.ts +1 -1
  50. package/dist/types/src/graph/types.d.ts.map +1 -1
  51. package/dist/types/src/hooks/useZoom.d.ts.map +1 -1
  52. package/dist/types/src/stories/hooks.stories.d.ts +13 -8
  53. package/dist/types/src/stories/hooks.stories.d.ts.map +1 -1
  54. package/dist/types/src/testing/data.d.ts +4 -4
  55. package/dist/types/src/testing/data.d.ts.map +1 -1
  56. package/dist/types/src/testing/hooks.d.ts +2 -16
  57. package/dist/types/src/testing/hooks.d.ts.map +1 -1
  58. package/dist/types/src/testing/model.d.ts +4 -8
  59. package/dist/types/src/testing/model.d.ts.map +1 -1
  60. package/dist/types/src/util/index.d.ts +2 -2
  61. package/dist/types/src/util/index.d.ts.map +1 -1
  62. package/dist/types/src/util/screen.d.ts +1 -1
  63. package/dist/types/src/util/screen.d.ts.map +1 -1
  64. package/dist/types/src/util/vector.d.ts +1 -1
  65. package/dist/types/src/util/vector.d.ts.map +1 -1
  66. package/dist/types/tsconfig.tsbuildinfo +1 -1
  67. package/package.json +31 -26
  68. package/src/components/Graph/Graph.stories.tsx +58 -56
  69. package/src/components/Graph/Graph.tsx +19 -12
  70. package/src/components/Mesh/Mesh.stories.tsx +11 -9
  71. package/src/components/Mesh/Mesh.tsx +1 -1
  72. package/src/components/SVG/Grid.tsx +2 -2
  73. package/src/components/SVG/Markers.tsx +1 -1
  74. package/src/components/SVG/Root.tsx +2 -2
  75. package/src/components/SVG/SVG.tsx +14 -4
  76. package/src/components/SVG/Svg.stories.tsx +23 -18
  77. package/src/components/SVG/Zoom.tsx +1 -1
  78. package/src/fx/fx.stories.tsx +15 -11
  79. package/src/graph/projector/graph-drag.ts +2 -1
  80. package/src/graph/projector/graph-force-projector.ts +4 -3
  81. package/src/graph/projector/graph-forces.ts +1 -1
  82. package/src/graph/projector/graph-hierarchical-projector.ts +5 -4
  83. package/src/graph/projector/graph-projector.ts +6 -5
  84. package/src/graph/projector/graph-radial-projector.ts +4 -3
  85. package/src/graph/projector/graph-relational-projector.ts +5 -4
  86. package/src/graph/projector/projector.ts +1 -1
  87. package/src/graph/renderer/bullets.ts +1 -1
  88. package/src/graph/renderer/graph-renderer.ts +4 -3
  89. package/src/graph/renderer/renderer.ts +1 -1
  90. package/src/graph/types.ts +1 -1
  91. package/src/hooks/useStateRef.ts +1 -1
  92. package/src/hooks/useZoom.ts +1 -1
  93. package/src/stories/hooks.stories.tsx +42 -35
  94. package/src/testing/data.ts +5 -5
  95. package/src/testing/hooks.ts +4 -3
  96. package/src/testing/model.ts +3 -3
  97. package/src/util/index.ts +2 -2
  98. package/src/util/screen.test.ts +1 -1
  99. package/src/util/screen.ts +1 -1
  100. package/src/util/vector.ts +1 -1
  101. package/dist/lib/browser/chunk-4C5Z5DAE.mjs +0 -979
  102. package/dist/lib/browser/chunk-4C5Z5DAE.mjs.map +0 -7
  103. package/dist/lib/node-esm/chunk-AK6HNA4R.mjs +0 -981
  104. package/dist/lib/node-esm/chunk-AK6HNA4R.mjs.map +0 -7
@@ -2,19 +2,18 @@
2
2
  // Copyright 2025 DXOS.org
3
3
  //
4
4
 
5
- import '@dxos-theme';
6
-
7
- import { type Meta } from '@storybook/react-vite';
5
+ import { type Meta, type StoryObj } from '@storybook/react-vite';
8
6
  import { select } from 'd3';
9
7
  import React, { type FC, useEffect, useMemo, useRef } from 'react';
10
8
 
11
- import { withLayout, withTheme } from '@dxos/storybook-utils';
9
+ import { withTheme } from '@dxos/react-ui/testing';
12
10
 
13
- import { Pulsar } from './pulsar';
14
11
  import { SVG } from '../components';
15
12
  import { useGrid, useZoom } from '../hooks';
16
13
  import { type D3Callable } from '../util';
17
14
 
15
+ import { Pulsar } from './pulsar';
16
+
18
17
  import '../../styles/graph.css';
19
18
 
20
19
  type Datum = {
@@ -53,7 +52,7 @@ const StoryComponent: FC<StoryProps> = ({ count = 1, ...options }) => {
53
52
  const grid = useGrid({ axis: true, grid: false });
54
53
  const zoom = useZoom();
55
54
 
56
- const root = useRef<SVGGElement>();
55
+ const root = useRef<SVGGElement>(null);
57
56
  useEffect(() => {
58
57
  select(root.current)
59
58
  .selectAll('g')
@@ -92,21 +91,26 @@ const createNode: D3Callable<SVGGElement, Datum> = (group, classNames, options)
92
91
  });
93
92
  };
94
93
 
95
- const meta: Meta = {
94
+ const meta = {
96
95
  title: 'ui/react-ui-graph/fx',
97
96
  render: DefaultStory,
98
- decorators: [withTheme, withLayout({ fullscreen: true })],
99
- };
97
+ decorators: [withTheme],
98
+ parameters: {
99
+ layout: 'fullscreen',
100
+ },
101
+ } satisfies Meta<typeof DefaultStory>;
100
102
 
101
103
  export default meta;
102
104
 
103
- export const DefaultPulse = {
105
+ type Story = StoryObj<typeof meta>;
106
+
107
+ export const DefaultPulse: Story = {
104
108
  args: {
105
109
  count: 20,
106
110
  },
107
111
  };
108
112
 
109
- export const RapidPulse = {
113
+ export const RapidPulse: Story = {
110
114
  args: {
111
115
  count: 20,
112
116
  duration: 1_000,
@@ -4,11 +4,12 @@
4
4
 
5
5
  import { drag, pointer, select } from 'd3';
6
6
 
7
- import { type GraphProjector } from './graph-projector';
8
7
  import { type SVGContext } from '../../hooks';
9
8
  import { type Point } from '../../util';
10
9
  import { type GraphLayoutNode } from '../types';
11
10
 
11
+ import { type GraphProjector } from './graph-projector';
12
+
12
13
  enum Mode {
13
14
  MOVE = 0,
14
15
  LINK = 1,
@@ -17,9 +17,10 @@ import {
17
17
  import { type Graph } from '@dxos/graph';
18
18
  import { log } from '@dxos/log';
19
19
 
20
+ import { type GraphLayoutEdge, type GraphLayoutNode } from '../types';
21
+
20
22
  import { forcePoint } from './graph-forces';
21
23
  import { GraphProjector, type GraphProjectorOptions } from './graph-projector';
22
- import { type GraphLayoutEdge, type GraphLayoutNode } from '../types';
23
24
 
24
25
  /**
25
26
  * Return value or invoke function.
@@ -50,7 +51,7 @@ const maybeForce = <Config>(
50
51
  cb: (config: Config) => Force<any, any>,
51
52
  defaultValue = undefined,
52
53
  ) => {
53
- const value = typeof options === 'boolean' ? (options ? {} : undefined) : options ?? defaultValue;
54
+ const value = typeof options === 'boolean' ? (options ? {} : undefined) : (options ?? defaultValue);
54
55
  if (value) {
55
56
  return cb(value);
56
57
  }
@@ -222,7 +223,7 @@ export class GraphForceProjector<NodeData = any> extends GraphProjector<NodeData
222
223
  this.restart();
223
224
  }
224
225
 
225
- override onUpdate(graph?: Graph): void {
226
+ override onUpdate(graph?: Graph.Any): void {
226
227
  log('onUpdate', { graph: { nodes: graph?.nodes.length, edges: graph?.edges.length } });
227
228
  this._simulation.stop();
228
229
  this.mergeData(graph);
@@ -2,7 +2,7 @@
2
2
  // Copyright 2025 DXOS.org
3
3
  //
4
4
 
5
- import { type Force, forceX, forceY, type SimulationLinkDatum, type SimulationNodeDatum } from 'd3';
5
+ import { type Force, type SimulationLinkDatum, type SimulationNodeDatum, forceX, forceY } from 'd3';
6
6
 
7
7
  export type ForcePointOptions = {
8
8
  x?: number;
@@ -5,9 +5,10 @@
5
5
  import { type Graph } from '@dxos/graph';
6
6
  import { log } from '@dxos/log';
7
7
 
8
+ import { type GraphLayoutNode } from '../types';
9
+
8
10
  import { type GraphProjectorOptions } from './graph-projector';
9
11
  import { GraphRadialProjector, layoutRadial, updateNode } from './graph-radial-projector';
10
- import { type GraphLayoutNode } from '../types';
11
12
 
12
13
  export type GraphHierarchicalProjectorOptions = GraphProjectorOptions & {
13
14
  radius?: number;
@@ -19,14 +20,14 @@ export class GraphHierarchicalProjector<
19
20
  NodeData = any,
20
21
  Options extends GraphHierarchicalProjectorOptions = any,
21
22
  > extends GraphRadialProjector<NodeData, Options> {
22
- protected override onUpdate(graph?: Graph) {
23
+ protected override onUpdate(graph?: Graph.Any) {
23
24
  log('onUpdate', {
24
25
  graph: { nodes: graph?.nodes.length, edges: graph?.edges.length },
25
- selection: this.selection?.selected.value,
26
+ selection: this.selection?.getSelectedIds(),
26
27
  });
27
28
 
28
29
  this.mergeData(graph);
29
- const selected = this.selection?.selected.value?.[0];
30
+ const selected = this.selection?.getSelectedIds()[0];
30
31
  if (selected) {
31
32
  const node = this.layout.graph.nodes.find((node) => node.id === selected);
32
33
  if (node) {
@@ -2,11 +2,12 @@
2
2
  // Copyright 2025 DXOS.org
3
3
  //
4
4
 
5
- import { type SelectionModel, type Graph } from '@dxos/graph';
5
+ import { type Graph, type SelectionModel } from '@dxos/graph';
6
6
 
7
- import { Projector, type ProjectorOptions } from './projector';
8
7
  import { type SVGContext } from '../../hooks';
9
- import { emptyGraph, type GraphLayout, type GraphLayoutNode } from '../types';
8
+ import { type GraphLayout, type GraphLayoutNode, emptyGraph } from '../types';
9
+
10
+ import { Projector, type ProjectorOptions } from './projector';
10
11
 
11
12
  export type GraphProjectorOptions = ProjectorOptions & {};
12
13
 
@@ -14,7 +15,7 @@ export type GraphProjectorOptions = ProjectorOptions & {};
14
15
  * Base class for graph projectors.
15
16
  */
16
17
  export abstract class GraphProjector<NodeData = any, Options extends GraphProjectorOptions = any> extends Projector<
17
- Graph,
18
+ Graph.Any,
18
19
  GraphLayout<NodeData>,
19
20
  Options
20
21
  > {
@@ -59,7 +60,7 @@ export abstract class GraphProjector<NodeData = any, Options extends GraphProjec
59
60
  * Merge external data with internal representation (e.g., so force properties like position are preserved).
60
61
  * @param data
61
62
  */
62
- protected mergeData(data: Graph = emptyGraph) {
63
+ protected mergeData(data: Graph.Any = emptyGraph) {
63
64
  // Merge nodes.
64
65
  const nodes: GraphLayoutNode[] = data.nodes.map((node) => {
65
66
  let current: GraphLayoutNode = this._layout.graph.nodes.find((n) => n.id === this.options.idAccessor(node));
@@ -2,15 +2,16 @@
2
2
  // Copyright 2025 DXOS.org
3
3
  //
4
4
 
5
- import { easeCubic, timer, type Timer } from 'd3';
5
+ import { type Timer, easeCubic, timer } from 'd3';
6
6
 
7
7
  import { type Graph } from '@dxos/graph';
8
8
  import { log } from '@dxos/log';
9
9
 
10
- import { GraphProjector, type GraphProjectorOptions } from './graph-projector';
11
10
  import { type Point } from '../../util';
12
11
  import { type GraphLayoutNode } from '../types';
13
12
 
13
+ import { GraphProjector, type GraphProjectorOptions } from './graph-projector';
14
+
14
15
  export type GraphRadialProjectorOptions = GraphProjectorOptions & {
15
16
  radius?: number;
16
17
  margin?: number;
@@ -27,7 +28,7 @@ export class GraphRadialProjector<
27
28
  return undefined;
28
29
  }
29
30
 
30
- protected override onUpdate(graph?: Graph) {
31
+ protected override onUpdate(graph?: Graph.Any) {
31
32
  log('onUpdate', { graph: { nodes: graph?.nodes.length, edges: graph?.edges.length } });
32
33
  this.mergeData(graph);
33
34
  this.doRadialLayout();
@@ -5,9 +5,10 @@
5
5
  import { type Graph } from '@dxos/graph';
6
6
  import { log } from '@dxos/log';
7
7
 
8
+ import { type GraphLayoutNode } from '../types';
9
+
8
10
  import { type GraphProjectorOptions } from './graph-projector';
9
11
  import { GraphRadialProjector, layoutHorizontal, layoutRadial, updateNode } from './graph-radial-projector';
10
- import { type GraphLayoutNode } from '../types';
11
12
 
12
13
  export type GraphRelationalProjectorOptions = GraphProjectorOptions & {
13
14
  radius?: number;
@@ -19,14 +20,14 @@ export class GraphRelationalProjector<
19
20
  NodeData = any,
20
21
  Options extends GraphRelationalProjectorOptions = any,
21
22
  > extends GraphRadialProjector<NodeData, Options> {
22
- protected override onUpdate(graph?: Graph) {
23
+ protected override onUpdate(graph?: Graph.Any) {
23
24
  log('onUpdate', {
24
25
  graph: { nodes: graph?.nodes.length, edges: graph?.edges.length },
25
- selection: this.selection?.selected.value,
26
+ selection: this.selection?.getSelectedIds(),
26
27
  });
27
28
 
28
29
  this.mergeData(graph);
29
- const selected = this.selection?.selected.value?.[0];
30
+ const selected = this.selection?.getSelectedIds()[0];
30
31
  if (selected) {
31
32
  const node = this.layout.graph.nodes.find((node) => node.id === selected);
32
33
  if (node) {
@@ -4,7 +4,7 @@
4
4
 
5
5
  import { type SVGContext } from '../../hooks';
6
6
  import { EventEmitter } from '../../util';
7
- import { defaultIdAccessor, type IdAccessor } from '../types';
7
+ import { type IdAccessor, defaultIdAccessor } from '../types';
8
8
 
9
9
  export type ProjectorOptions = {
10
10
  idAccessor?: IdAccessor;
@@ -4,7 +4,7 @@
4
4
 
5
5
  import { easeLinear, interpolate, select } from 'd3';
6
6
 
7
- import { type D3Selection, type D3Callable } from '../../util';
7
+ import { type D3Callable, type D3Selection } from '../../util';
8
8
 
9
9
  export type BulletOptions = {
10
10
  max?: number;
@@ -2,15 +2,16 @@
2
2
  // Copyright 2021 DXOS.org
3
3
  //
4
4
 
5
- import { line, select, polygonHull, easeCubicOut } from 'd3';
5
+ import { easeCubicOut, line, polygonHull, select } from 'd3';
6
6
  import * as Clipper from 'js-clipper';
7
7
 
8
8
  import { log } from '@dxos/log';
9
9
 
10
+ import { type D3Callable, type D3Selection, type Point, getCircumferencePoints } from '../../util';
11
+ import { type GraphLayout, type GraphLayoutEdge, type GraphLayoutNode } from '../types';
12
+
10
13
  import { createBullets } from './bullets';
11
14
  import { Renderer, type RendererOptions } from './renderer';
12
- import { getCircumferencePoints, type D3Selection, type D3Callable, type Point } from '../../util';
13
- import { type GraphLayout, type GraphLayoutEdge, type GraphLayoutNode } from '../types';
14
15
 
15
16
  const createLine = line<Point>();
16
17
 
@@ -7,7 +7,7 @@ import { type RefObject } from 'react';
7
7
  import { invariant } from '@dxos/invariant';
8
8
 
9
9
  import { type SVGContext } from '../../hooks';
10
- import { defaultIdAccessor, type IdAccessor } from '../types';
10
+ import { type IdAccessor, defaultIdAccessor } from '../types';
11
11
 
12
12
  export type RendererOptions<T = any> = {
13
13
  idAccessor: IdAccessor;
@@ -19,7 +19,7 @@ export const defaultIdAccessor: IdAccessor = (node: any) => {
19
19
  return node.id;
20
20
  };
21
21
 
22
- export const emptyGraph: Graph = {
22
+ export const emptyGraph: Graph.Any = {
23
23
  nodes: [],
24
24
  edges: [],
25
25
  };
@@ -16,7 +16,7 @@ export const useStateRef = <V>(
16
16
  initialValue?: V | (() => V),
17
17
  ): [V | undefined, Dispatch<SetStateAction<V | undefined>>, RefObject<V | undefined>] => {
18
18
  const [value, setValue] = useState<V | undefined>(initialValue);
19
- const ref = useRef<V>();
19
+ const ref = useRef<V>(null);
20
20
  useEffect(() => {
21
21
  ref.current = value;
22
22
  }, [initialValue, value]);
@@ -2,7 +2,7 @@
2
2
  // Copyright 2022 DXOS.org
3
3
  //
4
4
 
5
- import { select, zoom, type ZoomBehavior, zoomIdentity, type ZoomTransform } from 'd3';
5
+ import { type ZoomBehavior, type ZoomTransform, select, zoom, zoomIdentity } from 'd3';
6
6
  import defaultsDeep from 'lodash.defaultsdeep';
7
7
  import { type RefObject, useEffect, useMemo, useRef } from 'react';
8
8
 
@@ -2,34 +2,35 @@
2
2
  // Copyright 2020 DXOS.org
3
3
  //
4
4
 
5
- import '@dxos-theme';
6
-
7
- import { type StoryObj } from '@storybook/react-vite';
5
+ import { RegistryContext } from '@effect-atom/atom-react';
6
+ import { type Meta, type StoryObj } from '@storybook/react-vite';
8
7
  import { select } from 'd3';
9
- import React, { type PropsWithChildren, useEffect, useMemo, useRef } from 'react';
8
+ import React, { type PropsWithChildren, useContext, useEffect, useMemo, useRef } from 'react';
10
9
 
11
10
  import { combine } from '@dxos/async';
11
+ import { type Graph } from '@dxos/graph';
12
12
  import { log } from '@dxos/log';
13
- import { type Meta, withLayout, withTheme } from '@dxos/storybook-utils';
13
+ import { withTheme } from '@dxos/react-ui/testing';
14
+ import { withRegistry } from '@dxos/storybook-utils';
14
15
 
15
16
  import { SVG } from '../components';
16
17
  import {
17
18
  GraphForceProjector,
18
- type GraphLayoutNode,
19
+ type GraphForceProjectorOptions,
19
20
  type GraphLayoutEdge,
21
+ type GraphLayoutNode,
20
22
  GraphRenderer,
21
- type GraphForceProjectorOptions,
22
- createMarkers,
23
23
  createGraphDrag,
24
+ createMarkers,
24
25
  linkerRenderer,
25
26
  } from '../graph';
26
- import { useSvgContext, useZoom, useGrid } from '../hooks';
27
- import { convertTreeToGraph, createTree, TestGraphModel, type TestNode } from '../testing';
27
+ import { useGrid, useSvgContext, useZoom } from '../hooks';
28
+ import { TestGraphModel, type TestNode, convertTreeToGraph, createTree } from '../testing';
28
29
 
29
30
  import '../../styles/graph.css';
30
31
 
31
- type StoryProps = PropsWithChildren<{
32
- model: TestGraphModel;
32
+ type ComponentProps = PropsWithChildren<{
33
+ graph: () => Graph.Graph<TestNode, Graph.Edge.Any>;
33
34
  projectorOptions?: GraphForceProjectorOptions;
34
35
  count?: number;
35
36
  interval?: number;
@@ -37,28 +38,20 @@ type StoryProps = PropsWithChildren<{
37
38
  grid?: boolean;
38
39
  }>;
39
40
 
40
- const DefaultStory = ({ children, ...props }: StoryProps) => {
41
- return (
42
- <>
43
- <SVG.Root>
44
- <StoryComponent {...props} />
45
- </SVG.Root>
46
- {children && <div className='absolute left-4 bottom-4 font-mono text-green-500 text-xs'>{children}</div>}
47
- </>
48
- );
49
- };
50
-
51
- const StoryComponent = ({
52
- model,
41
+ const Component = ({
42
+ graph: graphFactory,
53
43
  projectorOptions,
54
44
  count = 0,
55
45
  interval = 200,
56
46
  link = false,
57
47
  grid: showGrid = false,
58
- }: StoryProps) => {
48
+ }: ComponentProps) => {
49
+ const registry = useContext(RegistryContext);
50
+ const model = useMemo(() => new TestGraphModel(registry, graphFactory()), [registry, graphFactory]);
51
+
59
52
  const context = useSvgContext();
60
- const graphRef = useRef<SVGGElement>();
61
- const markersRef = useRef<SVGGElement>();
53
+ const graphRef = useRef<SVGGElement>(null);
54
+ const markersRef = useRef<SVGGElement>(null);
62
55
  const grid = useGrid({ visible: showGrid });
63
56
  const zoom = useZoom();
64
57
 
@@ -94,7 +87,7 @@ const StoryComponent = ({
94
87
  labels: {
95
88
  text: (node: GraphLayoutNode<TestNode>) => node.id.substring(0, 4),
96
89
  },
97
- onNodeClick: (node: GraphLayoutNode<TestNode>, event: MouseEvent) => {
90
+ onNodeClick: (node: GraphLayoutNode<TestNode>) => {
98
91
  renderer.fireBullet(node);
99
92
  },
100
93
  onLinkClick: (edge: GraphLayoutEdge<TestNode>, event: MouseEvent) => {
@@ -151,19 +144,33 @@ const StoryComponent = ({
151
144
  );
152
145
  };
153
146
 
154
- const meta: Meta = {
147
+ const DefaultStory = ({ children, ...props }: ComponentProps) => {
148
+ return (
149
+ <>
150
+ <SVG.Root>
151
+ <Component {...props} />
152
+ </SVG.Root>
153
+ {children && <div className='absolute left-4 bottom-4 font-mono text-green-500 text-xs'>{children}</div>}
154
+ </>
155
+ );
156
+ };
157
+
158
+ const meta = {
155
159
  title: 'ui/react-ui-graph/hooks',
156
160
  render: DefaultStory,
157
- decorators: [withTheme, withLayout({ fullscreen: true })],
158
- };
161
+ decorators: [withRegistry, withTheme],
162
+ parameters: {
163
+ layout: 'fullscreen',
164
+ },
165
+ } satisfies Meta<typeof DefaultStory>;
159
166
 
160
167
  export default meta;
161
168
 
162
- type Story = StoryObj<typeof DefaultStory>;
169
+ type Story = StoryObj<typeof meta>;
163
170
 
164
171
  export const Default: Story = {
165
172
  args: {
166
- model: new TestGraphModel(convertTreeToGraph(createTree({ depth: 3 }))),
173
+ graph: () => convertTreeToGraph(createTree({ depth: 3 })),
167
174
  grid: true,
168
175
  count: 300,
169
176
  interval: 20,
@@ -188,7 +195,7 @@ export const Default: Story = {
188
195
 
189
196
  export const Bullets: Story = {
190
197
  args: {
191
- model: new TestGraphModel(convertTreeToGraph(createTree({ depth: 4 }))),
198
+ graph: () => convertTreeToGraph(createTree({ depth: 4 })),
192
199
  children: <span>⌘-DRAG to edge or create node; ⌘-CLICK to delete edge.</span>,
193
200
  link: true,
194
201
  grid: false,
@@ -2,7 +2,7 @@
2
2
  // Copyright 2020 DXOS.org
3
3
  //
4
4
 
5
- import { type BaseGraphEdge, type Graph } from '@dxos/graph';
5
+ import { type Graph } from '@dxos/graph';
6
6
  import { faker } from '@dxos/random';
7
7
 
8
8
  import { type TestNode } from './model';
@@ -23,7 +23,7 @@ export const createNode = (type: string = undefined): TestNode => ({
23
23
  export const createNodes = (n = 0, types?: string[]): TestNode[] =>
24
24
  Array.from({ length: n }, () => createNode(types ? faker.helpers.arrayElement(types) : undefined));
25
25
 
26
- export const createEdge = (source: TestNode, target: TestNode): BaseGraphEdge => ({
26
+ export const createEdge = (source: TestNode, target: TestNode): Graph.Edge.Any => ({
27
27
  id: `${source.id}-${target.id}`,
28
28
  source: source.id,
29
29
  target: target.id,
@@ -54,8 +54,8 @@ export const createTree = ({ depth = 2, children = 3 } = {}): TestNode => {
54
54
  * Converts a tree into a graph.
55
55
  * @param root
56
56
  */
57
- export const convertTreeToGraph = (root: TestNode): Graph => {
58
- const traverse = (node: TestNode, graph: Graph) => {
57
+ export const convertTreeToGraph = (root: TestNode): Graph.Any => {
58
+ const traverse = (node: TestNode, graph: Graph.Any) => {
59
59
  graph.nodes.push(node);
60
60
  node.children?.forEach((child) => {
61
61
  graph.edges.push(createEdge(node, child));
@@ -76,7 +76,7 @@ export const convertTreeToGraph = (root: TestNode): Graph => {
76
76
  * @param numNodes
77
77
  * @param numEdges
78
78
  */
79
- export const createGraph = (numNodes = 0, numEdges = 0, types?: string[]): Graph => {
79
+ export const createGraph = (numNodes = 0, numEdges = 0, types?: string[]): Graph.Any => {
80
80
  const nodes = createNodes(numNodes, types);
81
81
 
82
82
  const edges = new Map();
@@ -7,10 +7,11 @@ import update from 'immutability-helper';
7
7
  import { type Graph } from '@dxos/graph';
8
8
  import { faker } from '@dxos/random';
9
9
 
10
- import { createNode, createEdge } from './data';
11
- import { type TestNode } from './model';
12
10
  import { useStateRef } from '../hooks';
13
11
 
12
+ import { createEdge, createNode } from './data';
13
+ import { type TestNode } from './model';
14
+
14
15
  export type ObjectMutator<T> = [
15
16
  T, // Current value.
16
17
  (data: T) => void, // Set value.
@@ -46,7 +47,7 @@ export const useObjectMutator = <T>(initalValue: T): ObjectMutator<T> => {
46
47
  /**
47
48
  * Test data set generator and mutator.
48
49
  */
49
- export const useGraphGenerator = (options: { data?: Graph } = {}) => {
50
+ export const useGraphGenerator = (options: { data?: Graph.Any } = {}) => {
50
51
  const [data, setData, updateData] = useObjectMutator(options.data || { nodes: [], edges: [] });
51
52
 
52
53
  let interval;
@@ -2,17 +2,17 @@
2
2
  // Copyright 2022 DXOS.org
3
3
  //
4
4
 
5
- import { type BaseGraphNode, ReactiveGraphModel } from '@dxos/graph';
5
+ import { type Graph, GraphModel } from '@dxos/graph';
6
6
  import { faker } from '@dxos/random';
7
7
 
8
8
  import { createEdge, createNode } from './data';
9
9
 
10
- export type TestNode = BaseGraphNode & {
10
+ export type TestNode = Graph.Node.Any & {
11
11
  label?: string;
12
12
  children?: TestNode[];
13
13
  };
14
14
 
15
- export class TestGraphModel extends ReactiveGraphModel<TestNode> {
15
+ export class TestGraphModel extends GraphModel.ReactiveGraphModel<TestNode> {
16
16
  getRandomNode() {
17
17
  return faker.helpers.arrayElement(this._graph.nodes);
18
18
  }
package/src/util/index.ts CHANGED
@@ -2,11 +2,11 @@
2
2
  // Copyright 2021 DXOS.org
3
3
  //
4
4
 
5
- export * from './d3-types';
5
+ export type * from './d3-types';
6
6
  export * from './events';
7
7
  export * from './fraction';
8
8
  export * from './points';
9
9
  export * from './scale';
10
10
  export * from './screen';
11
- export * from './types';
11
+ export type * from './types';
12
12
  export * from './vector';
@@ -5,7 +5,7 @@
5
5
  import { expect, test } from 'vitest';
6
6
 
7
7
  import { Screen } from './screen';
8
- import { type Rect, type Point } from './types';
8
+ import { type Point, type Rect } from './types';
9
9
 
10
10
  test('createBounds', () => {
11
11
  const tests: { p1: Point; p2: Point; bounds: Rect }[] = [
@@ -2,7 +2,7 @@
2
2
  // Copyright 2020 DXOS.org
3
3
  //
4
4
 
5
- import { type Rect, type Point } from './types';
5
+ import { type Point, type Rect } from './types';
6
6
 
7
7
  export type Modifiers = {
8
8
  center?: boolean;
@@ -2,7 +2,7 @@
2
2
  // Copyright 2020 DXOS.org
3
3
  //
4
4
 
5
- import { type Num, type Fraction, FractionUtil } from './fraction';
5
+ import { type Fraction, FractionUtil, type Num } from './fraction';
6
6
 
7
7
  export type Vertex = { x: Fraction; y: Fraction };
8
8