@dxos/react-ui-graph 0.8.2-main.10c050d

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 (206) hide show
  1. package/LICENSE +8 -0
  2. package/README.md +25 -0
  3. package/dist/lib/browser/chunk-2YMRPQU7.mjs +979 -0
  4. package/dist/lib/browser/chunk-2YMRPQU7.mjs.map +7 -0
  5. package/dist/lib/browser/index.mjs +1546 -0
  6. package/dist/lib/browser/index.mjs.map +7 -0
  7. package/dist/lib/browser/meta.json +1 -0
  8. package/dist/lib/browser/testing/index.mjs +180 -0
  9. package/dist/lib/browser/testing/index.mjs.map +7 -0
  10. package/dist/lib/node/chunk-WFHWEMPB.cjs +919 -0
  11. package/dist/lib/node/chunk-WFHWEMPB.cjs.map +7 -0
  12. package/dist/lib/node/index.cjs +1547 -0
  13. package/dist/lib/node/index.cjs.map +7 -0
  14. package/dist/lib/node/meta.json +1 -0
  15. package/dist/lib/node/testing/index.cjs +214 -0
  16. package/dist/lib/node/testing/index.cjs.map +7 -0
  17. package/dist/lib/node-esm/chunk-ITHTKQOY.mjs +981 -0
  18. package/dist/lib/node-esm/chunk-ITHTKQOY.mjs.map +7 -0
  19. package/dist/lib/node-esm/index.mjs +1547 -0
  20. package/dist/lib/node-esm/index.mjs.map +7 -0
  21. package/dist/lib/node-esm/meta.json +1 -0
  22. package/dist/lib/node-esm/testing/index.mjs +181 -0
  23. package/dist/lib/node-esm/testing/index.mjs.map +7 -0
  24. package/dist/types/src/components/Graph/Graph.d.ts +26 -0
  25. package/dist/types/src/components/Graph/Graph.d.ts.map +1 -0
  26. package/dist/types/src/components/Graph/Graph.stories.d.ts +29 -0
  27. package/dist/types/src/components/Graph/Graph.stories.d.ts.map +1 -0
  28. package/dist/types/src/components/Graph/index.d.ts +2 -0
  29. package/dist/types/src/components/Graph/index.d.ts.map +1 -0
  30. package/dist/types/src/components/Mesh/Mesh.d.ts +6 -0
  31. package/dist/types/src/components/Mesh/Mesh.d.ts.map +1 -0
  32. package/dist/types/src/components/Mesh/Mesh.stories.d.ts +12 -0
  33. package/dist/types/src/components/Mesh/Mesh.stories.d.ts.map +1 -0
  34. package/dist/types/src/components/Mesh/index.d.ts +2 -0
  35. package/dist/types/src/components/Mesh/index.d.ts.map +1 -0
  36. package/dist/types/src/components/SVG/Grid.d.ts +9 -0
  37. package/dist/types/src/components/SVG/Grid.d.ts.map +1 -0
  38. package/dist/types/src/components/SVG/Markers.d.ts +10 -0
  39. package/dist/types/src/components/SVG/Markers.d.ts.map +1 -0
  40. package/dist/types/src/components/SVG/Root.d.ts +12 -0
  41. package/dist/types/src/components/SVG/Root.d.ts.map +1 -0
  42. package/dist/types/src/components/SVG/SVG.d.ts +23 -0
  43. package/dist/types/src/components/SVG/SVG.d.ts.map +1 -0
  44. package/dist/types/src/components/SVG/Svg.stories.d.ts +17 -0
  45. package/dist/types/src/components/SVG/Svg.stories.d.ts.map +1 -0
  46. package/dist/types/src/components/SVG/Zoom.d.ts +12 -0
  47. package/dist/types/src/components/SVG/Zoom.d.ts.map +1 -0
  48. package/dist/types/src/components/SVG/index.d.ts +2 -0
  49. package/dist/types/src/components/SVG/index.d.ts.map +1 -0
  50. package/dist/types/src/components/index.d.ts +4 -0
  51. package/dist/types/src/components/index.d.ts.map +1 -0
  52. package/dist/types/src/fx/fx.stories.d.ts +19 -0
  53. package/dist/types/src/fx/fx.stories.d.ts.map +1 -0
  54. package/dist/types/src/fx/index.d.ts +2 -0
  55. package/dist/types/src/fx/index.d.ts.map +1 -0
  56. package/dist/types/src/fx/pulsar.d.ts +17 -0
  57. package/dist/types/src/fx/pulsar.d.ts.map +1 -0
  58. package/dist/types/src/graph/index.d.ts +5 -0
  59. package/dist/types/src/graph/index.d.ts.map +1 -0
  60. package/dist/types/src/graph/markers.d.ts +11 -0
  61. package/dist/types/src/graph/markers.d.ts.map +1 -0
  62. package/dist/types/src/graph/projector/graph-drag.d.ts +16 -0
  63. package/dist/types/src/graph/projector/graph-drag.d.ts.map +1 -0
  64. package/dist/types/src/graph/projector/graph-force-projector.d.ts +96 -0
  65. package/dist/types/src/graph/projector/graph-force-projector.d.ts.map +1 -0
  66. package/dist/types/src/graph/projector/graph-forces.d.ts +8 -0
  67. package/dist/types/src/graph/projector/graph-forces.d.ts.map +1 -0
  68. package/dist/types/src/graph/projector/graph-hierarchical-projector.d.ts +14 -0
  69. package/dist/types/src/graph/projector/graph-hierarchical-projector.d.ts.map +1 -0
  70. package/dist/types/src/graph/projector/graph-projector.d.ts +25 -0
  71. package/dist/types/src/graph/projector/graph-projector.d.ts.map +1 -0
  72. package/dist/types/src/graph/projector/graph-radial-projector.d.ts +34 -0
  73. package/dist/types/src/graph/projector/graph-radial-projector.d.ts.map +1 -0
  74. package/dist/types/src/graph/projector/graph-relational-projector.d.ts +14 -0
  75. package/dist/types/src/graph/projector/graph-relational-projector.d.ts.map +1 -0
  76. package/dist/types/src/graph/projector/index.d.ts +8 -0
  77. package/dist/types/src/graph/projector/index.d.ts.map +1 -0
  78. package/dist/types/src/graph/projector/projector.d.ts +32 -0
  79. package/dist/types/src/graph/projector/projector.d.ts.map +1 -0
  80. package/dist/types/src/graph/renderer/bullets.d.ts +16 -0
  81. package/dist/types/src/graph/renderer/bullets.d.ts.map +1 -0
  82. package/dist/types/src/graph/renderer/graph-renderer.d.ts +43 -0
  83. package/dist/types/src/graph/renderer/graph-renderer.d.ts.map +1 -0
  84. package/dist/types/src/graph/renderer/index.d.ts +4 -0
  85. package/dist/types/src/graph/renderer/index.d.ts.map +1 -0
  86. package/dist/types/src/graph/renderer/linker.d.ts +12 -0
  87. package/dist/types/src/graph/renderer/linker.d.ts.map +1 -0
  88. package/dist/types/src/graph/renderer/renderer.d.ts +20 -0
  89. package/dist/types/src/graph/renderer/renderer.d.ts.map +1 -0
  90. package/dist/types/src/graph/types.d.ts +52 -0
  91. package/dist/types/src/graph/types.d.ts.map +1 -0
  92. package/dist/types/src/hooks/index.d.ts +5 -0
  93. package/dist/types/src/hooks/index.d.ts.map +1 -0
  94. package/dist/types/src/hooks/useGrid.d.ts +24 -0
  95. package/dist/types/src/hooks/useGrid.d.ts.map +1 -0
  96. package/dist/types/src/hooks/useStateRef.d.ts +22 -0
  97. package/dist/types/src/hooks/useStateRef.d.ts.map +1 -0
  98. package/dist/types/src/hooks/useSvgContext.d.ts +41 -0
  99. package/dist/types/src/hooks/useSvgContext.d.ts.map +1 -0
  100. package/dist/types/src/hooks/useZoom.d.ts +34 -0
  101. package/dist/types/src/hooks/useZoom.d.ts.map +1 -0
  102. package/dist/types/src/index.d.ts +6 -0
  103. package/dist/types/src/index.d.ts.map +1 -0
  104. package/dist/types/src/stories/hooks.stories.d.ts +22 -0
  105. package/dist/types/src/stories/hooks.stories.d.ts.map +1 -0
  106. package/dist/types/src/testing/data.d.ts +27 -0
  107. package/dist/types/src/testing/data.d.ts.map +1 -0
  108. package/dist/types/src/testing/data.test.d.ts +2 -0
  109. package/dist/types/src/testing/data.test.d.ts.map +1 -0
  110. package/dist/types/src/testing/hooks.d.ts +42 -0
  111. package/dist/types/src/testing/hooks.d.ts.map +1 -0
  112. package/dist/types/src/testing/index.d.ts +4 -0
  113. package/dist/types/src/testing/index.d.ts.map +1 -0
  114. package/dist/types/src/testing/model.d.ts +14 -0
  115. package/dist/types/src/testing/model.d.ts.map +1 -0
  116. package/dist/types/src/util/d3-types.d.ts +5 -0
  117. package/dist/types/src/util/d3-types.d.ts.map +1 -0
  118. package/dist/types/src/util/events.d.ts +10 -0
  119. package/dist/types/src/util/events.d.ts.map +1 -0
  120. package/dist/types/src/util/fraction.d.ts +65 -0
  121. package/dist/types/src/util/fraction.d.ts.map +1 -0
  122. package/dist/types/src/util/fraction.test.d.ts +2 -0
  123. package/dist/types/src/util/fraction.test.d.ts.map +1 -0
  124. package/dist/types/src/util/index.d.ts +9 -0
  125. package/dist/types/src/util/index.d.ts.map +1 -0
  126. package/dist/types/src/util/points.d.ts +15 -0
  127. package/dist/types/src/util/points.d.ts.map +1 -0
  128. package/dist/types/src/util/scale.d.ts +57 -0
  129. package/dist/types/src/util/scale.d.ts.map +1 -0
  130. package/dist/types/src/util/scale.test.d.ts +2 -0
  131. package/dist/types/src/util/scale.test.d.ts.map +1 -0
  132. package/dist/types/src/util/screen.d.ts +25 -0
  133. package/dist/types/src/util/screen.d.ts.map +1 -0
  134. package/dist/types/src/util/screen.test.d.ts +2 -0
  135. package/dist/types/src/util/screen.test.d.ts.map +1 -0
  136. package/dist/types/src/util/types.d.ts +4 -0
  137. package/dist/types/src/util/types.d.ts.map +1 -0
  138. package/dist/types/src/util/vector.d.ts +48 -0
  139. package/dist/types/src/util/vector.d.ts.map +1 -0
  140. package/dist/types/src/util/vector.test.d.ts +2 -0
  141. package/dist/types/src/util/vector.test.d.ts.map +1 -0
  142. package/dist/types/tsconfig.tsbuildinfo +1 -0
  143. package/package.json +86 -0
  144. package/src/components/Graph/Graph.stories.tsx +457 -0
  145. package/src/components/Graph/Graph.tsx +130 -0
  146. package/src/components/Graph/index.ts +5 -0
  147. package/src/components/Mesh/Mesh.stories.tsx +62 -0
  148. package/src/components/Mesh/Mesh.tsx +143 -0
  149. package/src/components/Mesh/index.ts +5 -0
  150. package/src/components/SVG/Grid.tsx +20 -0
  151. package/src/components/SVG/Markers.tsx +25 -0
  152. package/src/components/SVG/Root.tsx +55 -0
  153. package/src/components/SVG/SVG.tsx +27 -0
  154. package/src/components/SVG/Svg.stories.tsx +57 -0
  155. package/src/components/SVG/Zoom.tsx +27 -0
  156. package/src/components/SVG/index.ts +5 -0
  157. package/src/components/index.ts +7 -0
  158. package/src/fx/fx.stories.tsx +116 -0
  159. package/src/fx/index.ts +5 -0
  160. package/src/fx/pulsar.ts +58 -0
  161. package/src/graph/index.ts +8 -0
  162. package/src/graph/markers.ts +85 -0
  163. package/src/graph/projector/graph-drag.ts +124 -0
  164. package/src/graph/projector/graph-force-projector.ts +414 -0
  165. package/src/graph/projector/graph-forces.ts +35 -0
  166. package/src/graph/projector/graph-hierarchical-projector.ts +80 -0
  167. package/src/graph/projector/graph-projector.ts +99 -0
  168. package/src/graph/projector/graph-radial-projector.ts +142 -0
  169. package/src/graph/projector/graph-relational-projector.ts +110 -0
  170. package/src/graph/projector/index.ts +11 -0
  171. package/src/graph/projector/projector.ts +72 -0
  172. package/src/graph/renderer/bullets.ts +72 -0
  173. package/src/graph/renderer/graph-renderer.ts +484 -0
  174. package/src/graph/renderer/index.ts +7 -0
  175. package/src/graph/renderer/linker.ts +42 -0
  176. package/src/graph/renderer/renderer.ts +49 -0
  177. package/src/graph/types.ts +80 -0
  178. package/src/hooks/index.ts +8 -0
  179. package/src/hooks/useGrid.ts +194 -0
  180. package/src/hooks/useStateRef.ts +60 -0
  181. package/src/hooks/useSvgContext.ts +96 -0
  182. package/src/hooks/useZoom.ts +118 -0
  183. package/src/index.ts +9 -0
  184. package/src/stories/hooks.stories.tsx +212 -0
  185. package/src/testing/data.test.ts +12 -0
  186. package/src/testing/data.ts +100 -0
  187. package/src/testing/hooks.ts +103 -0
  188. package/src/testing/index.ts +7 -0
  189. package/src/testing/model.ts +29 -0
  190. package/src/typings.d.ts +5 -0
  191. package/src/util/d3-types.ts +26 -0
  192. package/src/util/events.ts +24 -0
  193. package/src/util/fraction.test.ts +76 -0
  194. package/src/util/fraction.ts +175 -0
  195. package/src/util/index.ts +12 -0
  196. package/src/util/points.ts +68 -0
  197. package/src/util/scale.test.ts +31 -0
  198. package/src/util/scale.ts +137 -0
  199. package/src/util/screen.test.ts +41 -0
  200. package/src/util/screen.ts +91 -0
  201. package/src/util/types.ts +9 -0
  202. package/src/util/vector.test.ts +15 -0
  203. package/src/util/vector.ts +67 -0
  204. package/styles/graph.css +142 -0
  205. package/testing.d.ts +11 -0
  206. package/testing.js +5 -0
@@ -0,0 +1,143 @@
1
+ //
2
+ // Copyright 2024 DXOS.org
3
+ //
4
+
5
+ import { geoPath, select, type GeoStream } from 'd3';
6
+ import { useEffect } from 'react';
7
+ import { feature, mesh } from 'topojson-client';
8
+ import { type GeometryCollection, type GeometryObject, type Objects, type Topology } from 'topojson-specification';
9
+
10
+ import { useSvgContext } from '../../hooks';
11
+ import { type Size } from '../../util';
12
+
13
+ export type MeshProps = {
14
+ radius?: number;
15
+ value?: number;
16
+ };
17
+
18
+ // https://d3og.com/mbostock/5249328
19
+ export const Mesh = ({ radius = 16, value = 0.5 }: MeshProps) => {
20
+ const { svg, size } = useSvgContext();
21
+
22
+ useEffect(() => {
23
+ if (size) {
24
+ // TODO(burdon): Resize doesn't trigger.
25
+ const topology = hexTopology(size, radius, value);
26
+ const projection = hexProjection(radius);
27
+ const path = geoPath().projection(projection);
28
+
29
+ select(svg)
30
+ .append('g')
31
+ .attr('class', 'hexagon')
32
+ .selectAll('path')
33
+ .data(topology.objects.hexagons.geometries)
34
+ .enter()
35
+ .append('path')
36
+ .attr('d', (d) => path(feature(topology, d)))
37
+ .attr('class', (d) => ((d.properties as Custom)?.fill ? 'fill' : null));
38
+ // .on('mousedown', mousedown)
39
+ // .on('mousemove', mousemove)
40
+ // .on('mouseup', mouseup);
41
+
42
+ select(svg).append('path').datum(mesh(topology, topology.objects.hexagons)).attr('class', 'mesh').attr('d', path);
43
+
44
+ const redraw = (border: any) => {
45
+ border.attr(
46
+ 'd',
47
+ path(
48
+ mesh(
49
+ topology,
50
+ topology.objects.hexagons,
51
+ (a, b) => (a.properties as Custom).fill !== (b.properties as Custom).fill,
52
+ ),
53
+ ),
54
+ );
55
+ };
56
+
57
+ select(svg).append('path').attr('class', 'border').call(redraw);
58
+ }
59
+ }, [svg, size, value]);
60
+
61
+ return null;
62
+ };
63
+
64
+ type Custom = { fill: boolean };
65
+
66
+ interface HexObjects extends Objects<{ fill: boolean }> {
67
+ hexagons: GeometryCollection<Custom>;
68
+ }
69
+
70
+ const hexTopology = ({ width, height }: Size, radius: number, value = 1): Topology<HexObjects> => {
71
+ const dx = radius * 2 * Math.sin(Math.PI / 3);
72
+ const dy = radius * 1.5;
73
+ const m = Math.ceil((height + radius) / dy) + 1;
74
+ const n = Math.ceil(width / dx) + 1;
75
+ const geometries: Array<GeometryObject<{ fill: boolean }>> = [];
76
+ const arcs = [];
77
+
78
+ for (let j = -1; j <= m; ++j) {
79
+ for (let i = -1; i <= n; ++i) {
80
+ const y = j * 2;
81
+ const x = (i + (j & 1) / 2) * 2;
82
+ arcs.push(
83
+ [
84
+ [x, y - 1],
85
+ [1, 1],
86
+ ],
87
+ [
88
+ [x + 1, y],
89
+ [0, 1],
90
+ ],
91
+ [
92
+ [x + 1, y + 1],
93
+ [-1, 1],
94
+ ],
95
+ );
96
+ }
97
+ }
98
+
99
+ for (let j = 0, q = 3; j < m; ++j, q += 6) {
100
+ for (let i = 0; i < n; ++i, q += 3) {
101
+ geometries.push({
102
+ type: 'Polygon',
103
+ arcs: [[q, q + 1, q + 2, ~(q + (n + 2 - (j & 1)) * 3), ~(q - 2), ~(q - (n + 2 + (j & 1)) * 3 + 2)]],
104
+ properties: {
105
+ fill: Math.random() > (i / n) * (1 / value),
106
+ },
107
+ });
108
+ }
109
+ }
110
+
111
+ const hexagons: GeometryCollection<{ fill: boolean }> = { type: 'GeometryCollection', geometries };
112
+
113
+ return {
114
+ type: 'Topology',
115
+ transform: { translate: [0, 0], scale: [1, 1] },
116
+ objects: { hexagons },
117
+ arcs,
118
+ };
119
+ };
120
+
121
+ const hexProjection = (radius: number) => {
122
+ const dx = radius * 2 * Math.sin(Math.PI / 3);
123
+ const dy = radius * 1.5;
124
+ return {
125
+ stream: (stream: GeoStream) => ({
126
+ point: (x: number, y: number) => {
127
+ stream.point((x * dx) / 2, ((y - (2 - (y & 1)) / 3) * dy) / 2);
128
+ },
129
+ lineStart: () => {
130
+ stream.lineStart();
131
+ },
132
+ lineEnd: () => {
133
+ stream.lineEnd();
134
+ },
135
+ polygonStart: () => {
136
+ stream.polygonStart();
137
+ },
138
+ polygonEnd: () => {
139
+ stream.polygonEnd();
140
+ },
141
+ }),
142
+ };
143
+ };
@@ -0,0 +1,5 @@
1
+ //
2
+ // Copyright 2020 DXOS.org
3
+ //
4
+
5
+ export * from './Mesh';
@@ -0,0 +1,20 @@
1
+ //
2
+ // Copyright 2022 DXOS.org
3
+ //
4
+
5
+ import React from 'react';
6
+
7
+ import { type ThemedClassName } from '@dxos/react-ui';
8
+ import { mx } from '@dxos/react-ui-theme';
9
+
10
+ import { useGrid, type GridOptions } from '../../hooks';
11
+
12
+ export type GridProps = ThemedClassName<GridOptions>;
13
+
14
+ /**
15
+ * SVG grid wrapper.
16
+ */
17
+ export const Grid = ({ classNames, ...props }: GridProps) => {
18
+ const { ref } = useGrid(props);
19
+ return <g ref={ref} className={mx('dx-grid', classNames)} />;
20
+ };
@@ -0,0 +1,25 @@
1
+ //
2
+ // Copyright 2022 DXOS.org
3
+ //
4
+
5
+ import { select } from 'd3';
6
+ import React, { useEffect, useRef } from 'react';
7
+
8
+ import { createMarkers } from '../../graph';
9
+
10
+ export type MarkersProps = {
11
+ arrowSize?: number;
12
+ className?: string;
13
+ };
14
+
15
+ /**
16
+ * SVG markers wrapper.
17
+ */
18
+ export const Markers = ({ arrowSize, className }: MarkersProps) => {
19
+ const ref = useRef();
20
+ useEffect(() => {
21
+ select(ref.current).call(createMarkers({ arrowSize }));
22
+ }, [ref]);
23
+
24
+ return <defs ref={ref} className={className} />;
25
+ };
@@ -0,0 +1,55 @@
1
+ //
2
+ // Copyright 2022 DXOS.org
3
+ //
4
+
5
+ import { select } from 'd3';
6
+ import React, { forwardRef, type PropsWithChildren, useEffect, useMemo, useImperativeHandle } from 'react';
7
+ import { useResizeDetector } from 'react-resize-detector';
8
+
9
+ import { type ThemedClassName } from '@dxos/react-ui';
10
+ import { mx } from '@dxos/react-ui-theme';
11
+
12
+ import { SVGContext, type SVGContextOptions, SVGContextProvider } from '../../hooks';
13
+
14
+ export type RootProps = ThemedClassName<PropsWithChildren<SVGContextOptions>>;
15
+
16
+ /**
17
+ * Makes the SVG context available to child nodes.
18
+ * Automatically resizes the SVG element, which expands to fit the container.
19
+ */
20
+ export const Root = forwardRef<SVGContext, RootProps>(({ classNames, children, ...props }, ref) => {
21
+ const { ref: containerRef, width = 0, height = 0 } = useResizeDetector({ refreshRate: 200 });
22
+
23
+ const context = useMemo<SVGContext>(() => {
24
+ return new SVGContext(props);
25
+ }, [props.scale, props.centered]);
26
+
27
+ useImperativeHandle(ref, () => context, [context]);
28
+
29
+ useEffect(() => {
30
+ if (width && height) {
31
+ context.setSize({ width, height });
32
+ select(context.svg)
33
+ .attr('display', 'block')
34
+ .attr('viewBox', context.viewBox)
35
+ .attr('width', width)
36
+ .attr('height', height);
37
+ }
38
+ }, [context, width, height]);
39
+
40
+ const ready = width !== 0 && height !== 0;
41
+
42
+ return (
43
+ <SVGContextProvider value={context}>
44
+ <div ref={containerRef} className={mx('grid grow overflow-hidden', classNames)}>
45
+ <svg
46
+ xmlns='http://www.w3.org/2000/svg'
47
+ ref={context.ref}
48
+ className={mx('transition-opacity opacity-0 duration-1000', ready && 'opacity-100')}
49
+ >
50
+ {ready && children}
51
+ </svg>
52
+ </div>
53
+ </SVGContextProvider>
54
+ );
55
+ });
@@ -0,0 +1,27 @@
1
+ //
2
+ // Copyright 2022 DXOS.org
3
+ //
4
+
5
+ import { type PropsWithChildren } from 'react';
6
+
7
+ import { type ThemedClassName } from '@dxos/react-ui';
8
+
9
+ import { Grid, type GridProps as SVGGridProps } from './Grid';
10
+ import { Markers, type MarkersProps as SVGMarkersProps } from './Markers';
11
+ import { Root, type RootProps as SVGRootProps } from './Root';
12
+ import { Zoom, type ZoomProps as SVGZoomProps } from './Zoom';
13
+ import { Graph, type GraphProps as SVGGraphProps } from '../Graph';
14
+ import { Mesh, type MeshProps as SVGMeshProps } from '../Mesh';
15
+
16
+ export type SVGProps = PropsWithChildren<ThemedClassName>;
17
+
18
+ export const SVG = {
19
+ Root,
20
+ Grid,
21
+ Markers,
22
+ Mesh,
23
+ Zoom,
24
+ Graph,
25
+ };
26
+
27
+ export type { SVGRootProps, SVGGridProps, SVGMarkersProps, SVGZoomProps, SVGGraphProps, SVGMeshProps };
@@ -0,0 +1,57 @@
1
+ //
2
+ // Copyright 2022 DXOS.org
3
+ //
4
+
5
+ import '@dxos-theme';
6
+
7
+ import React, { useRef } from 'react';
8
+
9
+ import { type Meta, withLayout, withTheme } from '@dxos/storybook-utils';
10
+
11
+ import { SVG } from './SVG';
12
+ import { useGrid, useZoom, type SVGContext } from '../../hooks';
13
+
14
+ import '../../../styles/graph.css';
15
+
16
+ type ComponentProps = {
17
+ grid?: boolean;
18
+ zoom?: boolean;
19
+ };
20
+
21
+ const DefaultStory = (props: ComponentProps) => {
22
+ const context = useRef<SVGContext>(null);
23
+
24
+ return (
25
+ <SVG.Root ref={context}>
26
+ <Component {...props} />
27
+ </SVG.Root>
28
+ );
29
+ };
30
+
31
+ const Component = (options: ComponentProps) => {
32
+ const grid = useGrid({ visible: options.grid, axis: true });
33
+ const zoom = useZoom({ enabled: options.zoom });
34
+
35
+ return (
36
+ <>
37
+ <g ref={grid.ref} className='dx-grid' />
38
+ <g ref={zoom.ref} className='[&>circle]:stroke-red-500'>
39
+ <circle cx={0} cy={0} r={128} />
40
+ </g>
41
+ </>
42
+ );
43
+ };
44
+
45
+ const meta: Meta<typeof Component> = {
46
+ title: 'ui/react-ui-graph/SVGRoot',
47
+ render: DefaultStory,
48
+ decorators: [withTheme, withLayout({ fullscreen: true })],
49
+ };
50
+
51
+ export default meta;
52
+
53
+ export const Default = {
54
+ args: {
55
+ grid: true,
56
+ },
57
+ };
@@ -0,0 +1,27 @@
1
+ //
2
+ // Copyright 2022 DXOS.org
3
+ //
4
+
5
+ import React, { type ReactNode, memo, useMemo } from 'react';
6
+
7
+ import { useZoom, type ZoomExtent } from '../../hooks';
8
+
9
+ export type ZoomProps = {
10
+ extent?: ZoomExtent;
11
+ className?: string;
12
+ children?: ReactNode;
13
+ };
14
+
15
+ /**
16
+ * SVG zoomable component wrapper.
17
+ */
18
+ export const Zoom = memo(({ extent, className, children }: ZoomProps) => {
19
+ const options = useMemo(() => ({ extent }), [JSON.stringify(extent)]); // TODO(burdon): Avoid stringify.
20
+ const zoom = useZoom(options);
21
+
22
+ return (
23
+ <g ref={zoom.ref as any} className={className}>
24
+ {children}
25
+ </g>
26
+ );
27
+ });
@@ -0,0 +1,5 @@
1
+ //
2
+ // Copyright 2020 DXOS.org
3
+ //
4
+
5
+ export * from './SVG';
@@ -0,0 +1,7 @@
1
+ //
2
+ // Copyright 2020 DXOS.org
3
+ //
4
+
5
+ export * from './Graph';
6
+ export * from './Mesh';
7
+ export * from './SVG';
@@ -0,0 +1,116 @@
1
+ //
2
+ // Copyright 2025 DXOS.org
3
+ //
4
+
5
+ import '@dxos-theme';
6
+
7
+ import { type Meta } from '@storybook/react';
8
+ import { select } from 'd3';
9
+ import React, { type FC, useEffect, useMemo, useRef } from 'react';
10
+
11
+ import { withLayout, withTheme } from '@dxos/storybook-utils';
12
+
13
+ import { Pulsar } from './pulsar';
14
+ import { SVG } from '../components';
15
+ import { useGrid, useZoom } from '../hooks';
16
+ import { type D3Callable } from '../util';
17
+
18
+ import '../../styles/graph.css';
19
+
20
+ type Datum = {
21
+ x: number;
22
+ y: number;
23
+ r: number;
24
+ };
25
+
26
+ type StoryProps = {
27
+ count: number;
28
+ } & Pulsar.Options;
29
+
30
+ const DefaultStory = (props: StoryProps) => {
31
+ return (
32
+ <SVG.Root>
33
+ <StoryComponent {...props} />
34
+ </SVG.Root>
35
+ );
36
+ };
37
+
38
+ const StoryComponent: FC<StoryProps> = ({ count = 1, ...options }) => {
39
+ const items = useMemo<Datum[]>(
40
+ () =>
41
+ Array.from({ length: count }, () => {
42
+ const a = Math.random() * Math.PI * 2;
43
+ const r = 100 + Math.random() * 200;
44
+ return {
45
+ x: r * Math.cos(a),
46
+ y: r * Math.sin(a),
47
+ r: Math.floor(Math.random() * 10) + 4,
48
+ };
49
+ }),
50
+ [count],
51
+ );
52
+
53
+ const grid = useGrid({ axis: true, grid: false });
54
+ const zoom = useZoom();
55
+
56
+ const root = useRef<SVGGElement>();
57
+ useEffect(() => {
58
+ select(root.current)
59
+ .selectAll('g')
60
+ .data(items)
61
+ .join((enter) =>
62
+ enter
63
+ .append('g')
64
+ .attr('transform', (d) => `translate(${d.x}, ${d.y})`)
65
+ .call(createNode, 'stroke-blue-500 hover:fill-blue-500', options),
66
+ );
67
+ }, [items]);
68
+
69
+ return (
70
+ <>
71
+ <g ref={grid.ref} className='dx-grid' />
72
+ <g ref={zoom.ref}>
73
+ <g ref={root} className='dx-graph' />
74
+ </g>
75
+ </>
76
+ );
77
+ };
78
+
79
+ const createNode: D3Callable<SVGGElement, Datum> = (group, classNames, options) => {
80
+ group
81
+ .append('circle')
82
+ .classed(classNames, true)
83
+ .attr('r', (d) => d.r)
84
+ .on('click', function () {
85
+ const d = select<SVGCircleElement, Datum>(this).datum();
86
+ const group = select<SVGGElement, Datum>(this.parentElement as unknown as SVGGElement);
87
+ if (Pulsar.has(group)) {
88
+ Pulsar.create(group, d.r, options);
89
+ } else {
90
+ Pulsar.remove(group);
91
+ }
92
+ });
93
+ };
94
+
95
+ const meta: Meta = {
96
+ title: 'ui/react-ui-graph/fx',
97
+ render: DefaultStory,
98
+ decorators: [withTheme, withLayout({ fullscreen: true })],
99
+ };
100
+
101
+ export default meta;
102
+
103
+ export const DefaultPulse = {
104
+ args: {
105
+ count: 20,
106
+ },
107
+ };
108
+
109
+ export const RapidPulse = {
110
+ args: {
111
+ count: 20,
112
+ duration: 1_000,
113
+ period: 1_000,
114
+ ratio: 5,
115
+ },
116
+ };
@@ -0,0 +1,5 @@
1
+ //
2
+ // Copyright 2025 DXOS.org
3
+ //
4
+
5
+ export * from './pulsar';
@@ -0,0 +1,58 @@
1
+ //
2
+ // Copyright 2025 DXOS.org
3
+ //
4
+
5
+ import { easeCubicOut, select } from 'd3';
6
+
7
+ import { type D3Callable, type D3Selection } from '../util';
8
+
9
+ const PROP_PULSAR_ACTIVE = 'dx-pulsar-active';
10
+
11
+ /**
12
+ * Pulsar effect.
13
+ */
14
+ export namespace Pulsar {
15
+ export const has = (group: D3Selection<SVGGElement>) => group.select('circle.dx-pulsar').empty();
16
+
17
+ export const create: D3Callable<SVGGElement> = (group, r: number, options: PulsarOptions = {}) => {
18
+ const pulsar = group
19
+ .append('circle')
20
+ .classed('dx-pulsar', true)
21
+ .classed('stroke-2 stroke-red-500 fill-none', true)
22
+ .property(PROP_PULSAR_ACTIVE, true);
23
+
24
+ startAnimation(pulsar, r, options);
25
+ };
26
+
27
+ export const remove: D3Callable<SVGGElement> = (group) => {
28
+ group.select('circle.dx-pulsar').property(PROP_PULSAR_ACTIVE, false);
29
+ };
30
+
31
+ export type Options = PulsarOptions;
32
+ }
33
+
34
+ type PulsarOptions = {
35
+ period?: number;
36
+ duration?: number;
37
+ ratio?: number;
38
+ };
39
+
40
+ const startAnimation = (pulsar: D3Selection<SVGCircleElement>, r: number, options: PulsarOptions = {}) => {
41
+ const { period = 1_000, duration = 800, ratio = 3 } = options;
42
+ return pulsar
43
+ .attr('r', r)
44
+ .attr('opacity', 1)
45
+ .transition('pulsar')
46
+ .delay(period - (Date.now() % period))
47
+ .duration(Math.min(duration, period - 20))
48
+ .ease(easeCubicOut)
49
+ .attr('r', r * ratio)
50
+ .attr('opacity', -0.1)
51
+ .on('end', function () {
52
+ if (select(this).property(PROP_PULSAR_ACTIVE)) {
53
+ startAnimation(select(this), r, options);
54
+ } else {
55
+ select(this).remove();
56
+ }
57
+ });
58
+ };
@@ -0,0 +1,8 @@
1
+ //
2
+ // Copyright 2022 DXOS.org
3
+ //
4
+
5
+ export * from './markers';
6
+ export * from './projector';
7
+ export * from './renderer';
8
+ export * from './types';
@@ -0,0 +1,85 @@
1
+ //
2
+ // Copyright 2022 DXOS.org
3
+ //
4
+
5
+ import { line, select } from 'd3';
6
+
7
+ import { type D3Callable } from '../util';
8
+
9
+ const createLine = line();
10
+
11
+ export type MarkerOptions = {
12
+ arrowSize?: number;
13
+ };
14
+
15
+ const createArrow =
16
+ (length: number, offset: number, start: boolean): D3Callable =>
17
+ (el) => {
18
+ const height = length * 0.5;
19
+ el.attr('markerWidth', length * 2)
20
+ .attr('markerHeight', height * 2)
21
+ .attr('viewBox', `-${length},-${height},${length * 2},${height * 2}`)
22
+ .attr('orient', 'auto')
23
+ .attr('refX', offset)
24
+ .append('path')
25
+ .attr('fill', 'none')
26
+ // Inherit stroke from path.
27
+ .attr('stroke', 'context-stroke')
28
+ .attr(
29
+ 'd',
30
+ createLine([
31
+ start ? [length, height] : [-length, -height],
32
+ [0, 0],
33
+ start ? [length, -height] : [-length, height],
34
+ ]),
35
+ );
36
+ };
37
+
38
+ const createDot =
39
+ (size: number): D3Callable =>
40
+ (el) => {
41
+ el.attr('markerWidth', size * 2)
42
+ .attr('markerHeight', size * 2)
43
+ .attr('viewBox', `-${size},-${size},${size * 2},${size * 2}`)
44
+ .attr('orient', 'auto')
45
+ .append('circle')
46
+ // Inherit stroke from path.
47
+ .attr('fill', 'context-stroke')
48
+ .attr('r', size / 2 - 1);
49
+ };
50
+
51
+ /**
52
+ * https://developer.mozilla.org/en-US/docs/Web/SVG/Element/marker
53
+ * https://www.dashingd3js.com/svg-paths-and-d3js
54
+ * http://bl.ocks.org/dustinlarimer/5888271
55
+ */
56
+ export const createMarkers =
57
+ ({ arrowSize = 12 }: MarkerOptions = {}): D3Callable =>
58
+ (group) => {
59
+ return group
60
+ .selectAll('marker')
61
+ .data([
62
+ {
63
+ id: 'marker-arrow-start',
64
+ generator: createArrow(arrowSize, -0.5, true),
65
+ className: 'dx-arrow',
66
+ },
67
+ {
68
+ id: 'marker-arrow-end',
69
+ generator: createArrow(arrowSize, 0.5, false),
70
+ className: 'dx-arrow',
71
+ },
72
+ {
73
+ id: 'marker-dot',
74
+ generator: createDot(6),
75
+ className: 'dx-dot',
76
+ },
77
+ ])
78
+ .join('marker')
79
+ .attr('id', (d) => d.id)
80
+ .attr('markerUnits', 'strokeWidth')
81
+ .attr('class', (d) => d.className)
82
+ .each((d, i, nodes) => {
83
+ select(nodes[i]).call(d.generator);
84
+ });
85
+ };