|
1 | 1 | // 🌘 CSS Scope Inline (https://github.com/gnat/css-scope-inline)
|
2 | 2 | window.cssScopeCount ??= 1 // Let extra copies share the scope count.
|
3 |
| -new MutationObserver(mutations => { |
4 |
| - document?.body?.querySelectorAll('style').forEach(node => { // Faster than walking MutationObserver results when recieving subtree (DOM swap, htmx, ajax, jquery). |
5 |
| - if (node.cssScopeInlineDone) return // Skip if node was processed. |
6 |
| - if (!node.parentNode) return // Skip if no parent. |
7 |
| - var scope = 'self__'+(window.cssScopeCount++) // Ready. Make unique scope, example: .self__1234 |
| 3 | +window.cssScope ??= new MutationObserver(mutations => { // Allow 1 observer. |
| 4 | + document?.body?.querySelectorAll('style:not([ready])').forEach(node => { // Faster than walking MutationObserver results when recieving subtree (DOM swap, htmx, ajax, jquery). |
| 5 | + var scope = 'me__'+(window.cssScopeCount++) // Ready. Make unique scope, example: .me__1234 |
8 | 6 | node.parentNode.classList.add(scope)
|
9 |
| - node.textContent = node.textContent.replace(/(^|\.|(?<=\s|[^a-zA-Z0-9\-\_]))(me|this|self)(?![a-zA-Z])/g, '.'+scope) // Can use: me this self |
10 |
| - node.cssScopeInlineDone = 1 |
11 |
| - // Optional. Responsive design. Mobile First (above breakpoint): 🟢 None sm md lg xl xx 🏁 Desktop First (below breakpoint): 🏁 xs- sm- md- lg- xl- None 🟢 |
12 |
| - node.textContent = node.textContent.replace(/(?:@media)\s(xs-|sm-|md-|lg-|xl-|sm|md|lg|xl|xx)/g, // *- matches must be first! |
| 7 | + node.textContent = node.textContent |
| 8 | + .replace(/(?:^|\.|(\s|[^a-zA-Z0-9\-\_]))(me|this|self)(?![a-zA-Z])/g, '$1.'+scope) // Can use: me this self |
| 9 | + .replace(/((@keyframes|animation:|animation-name:)[^{};]*)\.me__/g, '$1me__') // Optional. Removes need to escape names using \ |
| 10 | + .replace(/(?:@media)\s(xs-|sm-|md-|lg-|xl-|sm|md|lg|xl|xx)/g, // Optional. Responsive design. Mobile First (above breakpoint): 🟢 None sm md lg xl xx 🏁 Desktop First (below breakpoint): 🏁 xs- sm- md- lg- xl- None 🟢 *- matches must be first! |
13 | 11 | (match, part1) => { return '@media '+({'sm':'(min-width: 640px)','md':'(min-width: 768px)', 'lg':'(min-width: 1024px)', 'xl':'(min-width: 1280px)', 'xx':'(min-width: 1536px)', 'xs-':'(max-width: 639px)', 'sm-':'(max-width: 767px)', 'md-':'(max-width: 1023px)', 'lg-':'(max-width: 1279px)', 'xl-':'(max-width: 1535px)'}[part1]) }
|
14 | 12 | )
|
| 13 | + node.setAttribute('ready', '') |
15 | 14 | })
|
16 | 15 | }).observe(document.documentElement, {childList: true, subtree: true})
|
0 commit comments