Skip to content

Commit 6152a24

Browse files
mainawycliffemoshloop
authored andcommitted
feat: add support for color to the Icons component
Fixes #2208
1 parent 55276c7 commit 6152a24

File tree

2 files changed

+64
-20
lines changed

2 files changed

+64
-20
lines changed

src/ui/Icons/Icon.stories.tsx

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,3 +167,24 @@ export const Primary: Story = {
167167
</div>
168168
)
169169
};
170+
171+
const iconWithColors = [
172+
"error:red",
173+
"info:blue",
174+
"success:green",
175+
"error:orange",
176+
"shutdown:red"
177+
];
178+
179+
export const IconWithColor: Story = {
180+
render: () => (
181+
<div className="flex flex-row flex-wrap">
182+
{iconWithColors.map((icon) => (
183+
<div key={icon}>
184+
{icon}: <Icon iconWithColor={icon} /> &nbsp;
185+
<br />
186+
</div>
187+
))}
188+
</div>
189+
)
190+
};

src/ui/Icons/Icon.tsx

Lines changed: 43 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { IconType } from "@flanksource/icons";
22
import { IconMap as Icons } from "@flanksource/icons/mi";
33
import { isEmpty } from "lodash";
4-
import React, { useMemo } from "react";
4+
import { useMemo } from "react";
55

66
type IconMap = Record<string, string>;
77
export const aliases: IconMap = {
@@ -760,13 +760,28 @@ export const findByName = function (name?: string): IconType | undefined {
760760
return icon;
761761
};
762762

763+
const iconColorsClasssNamesMap = {
764+
error: "fill-red-500",
765+
success: "fill-green-500",
766+
healthy: "fill-green-500",
767+
unhealthy: "fill-red-500",
768+
warning: "fill-orange-500",
769+
unknown: "fill-gray-500",
770+
orange: "fill-orange-500",
771+
red: "fill-red-500",
772+
green: "fill-green-500",
773+
blue: "fill-blue-500",
774+
gray: "fill-gray-500"
775+
} satisfies Readonly<Record<string, string>>;
776+
763777
export type IconProps = {
764778
name?: string;
765779
secondary?: string;
766780
className?: string;
767781
alt?: string;
768782
prefix?: any;
769783
size?: any;
784+
iconWithColor?: string;
770785
};
771786

772787
export function Icon({
@@ -775,9 +790,20 @@ export function Icon({
775790
className = "h-6 max-w-6",
776791
alt = "",
777792
prefix,
793+
iconWithColor,
778794
...props
779795
}: IconProps) {
780796
const IconSVG = useMemo(() => {
797+
if (iconWithColor) {
798+
const [icon] = iconWithColor.split(":");
799+
if (icon) {
800+
const iconType = findByName(icon);
801+
if (iconType) {
802+
return iconType;
803+
}
804+
}
805+
}
806+
781807
if (!name && !secondary) {
782808
return undefined;
783809
}
@@ -792,7 +818,21 @@ export function Icon({
792818
}
793819
const secondaryIcon = findByName(secondary);
794820
return secondaryIcon;
795-
}, [name, secondary]);
821+
}, [iconWithColor, name, secondary]);
822+
823+
const colorClassName = useMemo(() => {
824+
if (iconWithColor) {
825+
const [_, color] = iconWithColor.split(":");
826+
if (color) {
827+
return iconColorsClasssNamesMap[
828+
color as keyof typeof iconColorsClasssNamesMap
829+
];
830+
}
831+
}
832+
return "text-gray-700";
833+
}, [iconWithColor]);
834+
835+
console.log("IconSVG", IconSVG, colorClassName);
796836

797837
if (name && (name.startsWith("http:") || name.startsWith("https://"))) {
798838
// eslint-disable-next-line @next/next/no-img-element
@@ -807,26 +847,9 @@ export function Icon({
807847
<>
808848
{prefix}{" "}
809849
<IconSVG
810-
className={`inline-block fill-current object-center text-gray-700 ${className}`}
850+
className={`inline-block fill-current object-center ${className} ${colorClassName}`}
811851
{...props}
812852
/>
813853
</>
814854
);
815855
}
816-
817-
type AvatarProps = React.ImgHTMLAttributes<HTMLImageElement> & {
818-
url: string;
819-
alt?: string;
820-
};
821-
822-
export function Avatar({ url, alt = "", ...props }: AvatarProps) {
823-
return (
824-
// eslint-disable-next-line @next/next/no-img-element
825-
<img
826-
className="flex h-10 w-10 items-center justify-center rounded-full bg-gray-400 ring-8 ring-white"
827-
src={url}
828-
alt={alt}
829-
{...props}
830-
/>
831-
);
832-
}

0 commit comments

Comments
 (0)