GithubHelp home page GithubHelp logo

kripod / astro-icon Goto Github PK

View Code? Open in Web Editor NEW

This project forked from natemoo-re/astro-icon

0.0 1.0 0.0 737 KB

Inline and sprite-based SVGs in Astro made easy!

Home Page: https://astroicon.dev

License: Other

TypeScript 77.70% CSS 6.62% Astro 15.68%

astro-icon's Introduction

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.

astro-icon's People

Contributors

chuygil avatar dwightjack avatar fredkschott avatar germanz avatar github-actions[bot] avatar jasikpark avatar magicbyt3 avatar matthewp avatar natemoo-re avatar rafidmuhymin avatar stramel avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.