|
| 1 | +(function () { |
| 2 | + const breakpoints = { sm: 640, md: 768, lg: 1024, xl: 1280, "2xl": 1536 }; |
| 3 | + |
| 4 | + function applyDynamicClasses() { |
| 5 | + const allElements = document.querySelectorAll("*"); |
| 6 | + |
| 7 | + allElements.forEach(el => { |
| 8 | + el.classList.forEach(cls => { |
| 9 | + let prefix = null; |
| 10 | + let className = cls; |
| 11 | + |
| 12 | + const matchPrefix = cls.match(/^([a-z]+):(.+)$/); |
| 13 | + if (matchPrefix) { prefix = matchPrefix[1]; className = matchPrefix[2]; } |
| 14 | + |
| 15 | + const bgMatch = className.match(/^bg-\[#([0-9A-Fa-f]{3,6})\]$/); |
| 16 | + if (bgMatch) { |
| 17 | + const color = `#${bgMatch[1]}`; |
| 18 | + if (!prefix) el.style.backgroundColor = color; |
| 19 | + else applyResponsive(el, prefix, "backgroundColor", color); |
| 20 | + } |
| 21 | + |
| 22 | + const textMatch = className.match(/^text-\[#([0-9A-Fa-f]{3,6})\]$/); |
| 23 | + if (textMatch) { |
| 24 | + const color = `#${textMatch[1]}`; |
| 25 | + if (!prefix) el.style.color = color; |
| 26 | + else applyResponsive(el, prefix, "color", color); |
| 27 | + } |
| 28 | + }); |
| 29 | + }); |
| 30 | + } |
| 31 | + |
| 32 | + function applyResponsive(el, prefix, prop, value) { |
| 33 | + const width = breakpoints[prefix]; |
| 34 | + if (!width) return; |
| 35 | + |
| 36 | + const mql = window.matchMedia(`(min-width: ${width}px)`); |
| 37 | + const applyStyle = e => { if (e.matches) el.style[prop] = value; else el.style[prop] = ""; }; |
| 38 | + |
| 39 | + mql.addEventListener("change", applyStyle); |
| 40 | + applyStyle(mql); |
| 41 | + } |
| 42 | + |
| 43 | + if (document.readyState === "loading") { |
| 44 | + document.addEventListener("DOMContentLoaded", applyDynamicClasses); |
| 45 | + } else { applyDynamicClasses(); } |
| 46 | +})(); |
0 commit comments