11ty Plugin: Phosphor Icons

npm npm npm npm license license

This plugin adds a shortcode to your Eleventy project to easily include icons from the Phosphor Icons collection.

Installation

Install the plugin from npm:

npm install eleventy-plugin-phosphoricons --save-dev

Configuration

If render is set to image or img, the following attributes can be used:

Usage

Add it to your Eleventy Config config file:

const eleventyPluginPhosphoricons = require('eleventy-plugin-phosphoricons');

module.exports = function (eleventyConfig) {
    eleventyConfig.addPlugin(eleventyPluginPhosphoricons);
};

Advanced usage:

const eleventyPluginPhosphoricons = require('eleventy-plugin-phosphoricons');

module.exports = function (eleventyConfig) {
    eleventyConfig.addPlugin(eleventyPluginPhosphoricons, {
        class: "phicon",
        style: "vertical-align: middle;",
        size: 32,
        fill: "currentColor"
    });
};

Using transformFill callback function

May be useful if you using a CSS framework like Tailwind CSS, Bootstrap, etc. and you want to map the fill attribute to the text color classes.

TailwindCSS usage example:

const eleventyPluginPhosphoricons = require('eleventy-plugin-phosphoricons');
const resolveConfig = require('tailwindcss/resolveConfig.js')
const tailwindConfig = require('tailwind.config.js')

const fullConfig = resolveConfig(tailwindConfig)
module.exports = function (eleventyConfig) {
    eleventyConfig.addPlugin(eleventyPluginPhosphoricons, {
        class: "phicon",
        style: "vertical-align: middle;",
        size: 32,
        fill: "currentColor",
        transformFill: (color) => {
            const [baseColor, shade] = color.replace('text-', '').split('-');
            return shade ? fullConfig.theme.colors[baseColor][shade] : fullConfig.theme.colors[baseColor]['DEFAULT'];
        }
    });
};

Demo

SVG Example

SVG example contains 16 child elements.

Image Example

Image example contains 7 child elements.

Why render icons as an image?

Rendering icons as images can help to avoid an excessive DOM size. This can be useful when you have a lot of icons on a page, or when you want to avoid the overhead of rendering SVGs in the DOM.

>Note: rendered icons as image, can't get the same color as the text color.

License

MIT License


Fork me on GitHub