@ts-graphviz/react 0.10.6 → 0.11.1

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 (54) hide show
  1. package/CHANGELOG.md +497 -0
  2. package/README.md +630 -67
  3. package/example/example.tsx +1 -1
  4. package/lib/react.d.ts +198 -96
  5. package/lib/react.js +558 -215
  6. package/package.json +15 -20
  7. package/tsconfig.test.json +11 -0
  8. package/lib/react.cjs +0 -457
  9. package/src/components/Digraph.spec.tsx +0 -18
  10. package/src/components/Digraph.tsx +0 -41
  11. package/src/components/Edge.tsx +0 -17
  12. package/src/components/Graph.spec.tsx +0 -18
  13. package/src/components/Graph.tsx +0 -36
  14. package/src/components/GraphPortal.tsx +0 -22
  15. package/src/components/Node.spec.tsx +0 -101
  16. package/src/components/Node.tsx +0 -21
  17. package/src/components/Subgraph.tsx +0 -30
  18. package/src/contexts/CurrentGraph.ts +0 -5
  19. package/src/contexts/GraphContainer.ts +0 -5
  20. package/src/contexts/GraphMap.ts +0 -5
  21. package/src/contexts/GraphvizContext.ts +0 -11
  22. package/src/hooks/__tests__/wrapper.tsx +0 -58
  23. package/src/hooks/useCurrentGraph.spec.ts +0 -56
  24. package/src/hooks/useCurrentGraph.ts +0 -18
  25. package/src/hooks/useDigraph.spec.ts +0 -15
  26. package/src/hooks/useDigraph.ts +0 -33
  27. package/src/hooks/useEdge.spec.ts +0 -46
  28. package/src/hooks/useEdge.ts +0 -35
  29. package/src/hooks/useGraph.spec.ts +0 -15
  30. package/src/hooks/useGraph.ts +0 -33
  31. package/src/hooks/useGraphAttributes.ts +0 -32
  32. package/src/hooks/useGraphContainer.spec.ts +0 -45
  33. package/src/hooks/useGraphContainer.ts +0 -10
  34. package/src/hooks/useGraphMap.ts +0 -7
  35. package/src/hooks/useGraphvizContext.spec.ts +0 -26
  36. package/src/hooks/useGraphvizContext.ts +0 -16
  37. package/src/hooks/useHasAttributes.ts +0 -16
  38. package/src/hooks/useHasComment.ts +0 -8
  39. package/src/hooks/useNode.spec.ts +0 -16
  40. package/src/hooks/useNode.ts +0 -29
  41. package/src/hooks/useRenderedID.spec.tsx +0 -28
  42. package/src/hooks/useRenderedID.ts +0 -6
  43. package/src/hooks/useSubgraph.spec.ts +0 -41
  44. package/src/hooks/useSubgraph.ts +0 -39
  45. package/src/react.ts +0 -16
  46. package/src/reconciler.ts +0 -272
  47. package/src/render.test.tsx +0 -71
  48. package/src/render.ts +0 -142
  49. package/src/renderHTMLLike.ts +0 -9
  50. package/src/renderId.ts +0 -9
  51. package/src/types.ts +0 -107
  52. package/tsconfig.json +0 -8
  53. package/typedoc.json +0 -5
  54. package/vite.config.ts +0 -31
@@ -1,4 +1,4 @@
1
- import { Digraph, Edge, Node, Subgraph, renderToDot } from '@ts-graphviz/react';
1
+ import { Digraph, Edge, Node, renderToDot, Subgraph } from '@ts-graphviz/react';
2
2
 
3
3
  const Example = () => (
4
4
  <Digraph
package/lib/react.d.ts CHANGED
@@ -1,19 +1,86 @@
1
+ import { ComponentProps as ComponentProps_2 } from 'react';
2
+ import { DotObjectModel } from 'ts-graphviz';
1
3
  import { EdgeAttributesObject } from '@ts-graphviz/common';
2
- import { EdgeModel } from 'ts-graphviz';
4
+ import { EdgeModel } from '@ts-graphviz/common';
5
+ import { EdgeModel as EdgeModel_2 } from 'ts-graphviz';
3
6
  import { EdgeTargetLikeTuple } from '@ts-graphviz/common';
4
7
  import { EdgeTargetLikeTuple as EdgeTargetLikeTuple_2 } from 'ts-graphviz';
5
8
  import { FC } from 'react';
9
+ import { FilterableModel } from 'ts-graphviz';
6
10
  import { GraphAttributesObject } from '@ts-graphviz/common';
7
- import { GraphBaseModel } from 'ts-graphviz';
11
+ import { GraphBaseModel } from '@ts-graphviz/common';
12
+ import { GraphBaseModel as GraphBaseModel_2 } from 'ts-graphviz';
8
13
  import { HasComment } from '@ts-graphviz/common';
14
+ import { isAttributeListModel } from 'ts-graphviz';
15
+ import { isEdgeModel } from 'ts-graphviz';
16
+ import { isNodeModel } from 'ts-graphviz';
17
+ import { isRootGraphModel } from 'ts-graphviz';
18
+ import { isSubgraphModel } from 'ts-graphviz';
9
19
  import { NodeAttributesObject } from '@ts-graphviz/common';
10
- import { NodeModel } from 'ts-graphviz';
20
+ import { NodeModel } from '@ts-graphviz/common';
21
+ import { NodeModel as NodeModel_2 } from 'ts-graphviz';
11
22
  import { ReactElement } from 'react';
12
23
  import { ReactNode } from 'react';
24
+ import { Ref } from 'react';
13
25
  import { RootGraphModel } from 'ts-graphviz';
14
26
  import { SubgraphAttributesObject } from '@ts-graphviz/common';
15
27
  import { SubgraphModel } from 'ts-graphviz';
16
28
 
29
+ /**
30
+ * Type that represents any graph-like container that can be used for rendering.
31
+ * This includes both base interfaces (GraphBaseModel) and root graph interfaces (RootGraphModel).
32
+ * Since Graph and Digraph implement RootGraphModel, they are covered by this union.
33
+ */
34
+ export declare type AnyGraphContainer = GraphBaseModel_2 | RootGraphModel;
35
+
36
+ /**
37
+ * Props for Graphviz components
38
+ */
39
+ export declare type ComponentProps = ComponentProps_2<any>;
40
+
41
+ /**
42
+ * React component type for Graphviz elements
43
+ */
44
+ export declare type ComponentType = FC<any>;
45
+
46
+ /**
47
+ * Create a Graphviz root for rendering React elements to Graphviz models
48
+ * @param options - Options for creating the root, including optional container
49
+ */
50
+ export declare function createRoot<Container extends AnyGraphContainer = AnyGraphContainer>(options?: CreateRootOptions<Container>): GraphvizRoot;
51
+
52
+ /**
53
+ * Options for creating a Graphviz root
54
+ */
55
+ export declare interface CreateRootOptions<Container extends AnyGraphContainer = AnyGraphContainer> {
56
+ /**
57
+ * Container graph for rendering components.
58
+ * When provided, components will be rendered into this existing graph model.
59
+ */
60
+ container?: Container;
61
+ /**
62
+ * Called when an error is not caught by any error boundary
63
+ */
64
+ onUncaughtError?: (error: Error, errorInfo: ErrorInfo) => void;
65
+ /**
66
+ * Called when an error is caught by an error boundary
67
+ */
68
+ onCaughtError?: (error: Error, errorInfo: ErrorInfo) => void;
69
+ /**
70
+ * Called when React automatically recovers from errors
71
+ */
72
+ onRecoverableError?: (error: Error, errorInfo: ErrorInfo) => void;
73
+ /**
74
+ * Called when rendering completes successfully
75
+ */
76
+ onRenderComplete?: (models: DotObjectModel[]) => void;
77
+ /**
78
+ * Timeout for async rendering in milliseconds
79
+ * @default 5000
80
+ */
81
+ timeout?: number;
82
+ }
83
+
17
84
  /**
18
85
  * `Digraph` component.
19
86
  */
@@ -34,8 +101,18 @@ export declare interface EdgeProps extends Omit<EdgeOptions, 'label'> {
34
101
  targets: EdgeTargetLikeTuple;
35
102
  /** Edge label */
36
103
  label?: ReactElement | string;
104
+ ref?: Ref<EdgeModel>;
105
+ }
106
+
107
+ /**
108
+ * Error information for React error handling
109
+ */
110
+ export declare interface ErrorInfo {
111
+ componentStack?: string;
37
112
  }
38
113
 
114
+ export { FilterableModel }
115
+
39
116
  /**
40
117
  * `Graph` component.
41
118
  */
@@ -66,6 +143,58 @@ export declare interface GraphPortalProps {
66
143
  children?: ReactNode;
67
144
  }
68
145
 
146
+ /**
147
+ * Graphviz root interface
148
+ */
149
+ export declare interface GraphvizRoot {
150
+ /**
151
+ * Render a React element asynchronously
152
+ */
153
+ render(element: ReactElement): Promise<void>;
154
+ /**
155
+ * Unmount the root and cleanup resources
156
+ */
157
+ unmount(): void;
158
+ /**
159
+ * Get all top-level models that were rendered (no type parameters)
160
+ * @returns Array of rendered models. In container mode, returns all non-container models.
161
+ * In non-container mode, returns top-level graph models.
162
+ */
163
+ getTopLevelModels(): DotObjectModel[];
164
+ /**
165
+ * Get top-level models with generic type casting or type guard filtering
166
+ *
167
+ * @overload@overload
168
+ * When called with a type guard function, filters models by type using runtime type checking
169
+ * @param typeGuard - Type guard function to filter models
170
+ * @returns Array of models matching the specified type
171
+ *
172
+ * @overload@overload
173
+ * When called with only a generic type parameter, casts all models to the specified type (trusted user assertion)
174
+ * @returns Array of models cast to the specified type T
175
+ *
176
+ * @example
177
+ * ```typescript
178
+ * // Runtime type filtering with type guard
179
+ * const nodes = root.getTopLevelModels(isNodeModel);
180
+ *
181
+ * // Direct type casting (user knows the type)
182
+ * const nodes = root.getTopLevelModels<NodeModel>();
183
+ * ```
184
+ */
185
+ getTopLevelModels<T extends DotObjectModel>(typeGuard?: (model: DotObjectModel) => model is T): T[];
186
+ }
187
+
188
+ export { isAttributeListModel }
189
+
190
+ export { isEdgeModel }
191
+
192
+ export { isNodeModel }
193
+
194
+ export { isRootGraphModel }
195
+
196
+ export { isSubgraphModel }
197
+
69
198
  /**
70
199
  * `Node` component.
71
200
  */
@@ -83,77 +212,78 @@ export declare interface NodeProps extends Omit<NodeOptions, 'label' | 'xlabel'>
83
212
  label?: ReactElement | string;
84
213
  /** Node xlabel */
85
214
  xlabel?: ReactElement | string;
215
+ ref?: Ref<NodeModel>;
86
216
  }
87
217
 
218
+ export { ReactNode }
219
+
88
220
  /**
89
- * Convert the given element to Graphviz model.
90
- *
91
- * @example Example of giving a cluster as a container with the second argument.
221
+ * Converts a React element into a custom HTML-like string representation.
92
222
  *
93
- * ```tsx
94
- * import React, { FC } from 'react';
95
- * import { digraph, toDot } from 'ts-graphviz';
96
- * import { Node, Subgraph, render, Edge } from '@ts-graphviz/react';
97
- *
98
- * const Example: FC = () => (
99
- * <>
100
- * <Node id="a" />
223
+ * If no {@link label} is provided, returns the string '<>'. Otherwise, renders the React element to static markup, transforms any `dot:`-prefixed tags by removing the prefix, and wraps the result in angle brackets.
101
224
  *
102
- * <Subgraph id="my_cluster">
103
- * <Node id="b" />
104
- * </Subgraph>
105
- * <Edge targets={['b', 'a']} />
106
- * </>
107
- * );
108
- *
109
- * const G = digraph((g) => render(<Example />, g));
110
- * console.log(toDot(G));
111
- * // digraph {
112
- * // "a";
113
- * // subgraph "my_cluster" {
114
- * // "b";
115
- * // }
116
- * // "b" -> "a";
117
- * // }
118
- * ```
225
+ * @param label - Optional React element to render.
226
+ * @returns A string representing the transformed HTML-like markup.
119
227
  */
120
- export declare function render<T extends GraphBaseModel>(element: ReactElement, container?: T): T;
121
-
122
228
  export declare function renderHTMLLike(label?: ReactElement): string;
123
229
 
124
230
  /**
125
- * Converts the given element to DOT language and returns it.
231
+ * Converts the given React element to DOT language asynchronously.
126
232
  *
127
- * @example
233
+ * This function creates a Graphviz root, renders the element, and returns the DOT representation.
128
234
  *
235
+ * @example Single top-level graph
129
236
  * ```tsx
130
- * import React, { FC } from 'react';
131
- * import { Digraph, Node, Subgraph, renderToDot, Edge } from '@ts-graphviz/react';
237
+ * const dot = await renderToDot(
238
+ * <Digraph id="example">
239
+ * <Node id="a" />
240
+ * <Node id="b" />
241
+ * <Edge targets={['a', 'b']} />
242
+ * </Digraph>
243
+ * );
244
+ * console.log(dot);
245
+ * // digraph "example" {
246
+ * // "a";
247
+ * // "b";
248
+ * // "a" -> "b";
249
+ * // }
250
+ * ```
132
251
  *
133
- * const Example: FC = () => (
134
- * <Digraph>
252
+ * @example With container
253
+ * ```tsx
254
+ * const container = new DigraphModel('main');
255
+ * const dot = await renderToDot(
256
+ * <>
135
257
  * <Node id="a" />
136
- * <Subgraph id="my_cluster">
258
+ * <Subgraph id="cluster_sub">
137
259
  * <Node id="b" />
138
260
  * </Subgraph>
139
- * <Edge targets={['b', 'a']} />
140
- * </Digraph>
261
+ * <Edge targets={['a', 'b']} />
262
+ * </>,
263
+ * { container }
141
264
  * );
142
- *
143
- * const dot = renderToDot(<Example />);
144
265
  * console.log(dot);
145
- * // digraph {
266
+ * // digraph "main" {
146
267
  * // "a";
147
- * // subgraph "my_cluster" {
268
+ * // subgraph "cluster_sub" {
148
269
  * // "b";
149
270
  * // }
150
- * // "b" -> "a";
271
+ * // "a" -> "b";
151
272
  * // }
152
273
  * ```
153
- *
154
- * @returns Rendered dot string
155
274
  */
156
- export declare function renderToDot(element: ReactElement, container?: RootGraphModel): string;
275
+ export declare function renderToDot<Container extends AnyGraphContainer = AnyGraphContainer>(element: ReactElement, options?: RenderToDotOptions<Container>): Promise<string>;
276
+
277
+ /**
278
+ * Rendering options for renderToDot
279
+ */
280
+ export declare interface RenderToDotOptions<Container extends AnyGraphContainer = AnyGraphContainer> extends CreateRootOptions {
281
+ /**
282
+ * Container graph to render into. When provided, the element will be rendered
283
+ * as children of this container and the container's DOT will be returned.
284
+ */
285
+ container?: Container;
286
+ }
157
287
 
158
288
  /** Options for RootGraph */
159
289
  export declare interface RootGraphOptions extends GraphAttributesObject, GraphBaseAttributesProps {
@@ -165,6 +295,7 @@ export declare interface RootGraphOptions extends GraphAttributesObject, GraphBa
165
295
  export declare interface RootGraphProps extends Omit<RootGraphOptions, 'label'> {
166
296
  label?: ReactElement | string;
167
297
  children?: ReactNode;
298
+ ref?: Ref<GraphBaseModel>;
168
299
  }
169
300
 
170
301
  /**
@@ -183,14 +314,17 @@ export declare interface SubgraphProps extends Omit<SubgraphOptions, 'label'> {
183
314
  /** Subgraph label */
184
315
  label?: ReactElement | string;
185
316
  children?: ReactNode;
317
+ ref?: Ref<GraphBaseModel>;
186
318
  }
187
319
 
320
+ export declare type TopLevelModel = FilterableModel;
321
+
188
322
  /**
189
323
  * Hook to get the current cluster(Digraph, Graph or Subgraph).
190
324
  *
191
325
  * @throws If it is out of the context of Cluster, it throws an exception.
192
326
  */
193
- export declare function useCurrentGraph(): GraphBaseModel;
327
+ export declare function useCurrentGraph(): AnyGraphContainer;
194
328
 
195
329
  /**
196
330
  * `useDigraph` is a hook that creates an instance of Digraph
@@ -202,7 +336,7 @@ export declare function useDigraph(options?: RootGraphOptions): RootGraphModel;
202
336
  * `useEdge` is a hook that creates an instance of Edge
203
337
  * according to the object given by props.
204
338
  */
205
- export declare function useEdge(targets: EdgeTargetLikeTuple_2, props?: EdgeOptions): EdgeModel;
339
+ export declare function useEdge(targets: EdgeTargetLikeTuple_2, props?: EdgeOptions): EdgeModel_2;
206
340
 
207
341
  /**
208
342
  * `useGraph` is a hook that creates an instance of Graph
@@ -213,13 +347,13 @@ export declare function useGraph(options?: RootGraphOptions): RootGraphModel;
213
347
  /**
214
348
  * Return the cluster of container.
215
349
  */
216
- export declare function useGraphContainer(): GraphBaseModel | null;
350
+ export declare function useGraphContainer(): AnyGraphContainer | null;
217
351
 
218
352
  /**
219
353
  * `useNode` is a hook that creates an instance of Node
220
354
  * according to the object given by props.
221
355
  */
222
- export declare function useNode(id: string, options?: NodeOptions): NodeModel;
356
+ export declare function useNode(id: string, options?: NodeOptions): NodeModel_2;
223
357
 
224
358
  /**
225
359
  * `useSubgraph` is a hook that creates an instance of Subgraph
@@ -230,49 +364,17 @@ export declare function useSubgraph(props?: SubgraphOptions): SubgraphModel;
230
364
  export { }
231
365
 
232
366
 
233
- declare global {
367
+ declare module 'react' {
368
+ namespace JSX {
369
+ interface IntrinsicElements extends JSXIntrinsicElements {
370
+ }
371
+ }
372
+ }
373
+
374
+
375
+ declare module 'react/jsx-runtime' {
234
376
  namespace JSX {
235
- interface IntrinsicElements {
236
- 'dot:port': {
237
- children: string;
238
- };
239
- 'dot:table': HTMLLikeLabel.TableAttributes & {
240
- children?: ReactNode;
241
- };
242
- 'dot:tr': HTMLLikeLabel.TrAttributes & {
243
- children?: ReactNode;
244
- };
245
- 'dot:td': HTMLLikeLabel.TdAttributes & {
246
- children?: ReactNode;
247
- };
248
- 'dot:font': HTMLLikeLabel.FontAttributes & {
249
- children?: ReactNode;
250
- };
251
- 'dot:br': HTMLLikeLabel.BrAttributes;
252
- 'dot:img': HTMLLikeLabel.ImgAttributes;
253
- 'dot:i': HTMLLikeLabel.IAttributes & {
254
- children?: ReactNode;
255
- };
256
- 'dot:b': HTMLLikeLabel.BAttributes & {
257
- children?: ReactNode;
258
- };
259
- 'dot:u': HTMLLikeLabel.UAttributes & {
260
- children?: ReactNode;
261
- };
262
- 'dot:o': HTMLLikeLabel.OAttributes & {
263
- children?: ReactNode;
264
- };
265
- 'dot:sub': HTMLLikeLabel.SubAttributes & {
266
- children?: ReactNode;
267
- };
268
- 'dot:sup': HTMLLikeLabel.SupAttributes & {
269
- children?: ReactNode;
270
- };
271
- 'dot:s': HTMLLikeLabel.SAttributes & {
272
- children?: ReactNode;
273
- };
274
- 'dot:hr': HTMLLikeLabel.HrAttributes;
275
- 'dot:vr': HTMLLikeLabel.VrAttributes;
377
+ interface IntrinsicElements extends JSXIntrinsicElements {
276
378
  }
277
379
  }
278
380
  }