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

use-favicon

v1.0.1

Published

A React hook to add dynamic favicons to your application

Downloads

17

Readme

use-favicon

A React hook to update the favicon of your app. Useful to quickly add a favicon to a side project, or to dynamically change the favicon based on the state of your application.

This library makes use of SVG icons, which are now supported in most modern browsers (check out the caniuse page).

Features

  • Favicon can be either a regular image icon, an emoji, a color palette, or a gradient
  • Dynamic favicon based on tab focus
  • Dynamic favicon based on light or dark mode
  • A notification badge can be triggered on the icon

Getting started

npm install use-favicon
import useFavicon from 'use-favicon';

// in your functional React component
useFavicon();

// 👆 without a config object this will set your favicon to a random emoji

API

useFavicon takes an options object with the following fields:

| Field | Type | Default | Description | | ------------ | ------------------------------------------------------------------------- | ------------ | --------------------------------------------------- | | type | 'icon' | 'emoji' | 'colors' | 'gradient' | emoji | What kind of favicon to use | | value | string (icon and emoji)string | string[] (colors and gradient) | random emoji | Value of the selected favicon | | awayVariant | FaviconOptions (this table) | undefined | Favicon to use when tab is unfocused | | darkVariant | FaviconOptions (in this table) | undefined | Favicon to use when user client is set to dark mode | | notification | NotificationOptions (see below) | See below | Options to use if favicon notification triggered |

If notification options are provided, it should look like the following:

| Field | Type | Default | Description | | -------- | ------------------------------- | ---------------- | --------------------------------------------- | | position | 'top left'...'bottom right' | 'bottom right' | Where the notification badge should be placed | | color | string | '#fb464c' | Color of the notification badge |

Examples

Emoji

import useFavicon from 'use-favicon';

export default function App() {
  useFavicon({
    type: 'emoji',
    value: '👾',
  });

  return <div>Example app!</div>;
}

Color gradient

import useFavicon from 'use-favicon';

export default function App() {
  useFavicon({
    type: 'gradient',
    value: ['#ff00ff', '#0000ff'], // purple to blue
    direction: '45deg',
  });

  return <div>Example app!</div>;
}

Away and dark variant

import useFavicon from 'use-favicon';

export default function App() {
  useFavicon({
    // default favicon
    type: 'emoji',
    value: '🌞',
    // favicon to use when user has dark mode
    darkVariant: {
      type: 'emoji',
      value: '🌝',
    },
    // favicon to use when tab is unfocused
    awayVariant: {
      type: 'emoji',
      value: '👽',
    },
  });

  return <div>Example app!</div>;
}

Favicon notifications

import useFavicon from 'use-favicon';

export default function App() {
  const { setFaviconNotification } = useFavicon({
    type: 'emoji',
    value: '🧠',
    notification: {
      position: 'bottom right',
      color: '#fb464c',
    },
  });

  return (
    <div>
      <button onClick={() => setFaviconNotification(true)}>
        Notification on
      </button>
      <button onClick={() => setFaviconNotification(false)}>
        Notification off
      </button>
    </div>
  );
}

withFavicon HOC

use-favicon also has a Higher Order Component (HOC) export. This can be useful if you only want it to run under certain conditions.

import { withFavicon } from 'use-favicon';

function App() {
  return <div>Your app here</div>;
}

const isDev = process.env.NODE_ENV === 'development';
export default isDev ? withFavicon(App, { type: 'emoji', value: '🧪' }) : App;