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

Ensure proper insert order of nested rules & atRules #321

Merged
merged 16 commits into from
Sep 22, 2017
17 changes: 14 additions & 3 deletions packages/emotion/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,33 @@ export function flush() {
sheet.inject()
}

function insertRule(rule) {
return sheet.insert(rule)
}

let rule = ''
const atRuleQueue = new Set()

function insertionPlugin(context, content, selector, parent) {
switch (context) {
case -2:
if (rule !== '') sheet.insert(rule)
if (rule !== '') insertRule(rule)
rule = ''
atRuleQueue.forEach(insertRule)
atRuleQueue.clear()
break
case 2: {
if (rule !== '') sheet.insert(rule)
if (rule !== '') insertRule(rule)
rule = `${selector.join(',')}{${content}}`
break
}
// after an at rule block
case 3: // eslint-disable-line no-fallthrough
sheet.insert(`${selector.join(',')}{${content}}`)
if (parent.join('')) {
atRuleQueue.add(`${selector.join(',')}{${content}}`)
} else {
insertRule(`${selector.join(',')}{${content}}`)
}
rule = ''
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/emotion/src/sheet.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ styleSheet.flush()
*/

function sheetForTag(tag) {
if (tag.sheet) {
if (tag && tag.sheet) {
return tag.sheet
}

Expand Down
42 changes: 0 additions & 42 deletions packages/emotion/test/__snapshots__/css.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -200,48 +200,6 @@ exports[`css handles array of objects 1`] = `
/>
`;

exports[`css handles media query merges 1`] = `
.glamor-0 {
color: darkslateblue;
color: red;
color: purple;
}

@media (min-width:420px) {
.glamor-0 {
color: amethyst;
}
}

@media (min-width:640px) {
.glamor-0 {
color: rebeccapurple;
}
}

@media (min-width:960px) {
.glamor-0 {
color: burlywood;
}
}

@media (min-width:640px) {
.glamor-0 {
color: blue;
}
}

@media (min-width:640px) {
.glamor-0 {
color: aquamarine;
}
}

<div
className="glamor-0"
/>
`;

exports[`css handles more than 10 dynamic properties 1`] = `
.glamor-0 {
text-decoration: underline;
Expand Down
81 changes: 81 additions & 0 deletions packages/emotion/test/__snapshots__/media-query.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`css handles media query merges 1`] = `
.glamor-0 {
color: darkslateblue;
color: red;
color: purple;
}

@media (min-width:420px) {
.glamor-0 {
color: amethyst;
}
}

@media (min-width:640px) {
.glamor-0 {
color: rebeccapurple;
}
}

@media (min-width:960px) {
.glamor-0 {
color: burlywood;
}
}

@media (min-width:640px) {
.glamor-0 {
color: blue;
}
}

@media (min-width:640px) {
.glamor-0 {
color: aquamarine;
}
}

<div
className="glamor-0"
/>
`;

exports[`css handles media query merges 2`] = `
".css-yno01n {
color: darkslateblue;
color: red;
color: purple;
}

@media (min-width:420px) {
.css-yno01n {
color: amethyst;
}
}

@media (min-width:640px) {
.css-yno01n {
color: rebeccapurple;
}
}

@media (min-width:960px) {
.css-yno01n {
color: burlywood;
}
}

@media (min-width:640px) {
.css-yno01n {
color: blue;
}
}

@media (min-width:640px) {
.css-yno01n {
color: aquamarine;
}
}"
`;
40 changes: 0 additions & 40 deletions packages/emotion/test/css.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,46 +101,6 @@ describe('css', () => {
expect(tree).toMatchSnapshot()
})

test('handles media query merges', () => {
const mq = [
'@media(min-width: 420px)',
'@media(min-width: 640px)',
'@media(min-width: 960px)'
]
const buttonCSS = [
{
color: 'red',
[mq[1]]: {
color: 'blue'
}
},
{
color: 'purple',
[mq[1]]: {
color: 'aquamarine'
}
}
]

const cls1 = css([
{
color: 'darkslateblue',
[mq[0]]: {
color: 'amethyst'
},
[mq[1]]: {
color: 'rebeccapurple'
},
[mq[2]]: {
color: 'burlywood'
}
},
buttonCSS
])
const tree = renderer.create(<div className={cls1} />).toJSON()
expect(tree).toMatchSnapshot()
})

test('computed key is only dynamic', () => {
const cls1 = css({
fontSize: 10,
Expand Down
46 changes: 46 additions & 0 deletions packages/emotion/test/media-query.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import React from 'react'
import renderer from 'react-test-renderer'
import { css, sheet } from 'emotion'

describe('css', () => {
test('handles media query merges', () => {
const mq = [
'@media(min-width: 420px)',
'@media(min-width: 640px)',
'@media(min-width: 960px)'
]
const buttonCSS = [
{
color: 'red',
[mq[1]]: {
color: 'blue'
}
},
{
color: 'purple',
[mq[1]]: {
color: 'aquamarine'
}
}
]

const cls1 = css([
{
color: 'darkslateblue',
[mq[0]]: {
color: 'amethyst'
},
[mq[1]]: {
color: 'rebeccapurple'
},
[mq[2]]: {
color: 'burlywood'
}
},
buttonCSS
])
const tree = renderer.create(<div className={cls1}/>).toJSON()
expect(tree).toMatchSnapshot()
expect(sheet).toMatchSnapshot()
})
})