-
Notifications
You must be signed in to change notification settings - Fork 0
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
5 changed files
with
202 additions
and
5 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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
(t=>{var r,i,a,d,l="",s={},u=document,b=1,n=e=>{(i=e.getAttribute?.(t))&&(d=s[i | ||
]??=b,a=t+d,e.removeAttribute(e[t]),e.setAttribute(a,""),e[t]=a,d==b)&&( | ||
l+=`[${a}]{${i}}`,b++)};new MutationObserver(e=>{e.map(e=>{ | ||
"childList"==e.type?e.addedNodes.forEach(e=>{n(e),[...e.querySelectorAll?.( | ||
`[${t}]`)||[]].map(n)}):n(e.target)}),l&&((r=u.createElement("style") | ||
).innerHTML=l,u.head.appendChild(r),l="")}).observe(u,{attributeFilter:[t], | ||
childList:!0,subtree:!0})})("x-style"); |
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 |
---|---|---|
@@ -0,0 +1,137 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<title>x-style Demo</title> | ||
<style> | ||
*, | ||
*::before, | ||
*::after { | ||
box-sizing: border-box; | ||
} | ||
|
||
* { | ||
margin: 0; | ||
} | ||
|
||
html, | ||
body { | ||
height: 100%; | ||
} | ||
|
||
body { | ||
line-height: 1.5; | ||
font-family: system-ui, "Segoe UI", Roboto, Helvetica, Arial, sans-serif, | ||
"Apple Color Emoji", "Segoe UI Emoji"; | ||
} | ||
|
||
code, | ||
kbd, | ||
samp, | ||
pre { | ||
font-family: ui-monospace, SFMono-Regular, Consolas, "Liberation Mono", | ||
Menlo, monospace; | ||
font-size: 1em; | ||
} | ||
|
||
small { | ||
font-size: 80%; | ||
} | ||
|
||
.test-class { | ||
color: red; | ||
} | ||
</style> | ||
<script src="x-style-nano.js"></script> | ||
<script> | ||
function testChange() { | ||
const el = document.getElementById("testChange"); | ||
el.setAttribute( | ||
"x-style", | ||
` | ||
color: blue; | ||
& span { | ||
color: red; | ||
} | ||
` | ||
); | ||
} | ||
function testAdd() { | ||
const el = document.createElement("li"); | ||
el.setAttribute( | ||
"x-style", | ||
` | ||
color: orange; | ||
&:hover { | ||
background-color: yellow; | ||
} | ||
& span { | ||
color: green; | ||
} | ||
` | ||
); | ||
el.innerHTML = "Test 4 <span>123</span>"; | ||
document.getElementById("testAdd").appendChild(el); | ||
} | ||
</script> | ||
</head> | ||
<body> | ||
<div | ||
x-style=" | ||
max-width: 640px; | ||
margin: 0 auto; | ||
" | ||
> | ||
<h1> | ||
<a href="https://github.com/samwillis/x-style/">x-style</a> Demo | ||
</h1> | ||
<p>This is a demo of the <code>x-style</code> attribute.</p> | ||
|
||
<p | ||
x-style=" | ||
color: red; | ||
font-size: 30px; | ||
&:hover { | ||
background-color: yellow; | ||
} | ||
& > span { | ||
color: green; | ||
&::after { | ||
display: inline; | ||
content: '!'; | ||
} | ||
} | ||
" | ||
> | ||
Hello <span>World</span> - (Hover me) | ||
</p> | ||
<p | ||
x-style=" | ||
color: red; | ||
& span { | ||
color: blue; | ||
} | ||
" | ||
> | ||
I'm red <span> and I'm blue</span> | ||
</p> | ||
|
||
<p | ||
id="testChange" | ||
x-style=" | ||
color: red; | ||
& span { | ||
color: blue; | ||
} | ||
" | ||
> | ||
I will change <span>to the other way around</span> | ||
<button onclick="testChange()">Change Styles</button> | ||
</p> | ||
<p> | ||
You can add elements with new styles | ||
<button onclick="testAdd()">Add Element</button> | ||
</p> | ||
<ul id="testAdd"></ul> | ||
</div> | ||
</body> | ||
</html> |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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 |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/** | ||
* x-style | ||
* @param {string} attr - HTML attribute that contains the css, usually "x-style" | ||
*/ | ||
((attr) => { | ||
var styleEl, | ||
style = "", | ||
processedCss = {}, | ||
doc = document, | ||
rawCss, | ||
selectorAttr, | ||
nextId = 1, | ||
id; | ||
|
||
var processEl = (el) => { | ||
rawCss = el.getAttribute?.(attr); | ||
if (rawCss) { | ||
id = processedCss[rawCss] ??= nextId; | ||
selectorAttr = attr + id; | ||
el.removeAttribute(el[attr]); | ||
el.setAttribute(selectorAttr, ""); | ||
el[attr] = selectorAttr; | ||
if (id == nextId) { | ||
style += `[${selectorAttr}]{${rawCss}}`; | ||
nextId++; | ||
} | ||
} | ||
}; | ||
|
||
new MutationObserver((mutations) => { | ||
mutations.map((mutation) => { | ||
if (mutation.type == "childList") { | ||
mutation.addedNodes.forEach((el) => { | ||
processEl(el); | ||
[...(el.querySelectorAll?.(`[${attr}]`) || [])].map(processEl); | ||
}); | ||
} else { | ||
processEl(mutation.target); | ||
} | ||
}); | ||
if (style) { | ||
styleEl = doc.createElement("style"); | ||
styleEl.innerHTML = style; | ||
doc.head.appendChild(styleEl); | ||
style = ""; | ||
} | ||
}).observe(doc, { | ||
attributeFilter: [attr], | ||
childList: true, | ||
subtree: true, | ||
}); | ||
})("x-style"); |