All Projects → kaoDev → react-lazy-svg

kaoDev / react-lazy-svg

Licence: MIT license
An easy and bundler agnostic way to use a sprite-sheet for your svgs. With server side rendering in mind

Programming Languages

typescript
32286 projects
javascript
184084 projects - #8 most used programming language
CSS
56736 projects

Projects that are alternatives of or similar to react-lazy-svg

07-Glitch-Garden-Original
Glitch Garden demo game from the Complete Unity Developer 2D course (http://gdev.tv/cudgithub). For our students to download the end-state of projects.
Stars: ✭ 43 (-12.24%)
Mutual labels:  sprite-sheet
Godot-Plugin-Particles-Renderer
A Godot plugin to render particles into a sprite sheet
Stars: ✭ 32 (-34.69%)
Mutual labels:  sprite-sheet
Teenyicons
Tiny minimal 1px icons designed to fit in the smallest places.
Stars: ✭ 1,631 (+3228.57%)
Mutual labels:  svgs
Browser Logos
🗂 High resolution web browser logos
Stars: ✭ 5,538 (+11202.04%)
Mutual labels:  svgs
tabler-icons-svelte
A library of SVG Svelte components for Tabler Icons.
Stars: ✭ 50 (+2.04%)
Mutual labels:  svgs
isometric
A lightweight JavaScript library, written in TypeScript to create isometric projections using SVGs
Stars: ✭ 53 (+8.16%)
Mutual labels:  svgs
phosphor-figma
A flexible icon family for Figma
Stars: ✭ 17 (-65.31%)
Mutual labels:  svgs
SketchSVG
Have icons in a Sketch file but don't want to manually extract and compress them as SVGs? Let our SketchSVG tool do it!
Stars: ✭ 23 (-53.06%)
Mutual labels:  svgs

sloth emoji

react-lazy-svg

The easy way to use SVG sprite-sheets

GitHub license npm npm GitHub issues minified bundle size

react-lazy-svg is a simple way to use SVGs with the performance benefits of a sprite-sheet and svg css styling possibilities. Without bloating the bundle. It automatically creates a sprite-sheet for all used SVGs on the client but also provides a function to create a server side rendered sprite-sheet for icons used in the first paint. So you can inject the critical svg

Usage

npm install --save react-lazy-svg

Examples on how to use react-lazy-svg with remix and next.js can be found in the ./example-nextjs/ and ./example-remix/ directory.

Wrap the App with the contextProvider and provide a function to resolve SVG strings by URL. If you are using server side rendering you should also call initOnClient() to hydrate the sprite sheet context.

import { SpriteContextProvider, initOnClient. Icon } from 'react-lazy-svg';
import icon1 from './icon1.svg';

const loadSVG = async (url: string) => {
  return await (await fetch(url)).text();
};
initOnClient();

const Root = () => (
  <SpriteContextProvider loadSVG={loadSVG}>
    <Icon url={icon1} className="icon"></Icon>
    <Icon url={icon1} className="icon red"></Icon>
  </SpriteContextProvider>
);

On the server the SVG resolver function could look like this, and load the svg contents from the file system.

const svgIconFiles = new Map<string, string>();
const readFile = promisify(fs.readFile);
const cdnBase = 'http://localhost:3001/static/media/';

export const readSvg = async (url: string) => {
  if (svgIconFiles.has(url)) {
    return svgIconFiles.get(url);
  }

  if (url.startsWith(cdnBase)) {
    url = path.join(
      process.cwd(),
      url.replace(cdnBase, './build/public/static/media/'),
    );

    const svgString = await readFile(url, { encoding: 'utf8' });
    svgIconFiles.set(url, svgString);

    return svgString;
  }

  return undefined;
};
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].