Skip to content

Latest commit

 

History

History
238 lines (191 loc) · 9.06 KB

readme.md

File metadata and controls

238 lines (191 loc) · 9.06 KB

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
Flags flags fl 1 x ? flags
Fontawesome fontawesome fa 5 x 1600+ fontawesome
Iconoir iconoir in 1 x 1600+ 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 9000+ phosphor
Super Tiny Icons supertiny st 1 x 386 supertiny
Materials Symbols symbols ms 3 v 2500+ material symbols
Tabler Icons tabler ti 1 v 5700+ 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/