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/dist/zum.cjs CHANGED
@@ -477,23 +477,41 @@ var _Zum = class _Zum {
477
477
  });
478
478
  }
479
479
  // ═══ AUTO-CONTRAST ═══
480
+ static _shouldSkipAutoContrast(el) {
481
+ if (!el || el.nodeType !== Node.ELEMENT_NODE || !el.matches) return true;
482
+ const skipTags = "input, textarea, select, option, button, a, img, svg, canvas, video, audio, script, style, meta, link, title, br, hr";
483
+ if (el.matches(skipTags)) return true;
484
+ if (el.hasAttribute("data-no-auto-contrast") || el.classList.contains("no-auto-contrast") || el.classList.contains("zum-keep-color")) {
485
+ return true;
486
+ }
487
+ if (el.style && el.style.color && el.dataset.contrastApplied !== "true") {
488
+ return true;
489
+ }
490
+ return false;
491
+ }
492
+ static _hasDirectText(el) {
493
+ return Array.from(el.childNodes).some(
494
+ (node) => node.nodeType === Node.TEXT_NODE && node.textContent && node.textContent.trim().length > 0
495
+ );
496
+ }
480
497
  static _initAutoContrast() {
481
498
  if (_Zum._contrastInitialized) return;
482
499
  if (!hasDOM()) return;
500
+ if (!_Zum.themeSettings.autoContrast) {
501
+ _Zum._contrastInitialized = true;
502
+ return;
503
+ }
483
504
  const styleEl = document.createElement("style");
484
505
  styleEl.setAttribute("data-Zum-contrast", "true");
485
506
  styleEl.textContent = `
486
- .zum-contrast, .zum-contrast * { transition: color 0.3s ease-in-out !important; }
487
- .zum-contrast-text { transition: color 0.3s ease-in-out !important; }
488
- @media (prefers-color-scheme: dark) { .zum-contrast-text { color: #f1f5f9 !important; } }
489
- @media (prefers-color-scheme: light) { .zum-contrast-text { color: #0f172a !important; } }
507
+ .zum-contrast-text { transition: color 0.25s ease-in-out; }
490
508
  `;
491
509
  document.head.appendChild(styleEl);
492
510
  const mutationObserver = new MutationObserver((mutations) => {
493
511
  mutations.forEach((mutation) => {
494
512
  if (mutation.addedNodes) {
495
513
  mutation.addedNodes.forEach((node) => {
496
- if (node.nodeType === Node.ELEMENT_NODE) {
514
+ if (node.nodeType === Node.ELEMENT_NODE && !_Zum._contrastProcessing.has(node)) {
497
515
  setTimeout(() => _Zum._applyContrastToElement(node), 50);
498
516
  }
499
517
  });
@@ -503,13 +521,14 @@ var _Zum = class _Zum {
503
521
  if (document.body) {
504
522
  mutationObserver.observe(document.body, { childList: true, subtree: true });
505
523
  }
524
+ const runInitial = () => setTimeout(() => _Zum._applyContrastToAll(), 100);
506
525
  if (document.readyState === "loading") {
507
- document.addEventListener("DOMContentLoaded", () => setTimeout(() => _Zum._applyContrastToAll(), 100));
526
+ document.addEventListener("DOMContentLoaded", runInitial);
508
527
  } else {
509
- setTimeout(() => _Zum._applyContrastToAll(), 100);
528
+ runInitial();
510
529
  }
511
530
  const darkModeMediaQuery = window.matchMedia("(prefers-color-scheme: dark)");
512
- const onSystemChange = () => setTimeout(() => _Zum._applyContrastToAll(), 50);
531
+ const onSystemChange = () => setTimeout(() => _Zum.refreshContrast(), 50);
513
532
  if (darkModeMediaQuery.addEventListener) {
514
533
  darkModeMediaQuery.addEventListener("change", onSystemChange);
515
534
  } else if (darkModeMediaQuery.addListener) {
@@ -519,40 +538,62 @@ var _Zum = class _Zum {
519
538
  zlog("\u2705 Auto-contrast system initialized");
520
539
  }
521
540
  static _getRealBackground(el) {
541
+ var _a;
522
542
  let current = el;
523
543
  let attempts = 0;
524
544
  while (current && attempts < 30) {
525
545
  const bg = getComputedStyle(current).backgroundColor;
526
- if (bg && bg !== "rgba(0, 0, 0, 0)" && bg !== "transparent" && bg !== "") return bg;
527
- if (current !== el && _Zum._contrastProcessing.has(current)) break;
546
+ if (bg && bg !== "rgba(0, 0, 0, 0)" && bg !== "transparent" && bg !== "") {
547
+ const match = bg.match(/rgba?\(([^)]+)\)/);
548
+ if (match) {
549
+ const parts = match[1].split(",").map((v) => parseFloat(v.trim()));
550
+ const alpha = parts.length === 4 ? parts[3] : 1;
551
+ if (alpha > 0.05) return bg;
552
+ } else {
553
+ return bg;
554
+ }
555
+ }
528
556
  if (current.parentElement) current = current.parentElement;
529
557
  else if (current.parentNode && current.parentNode.host) current = current.parentNode.host;
530
558
  else current = null;
531
559
  attempts++;
532
560
  }
533
561
  const theme = _Zum.getTheme();
534
- return theme ? theme.colors.background : "#ffffff";
562
+ return ((_a = theme == null ? void 0 : theme.colors) == null ? void 0 : _a.background) || "#ffffff";
563
+ }
564
+ static _processContrastNode(el) {
565
+ var _a, _b, _c, _d, _e, _f;
566
+ if (_Zum._shouldSkipAutoContrast(el)) return;
567
+ if (!_Zum._hasDirectText(el)) return;
568
+ if (_Zum._contrastProcessed.has(el) && el.dataset.contrastApplied === "true") return;
569
+ const bg = _Zum._getRealBackground(el);
570
+ const currentColor = getComputedStyle(el).color;
571
+ const minRatio = (_b = (_a = _Zum.themeSettings) == null ? void 0 : _a.minContrastRatio) != null ? _b : 3;
572
+ if (currentColor && el.dataset.contrastApplied !== "true") {
573
+ const existingRatio = _Zum.getContrastRatio(currentColor, bg);
574
+ if (existingRatio >= minRatio) {
575
+ _Zum._contrastProcessed.add(el);
576
+ return;
577
+ }
578
+ }
579
+ const lum = _Zum._calculateLuminance(bg);
580
+ const threshold = (_d = (_c = _Zum.themeSettings) == null ? void 0 : _c.contrastThreshold) != null ? _d : 0.5;
581
+ const theme = _Zum.getTheme();
582
+ 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";
583
+ 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";
584
+ const color = lum > threshold ? darkText : lightText;
585
+ el.style.color = color;
586
+ el.dataset.contrastColor = color;
587
+ el.dataset.contrastApplied = "true";
588
+ el.classList.add("zum-contrast-text");
589
+ _Zum._contrastProcessed.add(el);
535
590
  }
536
591
  static _applyContrastToAll() {
537
- if (!hasDOM() || !document.body) return;
538
- const skip = "input, textarea, select, img, svg, canvas, video, audio, script, style, meta, link, title, br, hr";
539
- const process2 = (el) => {
540
- if (!el || !el.matches) return;
541
- if (el.matches(skip)) return;
542
- if (!el.textContent || !el.textContent.trim()) return;
543
- if (_Zum._contrastProcessed.has(el) && el.dataset.contrastColor) return;
544
- const bg = _Zum._getRealBackground(el);
545
- const lum = _Zum._calculateLuminance(bg);
546
- const color = lum > 0.5 ? "#0f172a" : "#f1f5f9";
547
- el.style.setProperty("color", color, "important");
548
- el.dataset.contrastColor = color;
549
- el.classList.add("zum-contrast-text");
550
- _Zum._contrastProcessed.add(el);
551
- };
592
+ if (!hasDOM() || !document.body || !_Zum.themeSettings.autoContrast) return;
552
593
  const walk = (root) => {
553
594
  if (!root || !root.querySelectorAll) return;
554
595
  root.querySelectorAll("*").forEach((el) => {
555
- process2(el);
596
+ _Zum._processContrastNode(el);
556
597
  if (el.shadowRoot) walk(el.shadowRoot);
557
598
  });
558
599
  };
@@ -560,32 +601,17 @@ var _Zum = class _Zum {
560
601
  zlog("\u2705 Contrast applied to all elements");
561
602
  }
562
603
  static _applyContrastToElement(element) {
563
- if (!element || !hasDOM()) return;
564
- const skip = "input, textarea, select, img, svg, canvas, video, audio, script, style, meta, link, title, br, hr";
565
- const process2 = (el) => {
566
- if (!el || !el.matches) return;
567
- if (el.matches(skip)) return;
568
- if (!el.textContent || !el.textContent.trim()) return;
569
- if (_Zum._contrastProcessed.has(el) && el.dataset.contrastColor) return;
570
- const bg = _Zum._getRealBackground(el);
571
- const lum = _Zum._calculateLuminance(bg);
572
- const color = lum > 0.5 ? "#0f172a" : "#f1f5f9";
573
- el.style.setProperty("color", color, "important");
574
- el.dataset.contrastColor = color;
575
- el.classList.add("zum-contrast-text");
576
- _Zum._contrastProcessed.add(el);
577
- };
604
+ if (!element || !hasDOM() || !_Zum.themeSettings.autoContrast) return;
578
605
  _Zum._contrastProcessing.add(element);
579
- process2(element);
606
+ _Zum._processContrastNode(element);
580
607
  const walk = (root) => {
581
608
  if (!root || !root.querySelectorAll) return;
582
609
  root.querySelectorAll("*").forEach((el) => {
583
- process2(el);
610
+ _Zum._processContrastNode(el);
584
611
  if (el.shadowRoot) walk(el.shadowRoot);
585
612
  });
586
613
  };
587
614
  walk(element);
588
- element.classList.add("zum-contrast");
589
615
  setTimeout(() => _Zum._contrastProcessing.delete(element), 250);
590
616
  }
591
617
  static _resolveColorToRgb(color) {
@@ -647,6 +673,7 @@ var _Zum = class _Zum {
647
673
  if (!el) return;
648
674
  _Zum._contrastProcessed.delete(el);
649
675
  delete el.dataset.contrastColor;
676
+ delete el.dataset.contrastApplied;
650
677
  _Zum._applyContrastToElement(el);
651
678
  }
652
679
  static refreshContrast() {
@@ -656,6 +683,10 @@ var _Zum = class _Zum {
656
683
  root.querySelectorAll("*").forEach((el) => {
657
684
  _Zum._contrastProcessed.delete(el);
658
685
  delete el.dataset.contrastColor;
686
+ if (el.dataset.contrastApplied === "true") {
687
+ el.style.removeProperty("color");
688
+ delete el.dataset.contrastApplied;
689
+ }
659
690
  if (el.shadowRoot) reset(el.shadowRoot);
660
691
  });
661
692
  };
@@ -2396,9 +2427,6 @@ ${comp.css}`);
2396
2427
  const manualOverride = localStorage.getItem(_Zum.themeSettings.systemPreferenceKey);
2397
2428
  if (!manualOverride) {
2398
2429
  const newTheme = e.matches ? "dark" : "light";
2399
- document.querySelectorAll("*").forEach((el) => {
2400
- el._zumContrastApplied = false;
2401
- });
2402
2430
  _Zum.applyTheme(newTheme, { manual: false, smooth: true, duration: 400 });
2403
2431
  setTimeout(() => _Zum.refreshContrast(), 500);
2404
2432
  zlog(`\u{1F504} Auto-switched to ${newTheme} theme (system preference)`);
@@ -2452,10 +2480,7 @@ ${comp.css}`);
2452
2480
  _Zum._notifyThemeObservers(themeId);
2453
2481
  if (updateContrast && _Zum.themeSettings.autoContrast) {
2454
2482
  setTimeout(() => {
2455
- document.querySelectorAll("*").forEach((el) => {
2456
- el._zumContrastApplied = false;
2457
- });
2458
- _Zum._applyContrastToAll();
2483
+ _Zum.refreshContrast();
2459
2484
  }, smooth ? duration + 50 : 50);
2460
2485
  }
2461
2486
  zlog(`\u2705 Theme applied: ${themeId} (${theme.name})`);
@@ -3073,7 +3098,8 @@ __publicField(_Zum, "themeSettings", {
3073
3098
  transitionDuration: 400,
3074
3099
  transitionEasing: "ease-in-out",
3075
3100
  autoContrast: true,
3076
- contrastThreshold: 0.5
3101
+ contrastThreshold: 0.5,
3102
+ minContrastRatio: 3
3077
3103
  });
3078
3104
  __publicField(_Zum, "THEMES", {
3079
3105
  light: {
@@ -3581,9 +3607,6 @@ if (hasDOM()) {
3581
3607
  const onSystemChange = (e) => {
3582
3608
  if (localStorage.getItem(Zum.themeSettings.systemPreferenceKey)) return;
3583
3609
  const newTheme = e.matches ? "dark" : "light";
3584
- document.querySelectorAll("*").forEach((el) => {
3585
- el._zumContrastApplied = false;
3586
- });
3587
3610
  Zum.applyTheme(newTheme, { manual: false, smooth: true, duration: 400 });
3588
3611
  setTimeout(() => Zum.refreshContrast(), 500);
3589
3612
  zlog(`\u{1F504} Auto-switched to ${newTheme} (system preference)`);