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

@handy-ones/handy-svg

v1.0.2

Published

The most handy way to use <svg> on the web

Downloads

41

Readme

External SVGs that you can style

lint

How it works

  1. Fetches your SVGs in browser with fetch request. And caches of course.
  2. Puts it into sprite that is stored in your body.
  3. Provides you with the React component and standalone API to use this icon in your code.
  4. That's it. You now may use all the fancy css-styling like if it was inline SVG.

Why

  1. Sprites may become huge, containing hundreds of icons, and you don't need all of your icons on every page.
  2. Styling with css is a must-have, and this is the only way of getting it except for inlining.
  3. Inlining SVGs with React might be painful (there is a tool though), it also increases the bundle size a nd just doesn't feel right.

Usage

Install it from npm

npm i handy-svg

React


import {HandySvg} from 'handy-svg';
import iconSrc from './icon.svg';

export const Icon = () => (
    <HandySvg
        src={iconSrc}
        width="32"
        height="32"
    />
);

I assume here that you use file-loader for bundling your svg-files and get public url to SVG file in iconSrc, like so:

module.exports = {
    module: {
        rules: [
            {
                test: /\.(png|jpe?g|gif|svg)$/i,
                loader: 'file-loader',
                options: {
                    name: '[path][name].[ext]'
                }
            }
        ]
    }
};

But in fact there is no difference for the handy-svg where you get your SVGs urls from.

Standalone

You may also use handy-svg without React at all.

import {injector} from 'handy-svg/lib/injector';

const src = "https://cdn-server.net/icon.svg";

// Fetches svg content and puts it to sprite
injector.load(src);

// Gets the id of your svg in sprite
const id = injector.getId(src);

// Then you can use it at your will
const svg = `<svg><use href="#${id}" /></svg>`;

API

<HandySvg />

import {HandySvg} from 'handy-svg';

type HandySvgProps = {
  src: string; // your icon url
  loadTimeot?: number; // load timeout. 4800 by default
  loadRetryCount?: number; // load retry. 2 by default
  [key: string]: string | number | undefined; // any extra props will be passed to svg tag
}

<HandySvg {...props} />

injector.load()

import {injector} from 'handy-svg/lib/injector';

type LoadOptions = {
    flushImmediate?: boolean; // inject icon to the body without debouncing
    timeout?: number; // load timeout. 4800 by default
    retryCount?: number; // load retry. 2 by default
}

injector.load(src: string, options: LoadOptions): Promise<void>;

injector.getId()

import {injector} from 'handy-svg/lib/injector';

injector.getId(src: string): string;

CSP

You should add hostnames of your icons to the connect-src directive of your Content-Security-Policy header for this to work properly.

License

MIT