Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make css.global aware of nested object #367

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 19 additions & 13 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,20 +156,20 @@ function splitSelector(selector) {
return res
}

function selector(id, path) {
if(!id) {
function selector(class_, path) {
if(!class_) {
return path.replace(/\&/g, '')
}
if(!path) return `.css-${id},[data-css-${id}]`
if(!path) return `.${class_},[data-${class_}]`

let x = splitSelector(path)
.map(x => x.indexOf('&') >= 0 ?
[ x.replace(/\&/mg, `.css-${id}`), x.replace(/\&/mg, `[data-css-${id}]`) ].join(',') // todo - make sure each sub selector has an &
: `.css-${id}${x},[data-css-${id}]${x}`)
[ x.replace(/\&/mg, `.${class_}`), x.replace(/\&/mg, `[data-${class_}]`) ].join(',') // todo - make sure each sub selector has an &
: `.${class_}${x},[data-${class_}]${x}`)
.join(',')

if(canSimulate && /^\&\:/.exec(path) && !/\s/.exec(path)) {
x += `,.css-${id}[data-simulate-${simple(path)}],[data-css-${id}][data-simulate-${simple(path)}]`
x += `,.${class_}[data-simulate-${simple(path)}],[data-${class_}][data-simulate-${simple(path)}]`
}
return x

Expand Down Expand Up @@ -214,29 +214,33 @@ function deconstruct(style) {
return { plain, selects, medias, supports }
}

function deconstructedStyleToCSS(id, style) {
function _deconstructedStyleToCSS(class_, style) {
let css = []

// plugins here
let { plain, selects, medias, supports } = style
if(plain) {
css.push(toCSS({ style: plain, selector: selector(id) }))
css.push(toCSS({ style: plain, selector: selector(class_) }))
}
if(selects) {
Object.keys(selects).forEach(key =>
css.push(toCSS({ style: selects[key], selector: selector(id, key) })))
css.push(toCSS({ style: selects[key], selector: selector(class_, key) })))
}
if(medias) {
Object.keys(medias).forEach(key =>
css.push(`${key}{${ deconstructedStyleToCSS(id, medias[key]).join('')}}`))
css.push(`${key}{${ _deconstructedStyleToCSS(class_, medias[key]).join('')}}`))
}
if(supports) {
Object.keys(supports).forEach(key =>
css.push(`${key}{${ deconstructedStyleToCSS(id, supports[key]).join('')}}`))
css.push(`${key}{${ _deconstructedStyleToCSS(class_, supports[key]).join('')}}`))
}
return css
}

function deconstructedStyleToCSS(id, style) {
return _deconstructedStyleToCSS(id && id != '' ? `css-${id}` : null, style)
}

// this cache to track which rules have
// been inserted into the stylesheet
let inserted = styleSheet.inserted = {}
Expand Down Expand Up @@ -513,10 +517,12 @@ css.insert = (css) => {

export const insertRule = css.insert

css.global = (selector, style) => {
css.global = (class_, style, selector='') => {
style = clean(style)
if(style){
return css.insert(toCSS({ selector, style }))
let style_ = { label: [] }
build(style_, { selector, src: style }) // mutative! but worth it.
_deconstructedStyleToCSS(class_, deconstruct(style_)).forEach(rule => css.insert(rule))
}

}
Expand Down
26 changes: 26 additions & 0 deletions tests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { render, unmountComponentAtNode } from 'react-dom'
import { style, hover, nthChild, firstLetter, media, merge, compose, select, visited,
parent,
// fontFace, keyframes,
insertGlobal,
cssLabels,
simulations, simulate,
cssFor, attribsFor, idFor,
Expand Down Expand Up @@ -458,6 +459,31 @@ describe('glamor', () => {
expect(idFor(red)).toEqual('1ezp9xe')
})

it('checks global', () => {
simulations(true)
insertGlobal('test-global', {
color: 'red',
':hover': {
color: 'blue'
}
})
insertGlobal('test-global-selector', {
color: 'red',
':hover': {
color: 'blue'
}
}, 'body &')
expect(styleSheet.rules().map(x => x.cssText).join('\n')).toEqual(
`.test-global, [data-test-global] { color: red; }
.test-global:hover, [data-test-global]:hover, .test-global[data-simulate-hover], [data-test-global][data-simulate-hover] { color: blue; }
body .test-global-selector, body [data-test-global-selector] { color: red; }
body .test-global-selector:hover, body [data-test-global-selector]:hover { color: blue; }`)
render(<div className="test-global" {...simulate('hover')} />, node, () => {
simulations(false)
expect(childStyle(node).color).toEqual('rgb(0, 0, 255)')
})
})

it('checks for a cache miss', () => {
const myObscureStyle = { 'data-css-obscureclass': '"*"' }

Expand Down