Gebna

Access thousands of icons as components on-demand universally

Written by Bahaa Zidan

Today I’m showcasing unplugin-icons. An amazing library that basically solved the icon problem for me. Here’s a feature list from their README:

Features

Usage (React)

import IconAccessibility from "~icons/carbon/accessibility";
import IconAccountBox from "~icons/mdi/account-box";
 
function App() {
  return (
    <div>
      <IconAccessibility />
      <IconAccountBox style={{ fontSize: "2em", color: "red" }} />
    </div>
  );
}

Installation

Since it’s framework agnostic, I’m not gonna be listing all the different ways you can get it to work in your framework of choice. I’ll only show how I got it to work in SvelteKit

Install unplugin-icon

pnpm add -D unplugin-icons

Either install the full iconify pack or your icon pack of choice. I’ll install the tabler pack:

pnpm add @iconify-json/tabler

In your vite.config.ts:

import { sveltekit } from "@sveltejs/kit/vite";
import { defineConfig } from "vitest/config";
import Icons from "unplugin-icons/vite";
 
export default defineConfig({
  plugins: [
    sveltekit(),
    Icons({
      compiler: "svelte",
    }),
  ],
});

Finally, in your src/app.d.ts file:

import "unplugin-icons/types/svelte";
 
declare global {
  namespace App {}
}
 
export {};

And that’s it! Now you can import the icons across your SvelteKit project with ease:

<script>
  import GoogleIcon from "virtual:icons/tabler/brand-google-filled";
  import GitHubIcon from "virtual:icons/tabler/brand-github-filled";
</script>
 
<button class="btn btn-error btn-active text-white">
  <GoogleIcon width={32} height={32} />
  Google
</button>
<button class="btn btn-neutral btn-active">
  <GitHubIcon width={32} height={32} />
  GitHub
</button>

As you can see, you can also style them because at runtime it’s just an svg.