Astro Icon

repository·main·Indexed 23 days ago

https://github.com/natemoo-re/astro-icon

An integration for Astro that allows users to manage and use local SVG files and Iconify icons. It provides an Icon component that inlines SVGs into markup and utilizes an optimized SVG sprite system to reduce HTML size. It supports custom configuration for icon directories, SVGO optimization, and selective bundling of Iconify collections for server or hybrid output modes.

Tokens
5.6K
Snippets
26
Records
40
Agent score
79%

What's inside astro-icon

  1. Overview of Astro Icon features

    main

    Astro Icon is a tool designed to simplify working with icons in Astro projects. Key features include:

    • Local Icons: Automatically embed your own custom svg files.
    • Icon Sets: Full support for all @iconify-json/* packages, allowing you to use thousands of icons from the Iconify ecosystem.
    • Intelligent Sprites: Uses deduplicated sprites to provide icons with zero runtime overhead.
    • Fully Dynamic: Supports fully dynamic icon references, meaning you can use icon names as strings without needing explicit imports for every icon.
  2. How automatically optimized sprites work

    main

    To reduce HTML size, astro-icon uses an optimized SVG sprite system.

    1. First usage: The first time an icon is used on a page, the component defines a <symbol> with a unique ID containing the icon's contents, and renders it using a <use> element.
    2. Subsequent usages: Any further references to the same icon on that page will only render a <use> element pointing to the existing <symbol> ID, avoiding redundant SVG data.

    This approach leverages a single <svg> sprite for your entire site.

    ---
    import { Icon } from 'astro-icon/components'
    ---
    
    <Icon name="logo" />
    <!-- First usage generates the following HTML -->
    <svg data-icon="logo">
    	<symbol id="ai:uniqueid"><!-- contents of /src/icons/logo.svg --></symbol>
    	<use href="#ai:uniqueid"></use>
    </svg>
    
    <Icon name="logo" />
    <!-- Additional usage generates the following HTML -->
    <svg data-icon="logo">
    	<use href="#ai:uniqueid"></use>
    </svg>
  3. Understand the Astro project structure

    main

    A standard minimal Astro project follows this structure:

    • public/: Contains static assets like images that are served directly.
    • src/pages/: Contains .astro or .md files. Each file in this directory is automatically exposed as a route based on its filename.
    • src/components/: A conventional location for storing Astro, React, Vue, Svelte, or Preact components.
    • package.json: Defines project dependencies and scripts.
    /
    ├── public/
    ├── src/
    │   └── pages/
    │       └── index.astro
    └── package.json
  4. Use Iconify open source icon sets

    main

    Astro Icon supports any open source icon set distributed via Iconify. To use an icon set, you must install its corresponding @iconify-json/* package. Once installed, icons are available using their prefixed ID (e.g., prefix:icon-name).

    Warning for server or hybrid output: If your project uses these output modes, every icon that could potentially be referenced may be included in your server's JavaScript bundle by default. See the Deployment guide to optimize bundle size.

    ---
    import { Icon } from 'astro-icon/components'
    ---
    
    <!-- Embed the `account` icon from the `@iconify-json/mdi` set -->
    <Icon name="mdi:account" />
  5. Use local SVG icons

    main

    Astro Icon supports custom local SVG icons stored in your project. By default, it looks in src/icons/.

    1. Create a directory at src/icons/.
    2. Add your .svg files to that directory.
    3. Use the Icon component with the name prop matching the filename (without the extension).
    ---
    import { Icon } from 'astro-icon/components';
    ---
    
    <!-- Loads the SVG in `/src/icons/filename.svg` -->
    <Icon name="filename" />
  6. Style icons with custom CSS using attribute selectors

    main

    You can target Icon components using the [data-icon] attribute selector.

    • To apply styles to every icon in your project, use the [data-icon] selector.
    • To apply styles to a specific icon, use the [data-icon="name"] selector, where name is the icon name passed to the component.
    <Icon name="twitter" />
    <Icon name="mastodon" />
    
    <style>
    /* Targets all icons */
    [data-icon] {
        font-size: 2rem;
        color: var(--brand-color);
    }
    </style>
    
    <Icon name="logo" />
    
    <style>
    /* Targets only the 'logo' icon */
    [data-icon="logo"] {
        font-size: 2rem;
        color: var(--brand-color);
    }
    </style>