Skip to content

Commit 8049ee2

Browse files
committed
Combine child and parent css scope attribute assignments
1 parent c254501 commit 8049ee2

File tree

1 file changed

+2
-3
lines changed

1 file changed

+2
-3
lines changed

script.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
// 🌘 CSS Scope Inline (https://github.com/gnat/css-scope-inline)
22
window.cssScopeCount ??= 1 // Let other scripts read the scope count.
33
window.cssScope ??= new MutationObserver(mutations => { // Allow 1 observer.
4-
document?.body?.querySelectorAll('style:not([scoped-to])').forEach(node => { // Faster than walking MutationObserver results when recieving subtree (DOM swap, htmx, ajax, jquery).
5-
node.parentNode.dataset.cssScope ??= (window.cssScopeCount++)
4+
document?.body?.querySelectorAll('style:not([data-css-scope])').forEach(node => { // Faster than walking MutationObserver results when recieving subtree (DOM swap, htmx, ajax, jquery).
5+
node.dataset.cssScope = node.parentNode.dataset.cssScope ??= (window.cssScopeCount++)
66
node.textContent = node.textContent
77
.replace(/((@keyframes\s|animation:|animation-name:)[^{};]*)\bme(?![A-Za-z])/g, `$1css-scope-${node.parentNode.dataset.cssScope}`)
88
.replace(/(^|[^-\w])me(?![-\w])/g, `$1[data-css-scope='${node.parentNode.dataset.cssScope}']`)
99
.replace(/(?:@media)\s+(xs-|sm-|md-|lg-|xl-|sm|md|lg|xl|xx)(?![-\w])/g, // Optional responsive design shorthand for @media breakpoints. Mobile First (above breakpoint): 🟢 None sm md lg xl xx 🏁 Desktop First (below breakpoint): 🏁 xs- sm- md- lg- xl- None 🟢
1010
(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]) }
1111
)
12-
node.setAttribute('scoped-to', node.parentNode.dataset.cssScope)
1312
})
1413
}).observe(document.documentElement, {childList: true, subtree: true})

0 commit comments

Comments
 (0)