Skip to content

Commit 02417d8

Browse files
committed
Twiddle regex; only support me; attr-based scoping
1 parent a85531d commit 02417d8

File tree

1 file changed

+7
-8
lines changed

1 file changed

+7
-8
lines changed

script.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
// 🌘 CSS Scope Inline (https://github.com/gnat/css-scope-inline)
2-
window.cssScopeCount ??= 1 // Let extra copies share the scope count.
2+
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([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
6-
node.parentNode.classList.add(scope)
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++)
76
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!
7+
.replace(/(^|[^-\w])me(?![-\w])/g, `$1[data-css-scope='${node.parentNode.dataset.cssScope}']`)
8+
.replace(/((@keyframes\s|animation:|animation-name:)[^{};]*)\bme(?![A-Za-z])/g, `$1css-scope-${node.parentNode.dataset.cssScope}`)
9+
.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 🟢
1110
(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]) }
1211
)
13-
node.setAttribute('ready', '')
12+
node.setAttribute('scoped-to', node.parentNode.dataset.cssScope)
1413
})
1514
}).observe(document.documentElement, {childList: true, subtree: true})

0 commit comments

Comments
 (0)