npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

fg-iconoclash

v0.3.0

Published

Easy external SVG icons

Downloads

3,473

Readme

Iconoclash

A workflow for configurable external svg sets.

Demo output: https://filamentgroup.github.io/iconoclash/demo/output/icons.html

How it works:

Pass an array of SVG files to iconoclash and it will combine them into a single SVG file with each file still referenceable by name. For example, once this icons.svg file is generated, your SVG files will be addressable by ID within that file, so if you started with an SVG called vespa.svg, you can reference it from your page like this:

<svg width="100" height="100">
   <use xlink:href="icons.svg#vespa"></use>
</svg>

That single Icons.svg file can be used for all icons throughout your site and it only needs to load once, which can be nice for performance and caching. If you'd like though, you can keep the svgs separate in production instead by setting the writeIndividualFiles option to true. This works nicely when you have a great number of potential icons to load and don't want to load all of them every time. As external icons, your paths instead look like this:

<svg width="100" height="100">
   <use xlink:href="vespa.svg#vespa"></use>
</svg>

Regardless of sprite or no sprite, a typical downside to using external SVG like this is that you can't easily use CSS to style particular shapes within that external SVG, say, to change the fill color of a particular group. Iconoclash makes this problem go away by exposing CSS custom properties on the SVG elements themselves that you'd like to be able to style. By default, it will find all fill properties within the svg that share common colors, and assign them a configurable shared CSS property. Changing that property in your parent page will change all instances of where that property is used in your external SVGs.

Here we have an iconoclash-shared-0 property that will be shared by every other svg element in the set that has the same value. So you could override all icons to have a different fill for this one color by changing iconoclash-shared-0:

/* Iconoclash: CSS properties exposed from SVGs */
:root {
--iconoclash-shared-0: #E2574C;
}

These custom properties can be overridden from the HTML or another stylesheet, either globally, or to particular selectors or media queries.

<svg width="100" height="100">
    <use xlink:href="icons.svg#baseball" style="--iconoclash-shared-0:blue;"></use>
 </svg>

Additionally, SVG elements inside those files can have an ID attribute value that specifies CSS properties to exposed case-by-case so they can be customized. To specify properties, add the word iconoclash to the ID attribute followed by CSS property names you'd like to expose. For example, consider how this g element inside a file called vespa.svg starts to get transformed:

  • Before: <g stroke="#E2574C" id="iconoclash stroke">
  • After: <g stroke="#E2574C" style="fill: var(--vespa-path2-stroke, var(--iconoclash-shared-0, #E2574C));">

Note that Iconoclash sets the defaults for each of these CSS custom properties either to an existing value on the shape, or to 'initial'. It also sets the custom properties to share global values that are similar, so you can modify shared brand colors and values across your SVGs while still overriding single instances:

Usage

Pass an array of file paths and an output folder path to the Iconoclash function.

var Iconoclash = require('../src/iconoclash');
var iconoclash = new Iconoclash( ["./svg/lamp.svg", "./svg/skate.svg", "./svg/vespa.svg"], "./output/" );
iconoclash.process();

You can try this by running the testrun file like this:

$ cd demo && node testrun

options

The Iconoclash function accepts a third option to override these defaults:

{
    iconcss: 'icons.css',
    iconsvg: 'icons.svg',
    icondata: 'icons.json',
    iconhtml: "icons.html",
    htmlinput: "../src/preview.html",
    idKey: "iconoclash",
    autoExpose: ["fill"],
    setAutoExposeDefaults: false,
    writeIndividualFiles: false,
    ignoreInsideElems: 'a|altGlyphDef|clipPath|color-profile|cursor|filter|font|font-face|foreignObject|image|marker|mask|pattern|script|style|switch|text|view',
    banner: "/* Iconoclash: CSS properties exposed from SVGs */",
    svgstyles: "svg > g {display:none;} svg > g:target{display:inline}",
    verbose: false,
    logger: {
        verbose: console.info,
        fatal: console.error,
        ok: console.log
    }
}