-
Notifications
You must be signed in to change notification settings - Fork 114
H-5763: Add simple MiniMap #8286
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
4053aaf
2368f9a
1f256c2
b945e0b
f1905e8
3ba9d4b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| import { css } from "@hashintel/ds-helpers/css"; | ||
| import { use, useMemo } from "react"; | ||
| import type { MiniMapNodeProps, MiniMapProps } from "reactflow"; | ||
| import { MiniMap as ReactFlowMiniMap, useStore } from "reactflow"; | ||
|
|
||
| import { PANEL_MARGIN } from "../../../constants/ui"; | ||
| import { hexToHsl } from "../../../lib/hsl-color"; | ||
| import { EditorContext } from "../../../state/editor-context"; | ||
| import type { NodeType } from "../reactflow-types"; | ||
|
|
||
| const miniMapClassName = css({ | ||
| "& svg": { | ||
| borderRadius: "md.3", | ||
| }, | ||
| }); | ||
|
|
||
| /** Default colors for nodes without a type color */ | ||
| const DEFAULT_PLACE_FILL = "#f8f8f8"; | ||
| const DEFAULT_PLACE_STROKE = "#666666"; | ||
| const DEFAULT_TRANSITION_FILL = "#6b7280"; | ||
|
|
||
| const PLACE_STROKE_WIDTH = 2; | ||
|
|
||
| /** | ||
| * Custom node renderer for the MiniMap. | ||
| * Renders place nodes as circles and transition nodes as rectangles. | ||
| */ | ||
| const MiniMapNode: React.FC<MiniMapNodeProps> = ({ | ||
| id, | ||
| x, | ||
| y, | ||
| width, | ||
| height, | ||
| }) => { | ||
| // MiniMapNodeProps doesn't include node data, so we look it up from the store | ||
| const node = useStore( | ||
| (state) => state.nodeInternals.get(id) as NodeType | undefined, | ||
| ); | ||
|
|
||
| // Compute colors based on node type and type color | ||
| const { fill, stroke } = useMemo(() => { | ||
| if (node?.data.type === "place") { | ||
| const typeColor = node.data.typeColor; | ||
|
|
||
| if (typeColor) { | ||
| const hsl = hexToHsl(typeColor); | ||
| return { | ||
| fill: hsl.lighten(20).css(0.9), | ||
| stroke: hsl.lighten(-15).saturate(-20).css(1), | ||
| }; | ||
| } | ||
|
|
||
| return { fill: DEFAULT_PLACE_FILL, stroke: DEFAULT_PLACE_STROKE }; | ||
| } | ||
|
|
||
| // Transitions: solid grey with no stroke | ||
| return { fill: DEFAULT_TRANSITION_FILL, stroke: undefined }; | ||
| }, [node?.data]); | ||
|
|
||
| if (node?.data.type === "place") { | ||
| const radius = Math.min(width, height) / 2 - PLACE_STROKE_WIDTH / 2; | ||
| return ( | ||
| <circle | ||
| cx={x + width / 2} | ||
| cy={y + height / 2} | ||
| r={Math.max(radius, 1)} | ||
| fill={fill} | ||
| stroke={stroke} | ||
| strokeWidth={PLACE_STROKE_WIDTH} | ||
| /> | ||
| ); | ||
| } | ||
|
|
||
| return <rect x={x} y={y} width={width} height={height} fill={fill} />; | ||
| }; | ||
|
|
||
| /** | ||
| * A wrapper around ReactFlow's MiniMap with custom styling. | ||
| * Renders place nodes as circles and transition nodes as rectangles. | ||
| * Positions at top-right, offset by properties panel width when visible. | ||
| */ | ||
| export const MiniMap: React.FC<Omit<MiniMapProps, "style">> = (props) => { | ||
| const { selectedResourceId, propertiesPanelWidth } = use(EditorContext); | ||
|
|
||
| const isPropertiesPanelVisible = selectedResourceId !== null; | ||
| const rightOffset = isPropertiesPanelVisible | ||
| ? propertiesPanelWidth + PANEL_MARGIN * 2 | ||
| : PANEL_MARGIN; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. MiniMap offset ignores non-resource panel visibilityLow Severity
|
||
|
|
||
| return ( | ||
| <ReactFlowMiniMap | ||
| {...props} | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| ariaLabel="" | ||
| className={miniMapClassName} | ||
| style={{ | ||
| top: 0, | ||
| right: rightOffset, | ||
| bottom: "auto", | ||
| left: "auto", | ||
| width: 130, | ||
| height: 73, | ||
| }} | ||
| maskColor="rgba(0, 0, 0, 0.15)" | ||
| maskStrokeWidth={0} | ||
| nodeComponent={MiniMapNode} | ||
| offsetScale={2} | ||
| /> | ||
| ); | ||
| }; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Type signature allows props that are silently ignored
Low Severity
The component type
Omit<MiniMapProps, "style">suggests allMiniMapPropsexceptstylecan be customized, but props likeclassName,maskColor,maskStrokeWidth,nodeComponent, andoffsetScaleare hardcoded after the spread and will be silently overwritten. This creates a misleading API where callers could pass these props expecting them to work, but they'll be ignored. The type should exclude all fixed props to accurately reflect the component's actual API.