npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

astro-sprite

v1.1.3

Published

Sprite generation (png, webp, avif...) dedicated to Astro framework

Downloads

421

Readme

astro-sprite

astro-sprite is a npm package aimed at building a sprite from small images/icons, to be used with Astro. The sprite is created as a png, webp, or avif image.

Typically, from single small images/icons (png, webp, avif) and and and and , astro-sprite integration creates the following bigger image (the sprite), that contains all small icons:

as well as a .css file, that used by the html to display a small image from the sprite. Typically, it includes:

.astro-sprite { background-image:url(/img/astro-sprite.png?v=0cfc71);}
.astro-sprite-english { background-position: -0px -0px; width: 32px; height: 32px; }
.astro-sprite-facebook { background-position: -32px -0px; width: 32px; height: 32px; }
.astro-sprite-france { background-position: -64px -0px; width: 32px; height: 32px; }
.astro-sprite-play_20x20 { background-position: -96px -0px; width: 20px; height: 20px; }
.astro-sprite-youtube { background-position: -116px -0px; width: 32px; height: 32px; }

It is then rather easy to display the english flag in html, using for example:

<div class="astro-sprite astro-sprite-english">
</div>

For more information about sprites and their benefits, here is a link selection:

Usage

Installation

Quick install

To install astro-sprite, run the following from your project directory and follow the prompts:

  • Using NPM: npx astro add astro-sprite
  • Using Yarn: yarn astro add astro-sprite
  • Using PNPM: pnpx astro add astro-sprite

Manual install

First, install the astro-sprite package using your package manager. If you're using npm, run this in the terminal:

npm install astro-sprite

Then, apply this integration to your astro.config.mjs file using the integrations property:

import { defineConfig } from 'astro/config';
import sprite from 'astro-sprite'

export default defineConfig({
  integrations: [
    sprite()
  ],
});

The default behavior of astro-sprite is the following:

  • Look for all .png images in assets/astro-sprite located in the astro src directory
  • Save the resulting sprite image as img/astro-sprite.png, in the astro public dir
  • Save the resulting css file as css/astro-sprite.css in the astro src dir
    • the class name containing the background-image css property is .astro-sprite
    • each icon in the sprite have a dedicated class name that starts with .astro-sprite-, followed by the filename base. Typically, .astro-sprite-english reference icon in english.png

Customizations

sprite-astro can be customized to better fit your need. Here are the default values that can be customized:

import { defineConfig } from 'astro/config';
import sprite from 'astro-sprite'

export default defineConfig({
  integrations: [
    sprite({
      src: {
        dir: 'img/astro-sprite',
        extension: '.png',
      },
      dst: {
        spriteFile: 'img/astro-sprite.png',
        cssFile: 'css/astro-sprite.css',
        preloadFile: 'components/SpritePreload.astro',
        cssMainClass: '.astro-sprite',
        cssPrefix: '.astro-sprite-',
        cssSelector: '',
      },
      verbose: true,
    })
  ],
});

Customized properties are:

  • src: properties related to the source icons:
    • dir: directory where the single icons are located, relative to the astro srcDir.
    • extension: all files in src.dir with the provided extension will be used the sprite. .webp and .avif can be used
  • dst: properties related to the output of the integration
    • spriteFile: the output sprite filename, relative to the astro publicDir A .webp or .avif file can be used
    • cssFile: the output css filename, relative to the astro srcDir
    • preloadFile: an astro component, to be use in the head section of the html, in order to preload the sprite, not waiting for the css to be loaded. Set it to undefined not to generate this file.
    • cssMainClass: the css class that contains the property backgroud: url();
    • cssPrefix: each icon will be related to a css class, prefixed by this property, and suffixed by the icon file name
    • cssSelector: a css selector added to each icon class, such as ::before
  • verbose: verbose mode on or off

Best practices

Scss

Scss can be used to import the generated sprite css file in your scss file. Here is the astro documentation about scss.

In your main scss file, you may then add:

@import "../css/astro-sprite";

Preload

In order not to wait the css to be loaded to load the associated sprite, an astro component is created. Its name is dst.preloadFile, which equals components/SpritePreload.astro by default.

In order to preload the sprite, add in the head section this code:

---
import SpritePreload from "../components/SpritePreload.astro";
---
<head>
   ...
   <SpritePreload/>
   ...
</head>

Span

span is an easy way to add icons inlined your text, such as:

<p>
  <span class="astro-sprite astro-sprite-english"></span> the english flag
</p>

To achieve a correct result, ::before selector must be used, as with

export default defineConfig({
  integrations: [
    sprite({
      dst: {
        cssMainClass: '.astro-sprite::before',
        cssSelector: '::before',
      }
    })
  ],
});

And add in your main .css / .scss file the following:

.astro-sprite::before {
  content: "";
  display:inline-block;
  vertical-align:middle;
}

Get rid of .astro-sprite css class

Instead of using both astro-sprite astro-sprite-english to specify an icon, this is possible to specify only astro-sprite-english by using the following (cssMainClass applies to any css class containing astro-sprite-):

export default defineConfig({
  integrations: [
    sprite({
      dst: {
        cssMainClass: '[class*="astro-sprite-"]',
      }
    })
  ],
});