Skip to content

Instantly share code, notes, and snippets.

@cbdyzj
Created Jan 23, 2022
Embed
What would you like to do?
const classNameSet = new Set()
function getRandomClassName() {
const className = Math.random().toString(36).substring(2)
if (classNameSet.has(className)) {
return getRandomClassName()
}
classNameSet.add(className)
return `css-${className}`
}
function injectStyle(cssText) {
const styleRef = document.createElement('style')
styleRef.innerHTML = cssText
document.head.appendChild(styleRef)
}
export default function css(strings, ...keys) {
const className = getRandomClassName()
const cssText = strings.map((s, i) => s + (keys[i] ?? '')).join('').replace(/&/g, `.${className}`)
injectStyle(cssText)
return className
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment