1.1.0 ā€¢ Published 7 months ago

astro-svg-sprite v1.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
7 months ago

Latest Updates! šŸŽ‰ See the change log for details.

stro-svg-sprite

A zero-dependency Astro Integration that generates a sprite.svg from SVG files in your Astro project.

Installation

This package is compatible with Astro 2.0 and above, which support the Integrations API.

To automate the installation, use the astro add command-line tool. You can initialize it via npx, yarn, or pnpm based on your preference.

npx astro add astro-svg-sprite

Alternatively, you can manually install it by running the following command in your terminal:

npm install astro-svg-sprite

Usage

Add astro-svg-sprite to your astro.config.* file:

// astro.config.mjs
import { defineConfig } from "astro/config";
import svgSprite from "astro-svg-sprite";

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

Store the SVG files to be used for the generated sprite.svg in the src/assets/images/sprite directory.

/
ā”œā”€ā”€ astro.config.mjs
ā”œā”€ā”€ public
|   ā””ā”€ā”€ assets
|       ā””ā”€ā”€ images
|           ā””ā”€ā”€ sprite.svg
ā”œā”€ā”€ src
|   ā””ā”€ā”€ assets
|       ā””ā”€ā”€ images
|           ā””ā”€ā”€ sprite
|               ā””ā”€ā”€ 1.svg
|               ā””ā”€ā”€ 2.svg
|               ā””ā”€ā”€ *.svg
ā”œā”€ā”€ tsconfig.json
ā”œā”€ā”€ package.json

Generating the sprite.svg on build:

npm run dev

or

npm run build

The sprite.svg will be output to public/assets/images directory.

To use the generated sprite.svg file, create a Sprite component at components/Sprite.astro:

---
export interface props {
  name: string;
}

const { class:className, name } = Astro.props;
---
<svg class={className}>
  <use xlink:href=`${Astro.site}assets/images/sprite.svg#${name}`></use>
</svg>

Then call the Sprite.astro component on other pages.

---
import Sprite from 'components/Sprite.astro'
---
<Sprite name="fileName" class="customClassName"/>

Here is an example of an advanced full configuration. With the help of JSDoc, you can easily configure it.

// astro.config.mjs
import { defineConfig } from "astro/config";
import svgSprite from "astro-svg-sprite";

export default defineConfig({
  integrations: [
    svgSprite({
      mode: "verbose",
      include: [
        "./src/assets/images/sprite",
        "./src/assets/images",
        "./src/assets",
      ],
      emitFile: {
        compress: "standard",
        path: "assets/images",
      },
    }),
  ],
});

Note: emitFile.compress recommends using the default standard mode. The best mode will convert some svg tags into path tags.

Need Help or Feedback?

Submit your issues or feedback on our GitHub channel.

Changelog

See CHANGELOG.md for a history of changes to this Integration.

1.1.0

7 months ago

1.0.5

8 months ago

1.0.4

8 months ago

1.0.3

8 months ago

1.0.2

8 months ago

1.0.1

8 months ago

1.0.0

8 months ago

0.8.25

8 months ago

0.8.24

8 months ago

0.8.23

8 months ago

0.8.21-0.8.1

9 months ago

0.8.21-0.8

9 months ago

0.8.21-0.7

9 months ago

0.8.21-0.6

9 months ago

0.8.21-0.4

9 months ago

0.8.21-0.3.1

9 months ago

0.8.21-0.3

9 months ago

0.8.21-0.2

9 months ago

0.8.21-0.1

9 months ago

0.8.21-0.0.0

9 months ago

0.8.21-0.0

9 months ago

0.8.2-1.0

9 months ago

0.8.22

9 months ago

0.8.21-b1

9 months ago

0.8.21

9 months ago

0.8.20

9 months ago