Skip to content

Commit 92115bb

Browse files
committed
make MiniMap compatible with react flow v9 and v12
1 parent e3f1f81 commit 92115bb

4 files changed

Lines changed: 50 additions & 25 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,8 @@ If you use Jest then you can use the same aliases for the `moduleNameMapper` con
309309
- use always `<Label/>` component for `label` value
310310
- `<StickyNoteNode />`
311311
- Refactored data structure position and dimension (breaking change)
312+
- `<MiniMap />`
313+
- component supports now React Flow v9 and v12
312314

313315
### Deprecated
314316

src/cmem/react-flow/ReactFlow/ReactFlow.stories.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import {
2121
ApplicationContainer,
2222
EdgeTools,
2323
MiniMap,
24-
MiniMapV12,
2524
NodeTools,
2625
ReactFlowExtended,
2726
ReactFlowExtendedProps,
@@ -504,7 +503,7 @@ const ReactFlowExampleV12: FC<ReactFlowExtendedProps> = (args) => {
504503
<ApplicationContainer monitorDropzonesFor={args.dropzoneFor} style={{ background: "white" }}>
505504
<div style={{ height, width }}>
506505
<ReactFlowExtended {...reactFlowExtendedProps}>
507-
<MiniMapV12 enableNavigation />
506+
<MiniMap enableNavigation />
508507
<BackgroundV12 variant={BackgroundVariantV12.Dots} gap={16} />
509508
</ReactFlowExtended>
510509
</div>

src/extensions/react-flow/minimap/MiniMap.tsx

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
import React, { memo, useEffect } from "react";
2-
import { MiniMap as ReactFlowMiniMap, MiniMapProps as ReactFlowMiniMapProps, OnLoadParams } from "react-flow-renderer";
2+
import {
3+
MiniMap as ReactFlowMiniMapV9,
4+
MiniMapProps as ReactFlowMiniMapV9Props,
5+
OnLoadParams
6+
} from "react-flow-renderer";
37
import { FlowTransform } from "react-flow-renderer/dist/types";
48

5-
import { miniMapUtils } from "../minimap/utils";
9+
import { miniMapUtils } from "./utils";
10+
import { ReacFlowVersionSupportProps, ReactFlowVersions, useReactFlowVersion } from "../versionsupport";
11+
import { MiniMapV12, MiniMapV12Props } from "./MiniMapV12";
612

7-
export interface MiniMapProps extends ReactFlowMiniMapProps {
8-
/**
9-
* React-Flow instance
10-
*/
11-
flowInstance?: OnLoadParams;
13+
export interface MiniMapBasicProps {
1214
/**
1315
* Enable navigating the react-flow canvas by dragging and clicking on the mini-map.
1416
*/
@@ -23,6 +25,15 @@ export interface MiniMapProps extends ReactFlowMiniMapProps {
2325
>;
2426
}
2527

28+
export interface MiniMapV9Props extends MiniMapBasicProps, ReactFlowMiniMapV9Props {
29+
/**
30+
* React-Flow instance
31+
*/
32+
flowInstance?: OnLoadParams;
33+
}
34+
35+
export type MiniMapProps = (ReacFlowVersionSupportProps & MiniMapV9Props) | (ReacFlowVersionSupportProps & MiniMapV12Props);
36+
2637
interface configParams {
2738
// Key has been pressed down over the mini-map and navigation mode has thus started
2839
navigationOn: boolean;
@@ -40,6 +51,28 @@ let minimapCalcConf: configParams = {
4051

4152
/** An improved mini-map for react-flow that supports navigation via the mini-map. */
4253
export const MiniMap = memo(
54+
({
55+
flowVersion,
56+
...otherProps
57+
}: MiniMapProps) => {
58+
const flowVersionCheck = flowVersion || useReactFlowVersion();
59+
60+
switch (flowVersionCheck) {
61+
case ReactFlowVersions.V9:
62+
return <MiniMapV9 {...otherProps as MiniMapV9Props} />;
63+
case ReactFlowVersions.V12:
64+
return <MiniMapV12 {...otherProps as MiniMapV12Props} />;
65+
default:
66+
return <></>; // cannot exit on its own
67+
}
68+
}
69+
);
70+
71+
/**
72+
* Mini-map support for for React Flow v9.
73+
* @deprecated (v26) will be removed, use when `MiniMap` directly
74+
*/
75+
export const MiniMapV9 = memo(
4376
({
4477
flowInstance,
4578
enableNavigation = false,
@@ -49,7 +82,7 @@ export const MiniMap = memo(
4982
nodeStrokeColor = miniMapUtils.borderColor,
5083
wrapperProps,
5184
...minimapProps
52-
}: MiniMapProps) => {
85+
}: MiniMapV9Props) => {
5386
const minimapWrapper = React.useRef<HTMLDivElement | null>(null);
5487

5588
useEffect(() => {
@@ -130,7 +163,7 @@ export const MiniMap = memo(
130163
: wrapperProps?.style
131164
}
132165
>
133-
<ReactFlowMiniMap
166+
<ReactFlowMiniMapV9
134167
maskColor={maskColor}
135168
nodeClassName={nodeClassName}
136169
nodeColor={nodeColor}
@@ -141,3 +174,5 @@ export const MiniMap = memo(
141174
);
142175
}
143176
);
177+
178+
export default MiniMap;

src/extensions/react-flow/minimap/MiniMapV12.tsx

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,10 @@
11
import React, { memo } from "react";
22
import { MiniMap as ReactFlowMiniMap, MiniMapProps as ReactFlowMiniMapProps } from "@xyflow/react";
33

4-
import { miniMapUtils } from "../minimap/utils";
4+
import { miniMapUtils } from "./utils";
5+
import { MiniMapBasicProps } from "./MiniMap";
56

6-
export interface MiniMapV12Props extends Omit<ReactFlowMiniMapProps, "maskColor"> {
7-
/**
8-
* Enable navigating the react-flow canvas by dragging and clicking on the mini-map.
9-
*/
10-
enableNavigation?: boolean;
11-
/**
12-
* Properties are forwarded to the HTML `div` element used as minimap wrapper.
13-
* Data attributes for test ids could be included here.
14-
*/
15-
wrapperProps?: Omit<
16-
React.HTMLAttributes<HTMLDivElement>,
17-
"onMouseDown" | "onMouseUp" | "onMouseMove" | "onMouseLeave"
18-
>;
7+
export interface MiniMapV12Props extends MiniMapBasicProps, Omit<ReactFlowMiniMapProps, "maskColor"> {
198
}
209

2110
/**

0 commit comments

Comments
 (0)