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

last-icon

v2.2.0

Published

One custom icon element to rule them all

Downloads

31

Readme

last-icon

NPM Downloads

The last icon library you will ever need.

Key features:

  • Load as svg or font icons
  • Mix & match icon sets if needed
  • Bring your own icons
  • Fix iconsistencies
  • Lazy load your icons

How to use

Simply include the library

<script src="last-icon.js"></script>

NOTE: It is recommended to define this as early as possible so that all icons are resolved as soon as possible. Otherwise, you might see a delay before your icons are being displayed. Even when doing this, you might still see a very small delay as opposed as a font icon or an embedded SVG.

If you are fine with a little more delay, you can use this instead (which will be ignored on browsers that don’t support modules):

<!-- preload helps displaying things as early as possible, doesn't work in firefox and safari -->
<link rel="modulepreload" href="last-icon.js" />
<script type="module" src="./last-icon.js"></script>

And call your icons!

<l-i name="star"></l-i>
<l-i name="star" set="tb"></l-i>
<l-i name="star" size="32" set="tb"></l-i>

<l-i name="star" set="bx" type="solid"></l-i>
<l-i name="star" set="bx" type="regular"></l-i>
<l-i name="github" set="bx" type="logos"></l-i>

The following CSS is recommended:

l-i {
  --size: 1em;
  display: inline-flex;
  width: var(--size);
  height: var(--size);
  vertical-align: middle;
}
l-i svg {
  display: block;
  width: 100%;
  height: 100%;
}
p l-i,
button l-i,
a l-i,
span l-i {
  vertical-align: -0.125em;
}

Configuring

You can set any options using LastIcon.configure. The recommended way to call it is this way:

customElements.whenDefined("l-i").then(() => {
  // Access through registry
  customElements.get("l-i").configure({
    debug: true,
    // Transform stars to trash
    // replaceName: {
    //   star: "trash"
    // },
    // Use font icon
    // fonts: [
    //   "material",
    //   "phosphor",
    // ],
    // Change default set
    defaultSet: "tabler",
    // Change default stroke
    // defaultStroke: 1,
  });
});

All available options:

| Name | Type | Description | | ----------- | ------------------------------------------- | --------------------------------------------- | | debug | Boolean | Should we output messages to console | | lazy | Boolean | Load icons lazily | | replaceName | Object | Transparently replace icons with other values | | fonts | Array | Icon sets using font icons rather than SVG | | defaultSet | String | Default icon set | | sets | Object.<string, IconSet> | Available iconsets |

Supported icon sets

| Icon Set | Name | Alias | Types | Stroke | Count | Website | | ----------------- | ----------- | ----- | :---: | :----: | :---- | ------------------------------------------------------------------------ | | Bootstrap Icons | bootstrap | bs | 1 | x | 1800+ | bootstrap | | Boxicons | boxicons | bx | 3 | x | 1600+ | boxicons | | Bytesized Icons | bytesize | by | 1 | v | 101 | bytesize | | Css.gg | cssgg | gg | 1 | x | 700+ | cssgg | | Emoji | emojicc | em | 1 | x | ? | emojicc | | Eos Icons | eos | eo | 3 | x | 1000+ | eos | | Feather | feather | ft | 1 | v | 280+ | feather | | Flags | flags | fl | 1 | x | ? | flags | | Fontawesome | fontawesome | fa | 5 | x | 1600+ | fontawesome | | Iconoir | iconoir | in | 1 | x | 1300+ | iconoir | | IconPark | iconpark | ip | 4 | v | 2400+ | iconpark | | Lucide | lucide | lu | 1 | v | 1800+ | lucide | | Materials Icons | material | mi | 5 | x | 1100+ | material icons | | Phosphor | phosphor | ph | 6 | x | 6200+ | phosphor | | Super Tiny Icons | supertiny | st | 1 | x | ? | supertiny | | Materials Symbols | symbols | ms | 3 | v | 2500+ | material symbols | | Tabler Icons | tabler | ti | 1 | v | 4000+ | tabler |


Adding or updating an icon set

You can update any option for an icon set

| Name | Type | Description | | --------------- | ------------------------------------------ | --------------------------------------------------- | | alias | String | Short two letters alias | | svgPath | function | The SVG path | | [fixFill] | Boolean | Does this set needs fixing fill:currentColor? | | [useStroke] | String | Add stroke to svg | | [defaultStroke] | String | Default stroke to use (if supports stroke) | | [defaultType] | String | Default type to use (when there are multiple types) | | [prefixes] | Object.<string, string> | Types to prefixes | | [fontClass] | function | Font class | | [opticalFont] | Boolean | Is an optical font? | | [name] | String | Full name (injected automatically) |

Fill

Some icon sets include a default fill="currentColor" and some don't. In order to have all icon sets behave consistently, we apply a fill="currentColor" to all icon sets. This fix apply to: 'material', 'fontawesome'.

Why a custom element

  • External sprite or font is loading all the icons which lead to extra load time
  • Including SVG is leading to really super long HTML and not very developer friendly
  • No need for custom JS inliner, it feels cleaner overall

Why external CSS

A custom element CSS is not loaded until the component itself is loaded, which can lead to FOUC and things moving around as the icon appear. The solution I've found so far is to apply some global CSS rules than are known before the component is loaded.

You can check any extra SCSS that might be useful for you as well.

Using fonts

Sometimes it is easier to use an icon font. Indeed, it is fully cached by the browser and will not have any display glitch. Obviously, the downside is that you have to load the whole font, but it's cached after the first load. The advantage of using LastIcon over regular icons is that is allows you to switch easily between one way or the other.

First of all, load any relevant fonts style

<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet" />
<link href="https://fonts.googleapis.com/icon?family=Material+Icons+Two+Tone" rel="stylesheet" />
<link href="https://fonts.googleapis.com/icon?family=Material+Icons+Sharp" rel="stylesheet" />
<link href="https://fonts.googleapis.com/icon?family=Material+Icons+Round" rel="stylesheet" />
<link href="https://fonts.googleapis.com/icon?family=Material+Icons+Outlined" rel="stylesheet" />

And after that, use the font config to tell Last Icon to use the font over the SVG icons

customElements.whenDefined("l-i").then(() => {
  // Access through registry
  customElements.get("l-i").configure({
    debug: true,
    fonts: ["material"],
    material: {
      defaultType: "two-tone",
    },
  });
});

And then, update your styles:

l-i {
  --size: 1em;
  display: inline-flex;
  width: var(--size);
  height: var(--size);
  vertical-align: middle;

  svg {
    display: block;
    width: 100%;
    height: 100%;
  }
  i {
    font-size: var(--size) !important;
    color: currentColor;
  }
}
p,
button,
a,
span {
  l-i {
    vertical-align: -0.125em;
  }
}

.material-icons-two-tone {
  background-color: currentColor;
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
}

Demo

See demo.html or the following pen https://codepen.io/lekoalabe/pen/eYvdjqY

Worth looking at

You might also be interested in https://icon-sets.iconify.design/