UNPKG

@mantine/hooks

Version:

A collection of 50+ hooks for state and UI management

36 lines (33 loc) 1.03 kB
'use client'; import { useRef } from 'react'; import { useIsomorphicEffect } from '../use-isomorphic-effect/use-isomorphic-effect.mjs'; const MIME_TYPES = { ico: "image/x-icon", png: "image/png", svg: "image/svg+xml", gif: "image/gif" }; function useFavicon(url) { const link = useRef(null); useIsomorphicEffect(() => { if (!url) { return; } if (!link.current) { const existingElements = document.querySelectorAll('link[rel*="icon"]'); existingElements.forEach((element2) => document.head.removeChild(element2)); const element = document.createElement("link"); element.rel = "shortcut icon"; link.current = element; document.querySelector("head").appendChild(element); } const splittedUrl = url.split("."); link.current.setAttribute( "type", MIME_TYPES[splittedUrl[splittedUrl.length - 1].toLowerCase()] ); link.current.setAttribute("href", url); }, [url]); } export { useFavicon }; //# sourceMappingURL=use-favicon.mjs.map