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

nuxt-svg-icon-sprite

v1.0.2

Published

Generate SVG symbol sprites for icons and illustrations.

Downloads

759

Readme

Nuxt SVG Icon Sprite

Easy and performant way to use SVG icons in your Nuxt 3 app.

Automatically creates SVG <symbol> sprites during build and provides components and composables to use symbols.

  • Aggregate all SVG files into a one or more sprite files
  • Reduce bundle size and SSR rendered page size
  • Full HMR support
  • Provides <SpriteSymbol> component to render <svg> with <use>
  • Loads the sprite.svg from URL (/_nuxt/sprite.svg)
  • Typescript type checking for available symbols

Install

npm install --save nuxt-svg-icon-sprite
export default defineNuxtConfig({
  modules: ['nuxt-svg-icon-sprite'],

  svgIconSprite: {
    sprites: {
      default: {
        importPatterns: ['./assets/icons/**/*.svg'],
      },
    },
  },
})

Usage

Place the icons in the folder defined in nuxt.config.ts, by default it's ./assets/icons. The name of the SVG files is used to determine the symbol name.

NOTE: Per sprite each symbol must have an unique name!

So, if you have a file in ./assets/icons/user.svg the sprite will contain a <symbol> with id user.

You can now use the symbol using the provided component:

<SpriteSymbol name="user" />

This will render the following markup:

<svg>
  <use xlink:href="/_nuxt/sprite.svg#user"></use>
</svg>

The symbol is referenced from the sprite via URL.

Multiple Sprites

If you have a lot of icons it might make sense to split them into separate sprites.

A typical example would be to have SVGs that appear on every page (navbar, logo, footer, etc.) in the "default" sprite and put page-specific SVGs in separate sprites.

To create an additional sprite just define a new property on the sprites config object:

export default defineNuxtConfig({
  modules: ['nuxt-svg-icon-sprite'],

  svgIconSprite: {
    sprites: {
      default: {
        importPatterns: ['./assets/icons/**/*.svg'],
      },
      dashboard: {
        importPatterns: ['./assets/icons-dashboard/**/*.svg'],
      },
    },
  },
})

Assuming you have this file ~/assets/icons-dashboard/billing.svg, you can reference it like this:

<SpriteSymbol name="dashboard/billing" />

The symbol name is prefixed by the name of the sprite (e.g. the key used in the sprites config). The default sprite is always unprefixed.

<SpriteSymbol> component

In addition to the name prop you can also optionally pass the noWrapper prop to only render the <use> tag:

<SpriteSymbol name="dashboard/billing" :no-wrapper="true" />

This will render the following markup:

<use xlink:href="/_nuxt/sprite-dashboard.svg#billing"></use>

This is useful if you want to render multiple symbols in one <svg> tag.

You may also use the inline prop on the <SpriteSymbol /> component to render the SVG content directly instead of rendering the <use> tag.

useSpriteData() composable

Get information about the generated sprites and their symbols during runtime.

Useful if you want to render a list of all symbols in a styleguide:

<template>
  <SpriteSymbol v-for="symbol in symbols" :key="symbol" :name="symbol" />
</template>

<script setup lang="ts">
const { symbols } = useSpriteData()
</script>

Full Module Options

import { optimize } from 'svgo'

export default defineNuxtConfig({
  modules: ['nuxt-svg-icon-sprite'],

  svgIconSprite: {
    sprites: {
      default: {
        importPatterns: ['./assets/icons/**/*.svg'],

        // Directly provide symbol SVG by path.
        // These are added after the auto imports defined in the
        // `importPatterns`.
        symbolFiles: {
          email: '~/node_modules/some_package/assets/icons/email.svg'
        },

        processSvg(markup: string, filePath: string) {
          // Executed for each loaded <svg> file.
          // Do something with the markup, e.g. execute SVGO or do some string
          // replacements.
          return optimize(markup).data
        },
        processSymbol(symbol, filePath) {
          // Add attributes to the parsed symbol.
          symbol.attributes.width = '24'
          symbol.attributes.height = '24'
          symbol.attributes.viewBox = '0 0 24 24'

          // Afterwards the parsed symbol is converted to markup.
          return symbol
        },
        processSprite(markup, name) {
          // Executed for each sprite right before its saved.
          // Run SVGO or whatever you like.
          // Markup contains:
          // <svg>
          //   <symbol id="user">...</symbol>
          //   <symbol id="foobar">...</symbol>
          // </svg>
          return markup
        }
      },
    },
  },
})

The options are the same for each key in sprites.

TODO

  • Provide more information about generated sprite via composable
  • Provide option to inline sprite in SSR
  • Option to directly provide symbols as markup
  • Tests