Created
August 20, 2023 14:35
-
-
Save ikasoba/68614f3bb2b49897689858b4aeaf8229 to your computer and use it in GitHub Desktop.
クライアントサイドで簡易 css in js
This file contains hidden or 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
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