Skip to content

Instantly share code, notes, and snippets.

@jxnblk
Created August 12, 2017 22:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jxnblk/8759924538ae3aaf6f3f081ae4e4b636 to your computer and use it in GitHub Desktop.
Save jxnblk/8759924538ae3aaf6f3f081ae4e4b636 to your computer and use it in GitHub Desktop.
const rules = []
let insert = rule => rules.push(rule)
const cxs = (css, opts = {}) => {
const {
child = '',
media
} = opts
const className = opts.className || '_' + rules.length
const rule = '.' + className + child + '{' + css + '}'
if (!media) {
insert(rule)
} else {
const mediaRule = media + '{' + rule + '}'
insert(mediaRule)
}
return {
toString: () => className,
push: (style, opts) => cxs(style, Object.assign(opts, { className }))
}
}
if (typeof document !== 'undefined') {
const sheet = document.head.appendChild(
document.createElement('style')
).sheet
insert = rule => {
rules.push(rule)
sheet.insertRule(rule, sheet.cssRules.length)
}
}
module.exports.css = () => rules.join('')
module.exports = cxs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment