-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
68 lines (63 loc) · 2.14 KB
/
index.js
File metadata and controls
68 lines (63 loc) · 2.14 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
/**
* AgnosticStyles 0.1.12
*
* AgnosticStyles is a utility function that safely applies CSS styles or class changes
* to a DOM element if it exists. Logs or warns based on customizable debug flags.
*
* Developed by Claudio González (claudio@banshee.pro)
* for Banshee Technologies - https://www.banshee.pro
*
* MIT License
* Copyright (c) 2024 Banshee Technologies. All rights reserved.
*/
let globalDebugLog = false;
let globalDebugWarn = false;
export function agnosticStyles({ debugLog = false, debugWarn = false } = {}) {
globalDebugLog = debugLog;
globalDebugWarn = debugWarn;
return function (action, value) {
return function (elementId) {
if (typeof elementId !== "string" || !elementId.trim()) {
if (globalDebugWarn) {
console.error(`AgnosticStyles: Invalid elementId "${elementId}". It must be a non-empty string.`);
}
return;
}
const element = document.getElementById(elementId);
if (element) {
if (globalDebugLog) {
console.log(`AgnosticStyles: "${elementId}" exists. Proceeding with action "${action}".`);
}
if (action === "addClass") {
if (Array.isArray(value)) {
value.forEach((cls) => element.classList.add(cls));
} else {
element.classList.add(value);
}
} else if (action === "removeClass") {
if (Array.isArray(value)) {
value.forEach((cls) => element.classList.remove(cls));
} else {
element.classList.remove(value);
}
} else if (action === "setStyle") {
Object.keys(value).forEach((style) => {
element.style[style] = value[style];
});
} else if (action === "removeStyle") {
value.forEach((style) => {
element.style.removeProperty(style);
});
} else {
if (globalDebugWarn) {
console.warn(`AgnosticStyles: Unsupported action "${action}".`);
}
}
} else {
if (globalDebugWarn) {
console.warn(`AgnosticStyles: Element with ID "${elementId}" does not exist.`);
}
}
};
};
}