Git Product home page Git Product logo

svg-packer's Introduction

@Al00X/SVG-Packer

๐Ÿ—ƒ๏ธ Pack all your svg files into a single SVG file! (command-line tool)


So What's The Point Of This? ๐Ÿงโ”

Basically, the problem came up when we wanted to use svg icons in an SSR website, and cache them; well we could have used a caching service and logics to handle things but that would take a lot of time to write a decent code; which consist of handling multiple requests or nasty race conditions that occurred.

Or in React (and other frameworks using jsx) we tend to generate a jsx file for each svg and customize them.

What if we want to add a size property to all the icons to controls the width and height simultaneously. or when we want to add a disabled and onClick event handlers.


The Solution Is Here! โœจ

using this package you can combine all of your svg files into a singular file, each icon is inside a symbol, which then can be used by its id, and is accessed easily from anywhere with Benefits:

  • Just one fetch request to get all the icons
  • It's cached on the browser
  • A single component to rule all the svg properties
  • Type safe! icon names are saved into a d.ts file
  • washes the dishes for you
  • it's nice... really.

Getting Started ๐Ÿš€

Install it using your preferred package manager:

npm i @al00x/svg-packer

Now you can call it from your terminal:

npx svg-packer

gather some SVGs and you are ready to go!


How To Use It? ๐Ÿ”ฆ

The simplest way to generate the Packed SVG is to give an input & output to start the process:

npx svg-packer -i ./folder/to/svg/icons -o ./folder/to/output

After running this command you will get two files in the output destination:

  • Packed-SVG.svg contains all the svgs you pointed to.
  • Packed-SVG-types.d.ts contains all the icon names for type safety (extracted from the icons filename).

There's also some options to customize the process; use the command below to see the detailed usage farther more:

npx svg-packer --help

You are all set to use these files in anyway you like to; The samples below are made for each framework to gives you a quick start!

Samples ๐Ÿ“”


Angular

import { ChangeDetectionStrategy, Component, Input } from '@angular/core';
import { IconNameType } from './svg-packed-names';

@Component({
  selector: 'icon',
  template: `
    <div
      class="icon-wrapper"
    >
      <svg style="fill: currentColor !important; color: inherit" [style.width]="size" [style.height]="size">
        <use [attr.href]="'/assets/svg-packed.svg#' + name" />
      </svg>
    </div>
  `,
  styles: ``,
  standalone: true,
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class IconComponent {
  @Input({ required: true }) name!: IconNameType;
  @Input() size?: string = '1.5rem';
}

Qwik

import iconsSprite from './svg-packed.svg';
import type { IconName } from './svg-packed-names';
import { component$ } from '@builder.io/qwik';

export default component$(
  ({
    name,
    size = '1.5rem',
    ...props
  }: {
    name: IconName;
    size?: string;
    class?: string;
  }) => {
    return (
      <svg {...props} class={`${props.class ?? ''}`} style={{ width: size, height: size }}>
        <use href={`${iconsSprite}#${name}`} />
      </svg>
    );
  },
);

Issues & Contributes are welcomed! ๐Ÿ’ž

If you find a bug, got a fix, have an idea, made a new feature, or ... feel free to send it right away!

svg-packer's People

Contributors

al00x avatar

Watchers

 avatar imMohika 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.