deno.land / x / clazzx@0.0.2 / src / clazzx.ts

نووسراو ببینە
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
type Clx = string | Array<string | boolean>;export const clx = (input: Clx) => { if (Array.isArray(input)) { return input.filter(Boolean).join(" "); } else if (typeof input === "string") { return input.trim(); } return "";};
interface StyleOptions { before?: string | Array<string | boolean>; after?: string | Array<string | boolean>; additional?: Record<string | number, Clx>;}
type StyleProps<T> = { [K in keyof Partial< Omit< Omit< Omit<Omit<Omit<Omit<T, "base">, "classes">, "compounds">, "default">, "__before" >, "__after" > >]: boolean;};

export class Style { protected base: Clx = []; protected compounds: Array<{ states: Array<string>; classes: Clx; }> = []; protected default: Clx = []; protected __before: Clx = []; protected __after: Clx = [];
constructor(options?: StyleOptions) { if (options?.before) { this.__before = options.before; } if (options?.after) { this.__after = options.after; } }
classes(input?: StyleProps<this>): string { const acc = new Map();
acc.set("base", clx(this.base)); acc.set("before", clx(this.__before));
if (input) { for (const [key, value] of Object.entries(input)) { if ( key !== "base" && key !== "default" && key !== "compounds" && key !== "classes" && key !== "__before" && key !== "__after" && value === true ) { //@ts-ignore acc.set(key, clx(this[key])); } } } // If nothing provided, use default values if (!input || Object.entries(input).length === 0) { acc.set("default", clx(this.default)); }
// handle compounds for (const [i, compound] of this.compounds.entries()) { if ( compound.states.every((state) => { //@ts-ignore if (key in this && input[key]) return true; }) ) { acc.set("compound" + i, clx(compound.classes)); } } acc.set("after", clx(this.__after)); return Array.from(acc.values()).join(" ").replace(/\B\s+|\s+\B/, ""); }}



clazzx

Version Info

Tagged at
a year ago