Skip to main content

SVG-Export: add feature to export local variable names into svg code with static-color fallback


Tim_Clulow

When working with SVG assets within a design system we’re often using named color variables that are part of the design system so that our designs support different color modes (light, dark etc)

it would be super helpful if there was some way for these variables made their way into the SVG code (with a fallback for situations or usages where the variable may not exist)

So in your SVG export instead of this:

<rect x="0.331604" y="0.717941" width="20.9977" height="20.9977" fill="#ffffff"/>

you’d get something like this

<rect x="0.331604" y="0.717941" width="20.9977" height="20.9977" fill="var(--color-background, #ffffff)"/>

which would then be able to be able to be dynamically changed based on what is being set for --color-background in the consuming web app.

11 replies

Tim_Clulow
  • Author
  • 1 reply
  • November 14, 2023

would also be great to be able to strip out any fill opacity values from the svg when using color variables as the fill (as opacity is often already set within the variable)


Celine_Figma
Figmate
  • Community Support
  • 3213 replies
  • November 15, 2023

Hey Tim! Thanks for your feedback on SVH-export! I’ve gone ahead and switched your topic as “Share an idea”.


Michael_Gehrmann

This would be great.

I would add to have this option in the REST api GET images endpoint too.


Claudia4
  • 1 reply
  • February 4, 2024

I agree!! We need this so badly. We have to use hacks today and switch the color by one bit to identify different semantic colors so that we can handle them correctly in the code. Please add this feature!!


Good suggestion I’d love to see this.


Need this!!!
Could we do this in an optimized way, somehow?
Is there any plugin that could help me do this…?


Michael_Gehrmann

This would be such a boost for the use of themes in design systems.

Current Behavior:

SVG exports include hardcoded color values, for example:

<path d="..." fill="#008EF2" style="fill:#008EF2;fill:color(display-p3 0.0000 0.5569 0.9490);fill-opacity:1;" />

Proposed Behavior:

SVG exports should use CSS variables when Figma variables are employed in the design:

<path d="..." fill="#008ef2" style="fill: var(--illustration-deco-object-shade, #008ef2);fill-opacity:1;" />

This change would align SVG exports with the current CSS code generation in Dev Mode, which already utilizes variables:

.rectangle {
  background: var(--illustration-deco-object-shade, #008ef2);
  /* ... */
}

Benefits

  1. Consistency across different export formats (CSS and SVG)
  2. Improved maintainability of exported assets
  3. Easier integration with design systems using CSS variables
  4. Enhanced flexibility for developers working with Figma-exported SVGs

Ilya_Kantor

While this feature isn’t implemented, maybe there’s a workaround for CSS themes with exported SVGS, such as using ids on elements?


Sebastien_Rouif1

This would also be useful in Android when exporting svg that then get converted into vector Drawables


Avner_H
  • 2 replies
  • December 3, 2024

Hey there
Just published a plugin especially for generating the SVG code with color tokens:
https://www.figma.com/community/plugin/1442959482504071246/svg-with-tokens

Waiting for you feedback of how to improve it 🙂


Michael_Gehrmann

Oh Wow, @Avner_H !
This ist amazing. You brighten up my Christmas Mood!


Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings