@mantine/hooks
Version:
A collection of 50+ hooks for state and UI management
32 lines (31 loc) • 1.07 kB
JavaScript
"use client";
import { useIsomorphicEffect } from "../use-isomorphic-effect/use-isomorphic-effect.mjs";
import { useRef } from "react";
//#region packages/@mantine/hooks/src/use-favicon/use-favicon.ts
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) {
document.querySelectorAll("link[rel*=\"icon\"]").forEach((element) => document.head.removeChild(element));
const element = document.createElement("link");
element.rel = "shortcut icon";
link.current = element;
document.querySelector("head").appendChild(element);
}
const splittedUrl = url.split(".");
const mimeType = MIME_TYPES[splittedUrl[splittedUrl.length - 1].toLowerCase()];
if (mimeType) link.current.setAttribute("type", mimeType);
else link.current.removeAttribute("type");
link.current.setAttribute("href", url);
}, [url]);
}
//#endregion
export { useFavicon };
//# sourceMappingURL=use-favicon.mjs.map