Skip to content

tychenjiajun/svgo-plugin-add-classes-to-colors

Repository files navigation

svgo-plugin-add-classes-to-colors

GitHub license npm

Add corresponding class to element that has specific colors.

Install

npm install --dev svgo-plugin-add-classes-to-colors
# or
yarn add --dev svgo-plugin-add-classes-to-colors

Usage

// svgo.config.js
const addClassesToColors = require('svgo-plugin-add-classes-to-colors');
module.exports = {
  plugins: [
    {
      ...addClassesToColors,
      params: {
        mapping: {
          // [color]: className
          'currentColor': 'tone1',
          '#fff': 'tone2',
        }
      }
    }
  ],
};

will transform

<svg xmlns="http://www.w3.org/2000/svg">
    <g color="#fff"/>
    <g color="currentColor"/>
</svg>

to

<svg xmlns="http://www.w3.org/2000/svg">
    <g color="#fff" class="tone2"/>
    <g color="currentColor" class="tone1"/>
</svg>

More Configs Example

// svgo.config.js
const addClassesToColors = require('svgo-plugin-add-classes-to-colors');
module.exports = {
  plugins: [
    {
      ...addClassesToColors,
      params: {
        mapping: {
          // [color attribute]: { [color]: className }
          fill: {
            '#fff': 'fill--tone1',
          },
          stroke: 'fill' // will add class `fill--tone1` to stroke="#fff"
        }
      }
    }
  ],
};

Why

This will make controlling SVG colors easier. You can write CSS like

.tone1 {
    color: white;
    fill: white;
    stroke: white;
}

to change all the colors of SVG in your page.

About

SVGO plugin that add corresponding class to element that has specific

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 3

  •  
  •  
  •  
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy