2.2.0 • Published 12 months ago

last-icon v2.2.0

Weekly downloads
-
License
MIT
Repository
github
Last release
12 months ago

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:

NameTypeDescription
debugBooleanShould we output messages to console
lazyBooleanLoad icons lazily
replaceNameObjectTransparently replace icons with other values
fontsArrayIcon sets using font icons rather than SVG
defaultSetStringDefault icon set
setsObject.<string, IconSet>Available iconsets

Supported icon sets

Icon SetNameAliasTypesStrokeCountWebsite
Bootstrap Iconsbootstrapbs1x1800+bootstrap
Boxiconsboxiconsbx3x1600+boxicons
Bytesized Iconsbytesizeby1v101bytesize
Css.ggcssgggg1x700+cssgg
Emojiemojiccem1x?emojicc
Eos Iconseoseo3x1000+eos
Featherfeatherft1v280+feather
Flagsflagsfl1x?flags
Fontawesomefontawesomefa5x1600+fontawesome
Iconoiriconoirin1x1300+iconoir
IconParkiconparkip4v2400+iconpark
Lucidelucidelu1v1800+lucide
Materials Iconsmaterialmi5x1100+material icons
Phosphorphosphorph6x6200+phosphor
Super Tiny Iconssupertinyst1x?supertiny
Materials Symbolssymbolsms3v2500+material symbols
Tabler Iconstablerti1v4000+tabler

Adding or updating an icon set

You can update any option for an icon set

NameTypeDescription
aliasStringShort two letters alias
svgPathfunctionThe SVG path
fixFillBooleanDoes this set needs fixing fill:currentColor?
useStrokeStringAdd stroke to svg
defaultStrokeStringDefault stroke to use (if supports stroke)
defaultTypeStringDefault type to use (when there are multiple types)
prefixesObject.<string, string>Types to prefixes
fontClassfunctionFont class
opticalFontBooleanIs an optical font?
nameStringFull 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/

2.2.0

12 months ago

2.1.2

1 year ago

2.1.1

1 year ago

2.0.3

1 year ago

2.0.2

1 year ago

2.0.4

1 year ago

2.1.0

1 year ago

2.0.1

2 years ago

2.0.0

2 years ago

1.3.3

2 years ago

1.3.2

2 years ago

1.3.1

2 years ago

1.3.0

2 years ago

1.2.5

3 years ago

1.2.4

3 years ago

1.2.3

3 years ago

1.2.2

3 years ago

1.2.0

3 years ago

1.1.0

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago