-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tempoary switch to this PR: cxs-css/cxs#82 which fixes media query ordering to prevent incorrect css overriding.
- Loading branch information
Showing
3 changed files
with
68 additions
and
11 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
CXS taken from this PR which fixes media queries ordering: | ||
https://github.com/cxs-css/cxs/pull/82 | ||
*/ | ||
let cache = {} | ||
let mqs = 0 | ||
let prefix = 'x' | ||
const rules = [] | ||
let insert = rule => rules.push(rule) | ||
const hyph = s => s.replace(/[A-Z]|^ms/g, '-$&').toLowerCase() | ||
const mx = (rule, media) => media ? `${media}{${rule}}` : rule | ||
const rx = (cn, prop, val) => `.${cn}{${hyph(prop)}:${val}}` | ||
const noAnd = s => s.replace(/&/g, '') | ||
|
||
const parse = (obj, child = '', media) => | ||
Object.keys(obj).map(key => { | ||
const val = obj[key] | ||
if (val === null) return '' | ||
if (typeof val === 'object') { | ||
const m2 = /^@/.test(key) ? key : null | ||
const c2 = m2 ? child : child + key | ||
return parse(val, c2, m2 || media) | ||
} | ||
const _key = key + val + child + media | ||
if (cache[_key]) return cache[_key] | ||
const className = prefix + (rules.length).toString(36) | ||
insert(mx(rx(className + noAnd(child), key, val), media), !!media) | ||
cache[_key] = className | ||
return className | ||
}) | ||
.join(' ') | ||
|
||
module.exports = (...styles) => | ||
styles.map(style => parse(style)) | ||
.join(' ').trim() | ||
|
||
module.exports.css = () => rules.sort().join('') | ||
|
||
module.exports.reset = () => { | ||
cache = {} | ||
while (rules.length) rules.pop() | ||
} | ||
|
||
module.exports.prefix = (val) => { | ||
prefix = val | ||
} | ||
|
||
if (typeof document !== 'undefined') { | ||
const sheet = document.head.appendChild( | ||
document.createElement('style') | ||
).sheet | ||
insert = (rule, media) => { | ||
rules.push(rule) | ||
if (media) mqs += 1 | ||
sheet.insertRule(rule, sheet.cssRules.length - (media ? 0 : mqs)) | ||
} | ||
} |