-
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
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
Enable CSS-like linear gradient fills for flowchart nodes #5913
base: develop
Are you sure you want to change the base?
Enable CSS-like linear gradient fills for flowchart nodes #5913
Conversation
🦋 Changeset detectedLatest commit: 5f46a0f The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
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 |
✅ Deploy Preview for mermaid-js ready!
To edit notification comments on pull requests, go to your Netlify site configuration. |
commit: |
The latest updates on your projects. Learn more about Argos notifications ↗︎
|
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## develop #5913 +/- ##
==========================================
- Coverage 4.49% 4.42% -0.08%
==========================================
Files 382 383 +1
Lines 53914 54792 +878
Branches 621 622 +1
==========================================
Hits 2425 2425
- Misses 51489 52367 +878
Flags with carried forward coverage won't be shown. Click here to find out more.
|
5cf9e34
to
8f4b507
Compare
e13a378
to
35deffd
Compare
…order in bounds handling
…for transition stops only when needed
8a721ff
to
327ae28
Compare
327ae28
to
b7a6cbd
Compare
d271375
to
299bfea
Compare
299bfea
to
6e11005
Compare
6e11005
to
5f46a0f
Compare
@knsv @sidharthv96, your review on this PR would be appreciated when you have time. Thank you! |
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.
Hi @enourbakhsh thank you for your patience. This is a high-quality PR, with great tests and documentation. I wonder if there is an option to offload the complexity of gradients to CSS, instead of reimplementing the logic inside mermaid, or is there some technical limitation that prevents it?
Hi @sidharthv96, thank you for your feedback! I first tried the CSS approach using a The thing is, with the CSS-based approach, we are limited to rendering the gradient in the rectangular frame of the node's
As you can see, in the CSS method, the blue and red wings are mostly clipped in some nodes. It might not be a big deal for everyone, but it threw me off. |
Ahh, that makes sense! I'm not against adding extra complexity if there is a valid reason. As the codepen was also full of rectangles, I didn't really understand why we needed to re-invent the wheel (although I knew there would be a good one). Now that we know what the problem is, I'll discuss with the team on how to proceed. Thank you! |
// rendered on top, allowing layered effects like multiple semi-transparent backgrounds | ||
const styles = [...(vertex.cssStyles || []), ...(vertex.cssCompiledStyles || [])]; | ||
|
||
// Log all cssCompiledStyles for the node if available |
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.
// Log all cssCompiledStyles for the node if available | |
// Log the combined style array if available |
// Setup SVG coordinates | ||
const xc = bbox.x + nodeWidth / 2; | ||
const yc = bbox.y + nodeHeight / 2; | ||
const d1 = (gradientLineLength * (50 - minStop)) / 100; | ||
const d2 = (gradientLineLength * (50 - maxStop)) / 100; | ||
const { x1, y1, x2, y2 } = { x1: xc, y1: yc + d1, x2: xc, y2: yc + d2 }; // Vertical gradient @ 0deg (bottom to top) |
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.
This is where the magic happens—we use gradientLineLength
to precisely control how the gradient spans the exact node shape at the given angle, something CSS can only do for rectangular shapes.
📑 Summary
This PR adds support for CSS-like linear-gradient fills to Mermaid, following the CSS linear-gradient syntax. It introduces a reusable gradient utility that can be applied to other diagrams in the future, though this PR focuses on flowcharts as an example of how the feature can be used. A wide-ranging, editable collection of side-by-side examples comparing Mermaid and CSS linear gradients is available on this CodePen.
Resolves #2907 and #4476
📏 Design Decisions
The implementation involves updating the lexer to recognize the linear-gradient syntax and modifying the renderer to correctly apply the gradient fills to flowchart nodes. The design follows a CSS-like approach to maintain familiarity for users. See how gradient lines adapt dynamically to various shapes in this CodePen demo, which reflects the design principles behind the added feature.
📋 Tasks
Make sure you
MERMAID_RELEASE_VERSION
is used for all new features.pnpm changeset
and following the prompts. Changesets that add features should beminor
and those that fix bugs should bepatch
. Please prefix changeset messages withfeat:
,fix:
, orchore:
.