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

Allow labels to be composed #609

Merged
merged 3 commits into from
Apr 2, 2018
Merged
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
4 changes: 3 additions & 1 deletion packages/create-emotion/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ function createEmotion(
}

let name
let stylesWithLabel

const labelPattern = /label:\s*([^\s;\n{]+)\s*;/g

Expand Down Expand Up @@ -231,6 +232,7 @@ function createEmotion(
styles += strings[i + 1]
}
}, this)
stylesWithLabel = styles
styles = styles.replace(labelPattern, (match, p1: string) => {
identifierName += `-${p1}`
return ''
Expand Down Expand Up @@ -261,7 +263,7 @@ function createEmotion(
const selector = `${key}-${name}`

if (caches.registered[selector] === undefined) {
caches.registered[selector] = styles
caches.registered[selector] = stylesWithLabel
}
insert(`.${selector}`, styles)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,15 @@ Object {
justify-content: center;
}

.css-19m49uk-Main {
.css-1h80n0w-Main-hoverStyles {
color: hotpink;
display: -webkit-box;
display: -webkit-flex;
display: -ms-flexbox;
display: flex;
}

.css-19m49uk-Main:hover {
.css-1h80n0w-Main-hoverStyles:hover {
color: white;
background-color: lightgray;
border-color: aqua;
Expand All @@ -109,7 +109,7 @@ Object {
background-color: red;
}",
"html":
<main class="css-19m49uk-Main enylr5n0"
<main class="css-1h80n0w-Main-hoverStyles enylr5n0"
data-reactroot
>
<img size="30"
Expand All @@ -125,7 +125,7 @@ Object {
"n47v5l-getComponents",
"1fps0gg-bounce",
"1teldhi-getComponents",
"19m49uk-Main",
"1h80n0w-Main-hoverStyles",
"1m3qzmz-Image",
],
}
Expand Down Expand Up @@ -216,22 +216,22 @@ Object {
justify-content: center;
}

.css-19m49uk-Main {
.css-1h80n0w-Main-hoverStyles {
color: hotpink;
display: -webkit-box;
display: -webkit-flex;
display: -ms-flexbox;
display: flex;
}

.css-19m49uk-Main:hover {
.css-1h80n0w-Main-hoverStyles:hover {
color: white;
background-color: lightgray;
border-color: aqua;
box-shadow: -15px -15px 0 0 aqua,-30px -30px 0 0 cornflowerblue;
}",
"html":
<main class="css-19m49uk-Main enylr5n0"
<main class="css-1h80n0w-Main-hoverStyles enylr5n0"
data-reactroot
>
<div>
Expand All @@ -243,7 +243,7 @@ Object {
"n47v5l-getComponents",
"1fps0gg-bounce",
"1teldhi-getComponents",
"19m49uk-Main",
"1h80n0w-Main-hoverStyles",
],
}
`;
Expand Down Expand Up @@ -333,15 +333,15 @@ Object {
justify-content: center;
}

.css-19m49uk-Main {
.css-1h80n0w-Main-hoverStyles {
color: hotpink;
display: -webkit-box;
display: -webkit-flex;
display: -ms-flexbox;
display: flex;
}

.css-19m49uk-Main:hover {
.css-1h80n0w-Main-hoverStyles:hover {
color: white;
background-color: lightgray;
border-color: aqua;
Expand All @@ -357,7 +357,7 @@ Object {
background-color: red;
}",
"html":
<main class="css-19m49uk-Main enylr5n0"
<main class="css-1h80n0w-Main-hoverStyles enylr5n0"
data-reactroot
>
<img size="30"
Expand All @@ -373,7 +373,7 @@ Object {
"n47v5l-getComponents",
"1fps0gg-bounce",
"1teldhi-getComponents",
"19m49uk-Main",
"1h80n0w-Main-hoverStyles",
"1m3qzmz-Image",
],
}
Expand Down
13 changes: 12 additions & 1 deletion packages/emotion/test/auto-label/auto-label.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// @flow
import React from 'react'
import renderer from 'react-test-renderer'
import { css, flush, sheet } from 'emotion'
import { css, flush, sheet, cx } from 'emotion'

describe('meta', () => {
afterEach(() => flush())
Expand Down Expand Up @@ -106,4 +106,15 @@ describe('meta', () => {
expect(tree).toMatchSnapshot()
expect(sheet).toMatchSnapshot()
})
test('labels compose', () => {
const cls1 = css`
color: blue;
`
const cls2 = css`
color: hotpink;
`
expect(cls1).toContain('cls1')
expect(cls2).toContain('cls2')
expect(cx(cls1, cls2)).toContain('cls1-cls2')
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ exports[`styled with autoLabel composition 2`] = `
color: blue;
}

.css-1xoijq0-cssB {
.css-1mq2fmo-cssA-cssB {
color: blue;
color: red;
}

.css-1ncw0w-BlueH1-FinalH2 {
.css-1ep83l4-BlueH1-cssA-cssB-FinalH2 {
color: blue;
color: red;
color: blue;
Expand Down Expand Up @@ -72,7 +72,7 @@ exports[`styled with autoLabel composition with objects 2`] = `
}
}

.css-13djram-H1-H2 {
.css-vtmoew-H1-cssB-H2 {
color: #333;
font-size: 1.333em;
height: 64px;
Expand All @@ -81,7 +81,7 @@ exports[`styled with autoLabel composition with objects 2`] = `
}

@media only screen and (-webkit-min-device-pixel-ratio:1.5), only screen and (min--moz-device-pixel-ratio:1.5), only screen and (-o-min-device-pixel-ratio:1.5/1), only screen and (min-resolution:144dpi), only screen and (min-resolution:1.5dppx) {
.css-13djram-H1-H2 {
.css-vtmoew-H1-cssB-H2 {
font-size: 1.4323121856191332em;
}
}"
Expand All @@ -107,7 +107,7 @@ exports[`styled with autoLabel higher order component 2`] = `
background-color: #7fc8d6;
}

.css-t14qr0-Content-NewComponent {
.css-cairh6-Content-NewComponent-squirtleBlueBackground {
font-size: 20px;
background-color: #7fc8d6;
background-color: '#343a40';
Expand Down
4 changes: 2 additions & 2 deletions packages/react-emotion/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export interface CreateStyled<Props, Theme, IntrinsicProps> {
// overload for template string as styles
(
strings: TemplateStringsArray,
...vars: Array<Interpolation<ThemedProps<Props & IntrinsicProps, Theme>>>,
...vars: Array<Interpolation<ThemedProps<Props & IntrinsicProps, Theme>>>
): StyledComponent<Props, Theme, IntrinsicProps>;

// overload for object as styles
Expand All @@ -78,7 +78,7 @@ type ShorthandsFactories<Theme> = {
// overload for template string as styles
<Props = {}>(
strings: TemplateStringsArray,
...vars: Array<Interpolation<ThemedProps<Props & JSX.IntrinsicElements[Tag], Theme>>>,
...vars: Array<Interpolation<ThemedProps<Props & JSX.IntrinsicElements[Tag], Theme>>>
): StyledComponent<Props, Theme, ElementProps<Tag>>

// overload for object as styles
Expand Down