-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathicon.ts
More file actions
340 lines (299 loc) · 8.16 KB
/
Copy pathicon.ts
File metadata and controls
340 lines (299 loc) · 8.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
import { is } from "../shared/is";
import { IconAspect } from "../shared/types";
import { THEME } from "../variables";
import { Anchor } from "./anchor";
const iconColors = [
"blue",
"rose",
"green",
"red",
"yellow",
"cyan",
"pink",
"orange",
"sky",
"slate",
"emerald",
"zinc",
"neutral",
"amber",
"violet",
];
const iconLevel = 300;
const getIconColor = (index: number) => `bg-${iconColors[iconColors.length % index]}-${iconLevel}`;
interface IProps {
name: string;
index: number;
icon?: string;
iconLight?: string;
iconDark?: string;
iconColor?: string;
iconBG?: string;
iconBubble?: boolean;
iconBubblePadding?: boolean;
iconAspect?: IconAspect;
uri?: string;
newWindow?: boolean;
categoryBubblePadding?: boolean;
standalone?: boolean; // New prop to indicate if icon should create its own anchor
}
// Theme detection utility
function getCurrentTheme(): "light" | "dark" | "auto" {
return THEME as "light" | "dark" | "auto";
}
// Icon selection logic
function selectIcon(icon?: string, iconLight?: string, iconDark?: string): string | null {
const theme = getCurrentTheme();
switch (theme) {
case "light":
return iconLight || icon || null;
case "dark":
return iconDark || icon || null;
case "auto":
default:
// For auto theme, we'll handle this in the main function
return icon || null;
}
}
export const Icon = function (props: IProps): string {
const {
name,
uri,
icon,
iconLight,
iconDark,
index,
iconBG,
iconBubble,
iconBubblePadding,
iconColor,
iconAspect,
newWindow,
categoryBubblePadding,
standalone = false,
} = props;
const theme = getCurrentTheme();
let iconContent = "";
// Handle auto theme with dual rendering
if (theme === "auto" && (iconLight || iconDark)) {
const lightIcon = iconLight || icon;
const darkIcon = iconDark || icon;
let bubblePadding = categoryBubblePadding || false;
if (iconBubblePadding === true) {
bubblePadding = true;
} else if (iconBubblePadding === false) {
bubblePadding = false;
}
let result = "";
// Light theme icon
if (lightIcon) {
const lightIconHtml = IconBase({
icon: lightIcon,
iconBG,
iconColor,
iconBubble,
iconBubblePadding: bubblePadding,
iconAspect,
});
result += `<span class="block dark:hidden">${lightIconHtml}</span>`;
}
// Dark theme icon
if (darkIcon) {
const darkIconHtml = IconBase({
icon: darkIcon,
iconBG,
iconColor,
iconBubble,
iconBubblePadding: bubblePadding,
iconAspect,
});
result += `<span class="hidden dark:block">${darkIconHtml}</span>`;
}
iconContent = result || IconBlank({ index });
} else {
// Handle light/dark theme or fallback for auto theme
const selectedIcon = selectIcon(icon, iconLight, iconDark);
if (is.null(selectedIcon)) {
iconContent = IconBlank({ index });
} else {
let bubblePadding = categoryBubblePadding || false;
if (iconBubblePadding === true) {
bubblePadding = true;
} else if (iconBubblePadding === false) {
bubblePadding = false;
}
iconContent = IconBase({
icon: selectedIcon as string,
iconBG,
iconColor,
iconBubble,
iconBubblePadding: bubblePadding,
iconAspect,
});
}
}
// Only wrap in Anchor component if uri is provided AND standalone is true
// This prevents nested anchors when the icon is used within services components
if (!is.null(uri) && standalone) {
return Anchor({
uri: uri as string,
title: name,
newWindow,
children: iconContent,
});
}
return iconContent;
};
interface IIconBlankProps {
index: number;
}
function IconBlank(props: IIconBlankProps) {
const { index } = props;
return `
<span
class="${`block w-16 h-16 rounded-2xl border border-black/5 shadow-sm ${getIconColor(index)} overflow-hidden`}"
/>
`;
}
enum IconType {
uri,
material,
dashboard,
selfhst,
}
interface IIconBaseProps {
icon: string;
iconColor?: string;
iconBG?: string;
iconBubble?: boolean;
iconBubblePadding?: boolean;
iconAspect?: IconAspect;
}
function IconBase(props: IIconBaseProps) {
let { icon, iconBG, iconBubble, iconBubblePadding, iconColor, iconAspect = "square" } = props;
let iconType: IconType = IconType.uri;
if (icon.startsWith("http") || icon.startsWith("/")) {
iconType = IconType.uri;
} else if (icon.startsWith("mdi-")) {
iconType = IconType.material;
} else if (icon.startsWith("selfhst-")) {
iconType = IconType.selfhst;
} else {
iconType = IconType.dashboard;
}
// Everyone starts the same size
let bubbleClassName = "flex items-center justify-center overflow-hidden bg-contain";
let iconWrapperWidthHeightClassName = "";
let iconItselfWidthHeightClassName = "";
switch (iconAspect) {
case "width":
iconItselfWidthHeightClassName = "w-16";
iconWrapperWidthHeightClassName += " w-16";
if (iconBubblePadding) {
iconItselfWidthHeightClassName = "w-14";
}
break;
case "height":
iconItselfWidthHeightClassName = "h-16";
iconWrapperWidthHeightClassName += " h-16";
if (iconBubblePadding) {
iconItselfWidthHeightClassName = "h-14";
}
break;
case "square":
default:
iconItselfWidthHeightClassName = "w-16 h-16 object-contain";
iconWrapperWidthHeightClassName += " w-16 h-16";
if (iconBubblePadding) {
iconItselfWidthHeightClassName = "w-14 h-14 object-contain";
}
break;
}
bubbleClassName += iconWrapperWidthHeightClassName;
if (is.null(iconBubble) || iconBubble !== false) {
bubbleClassName += " rounded-2xl border border-black/5 shadow-sm";
}
const bubbleStyle: string[] = [];
switch (iconType) {
case IconType.uri:
case IconType.dashboard:
case IconType.selfhst:
// Default to bubble and no background for URI, Dashboard and selfhst icons
if (!is.null(iconBG)) {
if (iconBG?.startsWith("#")) {
bubbleStyle.push(`background-color: ${iconBG}`);
} else {
bubbleClassName += ` bg-${iconBG}`;
}
}
break;
case IconType.material:
// Material icons get a color and a background by default, then an optional bubble
if (is.null(iconBG)) {
bubbleClassName += ` bg-slate-200 dark:bg-gray-900`;
} else {
if (iconBG?.startsWith("#")) {
bubbleStyle.push(`background-color: ${iconBG}`);
} else {
bubbleClassName += ` bg-${iconBG}`;
}
}
if (is.null(iconColor)) {
iconColor = "black dark:bg-white";
}
break;
}
const mdiIconStyle: string[] = [];
let mdiIconColorFull = "bg-" + iconColor;
if (!is.null(iconColor) && iconColor?.startsWith("#")) {
mdiIconColorFull = "";
mdiIconStyle.push(`background-color: ${iconColor}`);
}
switch (iconType) {
case IconType.uri:
return `<span class="${bubbleClassName}" style="${unwrapStyles(
bubbleStyle
)}"><img src="${icon}" alt="" class="${iconItselfWidthHeightClassName || ""}" /></span>`;
case IconType.dashboard:
icon = icon.replace(".png", "").replace(".jpg", "").replace(".svg", "");
return `<span class="${bubbleClassName}" style="${unwrapStyles(bubbleStyle)}">
<img
src=${`https://cdn.jsdelivr.net/gh/walkxcode/dashboard-icons/png/${icon}.png`}
alt=""
class="${iconItselfWidthHeightClassName || ""}"
/>
</span>`;
case IconType.selfhst:
icon = icon.replace("selfhst-", "").replace(".png", "").replace(".svg", "");
return `<span class="${bubbleClassName}" style="${unwrapStyles(bubbleStyle)}">
<img
src=${`https://cdn.jsdelivr.net/gh/selfhst/icons/png/${icon}.png`}
alt=""
class="${iconItselfWidthHeightClassName || ""}"
/>
</span>`;
case IconType.material:
icon = icon.replace("mdi-", "").replace(".svg", "");
return `
<span class="${bubbleClassName}" style="${unwrapStyles(bubbleStyle)}">
<span>
<span
class="flex items-center justify-center ${iconItselfWidthHeightClassName} ${mdiIconColorFull} overflow-hidden"
style="${unwrapStyles(
mdiIconStyle.concat([
`mask: url(https://cdn.jsdelivr.net/npm/@mdi/svg@latest/svg/${icon}.svg) no-repeat center / contain`,
`webkit-mask: url(https://cdn.jsdelivr.net/npm/@mdi/svg@latest/svg/${icon}.svg) no-repeat center / contain`,
])
)}"
></span>
</span>
</span>
`;
}
}
function unwrapStyles(styles: string[]): string {
if (is.null(styles)) {
return "";
}
return styles.join(";");
}