Skip to content

Commit

Permalink
New nano version
Browse files Browse the repository at this point in the history
  • Loading branch information
samwillis committed Dec 28, 2024
1 parent f3be241 commit ebeba32
Show file tree
Hide file tree
Showing 5 changed files with 202 additions and 5 deletions.
7 changes: 7 additions & 0 deletions dist/x-style-nano.min.js
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");
137 changes: 137 additions & 0 deletions nano.html
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>
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build-x-style": "uglifyjs -c -m -b max_line_len=80,beautify=false -o dist/x-style.min.js -- x-style.js",
"build-x-style-nano": "uglifyjs -c -m -b max_line_len=80,beautify=false -o dist/x-style-nano.min.js -- x-style-nano.js",
"build-x-style-apply": "uglifyjs -c -m -b max_line_len=80,beautify=false -o dist/x-style-apply.min.js -- x-style-apply.js",
"build-x-style-envvar": "uglifyjs -c -m -b max_line_len=80,beautify=false -o dist/x-style-envvar.min.js -- x-style-envvar.js",
"build-x-style-unnest": "uglifyjs -c -m -b max_line_len=80,beautify=false -o dist/x-style-unnest.min.js -- x-style-unnest.js",
"build-x-style-all": "mkdir build && cat x-style.js x-style-apply.js x-style-envvar.js x-style-unnest.js > build/x-style-all.js && uglifyjs -c -m -b max_line_len=80,beautify=false -o dist/x-style-all.min.js -- build/x-style-all.js && rm -rf ./build",
"build": "npm run build-x-style && npm run build-x-style-apply && npm run build-x-style-envvar && npm run build-x-style-unnest && npm run build-x-style-all"
"build": "npm run build-x-style && npm run build-x-style-nano && npm run build-x-style-apply && npm run build-x-style-envvar && npm run build-x-style-unnest && npm run build-x-style-all"
},
"author": "Sam Willis",
"license": "MIT",
Expand Down
52 changes: 52 additions & 0 deletions x-style-nano.js
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");

0 comments on commit ebeba32

Please sign in to comment.