-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
12 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,16 @@ | ||
// 🌘 CSS Scope Inline (https://github.com/gnat/css-scope-inline) | ||
window.cssScopeCount ??= 1 // Let extra copies share the scope count. | ||
new MutationObserver(mutations => { | ||
for (var mutation of mutations) { | ||
if (mutation.type !== "childList") continue // Skip if not mutating nodes. | ||
var nodes = [...mutation.addedNodes] // Get new nodes. | ||
for (var node = nodes.shift(); node != null; node = nodes.shift()) { // Process nodes. | ||
nodes.push(...node.childNodes) // Also process children. | ||
if (node.nodeName !== 'STYLE') continue // Skip if not a <style> | ||
if (!node.parentNode || node.parentNode?.nodeName === 'HEAD') continue // Skip if no parent. Don't style <head> | ||
if (node.textContent.includes('.self__')) continue // Skip if already processed. | ||
var scope = 'self__'+(window.cssScopeCount++) // Ready. Make unique scope, example: .self__1234 | ||
node.parentNode.classList.add(scope) | ||
node.textContent = node.textContent.replace(/\.(me|this|self)(?![A-Za-z0-9\_\-])/g, '.'+scope) // Can use: .me .this .self | ||
} | ||
} | ||
document.body.querySelectorAll('style').forEach(node => { // Faster than walking MutationObserver results when recieving subtree (DOM swap, htmx, ajax, jquery). | ||
if (node.cssScopeInlineDone) return // Skip if node was processed. | ||
if (!node.parentNode) return // Skip if no parent. | ||
var scope = 'self__'+(window.cssScopeCount++) // Ready. Make unique scope, example: .self__1234 | ||
node.parentNode.classList.add(scope) | ||
node.textContent = node.textContent.replace(/\.(me|this|self)(?![a-zA-Z])/g, '.'+scope) // Can use: .me .this .self | ||
node.cssScopeInlineDone = 1 | ||
// Optional. Responsive design. Mobile First (above breakpoint): 🟢 None sm md lg xl xx 🏁 Desktop First (below breakpoint): 🏁 xs- sm- md- lg- xl- None 🟢 | ||
node.textContent = node.textContent.replace(/(?:@media)\s(xs-|sm-|md-|lg-|xl-|sm|md|lg|xl|xx)/g, // *- matches must be first! | ||
(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]) } | ||
) | ||
}) | ||
}).observe(document.documentElement, {childList: true, subtree: true}) |