mirror of
https://github.com/Tria-plc/emaui.git
synced 2026-08-28 09:00:58 +00:00
36 lines
1.0 KiB
JavaScript
36 lines
1.0 KiB
JavaScript
'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
|