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

atomic-icon-library

v0.0.2

Published

A library that creates JSX / React components from SVGs

Downloads

4

Readme

@matthill8286/atomic-icon-library

Builds all SVGs as react components.

Overall goal of this lib

Having a 3rd party plugin which built the SVGs during bundling is very handy because and it comes with almost no effort. But it does not solve an issue with having different SVGs for each theme (or product line).

You'd need to decide on which icon to use on the component level like so:

    case 'bookmark':
      return isPrimary ? <SvgBookmarkPrimary /> : <SvgBookmarkContent />
    case 'history':
      return isContent ? <SvgHistoryContent /> : <SvgHistoryPrimary />

These are just some loose icons. But we could have a different icon set for the content theme with a number of icons. One of these is e.g. the Arrow icon which would be used over 20 times, which would be 20 implemented if else cases:

const isPrimary = useContext(ThemeContext)

<Icon>
 {isPrimary ? <SvgArrowPrimary /> : <SvgArrowContent />}
</Icon>

Now you can just do it like this:

<Icon>
 <SvgArrow/>
</Icon>

The Icon decides for itself which theme to use.

Adding SVGs

The svgs folder currently holds 3 different categories:

  • common
  • primary
  • content

If you have a styleguide icon you can put it in the primary/styleguide folder. If the icon also exists for content you can put the content one into content/styleguide. These two SVGs will be combined into a react component which is aware of the theme and decides which svg path to use during runtime.

Once you added your svg...

Please keep in mind that the file name should be in kebabe case (e.g. material-arrow.svg). Then execute yarn build:svg. It will execute the buildComponents script and generate react components from the SVGs.

The name of the react component depends on the folder and is converted to pascal case. E.g. if you added your material-arrow.svg into the folder in svgs/common/icons, it will be named IconsMaterialArrow.tsx.

Thats it you can now use your svg react component :)

Usage

You can use a named import like import { IconsMaterialArrow } from @matthill8286/atomic-icon-library.

What is under the hood

The components are prebuild with the buildComponents.ts script. It uses a queue and a worker function to convert multiple files in parallel.

Based on the folder the worker decides on how to convert the svg. common SVGs are converted with svgo and svgr. Th output is the src/components/** directory.

primary and content are combined into one component. This is done by extracting the paths and adding them into a mustache template which is basically a function that returns the svg react component. The output looks like this:

import * as React from 'react'
import getSvgComponent from '../../../src/utils/GetSvgComponent'

export default getSvgComponent({
  primaryPaths: (
    <path d="somePath" />
  ),
  danonePaths: (
    <path d="someOtherPath" />
  ),
})

which renders a component that returns jsx with the path based on the product line name:

    <svg>
      {pathProps[name]}
    </svg>