zum-framework 1.0.1 → 1.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +15 -731
- package/dist/zum.cjs +79 -56
- package/dist/zum.iife.min.js +12 -15
- package/dist/zum.mjs +79 -56
- package/package.json +1 -1
package/dist/zum.mjs
CHANGED
|
@@ -454,23 +454,41 @@ var _Zum = class _Zum {
|
|
|
454
454
|
});
|
|
455
455
|
}
|
|
456
456
|
// ═══ AUTO-CONTRAST ═══
|
|
457
|
+
static _shouldSkipAutoContrast(el) {
|
|
458
|
+
if (!el || el.nodeType !== Node.ELEMENT_NODE || !el.matches) return true;
|
|
459
|
+
const skipTags = "input, textarea, select, option, button, a, img, svg, canvas, video, audio, script, style, meta, link, title, br, hr";
|
|
460
|
+
if (el.matches(skipTags)) return true;
|
|
461
|
+
if (el.hasAttribute("data-no-auto-contrast") || el.classList.contains("no-auto-contrast") || el.classList.contains("zum-keep-color")) {
|
|
462
|
+
return true;
|
|
463
|
+
}
|
|
464
|
+
if (el.style && el.style.color && el.dataset.contrastApplied !== "true") {
|
|
465
|
+
return true;
|
|
466
|
+
}
|
|
467
|
+
return false;
|
|
468
|
+
}
|
|
469
|
+
static _hasDirectText(el) {
|
|
470
|
+
return Array.from(el.childNodes).some(
|
|
471
|
+
(node) => node.nodeType === Node.TEXT_NODE && node.textContent && node.textContent.trim().length > 0
|
|
472
|
+
);
|
|
473
|
+
}
|
|
457
474
|
static _initAutoContrast() {
|
|
458
475
|
if (_Zum._contrastInitialized) return;
|
|
459
476
|
if (!hasDOM()) return;
|
|
477
|
+
if (!_Zum.themeSettings.autoContrast) {
|
|
478
|
+
_Zum._contrastInitialized = true;
|
|
479
|
+
return;
|
|
480
|
+
}
|
|
460
481
|
const styleEl = document.createElement("style");
|
|
461
482
|
styleEl.setAttribute("data-Zum-contrast", "true");
|
|
462
483
|
styleEl.textContent = `
|
|
463
|
-
.zum-contrast
|
|
464
|
-
.zum-contrast-text { transition: color 0.3s ease-in-out !important; }
|
|
465
|
-
@media (prefers-color-scheme: dark) { .zum-contrast-text { color: #f1f5f9 !important; } }
|
|
466
|
-
@media (prefers-color-scheme: light) { .zum-contrast-text { color: #0f172a !important; } }
|
|
484
|
+
.zum-contrast-text { transition: color 0.25s ease-in-out; }
|
|
467
485
|
`;
|
|
468
486
|
document.head.appendChild(styleEl);
|
|
469
487
|
const mutationObserver = new MutationObserver((mutations) => {
|
|
470
488
|
mutations.forEach((mutation) => {
|
|
471
489
|
if (mutation.addedNodes) {
|
|
472
490
|
mutation.addedNodes.forEach((node) => {
|
|
473
|
-
if (node.nodeType === Node.ELEMENT_NODE) {
|
|
491
|
+
if (node.nodeType === Node.ELEMENT_NODE && !_Zum._contrastProcessing.has(node)) {
|
|
474
492
|
setTimeout(() => _Zum._applyContrastToElement(node), 50);
|
|
475
493
|
}
|
|
476
494
|
});
|
|
@@ -480,13 +498,14 @@ var _Zum = class _Zum {
|
|
|
480
498
|
if (document.body) {
|
|
481
499
|
mutationObserver.observe(document.body, { childList: true, subtree: true });
|
|
482
500
|
}
|
|
501
|
+
const runInitial = () => setTimeout(() => _Zum._applyContrastToAll(), 100);
|
|
483
502
|
if (document.readyState === "loading") {
|
|
484
|
-
document.addEventListener("DOMContentLoaded",
|
|
503
|
+
document.addEventListener("DOMContentLoaded", runInitial);
|
|
485
504
|
} else {
|
|
486
|
-
|
|
505
|
+
runInitial();
|
|
487
506
|
}
|
|
488
507
|
const darkModeMediaQuery = window.matchMedia("(prefers-color-scheme: dark)");
|
|
489
|
-
const onSystemChange = () => setTimeout(() => _Zum.
|
|
508
|
+
const onSystemChange = () => setTimeout(() => _Zum.refreshContrast(), 50);
|
|
490
509
|
if (darkModeMediaQuery.addEventListener) {
|
|
491
510
|
darkModeMediaQuery.addEventListener("change", onSystemChange);
|
|
492
511
|
} else if (darkModeMediaQuery.addListener) {
|
|
@@ -496,40 +515,62 @@ var _Zum = class _Zum {
|
|
|
496
515
|
zlog("\u2705 Auto-contrast system initialized");
|
|
497
516
|
}
|
|
498
517
|
static _getRealBackground(el) {
|
|
518
|
+
var _a;
|
|
499
519
|
let current = el;
|
|
500
520
|
let attempts = 0;
|
|
501
521
|
while (current && attempts < 30) {
|
|
502
522
|
const bg = getComputedStyle(current).backgroundColor;
|
|
503
|
-
if (bg && bg !== "rgba(0, 0, 0, 0)" && bg !== "transparent" && bg !== "")
|
|
504
|
-
|
|
523
|
+
if (bg && bg !== "rgba(0, 0, 0, 0)" && bg !== "transparent" && bg !== "") {
|
|
524
|
+
const match = bg.match(/rgba?\(([^)]+)\)/);
|
|
525
|
+
if (match) {
|
|
526
|
+
const parts = match[1].split(",").map((v) => parseFloat(v.trim()));
|
|
527
|
+
const alpha = parts.length === 4 ? parts[3] : 1;
|
|
528
|
+
if (alpha > 0.05) return bg;
|
|
529
|
+
} else {
|
|
530
|
+
return bg;
|
|
531
|
+
}
|
|
532
|
+
}
|
|
505
533
|
if (current.parentElement) current = current.parentElement;
|
|
506
534
|
else if (current.parentNode && current.parentNode.host) current = current.parentNode.host;
|
|
507
535
|
else current = null;
|
|
508
536
|
attempts++;
|
|
509
537
|
}
|
|
510
538
|
const theme = _Zum.getTheme();
|
|
511
|
-
return theme ? theme.colors.background
|
|
539
|
+
return ((_a = theme == null ? void 0 : theme.colors) == null ? void 0 : _a.background) || "#ffffff";
|
|
540
|
+
}
|
|
541
|
+
static _processContrastNode(el) {
|
|
542
|
+
var _a, _b, _c, _d, _e, _f;
|
|
543
|
+
if (_Zum._shouldSkipAutoContrast(el)) return;
|
|
544
|
+
if (!_Zum._hasDirectText(el)) return;
|
|
545
|
+
if (_Zum._contrastProcessed.has(el) && el.dataset.contrastApplied === "true") return;
|
|
546
|
+
const bg = _Zum._getRealBackground(el);
|
|
547
|
+
const currentColor = getComputedStyle(el).color;
|
|
548
|
+
const minRatio = (_b = (_a = _Zum.themeSettings) == null ? void 0 : _a.minContrastRatio) != null ? _b : 3;
|
|
549
|
+
if (currentColor && el.dataset.contrastApplied !== "true") {
|
|
550
|
+
const existingRatio = _Zum.getContrastRatio(currentColor, bg);
|
|
551
|
+
if (existingRatio >= minRatio) {
|
|
552
|
+
_Zum._contrastProcessed.add(el);
|
|
553
|
+
return;
|
|
554
|
+
}
|
|
555
|
+
}
|
|
556
|
+
const lum = _Zum._calculateLuminance(bg);
|
|
557
|
+
const threshold = (_d = (_c = _Zum.themeSettings) == null ? void 0 : _c.contrastThreshold) != null ? _d : 0.5;
|
|
558
|
+
const theme = _Zum.getTheme();
|
|
559
|
+
const darkText = ((_e = theme == null ? void 0 : theme.colors) == null ? void 0 : _e.text) && _Zum._calculateLuminance(theme.colors.text) < 0.3 ? theme.colors.text : "#0f172a";
|
|
560
|
+
const lightText = ((_f = theme == null ? void 0 : theme.colors) == null ? void 0 : _f.text) && _Zum._calculateLuminance(theme.colors.text) > 0.7 ? theme.colors.text : "#f1f5f9";
|
|
561
|
+
const color = lum > threshold ? darkText : lightText;
|
|
562
|
+
el.style.color = color;
|
|
563
|
+
el.dataset.contrastColor = color;
|
|
564
|
+
el.dataset.contrastApplied = "true";
|
|
565
|
+
el.classList.add("zum-contrast-text");
|
|
566
|
+
_Zum._contrastProcessed.add(el);
|
|
512
567
|
}
|
|
513
568
|
static _applyContrastToAll() {
|
|
514
|
-
if (!hasDOM() || !document.body) return;
|
|
515
|
-
const skip = "input, textarea, select, img, svg, canvas, video, audio, script, style, meta, link, title, br, hr";
|
|
516
|
-
const process2 = (el) => {
|
|
517
|
-
if (!el || !el.matches) return;
|
|
518
|
-
if (el.matches(skip)) return;
|
|
519
|
-
if (!el.textContent || !el.textContent.trim()) return;
|
|
520
|
-
if (_Zum._contrastProcessed.has(el) && el.dataset.contrastColor) return;
|
|
521
|
-
const bg = _Zum._getRealBackground(el);
|
|
522
|
-
const lum = _Zum._calculateLuminance(bg);
|
|
523
|
-
const color = lum > 0.5 ? "#0f172a" : "#f1f5f9";
|
|
524
|
-
el.style.setProperty("color", color, "important");
|
|
525
|
-
el.dataset.contrastColor = color;
|
|
526
|
-
el.classList.add("zum-contrast-text");
|
|
527
|
-
_Zum._contrastProcessed.add(el);
|
|
528
|
-
};
|
|
569
|
+
if (!hasDOM() || !document.body || !_Zum.themeSettings.autoContrast) return;
|
|
529
570
|
const walk = (root) => {
|
|
530
571
|
if (!root || !root.querySelectorAll) return;
|
|
531
572
|
root.querySelectorAll("*").forEach((el) => {
|
|
532
|
-
|
|
573
|
+
_Zum._processContrastNode(el);
|
|
533
574
|
if (el.shadowRoot) walk(el.shadowRoot);
|
|
534
575
|
});
|
|
535
576
|
};
|
|
@@ -537,32 +578,17 @@ var _Zum = class _Zum {
|
|
|
537
578
|
zlog("\u2705 Contrast applied to all elements");
|
|
538
579
|
}
|
|
539
580
|
static _applyContrastToElement(element) {
|
|
540
|
-
if (!element || !hasDOM()) return;
|
|
541
|
-
const skip = "input, textarea, select, img, svg, canvas, video, audio, script, style, meta, link, title, br, hr";
|
|
542
|
-
const process2 = (el) => {
|
|
543
|
-
if (!el || !el.matches) return;
|
|
544
|
-
if (el.matches(skip)) return;
|
|
545
|
-
if (!el.textContent || !el.textContent.trim()) return;
|
|
546
|
-
if (_Zum._contrastProcessed.has(el) && el.dataset.contrastColor) return;
|
|
547
|
-
const bg = _Zum._getRealBackground(el);
|
|
548
|
-
const lum = _Zum._calculateLuminance(bg);
|
|
549
|
-
const color = lum > 0.5 ? "#0f172a" : "#f1f5f9";
|
|
550
|
-
el.style.setProperty("color", color, "important");
|
|
551
|
-
el.dataset.contrastColor = color;
|
|
552
|
-
el.classList.add("zum-contrast-text");
|
|
553
|
-
_Zum._contrastProcessed.add(el);
|
|
554
|
-
};
|
|
581
|
+
if (!element || !hasDOM() || !_Zum.themeSettings.autoContrast) return;
|
|
555
582
|
_Zum._contrastProcessing.add(element);
|
|
556
|
-
|
|
583
|
+
_Zum._processContrastNode(element);
|
|
557
584
|
const walk = (root) => {
|
|
558
585
|
if (!root || !root.querySelectorAll) return;
|
|
559
586
|
root.querySelectorAll("*").forEach((el) => {
|
|
560
|
-
|
|
587
|
+
_Zum._processContrastNode(el);
|
|
561
588
|
if (el.shadowRoot) walk(el.shadowRoot);
|
|
562
589
|
});
|
|
563
590
|
};
|
|
564
591
|
walk(element);
|
|
565
|
-
element.classList.add("zum-contrast");
|
|
566
592
|
setTimeout(() => _Zum._contrastProcessing.delete(element), 250);
|
|
567
593
|
}
|
|
568
594
|
static _resolveColorToRgb(color) {
|
|
@@ -624,6 +650,7 @@ var _Zum = class _Zum {
|
|
|
624
650
|
if (!el) return;
|
|
625
651
|
_Zum._contrastProcessed.delete(el);
|
|
626
652
|
delete el.dataset.contrastColor;
|
|
653
|
+
delete el.dataset.contrastApplied;
|
|
627
654
|
_Zum._applyContrastToElement(el);
|
|
628
655
|
}
|
|
629
656
|
static refreshContrast() {
|
|
@@ -633,6 +660,10 @@ var _Zum = class _Zum {
|
|
|
633
660
|
root.querySelectorAll("*").forEach((el) => {
|
|
634
661
|
_Zum._contrastProcessed.delete(el);
|
|
635
662
|
delete el.dataset.contrastColor;
|
|
663
|
+
if (el.dataset.contrastApplied === "true") {
|
|
664
|
+
el.style.removeProperty("color");
|
|
665
|
+
delete el.dataset.contrastApplied;
|
|
666
|
+
}
|
|
636
667
|
if (el.shadowRoot) reset(el.shadowRoot);
|
|
637
668
|
});
|
|
638
669
|
};
|
|
@@ -2373,9 +2404,6 @@ ${comp.css}`);
|
|
|
2373
2404
|
const manualOverride = localStorage.getItem(_Zum.themeSettings.systemPreferenceKey);
|
|
2374
2405
|
if (!manualOverride) {
|
|
2375
2406
|
const newTheme = e.matches ? "dark" : "light";
|
|
2376
|
-
document.querySelectorAll("*").forEach((el) => {
|
|
2377
|
-
el._zumContrastApplied = false;
|
|
2378
|
-
});
|
|
2379
2407
|
_Zum.applyTheme(newTheme, { manual: false, smooth: true, duration: 400 });
|
|
2380
2408
|
setTimeout(() => _Zum.refreshContrast(), 500);
|
|
2381
2409
|
zlog(`\u{1F504} Auto-switched to ${newTheme} theme (system preference)`);
|
|
@@ -2429,10 +2457,7 @@ ${comp.css}`);
|
|
|
2429
2457
|
_Zum._notifyThemeObservers(themeId);
|
|
2430
2458
|
if (updateContrast && _Zum.themeSettings.autoContrast) {
|
|
2431
2459
|
setTimeout(() => {
|
|
2432
|
-
|
|
2433
|
-
el._zumContrastApplied = false;
|
|
2434
|
-
});
|
|
2435
|
-
_Zum._applyContrastToAll();
|
|
2460
|
+
_Zum.refreshContrast();
|
|
2436
2461
|
}, smooth ? duration + 50 : 50);
|
|
2437
2462
|
}
|
|
2438
2463
|
zlog(`\u2705 Theme applied: ${themeId} (${theme.name})`);
|
|
@@ -3050,7 +3075,8 @@ __publicField(_Zum, "themeSettings", {
|
|
|
3050
3075
|
transitionDuration: 400,
|
|
3051
3076
|
transitionEasing: "ease-in-out",
|
|
3052
3077
|
autoContrast: true,
|
|
3053
|
-
contrastThreshold: 0.5
|
|
3078
|
+
contrastThreshold: 0.5,
|
|
3079
|
+
minContrastRatio: 3
|
|
3054
3080
|
});
|
|
3055
3081
|
__publicField(_Zum, "THEMES", {
|
|
3056
3082
|
light: {
|
|
@@ -3558,9 +3584,6 @@ if (hasDOM()) {
|
|
|
3558
3584
|
const onSystemChange = (e) => {
|
|
3559
3585
|
if (localStorage.getItem(Zum.themeSettings.systemPreferenceKey)) return;
|
|
3560
3586
|
const newTheme = e.matches ? "dark" : "light";
|
|
3561
|
-
document.querySelectorAll("*").forEach((el) => {
|
|
3562
|
-
el._zumContrastApplied = false;
|
|
3563
|
-
});
|
|
3564
3587
|
Zum.applyTheme(newTheme, { manual: false, smooth: true, duration: 400 });
|
|
3565
3588
|
setTimeout(() => Zum.refreshContrast(), 500);
|
|
3566
3589
|
zlog(`\u{1F504} Auto-switched to ${newTheme} (system preference)`);
|