@dxos/plugin-explorer 0.8.4-main.c4373fc → 0.8.4-main.d9fc60f731

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 (278) hide show
  1. package/LICENSE +102 -5
  2. package/PLUGIN.mdl +340 -0
  3. package/dist/lib/neutral/ExplorerArticle-4I7PNGDC.mjs +459 -0
  4. package/dist/lib/neutral/ExplorerArticle-4I7PNGDC.mjs.map +7 -0
  5. package/dist/lib/neutral/ExplorerPlugin.mjs +10 -0
  6. package/dist/lib/neutral/capabilities/index.mjs +11 -0
  7. package/dist/lib/neutral/capabilities/index.mjs.map +7 -0
  8. package/dist/lib/neutral/chunk-3D7BYXOR.mjs +37 -0
  9. package/dist/lib/neutral/chunk-3D7BYXOR.mjs.map +7 -0
  10. package/dist/lib/neutral/chunk-42BYLQQA.mjs +42 -0
  11. package/dist/lib/neutral/chunk-42BYLQQA.mjs.map +7 -0
  12. package/dist/lib/neutral/chunk-7XUDLV6E.mjs +287 -0
  13. package/dist/lib/neutral/chunk-7XUDLV6E.mjs.map +7 -0
  14. package/dist/lib/neutral/chunk-HI324IB4.mjs +69 -0
  15. package/dist/lib/neutral/chunk-HI324IB4.mjs.map +7 -0
  16. package/dist/lib/neutral/chunk-IKHJV3Q4.mjs +20 -0
  17. package/dist/lib/neutral/chunk-IKHJV3Q4.mjs.map +7 -0
  18. package/dist/lib/neutral/chunk-J5LGTIGS.mjs +10 -0
  19. package/dist/lib/{browser/chunk-UCDNCIRV.mjs → neutral/components/index.mjs} +882 -556
  20. package/dist/lib/neutral/components/index.mjs.map +7 -0
  21. package/dist/lib/neutral/containers/index.mjs +9 -0
  22. package/dist/lib/neutral/containers/index.mjs.map +7 -0
  23. package/dist/lib/neutral/create-object-F6TKVAGV.mjs +39 -0
  24. package/dist/lib/neutral/create-object-F6TKVAGV.mjs.map +7 -0
  25. package/dist/lib/neutral/hooks/index.mjs +45 -0
  26. package/dist/lib/neutral/hooks/index.mjs.map +7 -0
  27. package/dist/lib/neutral/index.mjs +14 -0
  28. package/dist/lib/neutral/meta.json +1 -0
  29. package/dist/lib/{browser → neutral}/meta.mjs +2 -1
  30. package/dist/lib/neutral/plugin.mjs +12 -0
  31. package/dist/lib/neutral/plugin.mjs.map +7 -0
  32. package/dist/lib/neutral/react-surface-APBW2VQG.mjs +26 -0
  33. package/dist/lib/neutral/react-surface-APBW2VQG.mjs.map +7 -0
  34. package/dist/lib/neutral/testing/index.mjs +139 -0
  35. package/dist/lib/neutral/testing/index.mjs.map +7 -0
  36. package/dist/lib/neutral/translations.mjs +33 -0
  37. package/dist/lib/neutral/translations.mjs.map +7 -0
  38. package/dist/lib/neutral/types/index.mjs +10 -0
  39. package/dist/lib/neutral/types/index.mjs.map +7 -0
  40. package/dist/types/data/cities.d.ts +4 -4
  41. package/dist/types/data/cities.d.ts.map +1 -1
  42. package/dist/types/data/countries-110m.d.ts +19 -22
  43. package/dist/types/data/countries-110m.d.ts.map +1 -1
  44. package/dist/types/src/ExplorerPlugin.d.ts +3 -1
  45. package/dist/types/src/ExplorerPlugin.d.ts.map +1 -1
  46. package/dist/types/src/ExplorerPlugin.test.d.ts +2 -0
  47. package/dist/types/src/ExplorerPlugin.test.d.ts.map +1 -0
  48. package/dist/types/src/capabilities/create-object.d.ts +11 -0
  49. package/dist/types/src/capabilities/create-object.d.ts.map +1 -0
  50. package/dist/types/src/capabilities/index.d.ts +8 -2
  51. package/dist/types/src/capabilities/index.d.ts.map +1 -1
  52. package/dist/types/src/capabilities/react-surface.d.ts +3 -2
  53. package/dist/types/src/capabilities/react-surface.d.ts.map +1 -1
  54. package/dist/types/src/components/Chart/Chart.d.ts +1 -1
  55. package/dist/types/src/components/Chart/Chart.d.ts.map +1 -1
  56. package/dist/types/src/components/Chart/Chart.stories.d.ts +4 -1
  57. package/dist/types/src/components/Chart/Chart.stories.d.ts.map +1 -1
  58. package/dist/types/src/components/Globe/Globe.d.ts +1 -1
  59. package/dist/types/src/components/Globe/Globe.d.ts.map +1 -1
  60. package/dist/types/src/components/Globe/Globe.stories.d.ts +5 -2
  61. package/dist/types/src/components/Globe/Globe.stories.d.ts.map +1 -1
  62. package/dist/types/src/components/Graph/CanvasForceGraph.d.ts +13 -0
  63. package/dist/types/src/components/Graph/CanvasForceGraph.d.ts.map +1 -0
  64. package/dist/types/src/components/Graph/CanvasForceGraph.stories.d.ts +17 -0
  65. package/dist/types/src/components/Graph/CanvasForceGraph.stories.d.ts.map +1 -0
  66. package/dist/types/src/components/Graph/ForceGraph.d.ts +12 -5
  67. package/dist/types/src/components/Graph/ForceGraph.d.ts.map +1 -1
  68. package/dist/types/src/components/Graph/ForceGraph.stories.d.ts +4 -2
  69. package/dist/types/src/components/Graph/ForceGraph.stories.d.ts.map +1 -1
  70. package/dist/types/src/components/Graph/{adapter.d.ts → graph-adapter.d.ts} +2 -2
  71. package/dist/types/src/components/Graph/graph-adapter.d.ts.map +1 -0
  72. package/dist/types/src/components/Graph/index.d.ts +1 -1
  73. package/dist/types/src/components/Graph/index.d.ts.map +1 -1
  74. package/dist/types/src/components/Lattice/Lattice.d.ts +20 -0
  75. package/dist/types/src/components/Lattice/Lattice.d.ts.map +1 -0
  76. package/dist/types/src/components/Lattice/Lattice.stories.d.ts +8 -0
  77. package/dist/types/src/components/Lattice/Lattice.stories.d.ts.map +1 -0
  78. package/dist/types/src/components/Lattice/index.d.ts +2 -0
  79. package/dist/types/src/components/Lattice/index.d.ts.map +1 -0
  80. package/dist/types/src/components/Tree/EdgeBundling.stories.d.ts +21 -0
  81. package/dist/types/src/components/Tree/EdgeBundling.stories.d.ts.map +1 -0
  82. package/dist/types/src/components/Tree/Tree.d.ts +20 -23
  83. package/dist/types/src/components/Tree/Tree.d.ts.map +1 -1
  84. package/dist/types/src/components/Tree/Tree.stories.d.ts +5 -12
  85. package/dist/types/src/components/Tree/Tree.stories.d.ts.map +1 -1
  86. package/dist/types/src/components/Tree/index.d.ts +2 -0
  87. package/dist/types/src/components/Tree/index.d.ts.map +1 -1
  88. package/dist/types/src/components/Tree/layout/HierarchicalEdgeBundling.d.ts +37 -2
  89. package/dist/types/src/components/Tree/layout/HierarchicalEdgeBundling.d.ts.map +1 -1
  90. package/dist/types/src/components/Tree/layout/RadialTree.d.ts +35 -2
  91. package/dist/types/src/components/Tree/layout/RadialTree.d.ts.map +1 -1
  92. package/dist/types/src/components/Tree/layout/TidyTree.d.ts +24 -2
  93. package/dist/types/src/components/Tree/layout/TidyTree.d.ts.map +1 -1
  94. package/dist/types/src/components/Tree/layout/hierarchy.d.ts +17 -0
  95. package/dist/types/src/components/Tree/layout/hierarchy.d.ts.map +1 -0
  96. package/dist/types/src/components/Tree/layout/index.d.ts +5 -4
  97. package/dist/types/src/components/Tree/layout/index.d.ts.map +1 -1
  98. package/dist/types/src/components/Tree/layout/slots.d.ts +7 -0
  99. package/dist/types/src/components/Tree/layout/slots.d.ts.map +1 -0
  100. package/dist/types/src/components/Tree/layout/useContainerSize.d.ts +15 -0
  101. package/dist/types/src/components/Tree/layout/useContainerSize.d.ts.map +1 -0
  102. package/dist/types/src/components/Tree/types/tree.d.ts +50 -27
  103. package/dist/types/src/components/Tree/types/tree.d.ts.map +1 -1
  104. package/dist/types/src/components/Tree/types/types.d.ts +14 -4
  105. package/dist/types/src/components/Tree/types/types.d.ts.map +1 -1
  106. package/dist/types/src/components/index.d.ts +1 -4
  107. package/dist/types/src/components/index.d.ts.map +1 -1
  108. package/dist/types/src/containers/ExplorerArticle/ExplorerArticle.d.ts +8 -0
  109. package/dist/types/src/containers/ExplorerArticle/ExplorerArticle.d.ts.map +1 -0
  110. package/dist/types/src/containers/ExplorerArticle/ExplorerArticle.stories.d.ts +15 -0
  111. package/dist/types/src/containers/ExplorerArticle/ExplorerArticle.stories.d.ts.map +1 -0
  112. package/dist/types/src/containers/ExplorerArticle/Visualization.d.ts +18 -0
  113. package/dist/types/src/containers/ExplorerArticle/Visualization.d.ts.map +1 -0
  114. package/dist/types/src/containers/ExplorerArticle/index.d.ts +2 -0
  115. package/dist/types/src/containers/ExplorerArticle/index.d.ts.map +1 -0
  116. package/dist/types/src/containers/ExplorerArticle/variants.d.ts +9 -0
  117. package/dist/types/src/containers/ExplorerArticle/variants.d.ts.map +1 -0
  118. package/dist/types/src/containers/index.d.ts +3 -0
  119. package/dist/types/src/containers/index.d.ts.map +1 -0
  120. package/dist/types/src/hooks/useGraphModel.d.ts +2 -2
  121. package/dist/types/src/hooks/useGraphModel.d.ts.map +1 -1
  122. package/dist/types/src/index.d.ts +1 -3
  123. package/dist/types/src/index.d.ts.map +1 -1
  124. package/dist/types/src/meta.d.ts +2 -2
  125. package/dist/types/src/meta.d.ts.map +1 -1
  126. package/dist/types/src/plugin.d.ts +3 -0
  127. package/dist/types/src/plugin.d.ts.map +1 -0
  128. package/dist/types/src/{components/Tree/testing → testing}/generator.d.ts +1 -1
  129. package/dist/types/src/testing/generator.d.ts.map +1 -0
  130. package/dist/types/src/testing/index.d.ts +4 -0
  131. package/dist/types/src/testing/index.d.ts.map +1 -0
  132. package/dist/types/src/testing/relations.d.ts +32 -0
  133. package/dist/types/src/testing/relations.d.ts.map +1 -0
  134. package/dist/types/src/translations.d.ts +31 -22
  135. package/dist/types/src/translations.d.ts.map +1 -1
  136. package/dist/types/src/types/ExplorerAction.d.ts +6 -0
  137. package/dist/types/src/types/ExplorerAction.d.ts.map +1 -0
  138. package/dist/types/src/types/Graph.d.ts +22 -0
  139. package/dist/types/src/types/Graph.d.ts.map +1 -0
  140. package/dist/types/src/types/index.d.ts +2 -2
  141. package/dist/types/src/types/index.d.ts.map +1 -1
  142. package/dist/types/src/util/index.d.ts +3 -0
  143. package/dist/types/src/util/index.d.ts.map +1 -0
  144. package/dist/types/src/util/node-color.d.ts +13 -0
  145. package/dist/types/src/util/node-color.d.ts.map +1 -0
  146. package/dist/types/src/{components → util}/plot.d.ts +1 -1
  147. package/dist/types/src/util/plot.d.ts.map +1 -0
  148. package/dist/types/tsconfig.tsbuildinfo +1 -1
  149. package/package.json +113 -65
  150. package/src/ExplorerPlugin.test.ts +26 -0
  151. package/src/ExplorerPlugin.tsx +20 -52
  152. package/src/capabilities/create-object.ts +36 -0
  153. package/src/capabilities/index.ts +3 -3
  154. package/src/capabilities/react-surface.tsx +24 -15
  155. package/src/components/Chart/Chart.stories.tsx +16 -23
  156. package/src/components/Chart/Chart.tsx +1 -1
  157. package/src/components/Globe/Globe.stories.tsx +19 -22
  158. package/src/components/Globe/Globe.tsx +1 -1
  159. package/src/components/Graph/CanvasForceGraph.stories.tsx +97 -0
  160. package/src/components/Graph/CanvasForceGraph.tsx +124 -0
  161. package/src/components/Graph/ForceGraph.stories.tsx +100 -35
  162. package/src/components/Graph/ForceGraph.tsx +105 -85
  163. package/src/components/Graph/{adapter.ts → graph-adapter.ts} +14 -8
  164. package/src/components/Graph/index.ts +1 -1
  165. package/src/components/Lattice/Lattice.stories.tsx +104 -0
  166. package/src/components/Lattice/Lattice.tsx +182 -0
  167. package/src/components/Lattice/index.ts +5 -0
  168. package/src/components/Tree/EdgeBundling.stories.tsx +144 -0
  169. package/src/components/Tree/Tree.stories.tsx +20 -38
  170. package/src/components/Tree/Tree.tsx +69 -95
  171. package/src/components/Tree/index.ts +2 -0
  172. package/src/components/Tree/layout/HierarchicalEdgeBundling.tsx +335 -0
  173. package/src/components/Tree/layout/RadialTree.tsx +242 -0
  174. package/src/components/Tree/layout/TidyTree.tsx +246 -0
  175. package/src/components/Tree/layout/hierarchy.ts +32 -0
  176. package/src/components/Tree/layout/index.ts +5 -5
  177. package/src/components/Tree/layout/slots.ts +19 -0
  178. package/src/components/Tree/layout/useContainerSize.ts +43 -0
  179. package/src/components/Tree/types/tree.test.ts +8 -7
  180. package/src/components/Tree/types/tree.ts +51 -35
  181. package/src/components/Tree/types/types.ts +38 -29
  182. package/src/components/index.ts +1 -4
  183. package/src/containers/ExplorerArticle/ExplorerArticle.stories.tsx +152 -0
  184. package/src/containers/ExplorerArticle/ExplorerArticle.tsx +120 -0
  185. package/src/containers/ExplorerArticle/Visualization.tsx +523 -0
  186. package/src/containers/ExplorerArticle/index.ts +5 -0
  187. package/src/containers/ExplorerArticle/variants.ts +47 -0
  188. package/src/containers/index.ts +7 -0
  189. package/src/hooks/useGraphModel.ts +25 -14
  190. package/src/index.ts +1 -4
  191. package/src/meta.ts +30 -6
  192. package/src/plugin.ts +9 -0
  193. package/src/{components/Tree/testing → testing}/generator.ts +6 -4
  194. package/src/testing/index.ts +9 -0
  195. package/src/testing/relations.ts +117 -0
  196. package/src/translations.ts +16 -13
  197. package/src/types/ExplorerAction.ts +20 -0
  198. package/src/types/Graph.ts +41 -0
  199. package/src/types/index.ts +2 -2
  200. package/src/typings.d.ts +8 -0
  201. package/src/util/index.ts +6 -0
  202. package/src/util/node-color.ts +23 -0
  203. package/src/{components → util}/plot.ts +16 -4
  204. package/src/vite-env.d.ts +10 -0
  205. package/dist/lib/browser/ExplorerContainer-S66JDOAF.mjs +0 -49
  206. package/dist/lib/browser/ExplorerContainer-S66JDOAF.mjs.map +0 -7
  207. package/dist/lib/browser/chunk-2DGFNLRO.mjs +0 -19
  208. package/dist/lib/browser/chunk-2DGFNLRO.mjs.map +0 -7
  209. package/dist/lib/browser/chunk-2MKBRIUT.mjs +0 -31
  210. package/dist/lib/browser/chunk-2MKBRIUT.mjs.map +0 -7
  211. package/dist/lib/browser/chunk-4ETQJYX4.mjs +0 -38
  212. package/dist/lib/browser/chunk-4ETQJYX4.mjs.map +0 -7
  213. package/dist/lib/browser/chunk-NXGP6NTP.mjs +0 -203
  214. package/dist/lib/browser/chunk-NXGP6NTP.mjs.map +0 -7
  215. package/dist/lib/browser/chunk-UCDNCIRV.mjs.map +0 -7
  216. package/dist/lib/browser/index.mjs +0 -112
  217. package/dist/lib/browser/index.mjs.map +0 -7
  218. package/dist/lib/browser/intent-resolver-OXJJ3PII.mjs +0 -24
  219. package/dist/lib/browser/intent-resolver-OXJJ3PII.mjs.map +0 -7
  220. package/dist/lib/browser/meta.json +0 -1
  221. package/dist/lib/browser/react-surface-C4EC6ZDZ.mjs +0 -31
  222. package/dist/lib/browser/react-surface-C4EC6ZDZ.mjs.map +0 -7
  223. package/dist/lib/browser/types/index.mjs +0 -10
  224. package/dist/lib/node-esm/ExplorerContainer-GIJN67DO.mjs +0 -50
  225. package/dist/lib/node-esm/ExplorerContainer-GIJN67DO.mjs.map +0 -7
  226. package/dist/lib/node-esm/chunk-3ODK27PU.mjs +0 -33
  227. package/dist/lib/node-esm/chunk-3ODK27PU.mjs.map +0 -7
  228. package/dist/lib/node-esm/chunk-6JACZE7E.mjs +0 -205
  229. package/dist/lib/node-esm/chunk-6JACZE7E.mjs.map +0 -7
  230. package/dist/lib/node-esm/chunk-AGHU3KVI.mjs +0 -39
  231. package/dist/lib/node-esm/chunk-AGHU3KVI.mjs.map +0 -7
  232. package/dist/lib/node-esm/chunk-PX6LHR2N.mjs +0 -21
  233. package/dist/lib/node-esm/chunk-PX6LHR2N.mjs.map +0 -7
  234. package/dist/lib/node-esm/chunk-WHKUQG5M.mjs +0 -11343
  235. package/dist/lib/node-esm/chunk-WHKUQG5M.mjs.map +0 -7
  236. package/dist/lib/node-esm/index.mjs +0 -113
  237. package/dist/lib/node-esm/index.mjs.map +0 -7
  238. package/dist/lib/node-esm/intent-resolver-GVM36TJX.mjs +0 -25
  239. package/dist/lib/node-esm/intent-resolver-GVM36TJX.mjs.map +0 -7
  240. package/dist/lib/node-esm/meta.json +0 -1
  241. package/dist/lib/node-esm/meta.mjs +0 -8
  242. package/dist/lib/node-esm/react-surface-YGGLTBF3.mjs +0 -32
  243. package/dist/lib/node-esm/react-surface-YGGLTBF3.mjs.map +0 -7
  244. package/dist/lib/node-esm/types/index.mjs +0 -11
  245. package/dist/types/src/capabilities/intent-resolver.d.ts +0 -4
  246. package/dist/types/src/capabilities/intent-resolver.d.ts.map +0 -1
  247. package/dist/types/src/components/ExplorerContainer.d.ts +0 -9
  248. package/dist/types/src/components/ExplorerContainer.d.ts.map +0 -1
  249. package/dist/types/src/components/Graph/D3ForceGraph.d.ts +0 -14
  250. package/dist/types/src/components/Graph/D3ForceGraph.d.ts.map +0 -1
  251. package/dist/types/src/components/Graph/D3ForceGraph.stories.d.ts +0 -15
  252. package/dist/types/src/components/Graph/D3ForceGraph.stories.d.ts.map +0 -1
  253. package/dist/types/src/components/Graph/adapter.d.ts.map +0 -1
  254. package/dist/types/src/components/Graph/testing.d.ts +0 -14
  255. package/dist/types/src/components/Graph/testing.d.ts.map +0 -1
  256. package/dist/types/src/components/Tree/testing/generator.d.ts.map +0 -1
  257. package/dist/types/src/components/Tree/testing/index.d.ts +0 -2
  258. package/dist/types/src/components/Tree/testing/index.d.ts.map +0 -1
  259. package/dist/types/src/components/plot.d.ts.map +0 -1
  260. package/dist/types/src/types/schema.d.ts +0 -12
  261. package/dist/types/src/types/schema.d.ts.map +0 -1
  262. package/dist/types/src/types/types.d.ts +0 -18
  263. package/dist/types/src/types/types.d.ts.map +0 -1
  264. package/src/capabilities/intent-resolver.ts +0 -19
  265. package/src/components/ExplorerContainer.tsx +0 -50
  266. package/src/components/Graph/D3ForceGraph.stories.tsx +0 -68
  267. package/src/components/Graph/D3ForceGraph.tsx +0 -101
  268. package/src/components/Graph/testing.ts +0 -55
  269. package/src/components/Tree/layout/HierarchicalEdgeBundling.ts +0 -162
  270. package/src/components/Tree/layout/RadialTree.ts +0 -94
  271. package/src/components/Tree/layout/TidyTree.ts +0 -101
  272. package/src/components/Tree/testing/index.ts +0 -5
  273. package/src/types/schema.ts +0 -16
  274. package/src/types/types.ts +0 -22
  275. /package/dist/lib/{browser/meta.mjs.map → neutral/ExplorerPlugin.mjs.map} +0 -0
  276. /package/dist/lib/{browser/types/index.mjs.map → neutral/chunk-J5LGTIGS.mjs.map} +0 -0
  277. /package/dist/lib/{node-esm/types → neutral}/index.mjs.map +0 -0
  278. /package/dist/lib/{node-esm → neutral}/meta.mjs.map +0 -0
@@ -0,0 +1,242 @@
1
+ //
2
+ // Copyright 2023 DXOS.org
3
+ // Copyright 2024 Observable, Inc.
4
+ //
5
+
6
+ import { cluster as d3Cluster, linkRadial, select, tree as d3Tree } from 'd3';
7
+ import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react';
8
+
9
+ import { type Obj } from '@dxos/echo';
10
+ import { type ThemedClassName } from '@dxos/react-ui';
11
+ import { mx } from '@dxos/ui-theme';
12
+
13
+ import { getNodeFillForObject } from '../../../util/node-color';
14
+ import { type TreeNode } from '../types';
15
+ import { buildHierarchy, isCollapsed, isLeaf } from './hierarchy';
16
+ import { type TreeLayoutSlots, defaultTreeLayoutSlots } from './slots';
17
+ import { useContainerSize } from './useContainerSize';
18
+
19
+ const TRANSITION_MS = 350;
20
+
21
+ export type RadialTreeProps = ThemedClassName<{
22
+ data: TreeNode;
23
+ label?: (d: TreeNode) => string;
24
+ slots?: TreeLayoutSlots;
25
+ /** Node radius. */
26
+ r?: number;
27
+ /** Optional padding (in screen pixels) reserved around the radial layout. */
28
+ padding?: number;
29
+ /** Initial set of collapsed node ids. */
30
+ initialCollapsed?: Iterable<string>;
31
+ /**
32
+ * Use `d3.cluster` (all leaves equidistant from center) instead of `d3.tree`.
33
+ * Matches https://observablehq.com/@d3/radial-cluster.
34
+ */
35
+ cluster?: boolean;
36
+ /** Notified when the user clicks a node. */
37
+ onNodeClick?: (node: TreeNode) => void;
38
+ /**
39
+ * Notified on pointerenter (and `null` on pointerleave) for nodes. Used to wire previews
40
+ * (dispatch `DxAnchorActivate`). The event target on enter is the hovered circle — dispatch
41
+ * from it so the preview anchors there.
42
+ */
43
+ onNodeHover?: (node: TreeNode | null, event?: MouseEvent) => void;
44
+ }>;
45
+
46
+ /**
47
+ * Radial tree layout based on the D3 reference component.
48
+ * https://observablehq.com/@d3/radial-tree-component
49
+ *
50
+ * Click a node with children to toggle collapse / expand.
51
+ */
52
+ export const RadialTree = ({
53
+ classNames,
54
+ data,
55
+ label = (d) => d.label ?? d.id,
56
+ slots = defaultTreeLayoutSlots,
57
+ r = 4,
58
+ padding = 80,
59
+ initialCollapsed,
60
+ cluster = false,
61
+ onNodeClick,
62
+ onNodeHover,
63
+ }: RadialTreeProps) => {
64
+ const svgRef = useRef<SVGSVGElement | null>(null);
65
+ const { setRef, width, height } = useContainerSize();
66
+
67
+ const [collapsed, setCollapsed] = useState<Set<string>>(() => new Set(initialCollapsed ?? []));
68
+ const toggle = useCallback((id: string) => {
69
+ setCollapsed((prev) => {
70
+ const next = new Set(prev);
71
+ if (next.has(id)) {
72
+ next.delete(id);
73
+ } else {
74
+ next.add(id);
75
+ }
76
+ return next;
77
+ });
78
+ }, []);
79
+
80
+ const handleClickRef = useRef<(node: TreeNode) => void>(() => {});
81
+ handleClickRef.current = (node: TreeNode) => {
82
+ onNodeClick?.(node);
83
+ if (node.children?.length) {
84
+ toggle(node.id);
85
+ }
86
+ };
87
+
88
+ const handleHoverRef = useRef<(node: TreeNode | null, event?: MouseEvent) => void>(() => {});
89
+ handleHoverRef.current = (node: TreeNode | null, event?: MouseEvent) => onNodeHover?.(node, event);
90
+
91
+ const root = useMemo(() => buildHierarchy(data, collapsed), [data, collapsed]);
92
+
93
+ useEffect(() => {
94
+ if (!svgRef.current || !width || !height) {
95
+ return;
96
+ }
97
+
98
+ const radius = Math.max(0, Math.min(width, height) / 2 - padding);
99
+ renderRadialTree(svgRef.current, root, {
100
+ radius,
101
+ r,
102
+ label,
103
+ slots,
104
+ collapsed,
105
+ cluster,
106
+ onNodeClick: (n) => handleClickRef.current(n),
107
+ onNodeHover: (n, e) => handleHoverRef.current(n, e),
108
+ });
109
+ }, [root, width, height, r, padding, label, slots, collapsed, cluster]);
110
+
111
+ return (
112
+ <div ref={setRef} className={mx('dx-expander relative', classNames)}>
113
+ {width > 0 && height > 0 && (
114
+ <svg
115
+ ref={svgRef}
116
+ xmlns='http://www.w3.org/2000/svg'
117
+ width={width}
118
+ height={height}
119
+ viewBox={`${-width / 2} ${-height / 2} ${width} ${height}`}
120
+ />
121
+ )}
122
+ </div>
123
+ );
124
+ };
125
+
126
+ type RenderOptions = {
127
+ radius: number;
128
+ r: number;
129
+ label: (d: TreeNode) => string;
130
+ slots: TreeLayoutSlots;
131
+ collapsed: Set<string>;
132
+ cluster: boolean;
133
+ onNodeClick: (node: TreeNode) => void;
134
+ onNodeHover: (node: TreeNode | null, event?: MouseEvent) => void;
135
+ };
136
+
137
+ const renderRadialTree = (svgElement: SVGSVGElement, root: any, options: RenderOptions) => {
138
+ const { radius, r, label, slots, collapsed, cluster, onNodeClick, onNodeHover } = options;
139
+ const svg = select(svgElement);
140
+
141
+ const layout = cluster ? d3Cluster<TreeNode>() : d3Tree<TreeNode>();
142
+ layout
143
+ .size([2 * Math.PI, radius])
144
+ .separation((a: any, b: any) => (a.parent === b.parent ? 1 : 2) / Math.max(1, a.depth))(root);
145
+
146
+ const g = svg.selectAll<SVGGElement, null>('g.dx-radial-root').data([null]).join('g').classed('dx-radial-root', true);
147
+
148
+ const linksLayer = g
149
+ .selectAll<SVGGElement, null>('g.dx-radial-links')
150
+ .data([null])
151
+ .join('g')
152
+ .classed('dx-radial-links', true);
153
+
154
+ const nodesLayer = g
155
+ .selectAll<SVGGElement, null>('g.dx-radial-nodes')
156
+ .data([null])
157
+ .join('g')
158
+ .classed('dx-radial-nodes', true);
159
+
160
+ const linkPath = linkRadial<any, any>()
161
+ .angle((d: any) => d.x)
162
+ .radius((d: any) => d.y);
163
+
164
+ linksLayer
165
+ .selectAll<SVGPathElement, any>('path')
166
+ .data(root.links(), (d: any) => `${d.source.data.id}->${d.target.data.id}`)
167
+ .join(
168
+ (enter) =>
169
+ enter
170
+ .append('path')
171
+ .attr('class', slots.path ?? '')
172
+ .attr('fill', 'none')
173
+ .attr('opacity', 0),
174
+ (update) => update,
175
+ (exit) => exit.transition().duration(TRANSITION_MS).attr('opacity', 0).remove(),
176
+ )
177
+ .transition()
178
+ .duration(TRANSITION_MS)
179
+ .attr('opacity', 1)
180
+ .attr('d', linkPath);
181
+
182
+ const node = nodesLayer
183
+ .selectAll<SVGGElement, any>('g.dx-radial-node')
184
+ .data(root.descendants(), (d: any) => d.data.id);
185
+
186
+ const nodeEnter = node
187
+ .enter()
188
+ .append('g')
189
+ .classed('dx-radial-node', true)
190
+ .attr('opacity', 0)
191
+ .attr('transform', (d: any) => `rotate(${(d.x * 180) / Math.PI - 90}) translate(${d.y},0)`)
192
+ .style('cursor', (d: any) => (d.data.children?.length ? 'pointer' : 'default'))
193
+ .on('click', (_, d: any) => onNodeClick(d.data));
194
+
195
+ nodeEnter
196
+ .append('circle')
197
+ .attr('r', r)
198
+ .on('pointerenter', (event: MouseEvent, d: any) => onNodeHover(d.data, event))
199
+ .on('pointerleave', (event: MouseEvent) => onNodeHover(null, event));
200
+
201
+ nodeEnter
202
+ .append('text')
203
+ .attr('dy', '0.32em')
204
+ .attr('paint-order', 'stroke')
205
+ .text((d: any) => label(d.data));
206
+
207
+ const nodeMerge = nodeEnter.merge(node as any);
208
+
209
+ nodeMerge
210
+ .transition()
211
+ .duration(TRANSITION_MS)
212
+ .attr('opacity', 1)
213
+ .attr('transform', (d: any) => `rotate(${(d.x * 180) / Math.PI - 90}) translate(${d.y},0)`);
214
+
215
+ nodeMerge
216
+ .select<SVGCircleElement>('circle')
217
+ .attr('class', (d: any) => {
218
+ const collapsedHere = isCollapsed(d.data, collapsed);
219
+ const leaf = isLeaf(d.data);
220
+ return [slots.node ?? '', collapsedHere ? 'dx-collapsed' : leaf ? 'dx-leaf' : 'dx-branch']
221
+ .filter(Boolean)
222
+ .join(' ');
223
+ })
224
+ .attr('r', r)
225
+ // Color leaves by typename so cluster matches the force / bundle / lattice variants.
226
+ // Branch nodes (groups, root) keep the default slot fill.
227
+ .style('fill', (d: any) => (isLeaf(d.data) ? getNodeFillForObject(d.data.data as Obj.Unknown | undefined) : null));
228
+
229
+ nodeMerge
230
+ .select<SVGTextElement>('text')
231
+ .attr('class', slots.text ?? '')
232
+ .attr('transform', (d: any) => (d.x >= Math.PI ? 'rotate(180)' : null))
233
+ // eslint-disable-next-line no-mixed-operators
234
+ .attr('x', (d: any) => (d.x < Math.PI === !d.children ? r + 4 : -(r + 4)))
235
+ // eslint-disable-next-line no-mixed-operators
236
+ .attr('text-anchor', (d: any) => (d.x < Math.PI === !d.children ? 'start' : 'end'))
237
+ .text((d: any) => label(d.data));
238
+
239
+ node.exit().transition().duration(TRANSITION_MS).attr('opacity', 0).remove();
240
+ };
241
+
242
+ export default RadialTree;
@@ -0,0 +1,246 @@
1
+ //
2
+ // Copyright 2023 DXOS.org
3
+ // Copyright 2024 Observable, Inc.
4
+ //
5
+
6
+ import { curveBumpX, link as d3Link, select, tree as d3Tree } from 'd3';
7
+ import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react';
8
+
9
+ import { type ThemedClassName } from '@dxos/react-ui';
10
+ import { mx } from '@dxos/ui-theme';
11
+
12
+ import { type TreeNode } from '../types';
13
+ import { buildHierarchy, isCollapsed, isLeaf } from './hierarchy';
14
+ import { type TreeLayoutSlots, defaultTreeLayoutSlots } from './slots';
15
+ import { useContainerSize } from './useContainerSize';
16
+
17
+ const TRANSITION_MS = 350;
18
+
19
+ export type TidyTreeProps = ThemedClassName<{
20
+ data: TreeNode;
21
+ label?: (d: TreeNode) => string;
22
+ slots?: TreeLayoutSlots;
23
+ /** Node radius. */
24
+ r?: number;
25
+ /** Margin in screen pixels reserved around the layout. */
26
+ margin?: number;
27
+ /** Initial set of collapsed node ids. */
28
+ initialCollapsed?: Iterable<string>;
29
+ /** Notified when the user clicks a node. */
30
+ onNodeClick?: (node: TreeNode) => void;
31
+ }>;
32
+
33
+ /**
34
+ * Tidy (horizontal) tree layout based on the D3 reference component.
35
+ * https://observablehq.com/@d3/tree-component
36
+ *
37
+ * Click a node with children to toggle collapse / expand.
38
+ */
39
+ export const TidyTree = ({
40
+ classNames,
41
+ data,
42
+ label = (d) => d.label ?? d.id,
43
+ slots = defaultTreeLayoutSlots,
44
+ r = 4,
45
+ margin = 24,
46
+ initialCollapsed,
47
+ onNodeClick,
48
+ }: TidyTreeProps) => {
49
+ const svgRef = useRef<SVGSVGElement | null>(null);
50
+ const { setRef, width, height } = useContainerSize();
51
+
52
+ const [collapsed, setCollapsed] = useState<Set<string>>(() => new Set(initialCollapsed ?? []));
53
+ const toggle = useCallback((id: string) => {
54
+ setCollapsed((prev) => {
55
+ const next = new Set(prev);
56
+ if (next.has(id)) {
57
+ next.delete(id);
58
+ } else {
59
+ next.add(id);
60
+ }
61
+ return next;
62
+ });
63
+ }, []);
64
+
65
+ // Stable click handler ref so the d3 render fn doesn't need to be re-bound.
66
+ const handleClickRef = useRef<(node: TreeNode) => void>(() => {});
67
+ handleClickRef.current = (node: TreeNode) => {
68
+ onNodeClick?.(node);
69
+ if (node.children?.length) {
70
+ toggle(node.id);
71
+ }
72
+ };
73
+
74
+ const root = useMemo(() => buildHierarchy(data, collapsed), [data, collapsed]);
75
+
76
+ useEffect(() => {
77
+ if (!svgRef.current || !width || !height) {
78
+ return;
79
+ }
80
+
81
+ renderTidyTree(svgRef.current, root, {
82
+ width,
83
+ height,
84
+ r,
85
+ margin,
86
+ label,
87
+ slots,
88
+ collapsed,
89
+ onNodeClick: (n) => handleClickRef.current(n),
90
+ });
91
+ }, [root, width, height, r, margin, label, slots, collapsed]);
92
+
93
+ return (
94
+ <div ref={setRef} className={mx('dx-expander relative', classNames)}>
95
+ {width > 0 && height > 0 && (
96
+ <svg
97
+ ref={svgRef}
98
+ xmlns='http://www.w3.org/2000/svg'
99
+ width={width}
100
+ height={height}
101
+ viewBox={`${-width / 2} ${-height / 2} ${width} ${height}`}
102
+ />
103
+ )}
104
+ </div>
105
+ );
106
+ };
107
+
108
+ type RenderOptions = {
109
+ width: number;
110
+ height: number;
111
+ r: number;
112
+ margin: number;
113
+ label: (d: TreeNode) => string;
114
+ slots: TreeLayoutSlots;
115
+ collapsed: Set<string>;
116
+ onNodeClick: (node: TreeNode) => void;
117
+ };
118
+
119
+ const renderTidyTree = (svgElement: SVGSVGElement, root: any, options: RenderOptions) => {
120
+ const { width, height, r, margin, label, slots, collapsed, onNodeClick } = options;
121
+ const svg = select(svgElement);
122
+
123
+ // Compute layout: nodeSize gives a stable, content-driven scale.
124
+ // dx = vertical spacing between siblings; dy = horizontal spacing between depths.
125
+ const dx = 18;
126
+ const dy = Math.max(60, (width - margin * 2) / Math.max(1, root.height + 1));
127
+ d3Tree<TreeNode>().nodeSize([dx, dy])(root);
128
+
129
+ // Center the tree vertically; lay it out horizontally from left.
130
+ let x0 = Infinity;
131
+ let x1 = -x0;
132
+ root.each((d: any) => {
133
+ if (d.x > x1) {
134
+ x1 = d.x;
135
+ }
136
+ if (d.x < x0) {
137
+ x0 = d.x;
138
+ }
139
+ });
140
+
141
+ const treeWidth = width - margin * 2;
142
+ const treeHeight = x1 - x0;
143
+ const scaleY = treeHeight > 0 ? Math.min(1, (height - margin * 2) / treeHeight) : 1;
144
+ const offsetX = -treeWidth / 2;
145
+ const offsetY = -(x0 + x1) / 2;
146
+
147
+ // Root group ensures consistent transforms on re-renders.
148
+ const g = svg.selectAll<SVGGElement, null>('g.dx-tidy-root').data([null]).join('g').classed('dx-tidy-root', true);
149
+
150
+ // Links layer.
151
+ const linksLayer = g
152
+ .selectAll<SVGGElement, null>('g.dx-tidy-links')
153
+ .data([null])
154
+ .join('g')
155
+ .classed('dx-tidy-links', true);
156
+
157
+ // Nodes layer (rendered above links).
158
+ const nodesLayer = g
159
+ .selectAll<SVGGElement, null>('g.dx-tidy-nodes')
160
+ .data([null])
161
+ .join('g')
162
+ .classed('dx-tidy-nodes', true);
163
+
164
+ // Links.
165
+ const linkPath = d3Link<any, any>(curveBumpX)
166
+ .x((d: any) => offsetX + d.y)
167
+ .y((d: any) => (d.x + offsetY) * scaleY);
168
+
169
+ linksLayer
170
+ .selectAll<SVGPathElement, any>('path')
171
+ .data(root.links(), (d: any) => `${d.source.data.id}->${d.target.data.id}`)
172
+ .join(
173
+ (enter) =>
174
+ enter
175
+ .append('path')
176
+ .attr('class', slots.path ?? '')
177
+ .attr('fill', 'none')
178
+ .attr('opacity', 0),
179
+ (update) => update,
180
+ (exit) => exit.transition().duration(TRANSITION_MS).attr('opacity', 0).remove(),
181
+ )
182
+ .transition()
183
+ .duration(TRANSITION_MS)
184
+ .attr('opacity', 1)
185
+ .attr('d', linkPath);
186
+
187
+ // Nodes.
188
+ const node = nodesLayer.selectAll<SVGGElement, any>('g.dx-tidy-node').data(root.descendants(), (d: any) => d.data.id);
189
+
190
+ const nodeEnter = node
191
+ .enter()
192
+ .append('g')
193
+ .classed('dx-tidy-node', true)
194
+ .attr('transform', (d: any) => `translate(${offsetX + d.y},${(d.x + offsetY) * scaleY})`)
195
+ .attr('opacity', 0)
196
+ .style('cursor', (d: any) => (d.data.children?.length ? 'pointer' : 'default'))
197
+ .on('click', (_, d: any) => onNodeClick(d.data));
198
+
199
+ nodeEnter.append('circle').attr('r', r);
200
+ nodeEnter
201
+ .append('text')
202
+ .attr('dy', '0.32em')
203
+ .attr('x', (d: any) => (d.children ? -(r + 4) : r + 4))
204
+ .attr('text-anchor', (d: any) => (d.children ? 'end' : 'start'))
205
+ .text((d: any) => label(d.data));
206
+
207
+ const nodeMerge = nodeEnter.merge(node as any);
208
+
209
+ nodeMerge
210
+ .transition()
211
+ .duration(TRANSITION_MS)
212
+ .attr('opacity', 1)
213
+ .attr('transform', (d: any) => `translate(${offsetX + d.y},${(d.x + offsetY) * scaleY})`);
214
+
215
+ // Circle: filled when collapsed (has hidden children), outlined when expanded or leaf.
216
+ nodeMerge
217
+ .select<SVGCircleElement>('circle')
218
+ .attr('class', (d: any) => {
219
+ const collapsedHere = isCollapsed(d.data, collapsed);
220
+ const leaf = isLeaf(d.data);
221
+ return [slots.node ?? '', collapsedHere ? 'dx-collapsed' : leaf ? 'dx-leaf' : 'dx-branch']
222
+ .filter(Boolean)
223
+ .join(' ');
224
+ })
225
+ .attr('r', r);
226
+
227
+ nodeMerge
228
+ .select<SVGTextElement>('text')
229
+ .attr('class', slots.text ?? '')
230
+ .attr('x', (d: any) => (d.children ? -(r + 4) : r + 4))
231
+ .attr('text-anchor', (d: any) => (d.children ? 'end' : 'start'))
232
+ .text((d: any) => label(d.data));
233
+
234
+ node
235
+ .exit()
236
+ .each(function () {
237
+ // Cancel any in-flight transitions so .remove() actually fires.
238
+ select(this).interrupt();
239
+ })
240
+ .transition()
241
+ .duration(TRANSITION_MS)
242
+ .attr('opacity', 0)
243
+ .remove();
244
+ };
245
+
246
+ export default TidyTree;
@@ -0,0 +1,32 @@
1
+ //
2
+ // Copyright 2026 DXOS.org
3
+ //
4
+
5
+ import { type HierarchyNode, hierarchy as d3Hierarchy } from 'd3';
6
+
7
+ import { type TreeNode } from '../types';
8
+
9
+ /**
10
+ * Build a d3 hierarchy from a TreeNode, pruning children of nodes whose ids are in `collapsed`.
11
+ * Nodes that have children but are collapsed retain their identity in the hierarchy and can be
12
+ * distinguished by `node._children` (the original list).
13
+ */
14
+ export const buildHierarchy = (data: TreeNode, collapsed: Set<string> = new Set()): HierarchyNode<TreeNode> => {
15
+ return d3Hierarchy<TreeNode>(data, (d) => {
16
+ if (!d.children?.length) {
17
+ return undefined;
18
+ }
19
+ return collapsed.has(d.id) ? undefined : d.children;
20
+ });
21
+ };
22
+
23
+ /**
24
+ * True when the node has children that have been hidden via collapse.
25
+ */
26
+ export const isCollapsed = (data: TreeNode, collapsed: Set<string>): boolean =>
27
+ Boolean(data.children?.length) && collapsed.has(data.id);
28
+
29
+ /**
30
+ * True when the node has no children at all (a real leaf, not a collapsed branch).
31
+ */
32
+ export const isLeaf = (data: TreeNode): boolean => !data.children?.length;
@@ -2,8 +2,8 @@
2
2
  // Copyright 2023 DXOS.org
3
3
  //
4
4
 
5
- import HierarchicalEdgeBundling from './HierarchicalEdgeBundling';
6
- import RadialTree from './RadialTree';
7
- import TidyTree from './TidyTree';
8
-
9
- export { HierarchicalEdgeBundling, RadialTree, TidyTree };
5
+ export * from './HierarchicalEdgeBundling';
6
+ export * from './RadialTree';
7
+ export * from './TidyTree';
8
+ export * from './hierarchy';
9
+ export * from './slots';
@@ -0,0 +1,19 @@
1
+ //
2
+ // Copyright 2026 DXOS.org
3
+ //
4
+
5
+ export type TreeLayoutSlots = {
6
+ node?: string;
7
+ path?: string;
8
+ text?: string;
9
+ };
10
+
11
+ export const defaultTreeLayoutSlots: TreeLayoutSlots = {
12
+ // Cursor + transition so the hover swap reads clearly; SVG circles support the `:hover` pseudo-class
13
+ // via Tailwind variants exactly like HTML elements.
14
+ node: 'fill-blue-600 hover:fill-orange-500 cursor-pointer transition-colors',
15
+ // 0.5px is fine on a white background, but on a dark Storybook background the lines disappear.
16
+ // Use stroke-1 with opacity 50% so they read in both themes; dx-bundle-dim/out/in further tune on hover.
17
+ path: 'fill-none stroke-blue-500/50 stroke-[1px] dark:stroke-blue-400/60',
18
+ text: 'fill-neutral-700 dark:fill-neutral-300 text-xs hover:fill-orange-500 cursor-pointer transition-colors',
19
+ };
@@ -0,0 +1,43 @@
1
+ //
2
+ // Copyright 2026 DXOS.org
3
+ //
4
+
5
+ import { useEffect, useState } from 'react';
6
+
7
+ /**
8
+ * Track an element's content-box size via ResizeObserver.
9
+ * Returns the most recently observed `{ width, height }` plus a callback ref to attach to the element.
10
+ *
11
+ * Why not `react-resize-detector` directly: its `targetRef` API doesn't pick up a ref whose
12
+ * `.current` is set later than the hook runs, and the returned-`ref` API forces the consumer to
13
+ * forward a callback ref through their own component — which is awkward for class refs.
14
+ * This hook returns a setter so the consumer assigns it directly to `<div ref={setRef}>`.
15
+ */
16
+ export const useContainerSize = (): {
17
+ setRef: (el: HTMLDivElement | null) => void;
18
+ width: number;
19
+ height: number;
20
+ } => {
21
+ const [el, setEl] = useState<HTMLDivElement | null>(null);
22
+ const [size, setSize] = useState<{ width: number; height: number }>({ width: 0, height: 0 });
23
+
24
+ useEffect(() => {
25
+ if (!el) {
26
+ return;
27
+ }
28
+ const rect = el.getBoundingClientRect();
29
+ setSize({ width: rect.width, height: rect.height });
30
+ const observer = new ResizeObserver((entries) => {
31
+ const entry = entries[0];
32
+ if (!entry) {
33
+ return;
34
+ }
35
+ const { width, height } = entry.contentRect;
36
+ setSize((prev) => (prev.width === width && prev.height === height ? prev : { width, height }));
37
+ });
38
+ observer.observe(el);
39
+ return () => observer.disconnect();
40
+ }, [el]);
41
+
42
+ return { setRef: setEl, width: size.width, height: size.height };
43
+ };
@@ -5,14 +5,13 @@
5
5
  import { describe, test } from 'vitest';
6
6
 
7
7
  import { Obj, Ref } from '@dxos/echo';
8
- import { faker } from '@dxos/random';
9
- import { DataType } from '@dxos/schema';
10
-
11
- import { createTree } from '../testing';
8
+ import { random } from '@dxos/random';
9
+ import { Task } from '@dxos/types';
12
10
 
11
+ import { createTree } from '../../../testing';
13
12
  import { type Tree } from './tree';
14
13
 
15
- faker.seed(0);
14
+ random.seed(0);
16
15
 
17
16
  const print = (tree: Tree) => {
18
17
  let count = 0;
@@ -124,11 +123,13 @@ describe('tree', () => {
124
123
  });
125
124
 
126
125
  test('task', ({ expect }) => {
127
- const task = Obj.make(DataType.Task, { title: 'Test task.' });
126
+ const task = Obj.make(Task.Task, { title: 'Test task.' });
128
127
  expect(task.title).to.eq('Test task.');
129
128
 
130
129
  const tree = createTree();
131
130
  const node = tree.addNode(tree.root);
132
- node.ref = Ref.make(task);
131
+ Obj.update(tree.tree, () => {
132
+ node.ref = Ref.make(task);
133
+ });
133
134
  });
134
135
  });