Skip to content

fix: click action for subgraphs - #7988

Open
thiagobarbosa wants to merge 3 commits into
mermaid-js:developfrom
thiagobarbosa:feat/click-action-for-subgraphs
Open

thiagobarbosa wants to merge 3 commits into
mermaid-js:developfrom
thiagobarbosa:feat/click-action-for-subgraphs

Conversation

@thiagobarbosa

@thiagobarbosa thiagobarbosa commented Jul 22, 2026 •

Copy link
Copy Markdown

Fixes #5428

Summary

Adds support for the click directive on flowchart subgraphs. Previously click only worked on individual nodes/vertices — writing click mySubgraph "https://example.com" had no effect because the subgraph lookup was never checked and the resulting group node never received link data.

Changes

  • flowDb.ts
    • setClickEvent now falls back to this.subGraphLookup.get(id) when id doesn't match a vertex, so callbacks can target subgraphs.
    • setLink now also writes link / linkTarget onto the matching subgraph entry, not just vertices.
    • When building cluster/group nodes, link, linkTarget, haveCallback, and tooltip are now carried over from the subgraph definition onto the rendered node.
  • types.ts: FlowSubGraph gains optional link, linkTarget, and haveCallback fields to carry this data through.
  • clusters.js: the rect cluster renderer wraps the cluster label in an <a> element when node.link is set (mirroring how regular node links are rendered), respecting linkTarget and forcing _top under securityLevel: 'sandbox'.
  • flow-interactions.spec.js: adds coverage for click <subgraph> "<url>" (with and without a target), asserting both the parsed subgraph data and the resulting rendered group node carry the link/class.
  • pnpm-lock.yaml: lockfile churn from a routine pnpm install (no intentional dependency changes).

Testing

  • Added unit tests in flow-interactions.spec.js covering parsing a click on a subgraph both with and without an explicit link target.
  • Manual verification recommended: render a flowchart with click on a subgraph and confirm the group label becomes a clickable link in the browser.

Notes / follow-ups

  • No changeset was added under .changeset/; consider adding one before merging per this repo's release process.
  • Docs (e.g. the interactions page) don't yet mention that subgraphs support click — worth a follow-up doc update.

Summary

Adds support for click directives on flowchart subgraphs.

  • Extends flowchart interaction handling so click/href applied to a subgraph ID correctly resolves the target (vertex or subgraph), records callback presence (haveCallback), and associates link metadata.
  • Updates link propagation in the flowchart DB so subgraphs (including their rendered nodes) receive link and linkTarget (formatted via existing formatUrl logic) and are marked clickable.
  • Enhances flowchart rendering data for subgraphs:
    • For collapsed subgraphs (shape: 'collapsedGroup'), and
    • For non-collapsed container subgraphs (shape: 'rect'),
      the emitted rendered node now includes link, linkTarget, haveCallback, and tooltip.
  • Updates cluster label rendering so when node.link is present the cluster label is wrapped in an SVG <a>:
    • Uses _top when securityLevel: 'sandbox',
    • Otherwise uses node.linkTarget (or defaults to _blank).
  • Extends the exported FlowSubGraph type with optional link, linkTarget, and haveCallback.
  • Adds/updates tests to verify subgraph click-to-link behavior (including linkTarget handling) and validates cluster label anchor rendering behavior with sandbox security rules.
  • Adds a changeset documenting the new support for click on flowchart subgraphs.

Resolves mermaid-js#5428
mermaid-js#5428

Generated by issue-coder. Review before pushing.
@netlify

netlify Bot commented Jul 22, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for mermaid-js ready!

Name Link
🔨 Latest commit f53dcc2
🔍 Latest deploy log https://app.netlify.com/projects/mermaid-js/deploys/6a612fb60c71600008fe0154
😎 Deploy Preview https://deploy-preview-7988--mermaid-js.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

@changeset-bot

changeset-bot Bot commented Jul 22, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: f53dcc2

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
mermaid Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-actions github-actions Bot added the Type: Enhancement New feature or request label Jul 22, 2026
@github-actions

Copy link
Copy Markdown
Contributor

❌ Lockfile Validation Failed

The following issue(s) were detected:

Please address these and push an update.

Posted automatically by GitHub Actions

@coderabbitai

coderabbitai Bot commented Jul 22, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

Flowchart subgraphs now support click callbacks and links, including link targets and interaction metadata for collapsed and non-collapsed nodes. Cluster labels render linked subgraphs as SVG anchors, with tests covering links, clickable classes, and explicit targets.

Changes

Subgraph interaction support

Layer / File(s) Summary
Subgraph interaction metadata
packages/mermaid/src/diagrams/flowchart/types.ts, packages/mermaid/src/diagrams/flowchart/flowDb.ts
Subgraphs store links, link targets, callback state, tooltips, and interaction metadata on rendered nodes.
Linked subgraph label rendering
packages/mermaid/src/rendering-util/rendering-elements/clusters.js, packages/mermaid/src/rendering-util/rendering-elements/clusters.spec.js
Cluster labels with links render inside SVG anchors using security-level and target rules, with DOM tests covering linked and unlinked labels.
Subgraph interaction tests and release note
packages/mermaid/src/diagrams/flowchart/parser/flow-interactions.spec.js, .changeset/click-action-for-subgraphs.md
Tests validate subgraph links, clickable classes, and explicit link targets; a patch release note documents the support.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Sequence Diagram(s)

sequenceDiagram
  participant FlowchartParser
  participant FlowDB
  participant ClusterRenderer
  participant SVG
  FlowchartParser->>FlowDB: Parse subgraph click link and target
  FlowDB->>ClusterRenderer: Provide subgraph interaction metadata
  ClusterRenderer->>SVG: Render linked label as an anchor
Loading
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title is concise and accurately describes the main change: enabling click actions for subgraphs.
Description check ✅ Passed The description covers summary, issue reference, implementation details, testing, and follow-ups, though the template checkboxes are omitted.
Linked Issues check ✅ Passed The changes implement subgraph click support, including link and target propagation plus clickable cluster labels, matching #5428.
Out of Scope Changes check ✅ Passed All changes relate to subgraph click support, tests, types, rendering, and the required changeset; no unrelated scope stands out.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@packages/mermaid/src/diagrams/flowchart/flowDb.ts`:
- Around line 1226-1229: Update the collapsed-subgraph handling in the flowchart
database logic to preserve and propagate link, linkTarget, haveCallback, and
tooltip metadata before rendering. Ensure both collapsed and non-collapsed
branches populate these fields, using subGraph.id with getTooltip for the
tooltip, so collapsed subgraph interactions remain functional.

In `@packages/mermaid/src/rendering-util/rendering-elements/clusters.js`:
- Around line 31-48: Update the link-target selection in the cluster label
anchor creation so non-sandbox links default to `_blank` when `node.linkTarget`
is absent, while preserving explicit `node.linkTarget` values and the sandbox
`_top` behavior.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 3017772d-817b-4bc6-b28f-385ceb258d7f

📥 Commits

Reviewing files that changed from the base of the PR and between f0ffb41 and 1c9a77f.

⛔ Files ignored due to path filters (1)
  • pnpm-lock.yaml is excluded by !**/pnpm-lock.yaml
📒 Files selected for processing (4)
  • packages/mermaid/src/diagrams/flowchart/flowDb.ts
  • packages/mermaid/src/diagrams/flowchart/parser/flow-interactions.spec.js
  • packages/mermaid/src/diagrams/flowchart/types.ts
  • packages/mermaid/src/rendering-util/rendering-elements/clusters.js

Comment thread packages/mermaid/src/diagrams/flowchart/flowDb.ts
Comment thread packages/mermaid/src/rendering-util/rendering-elements/clusters.js
- Propagate link/linkTarget/haveCallback/tooltip to collapsed subgraph
  nodes, not just the non-collapsed branch, so click on a collapsed
  subgraph works too.
- Default cluster label link target to `_blank` outside sandbox mode
  when no explicit linkTarget is set, instead of leaving it unset.
- Restore pnpm-lock.yaml to drop unintended churn that broke
  `validate-lockfile`/`--frozen-lockfile` installs in CI.
- Add missing changeset.
@pkg-pr-new

pkg-pr-new Bot commented Jul 22, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

@mermaid-js/examples

npm i https://pkg.pr.new/@mermaid-js/examples@7988

mermaid

npm i https://pkg.pr.new/mermaid@7988

@mermaid-js/layout-elk

npm i https://pkg.pr.new/@mermaid-js/layout-elk@7988

@mermaid-js/layout-tidy-tree

npm i https://pkg.pr.new/@mermaid-js/layout-tidy-tree@7988

@mermaid-js/mermaid-zenuml

npm i https://pkg.pr.new/@mermaid-js/mermaid-zenuml@7988

@mermaid-js/parser

npm i https://pkg.pr.new/@mermaid-js/parser@7988

@mermaid-js/tiny

npm i https://pkg.pr.new/@mermaid-js/tiny@7988

commit: f53dcc2

@argos-ci

argos-ci Bot commented Jul 22, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Argos notifications ↗︎

Build Status Details Updated (UTC)
default (Inspect) ✅ No changes detected - Jul 22, 2026, 9:20 PM

@codecov

codecov Bot commented Jul 22, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 77.51%. Comparing base (f0ffb41) to head (f53dcc2).

Additional details and impacted files

Impacted file tree graph

@@             Coverage Diff             @@
##           develop    #7988      +/-   ##
===========================================
+ Coverage    77.46%   77.51%   +0.04%     
===========================================
  Files          564      564              
  Lines        74906    74934      +28     
  Branches     12661    12667       +6     
===========================================
+ Hits         58026    58083      +57     
+ Misses       15881    15853      -28     
+ Partials       999      998       -1     
Flag Coverage Δ
e2e 70.65% <40.00%> (+0.12%) ⬆️
unit 74.84% <96.87%> (+0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
packages/mermaid/src/diagrams/flowchart/flowDb.ts 86.47% <100.00%> (+0.66%) ⬆️
.../src/rendering-util/rendering-elements/clusters.js 66.02% <100.00%> (+1.12%) ⬆️

... and 4 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

- Add tests to verify `<a>` wrapping for cluster labels based on `node.link`.
- Test different `linkTarget` values and behavior under `securityLevel: sandbox`.
- Ensure no anchor wrapping occurs when `node.link` is not set.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@packages/mermaid/src/rendering-util/rendering-elements/clusters.spec.js`:
- Around line 63-68: Update the test suite setup around the global
SVGElement.prototype.getBBox stub to capture its original property descriptor
before replacement and restore that descriptor in afterEach or afterAll. Ensure
the restoration removes the stub and reinstates the original implementation for
subsequent tests.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 56627fd2-dce9-4235-9ae8-c94570d835d7

📥 Commits

Reviewing files that changed from the base of the PR and between fb2c4f7 and f53dcc2.

📒 Files selected for processing (1)
  • packages/mermaid/src/rendering-util/rendering-elements/clusters.spec.js

Comment on lines +63 to +68
global.SVGElement.prototype.getBBox = () => ({
x: 0,
y: 0,
width: 40,
height: 20,
});

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win

Restore the global getBBox stub after the suite.

This permanently replaces SVGElement.prototype.getBBox, leaking the stub into later tests and potentially masking layout behavior. Save and restore the original property descriptor in afterEach/afterAll.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/mermaid/src/rendering-util/rendering-elements/clusters.spec.js`
around lines 63 - 68, Update the test suite setup around the global
SVGElement.prototype.getBBox stub to capture its original property descriptor
before replacement and restore that descriptor in afterEach or afterAll. Ensure
the restoration removes the stub and reinstates the original implementation for
subsequent tests.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Type: Enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

click action for subgraphs

1 participant