Git Product home page Git Product logo

use-favicon's Introduction

useFavicon

A simple react hook to show a dynamic favicon

Getting Started

Install and run the dev server

yarn && yarn dev

Open http://localhost:3000 with your browser to see the result.

Gimme the code

Open the utils/hooks/useFavicon.ts and copy that file over and boom you have it.

How to use?

Let's say we want to show FUCKYOU letter by letter in our favicon

const Example = () => {
  const [tick, setTick] = useState(0)

  useEffect(() => {
    // Tick every 1s
    const subs = setInterval(() => {
      setTick((_tick) => _tick + 1)
    }, 1000)

    return () => {
      clearInterval(subs)
    }
  }, [])

  const drawFavicon: FaviconDrawFunc = useCallback(
    (context) => {
      const array = 'FUCKYOU.'.split('')
      const idx = tick % array.length
      console.log(idx)
      if (array[idx] === '.') {
        return `https://fav.farm/๐ŸŒš`
      }
      context.font = '180px Arial'
      context.fillStyle = '#fff'
      context.textAlign = 'center'
      context.textBaseline = 'middle'
      context.fillText(array[idx], 100, 100)

      return true
    },
    [tick]
  )

  const options = useMemo(
    () => ({
      defaultIcon: '/favicon.ico',
      autoSetIcon: true
    }),
    []
  )

  const iconURL = useFavicon(drawFavicon, options)

  return null
}

NOTE: Do remember the useCallback and useMemo because every time your draw function or options change the icon is created again

The hook returns the icon url so if you are using some library to manage head tags

Options

Option Type Default Info
defaultIcon string null Default icon url to be shown (mainly used if using SSR)
width number 200 Width of the canvas
height number 200 Height of the canvas
autoSetIcon boolean false If the hook should automatically update the favicon
linkTagSelector string link[rel="shortcut icon"] The selector to select the favicon link tag. Used if autoSetIcon is true

use-favicon's People

Contributors

patheticgeek avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.