forge
Explore
Log in

Don't you dare go Hollow

hezarfen_frontend

Clone
★ 0

0 watching0 forksTypeScript5.5 MiBdcf123aBurak Boduroğlu2 months ago674 commits

illustrations.ts74 lines, 2.3 KiBHistoryRawDownload
1/**
2 * unDraw illustrations (https://undraw.co — free for commercial use, no
3 * attribution required; see src/assets/illustrations/README.md). The SVG files
4 * stay exactly as downloaded; their fixed palette is mapped onto theme tokens
5 * here, so they follow the picked brand color and the dark theme.
6 */
7export type IllustrationName =
8 | "calendar-empty"
9 | "charts"
10 | "coming-soon"
11 | "courses"
12 | "empty"
13 | "events"
14 | "exams"
15 | "homework"
16 | "meals"
17 | "modules"
18 | "messages"
19 | "no-results"
20 | "notes"
21 | "payments"
22 | "people"
23 | "schedule"
24 | "whiteboard"
25 | "work";
26
27const loaders = import.meta.glob<string>("../assets/illustrations/*.svg", {
28 query: "?raw",
29 import: "default",
30});
31
32// unDraw's stock colors → the --illu-* tokens declared in index.css. Skin
33// tones and the few food colors are left alone.
34const PALETTE: Record<string, string> = {
35 "#6c63ff": "var(--illu-accent)",
36 "#ff6584": "var(--illu-accent-2)",
37 "#fd6584": "var(--illu-accent-2)",
38 "#3f3d56": "var(--illu-ink)",
39 "#2f2e41": "var(--illu-ink)",
40 "#090814": "var(--illu-ink)",
41 "#ccc": "var(--illu-line)",
42 "#cacaca": "var(--illu-line)",
43 "#cbcbcb": "var(--illu-line)",
44 "#d6d6e3": "var(--illu-line)",
45 "#e6e6e6": "var(--illu-soft)",
46 "#e4e4e4": "var(--illu-soft)",
47 "#e2e2e2": "var(--illu-soft)",
48 "#e6e7e8": "var(--illu-soft)",
49 "#f0f0f0": "var(--illu-soft)",
50 "#f2f2f2": "var(--illu-soft)",
51 "#fff": "var(--illu-paper)",
52 "#ffffff": "var(--illu-paper)",
53};
54
55export function recolorIllustration(svg: string): string {
56 return svg
57 .replace(/#[0-9a-f]{3,6}\b/gi, (hex) => PALETTE[hex.toLowerCase()] ?? hex)
58 // Size comes from the wrapper's CSS, not the file's pixel dimensions.
59 .replace(/<svg([^>]*?)\s(?:width|height)="[^"]*"/g, "<svg$1")
60 .replace(/<svg([^>]*?)\s(?:width|height)="[^"]*"/g, "<svg$1")
61 .replace(/<svg/, '<svg aria-hidden="true" focusable="false" preserveAspectRatio="xMidYMid meet"');
62}
63
64const cache = new Map<IllustrationName, Promise<string>>();
65
66export function loadIllustration(name: IllustrationName): Promise<string> {
67 let pending = cache.get(name);
68 if (!pending) {
69 const load = loaders[`../assets/illustrations/${name}.svg`];
70 pending = load ? load().then(recolorIllustration) : Promise.resolve("");
71 cache.set(name, pending);
72 }
73 return pending;
74}