An Eleventy shortcode, allows Phosphor icons to be embedded as inline svg or render as an image in your Eleventy project.
Demo: https://eleventy-plugin-phosphoricons.netlify.app/
Install the plugin from npm:
npm install eleventy-plugin-phosphoricons --save-dev
class
- The class attribute to be added to the svg element. Default:phicon
style
- The style attribute to be added to the svg element. Default:undefined
size
- The width and height attribute to be added to the svg element. Default:256
fill
- The fill attribute to be added to the svg element. Default:currentColor
width
- The width attribute to be added to the img element. Default: taken from thesize
attributeheight
- The height attribute to be added to the img element. Default: taken from thesize
attributerender
- The render method allows you to render icon as inline svg or image. Default:svg
, other options:image
orimg
transformClass
- A CSS class that you want to map using a callback functiontransformFill
. Default:false
transformFill
- A callback function to transform the fill attribute, based ontransformClass
attribute. Default:undefined
If render
is set to image
or img
, the following attributes can be used:
alt
- The alt attribute to be added to the img element. Default:altPrefix + iconName
altPrefix
- The alt attribute prefix to be added to the img element. Default:icon
loading
- The loading attribute to be added to the img element. Default:lazy
decoding
- The decoding attribute to be added to the img element. Default:async
eleventyIgnore
- The eleventyIgnore attribute to be added to the img element to prevent@11ty/eleventy-img
plugin from processing the image. Default:true
Add it to your Eleventy Config file:
const eleventyPluginPhosphoricons = require('eleventy-plugin-phosphoricons');
module.exports = function (eleventyConfig) {
eleventyConfig.addPlugin(eleventyPluginPhosphoricons);
};
const eleventyPluginPhosphoricons = require('eleventy-plugin-phosphoricons');
module.exports = function (eleventyConfig) {
eleventyConfig.addPlugin(eleventyPluginPhosphoricons, {
class: "phicon",
style: "vertical-align: middle;",
size: 32,
fill: "currentColor"
});
};
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'];
}
});
};
The plugin turns 11ty shortcodes like this:
{% phosphor "phosphor-logo" %}
or
{% phicon "phosphor-logo" %}
into HTML code like this:
<svg xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 256 256"
fill="currentColor"
class="phicon"
style="vertical-align: middle;"
width="32" height="32">
<path d="M144,24H64a8,8,0,0,0-8,8V160a80.09,80.09,0,0,0,80,80,8,8,0,0,0,8-8V168a72,72,0,0,0,0-144ZM72,62.54,122.32,152H72Zm56,66.92L77.68,40H128ZM72.51,168H128v55.5A64.14,64.14,0,0,1,72.51,168ZM144,152V40a56,56,0,0,1,0,112Z"></path>
</svg>
or
{% phicon "phosphor-logo", { render: 'image' } %}
into HTML code like this:
<img src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 256 256' fill='currentColor' style='vertical-align: middle;' width='32' height='32'%3E%3Cpath d='M144,24H64a8,8,0,0,0-8,8V160a80.09,80.09,0,0,0,80,80,8,8,0,0,0,8-8V168a72,72,0,0,0,0-144ZM72,62.54,122.32,152H72Zm56,66.92L77.68,40H128ZM72.51,168H128v55.5A64.14,64.14,0,0,1,72.51,168ZM144,152V40a56,56,0,0,1,0,112Z'%3E%3C/path%3E%3C/svg%3E"
alt="phosphor-logo"
class="phicon"
style="vertical-align: middle;"
width="32"
height="32"
alt="icon ">
{% phicon "phosphor-logo", "duotone", {
style: "color:red"
size: 64,
class: "phicon bg-blue"
} %}
If you notice an issue, feel free to open an issue.
- Fork this repo
- Clone
git clone [email protected]:reatlat/eleventy-plugin-phosphoricons.git
- Install dependencies
npm install
- Build
npm run build
- Serve locally
npm run dev
The code is available under the MIT license.