homepage/src/components/resolvedicon.jsx

78 lines
2.1 KiB
React
Raw Normal View History

import { useContext } from "react";
import Image from "next/future/image";
import { SettingsContext } from "utils/contexts/settings";
import { ThemeContext } from "utils/contexts/theme";
const iconSetURLs = {
'mdi': "https://cdn.jsdelivr.net/npm/@mdi/svg@latest/svg/",
'si' : "https://cdn.jsdelivr.net/npm/simple-icons@latest/icons/",
};
2023-01-13 22:45:08 +01:00
export default function ResolvedIcon({ icon, width = 32, height = 32, alt = "logo" }) {
const { settings } = useContext(SettingsContext);
const { theme } = useContext(ThemeContext);
// direct or relative URLs
if (icon.startsWith("http") || icon.startsWith("/")) {
return (
<Image
src={`${icon}`}
width={width}
height={height}
style={{
width,
height,
objectFit: "contain",
2023-05-01 11:29:35 -07:00
maxHeight: "100%",
maxWidth: "100%",
}}
alt={alt}
/>
);
}
// check mdi- or si- prefixed icons
const prefix = icon.split("-")[0]
if (prefix in iconSetURLs) {
// get icon source
const iconName = icon.replace(`${prefix}-`, "").replace(".svg", "");
const iconSource = `${iconSetURLs[prefix]}${iconName}.svg`;
return (
<div
style={{
2022-12-11 10:30:04 -08:00
width,
height,
2022-11-06 14:41:02 -08:00
maxWidth: '100%',
maxHeight: '100%',
background: settings.iconStyle === "theme" ?
2023-05-01 17:58:01 -04:00
`rgb(var(--color-${ theme === "dark" ? 300 : 900 }) / var(--tw-text-opacity, 1))` :
"linear-gradient(180deg, rgb(var(--color-logo-start)), rgb(var(--color-logo-stop)))",
mask: `url(${iconSource}) no-repeat center / contain`,
WebkitMask: `url(${iconSource}) no-repeat center / contain`,
}}
/>
);
}
// fallback to dashboard-icons
const iconName = icon.replace(".png", "");
return (
<Image
src={`https://cdn.jsdelivr.net/gh/walkxcode/dashboard-icons/png/${iconName}.png`}
2022-12-11 10:30:04 -08:00
width={width}
height={height}
style={{
width,
height,
objectFit: "contain",
2023-05-01 11:29:35 -07:00
maxHeight: "100%",
maxWidth: "100%"
}}
2023-01-13 22:45:08 +01:00
alt={alt}
/>
);
2023-01-13 22:45:08 +01:00
}