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

react-svg-favicon

v1.0.0

Published

A React component that renders SVG to the site's favicon

Downloads

212

Readme

react-svg-favicon

react-svg-favicon is a simple package for using dynamically generated "favicons" in your React app with the power of SVG. You just supply the SVG as regular React components and react-svg-favicon handles the rest.

Installation

npm install react-svg-favicon

Example usage

import { useState } from "react";
import SVGIcon from "react-svg-favicon";

export function App() {
  var [notifications, setNotifications] = useState(0);

  return (
    <>
      <button onClick={() => setNotifications((n) => n + 1)}>
        Add notification
      </button>

      <SVGIcon>
        <svg viewBox="0 0 100 100">
          <circle cx={50} cy={50} r={45} fill="orange" />
          <path
            d="M25 65 L40 50 L25 35 L35 25 L50 40 L65 25 L75 35 L60 50 L75 65 L65 75 L50 60 L35 75 Z"
            fill="black"
          />

          {notifications > 0 && (
            <>
              <circle cx={70} cy={70} r={30} fill="red" />
              <text
                x={70}
                y={85}
                fontSize={50}
                fontFamily="monospace"
                textAnchor="middle"
              >
                {notifications}
              </text>
            </>
          )}
        </svg>
      </SVGIcon>
    </>
  );
}

Caveats

In some browsers, the custom icon won't override static icons specified in the html template.

The SVGIcon component adds a new link[rel=icon] element at the very end of the document head. It also has a sizes="any" attribute by default.

The browser should always select the last icon specified, unless it considers another icon better based on the type, sizes, and media attributes.

Since "any" should match every size possible, the new icon should always win, which it does in Firefox. Chromium, however, doesn't follow the specification quite correctly.

To make it work in Chrome, you can either specify the same one numerical size or any for both the dynamic and static icon, like this:

<link rel="icon" type="image/x-icon" sizes="32x32" href="favicon.ico" />
<SVGIcon sizes="32x32">{/* Your code */}</SVGIcon>

or like this: ("any" is the default)

<link rel="icon" type="image/x-icon" sizes="any" href="favicon.ico" />
<SVGIcon>{/* Your code */}</SVGIcon>

Alternatively, you can remove all old icons uppon page load by adding this script to index.js:

for (var icon of document.querySelectorAll("link[rel=icon]")) {
  icon.parentElement.removeChild(icon);
}

This method is pretty much guaranteed to work.

API

The package provides a single export:

SVGIcon

This component renders its children to a link[rel=icon] element it appends to the document head.

In addition to the children property, it also accepts the type, sizes, and media attributes, which are passed to the link element.

Example with all attributes:

<SVGIcon sizes="32x32" type="image/svg+xml" media="print">
  <svg viewBox="0 0 100 100">
    <circle cx={50} cy={50} r={50} fill="red" />
  </svg>
</SVGIcon>