• Stars
    star
    1,072
  • Rank 43,150 (Top 0.9 %)
  • Language MDX
  • License
    Other
  • Created almost 3 years ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Inline and sprite-based SVGs in Astro made easy!

Astro Icon

A straight-forward Icon component for Astro.

Install astro-icon.

npm i astro-icon
# or
yarn add astro-icon

Icon Packs

astro-icon automatically includes all of the most common icon packs, powered by Iconify!

To browse supported icons, check the official Icon Sets reference or visit IcΓ΄nes.

Usage

Icon will inline the SVG directly in your HTML.

---
import { Icon } from 'astro-icon'
---

<!-- Automatically fetches and inlines Material Design Icon's "account" SVG -->
<Icon pack="mdi" name="account" />

<!-- Equivalent shorthand -->
<Icon name="mdi:account" />

Sprite will reference the SVG from a spritesheet via <use>.

---
import { Sprite } from 'astro-icon'
---

<!-- Required ONCE per page as a parent of any <Sprite> components! Creates `<symbol>` for each icon -->
<!-- Can also be included in your Layout component! -->
<Sprite.Provider>
  <!-- Automatically fetches and inlines Material Design Icon's "account" SVG -->
  <Sprite pack="mdi" name="account" />

  <!-- Equivalent shorthand -->
  <Sprite name="mdi:account" />

</Sprite.Provider>

You may also create Local Icon Packs.

Local Icons

By default, astro-icon supports custom local svg icons. They are optimized with svgo automatically with no extra build step. See "A Pretty Good SVG Icon System" from CSS Tricks.

Usage

  1. Create a directory inside of src/ named icons/.
  2. Add each desired icon as an individual .svg file to src/icons/
  3. Reference a specific icon file using the name prop.

Icon will inline the SVG directly in your HTML.

---
import { Icon } from 'astro-icon';
---

<!-- Loads the SVG in `/src/icons/filename.svg` -->
<Icon name="filename" />

Sprite will reference the SVG from a spritesheet via <use>.

---
import { Sprite } from 'astro-icon';
---

<!-- Required ONCE per page as a parent of any <Sprite> components! Creates `<symbol>` for each icon -->
<!-- Can also be included in your Layout component! -->
<Sprite.Provider>
  <!-- Uses the sprite from `/src/icons/filename.svg` -->
  <Sprite name="filename" />
</Sprite.Provider>

Local Icon Packs

astro-icon supports custom local icon packs. These are also referenced with the pack and/or name props.

  1. Create a directory inside of src/ named icons/.
  2. Inside that directory, create a JS/TS file with your pack name inside of that directory, eg src/icons/my-pack.ts
  3. Export a default function that takes an icon name and returns a svg string. Utilize the createIconPack utility to handle most common situations.

If using a package from NPM, eg. heroicons, the icon pack file would resemble the following:

import { createIconPack } from "astro-icon/pack";

// Resolves `heroicons` dependency and reads SVG files from the `heroicons/outline` directory
export default createIconPack({ package: "heroicons", dir: "outline" });

If using an icon set from a remote server, the icon pack file would resemble the following:

import { createIconPack } from "astro-icon/pack";

// Resolves `name` from a remote server, like GitHub! Notice that the `dir` option is not required
export default createIconPack({
  url: "https://raw.githubusercontent.com/radix-ui/icons/master/packages/radix-icons/icons/",
});

If you have custom constraints, you can always create the resolver yourself. Export a default function that resolves the name argument to an SVG string.

import { loadMyPackSvg } from "my-pack";

export default async (name: string): Promise<string> => {
  const svgString = await loadMyPackSvg(name);
  return svgString;
};

Styling

Styling your astro-icon is straightforward. Any styles can be targeted to the [astro-icon] attribute selector. If you want to target a specific icon, you may target it by name using [astro-icon="filename"].

---
import { Icon } from 'astro-icon';
---

<style lang="css">
    [astro-icon] {
        color: blue;
        /* OR */
        fill: blue;
    }
    [astro-icon="annotation"] {
        color: red;
        /* OR */
        fill: red;
    }
</style>

<Icon name="adjustment" /> <!-- will be blue -->
<Icon name="annotation" /> <!-- will be red -->

<!-- Example using Tailwind to apply color -->
<Icon name="annotation" class="text-red-500" /> <!-- will be red-500 -->

Props

<Icon> and <Sprite> share the same interface.

The name prop references a specific icon. It is required.

The optimize prop is a boolean. Defaults to true. In the future it will control svgo options.

Both components also accepts any global HTML attributes and aria attributes. They will be forwarded to the rendered <svg> element.

See the Props.ts file for more details.

Troubleshooting

Icon not found

When an icon is not found, Icon uses a fallback icon of a black box. This is likely either a typo on the name prop or a missing svg file in the src/icons folder.

More Repositories

1

clack

Effortlessly build beautiful command-line apps
TypeScript
5,170
star
2

microsite

Do more with less JavaScript. Microsite is a smarter, performance-obsessed static site generator powered by Preact and Snowpack.
TypeScript
877
star
3

ultrahtml

TypeScript
521
star
4

natemoo-re

TypeScript
433
star
5

micromorph

A very tiny library for diffing DOM nodes
TypeScript
339
star
6

satori-html

An HTML adapter for Vercel's Satori
TypeScript
273
star
7

ultrafetch

RFC-7234 compliant caching for any `fetch` implementation
TypeScript
222
star
8

astro-remote

Render remote HTML or Markdown content in Astro with full control over the output
Astro
173
star
9

ultramatter

<1kB frontmatter parser that supports a reasonable subset of YAML
TypeScript
123
star
10

animate-presence

Effortless element entrance/exit animations
TypeScript
99
star
11

proload

Searches for and loads your tool's JavaScript configuration files with full support for CJS, ESM, TypeScript and more.
JavaScript
81
star
12

astro-capo

TypeScript
57
star
13

eslexer

Incredibly tiny, fast JS tokenizer
TypeScript
57
star
14

astro-php

Please don't use this in production
JavaScript
44
star
15

ultraflag

<1kB CLI flag parser
TypeScript
44
star
16

feed

Astro
43
star
17

magic-element

Automatic custom-elements with zero boilerplate
TypeScript
34
star
18

tslint-stencil

Contributes some helpful tslint rules for Stencil projects
TypeScript
22
star
19

tinyshell

A tiny `node:child_process` wrapper for shell scripts
TypeScript
21
star
20

konami-listener

Vanilla web component that listens for the Konami Code
JavaScript
20
star
21

ultramark

TypeScript
19
star
22

tsdx-monorepo

TypeScript
19
star
23

goldmark

JavaScript
19
star
24

project-template

TypeScript
19
star
25

tiny-decode

Decode HTML entities the browser-friendly way
JavaScript
18
star
26

prettier-plugin-style-order

Prettier plugin to automatically sort style declarations
JavaScript
16
star
27

stencil-forms

πŸ“πŸ€― Stencil component to automatically manage form state
TypeScript
14
star
28

miles-of-code

Astro
13
star
29

microsite-demo

TypeScript
11
star
30

stencil-tools

Editor enhancements and utilities for Stencil projects
TypeScript
10
star
31

emojex

Emoji-based Regular Expressions
TypeScript
10
star
32

astro-client-hints

TypeScript
9
star
33

astro-run

TypeScript
9
star
34

clack.cc

Astro
9
star
35

astro-form

Astro
7
star
36

raycast-microblog

TypeScript
7
star
37

topic

TypeScript
7
star
38

exo-store

TypeScript
6
star
39

astro-breadcrumbs

Astro
6
star
40

astro-beep

JavaScript
6
star
41

gamepad

Provides a simple, event-based API for Gamepads on the web
TypeScript
5
star
42

esm-create

npm init, but better
JavaScript
5
star
43

experiment--astro-editor

Astro
5
star
44

orbit

Astro
4
star
45

blocks

TypeScript
3
star
46

require-or-import

JavaScript
3
star
47

stencil-markdown

Stencil plugin that allows Markdown files to be imported as FunctionalComponents
TypeScript
3
star
48

a11y-types

Typescript Definitions for ARIA attribute constants
TypeScript
2
star
49

microstream

TypeScript
2
star
50

bottom-sheet

Bottom sheet web component
TypeScript
2
star
51

stencil-hooks

TypeScript
2
star
52

typescript-plugin-stencil

Typescript plugin for a smoother Developer Experience within Stencil projects
TypeScript
2
star
53

midu-astro

Astro
2
star
54

hibiscus

A *VERY ROUGH* proof of concept using Stencil to dynamically load and animate an SVG-based font.
TypeScript
1
star
55

astro-bundle-demo

JavaScript
1
star
56

node-shqxzf

Created with StackBlitz ⚑️
1
star
57

use-linked-context

JavaScript
1
star
58

pronouns

HTML
1
star
59

astro-playground

TypeScript
1
star
60

astro-dev-plugin-sample

Astro
1
star
61

actions-playground

Shell
1
star
62

test-social-web

1
star
63

astro-template-blog

1
star
64

astro-gh-pages-test

Astro
1
star
65

astro-minimal

JavaScript
1
star
66

microsite-templates

TypeScript
1
star
67

text-wrap-balance

1
star
68

node-wfhqwq

JavaScript
1
star
69

git-comments-test

1
star
70

astro-demo-named-slots

Astro
1
star
71

vscode-filename-snippets

TEST
TypeScript
1
star
72

astro-vercel-rewrites

Astro
1
star
73

action-test

TypeScript
1
star
74

bug--astro-netlify-libsql

TypeScript
1
star
75

natemoo-re.github.io

Public site
HTML
1
star