Skip to content

Instantly share code, notes, and snippets.

@ikasoba
Created August 20, 2023 14:35
Show Gist options
  • Save ikasoba/68614f3bb2b49897689858b4aeaf8229 to your computer and use it in GitHub Desktop.
Save ikasoba/68614f3bb2b49897689858b4aeaf8229 to your computer and use it in GitHub Desktop.
クライアントサイドで簡易 css in js
const styleElement = document.createElement("style");
document.head.append(styleElement);
const classes = {};
let lastIds = 0n;
export const css = (src, ...values) => src.map((x, i) => x + (i < values.length ? CSS.escape("" + values[i]) : "")).join("");
export const style = (name, code) => {
const cls = classes[name] = CSS.escape(`--cssinjs-${lastIds++}`);
styleElement.innerHTML += `.${cls}{${code}}`
return cls;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment