-
Notifications
You must be signed in to change notification settings - Fork 1
/
css-inherit.global.js
51 lines (47 loc) · 1.3 KB
/
css-inherit.global.js
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
/**
* css-inherit-fn
* BSD 2-Clause License
* Copyright (c) Jane Ori, PropJockey, 2022
*/
const cssInherit = (() => {
const buildDepthSelector = (evenOdd, sel, maxDepth) => {
let full = ""
let selX = sel
let i = 1
if (evenOdd === "even") {
selX = `${selX} ${sel}`
i = 2
}
while (i <= maxDepth) {
full += `${selX}:not(${selX} ${sel}), `
selX += ` ${sel} ${sel}`
i += 2
}
return `:where(${full.substring(0, full.length - 2)})`
}
const inheritchainswap = (ioA, ioB, assignments) => {
let output = ""
const arglen = assignments.length
let i = 0
while (i < arglen) {
const assignment = assignments[i]
const varName = assignment.replace(/([^:]+):.*/, "$1")
const val = assignment.replace(/[^:]+:\s*(.*)/, "$1").replace(/inherit\(--/g, `var(--${ioA}`)
output += `\n ${varName}: ${val};`
output += `\n ${varName.replace("--", "--" + ioB)}: var(${varName});`
i++
}
return output
}
const buildInherit = (sel, maxDepth, ...assignments) => `\
${buildDepthSelector("odd", sel, maxDepth)} {\
${inheritchainswap("io1_", "io2_", assignments)}
}
${buildDepthSelector("even", sel, maxDepth)} {\
${inheritchainswap("io2_", "io1_", assignments)}
}`
return {
buildDepthSelector,
buildInherit
}
})()