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

Emotion Primitives #658

Merged
merged 102 commits into from
Jul 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
102 commits
Select commit Hold shift + click to select a range
ec93579
Init commit
nitin42 May 14, 2018
3404e55
remove unused dependencies
nitin42 May 16, 2018
f989ea1
added new tests
nitin42 May 16, 2018
a886b94
refactored core
nitin42 May 16, 2018
775917e
prettir 🎉
nitin42 May 16, 2018
a06d8aa
use getPrimitive(primitive) in withComponent
nitin42 May 16, 2018
281b64e
Tweaked emotion-primitives package.json & rollup build
Andarist May 17, 2018
231e581
refactored convertStyles to not to use filter and map
nitin42 May 17, 2018
4694063
remove unneeded options from emotion
nitin42 May 17, 2018
97f5ac6
guard Proxy usage
nitin42 May 17, 2018
8772f90
updated tests
nitin42 May 17, 2018
2b49c02
Merge branch 'emotion-primitives' of https://github.com/nitin42/emoti…
nitin42 May 17, 2018
74492fd
added test for theme
nitin42 May 17, 2018
879e5eb
added emotion-theming and react-primitives to dev depend.
nitin42 May 17, 2018
7a0bdc8
updated snapshots
nitin42 May 17, 2018
21ce603
Tweaked externals list in rollup.config.js
Andarist May 18, 2018
608134b
don't forward ref to stateless comp.
nitin42 May 18, 2018
3059db8
added tests for innerRef and style composition
nitin42 May 18, 2018
9018019
Merge branch 'emotion-primitives' of https://github.com/nitin42/emoti…
nitin42 May 18, 2018
01f25cf
added tests for createRef()
nitin42 May 20, 2018
43f823a
don't use proxy in Sketch or Native
nitin42 May 20, 2018
3f1e514
use createRef() api for innerRef
nitin42 May 20, 2018
93f89af
remove primitive props check
nitin42 May 20, 2018
fca98c6
fix(emotion-primitives): put back missing dependency for tests
DavideDaniel May 21, 2018
989c582
Merge pull request #1 from DavideDaniel/emotion-primitives
nitin42 May 21, 2018
3ccc32e
updated tests to use .Text and etc
nitin42 May 21, 2018
7ff2302
forward children prop
nitin42 May 21, 2018
372a94e
update primitive API
nitin42 May 21, 2018
ff42dda
added get-lerna-packages depen.
nitin42 May 21, 2018
2a915b3
remove unnecessary properties from styled constructor
nitin42 May 21, 2018
d61b23e
Pass innerRef down directly
emmatown May 21, 2018
a670594
use ':' when splitting the css properties
nitin42 May 25, 2018
290e250
remove convertRuleOptions
nitin42 May 25, 2018
21c98fb
move React to peerDepend.
nitin42 May 25, 2018
6a40c31
Update package.json
emmatown May 25, 2018
d6f458f
Merge branch 'master' into emotion-primitives
emmatown May 25, 2018
3f665fc
omit styles array and use theme context
nitin42 May 25, 2018
34f1e42
use this.mergedProps in interpolations
nitin42 May 25, 2018
dc918d7
fixed failing tests
nitin42 May 25, 2018
d9eeac7
Init commit
nitin42 May 25, 2018
4a645c0
refactored
nitin42 May 25, 2018
e22c152
cleanup
nitin42 May 25, 2018
3e332c1
remove test for .styles property
nitin42 May 29, 2018
747acb3
remove unnecessary space
nitin42 Jun 6, 2018
b27ceb8
updated
nitin42 Jun 6, 2018
0fa0929
formatted
nitin42 Jun 6, 2018
49c8172
docs
nitin42 Jun 6, 2018
51eecab
Update docs.yaml
emmatown Jun 7, 2018
e05a360
Optimized splitProps a little bit
Andarist Jun 14, 2018
a0c8a79
Tweaked getPrimitive implementation
Andarist Jun 14, 2018
801c69d
Optimized isValidStyleProp implementation a little bit
Andarist Jun 14, 2018
1632b26
added test case for withComponent
nitin42 Jun 17, 2018
9c0b724
Merge branch 'emotion-primitives' of https://github.com/nitin42/emoti…
nitin42 Jun 17, 2018
6df7787
fixed grid layout
nitin42 Jun 17, 2018
99434b7
Revert "fixed grid layout"
nitin42 Jun 17, 2018
be28620
remove proxy
nitin42 Jun 26, 2018
841a01a
updated test case for invalid primitive
nitin42 Jun 26, 2018
b010d10
Merge branch 'master' of https://github.com/emotion-js/emotion into e…
tkh44 Jun 27, 2018
00c5108
Merge branch 'master' into emotion-primitives
tkh44 Jun 28, 2018
e5408dd
Merge branch 'master' into emotion-primitives
tkh44 Jun 28, 2018
1e5519f
basic example
nitin42 Jun 29, 2018
b67bab7
updated example
nitin42 Jun 29, 2018
c7bab6b
added link to react-sketchapp
nitin42 Jun 29, 2018
e91ab38
Merge branch 'emotion-primitives' of https://github.com/nitin42/emoti…
nitin42 Jun 29, 2018
633761f
use styled in import
nitin42 Jun 30, 2018
0ffcb92
Merge branch 'master' into emotion-primitives
tkh44 Jul 2, 2018
5f9b040
Update lock file... again
tkh44 Jul 2, 2018
75c57c9
Test against src and not dist file.
tkh44 Jul 2, 2018
0c5eb89
Remove style overrides as props, simplify some stuff, remove StyleShe…
emmatown Jul 6, 2018
8a84d7c
Remove props as css overrides from README example
emmatown Jul 6, 2018
53b984d
Merge branch 'master' into emotion-primitives
emmatown Jul 6, 2018
779c8c0
A WIP css function
emmatown Jul 6, 2018
b6d4c17
Merge branch 'emotion-primitives' of https://github.com/nitin42/emoti…
emmatown Jul 6, 2018
a912bff
Make interleave use for loop and add prop-types as peerDependency
emmatown Jul 6, 2018
b3b5e93
Use css in styled in emotion-primitives
emmatown Jul 6, 2018
437a9ab
Don't transform styled.View to styled('View')
emmatown Jul 6, 2018
477b0e2
Use @emotion/is-prop-valid
emmatown Jul 6, 2018
2cfe87a
Change interleave implementation
emmatown Jul 6, 2018
9b1920a
Mention peerDep on prop-types in README
emmatown Jul 6, 2018
c46233c
Add emotion-primitives to .flowconfig
emmatown Jul 6, 2018
d8f9810
Fix a thing
emmatown Jul 6, 2018
33d236f
Add test for composition
emmatown Jul 6, 2018
67fd321
Use css in README.md
emmatown Jul 6, 2018
e3a2055
Avoid creating an extra object in some cases
emmatown Jul 6, 2018
38a0243
Add a comment explaining a thing
emmatown Jul 6, 2018
d2fe4c4
Remove on* properties from list since @emotion/is-prop-valid allows a…
emmatown Jul 6, 2018
04e4bb3
Merge branch 'master' into emotion-primitives
emmatown Jul 6, 2018
cbdbf0f
Rename a variable
emmatown Jul 6, 2018
b97b54e
Hoist handleInterpolation and rename pick to pickTest
emmatown Jul 6, 2018
e41ce0f
Hoist iteratee in convertStyles
emmatown Jul 6, 2018
2254dd2
Fix flow types for styled and change no babel test
emmatown Jul 6, 2018
79ba84f
Fix eslint comment
emmatown Jul 6, 2018
d2d17a2
100% statement coverage for emotion-primitives 🎉
emmatown Jul 6, 2018
30349a7
Add a comment
emmatown Jul 6, 2018
6538b79
Add another comment
emmatown Jul 6, 2018
f8e6958
Improved documentation
nitin42 Jul 6, 2018
e572f96
Merge branch 'emotion-primitives' of https://github.com/nitin42/emoti…
nitin42 Jul 6, 2018
feeda5f
fix stuff
emmatown Jul 6, 2018
bab8aec
Rename to @emotion/primitives
emmatown Jul 6, 2018
5714031
Fix site
emmatown Jul 6, 2018
eb4b4fa
Remove incorrect comment
emmatown Jul 6, 2018
aa41a29
Remove old rollup config and dependencies in top level package.json
emmatown Jul 6, 2018
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
1 change: 1 addition & 0 deletions .flowconfig
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ module.name_mapper='^\(emotion-theming\)$' -> '<PROJECT_ROOT>/packages/\1/src'
module.name_mapper='^\(emotion-server\)$' -> '<PROJECT_ROOT>/packages/\1/src'
module.name_mapper='^\(create-emotion-server\)$' -> '<PROJECT_ROOT>/packages/\1/src'
module.name_mapper='^\(jest-emotion\)$' -> '<PROJECT_ROOT>/packages/\1/src'
module.name_mapper='^@emotion\/\([a-zA-Z0-9_\-]+\)$' -> '<PROJECT_ROOT>/packages/\1/src/index'
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ packages/site/build
package-lock.json
.DS_Store
.cache
public/
public/
/extract.test.emotion.css
2 changes: 1 addition & 1 deletion .yarnrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
workspaces-experimental true
--publish.access public
1 change: 1 addition & 0 deletions docs/docs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
- emotion-server
- emotion-theming
- jest-emotion
- "@emotion/primitives"
- create-emotion
- create-emotion-styled
- create-emotion-server
Expand Down
45 changes: 33 additions & 12 deletions packages/babel-plugin-emotion/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ export function buildStyledCallExpression(
args: Node[],
path: BabelPath,
state: EmotionBabelPluginPass,
isCallExpression: boolean,
t: Types
) {
// unpacking "manually" to prevent array out of bounds access (deopt)
Expand Down Expand Up @@ -281,9 +282,18 @@ export function buildStyledCallExpression(
targetProperty
)

return t.callExpression(
let styledCall =
t.isStringLiteral(tag) &&
!isCallExpression &&
// $FlowFixMe
t.callExpression(identifier, [tag, finalOptions, ...restArgs]),
tag.value[0] !== tag.value[0].toLowerCase()
? // $FlowFixMe
t.memberExpression(identifier, t.identifier(tag.value))
: // $FlowFixMe
t.callExpression(identifier, [tag, finalOptions, ...restArgs])

return t.callExpression(
styledCall,
appendStringToExpressions(
getExpressionsFromTemplateLiteral(path.node.quasi, t),
stringToAppend,
Expand All @@ -304,7 +314,7 @@ export function buildStyledObjectCallExpression(
const tag = t.isCallExpression(path.node.callee)
? path.node.callee.arguments[0]
: t.stringLiteral(path.node.callee.property.name)

let isCallExpression = t.isCallExpression(path.node.callee)
let styledOptions = null
let restStyledArgs = []
if (t.isCallExpression(path.node.callee)) {
Expand Down Expand Up @@ -335,14 +345,18 @@ export function buildStyledObjectCallExpression(

path.addComment('leading', '#__PURE__')

return t.callExpression(
t.callExpression(identifier, [
tag,
buildFinalOptions(t, styledOptions, targetProperty, labelProperty),
...restStyledArgs
]),
args
)
let styledCall =
t.isStringLiteral(tag) &&
!isCallExpression &&
tag.value[0] !== tag.value[0].toLowerCase()
? t.memberExpression(identifier, t.identifier(tag.value))
: t.callExpression(identifier, [
tag,
buildFinalOptions(t, styledOptions, targetProperty, labelProperty),
...restStyledArgs
])

return t.callExpression(styledCall, args)
}

const visited = Symbol('visited')
Expand All @@ -359,7 +373,12 @@ const importedNameKeys = Object.keys(defaultImportedNames).map(
key => (key === 'styled' ? 'default' : key)
)

const defaultEmotionPaths = ['emotion', 'react-emotion', 'preact-emotion']
const defaultEmotionPaths = [
'emotion',
'react-emotion',
'preact-emotion',
'@emotion/primitives'
]

function getRelativePath(filepath: string, absoluteInstancePath: string) {
let relativePath = nodePath.relative(
Expand Down Expand Up @@ -670,6 +689,7 @@ export default function(babel: Babel) {
[t.stringLiteral(path.node.tag.property.name)],
path,
state,
false,
t
)
)
Expand All @@ -684,6 +704,7 @@ export default function(babel: Babel) {
path.node.tag.arguments,
path,
state,
true,
t
)
)
Expand Down
2 changes: 2 additions & 0 deletions packages/babel-plugin-emotion/src/macro-styled.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ function macro(options) {
[t.stringLiteral(path.node.tag.property.name)],
path,
state,
false,
t
)
)
Expand All @@ -40,6 +41,7 @@ function macro(options) {
path.node.tag.arguments,
path,
state,
true,
t
)
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`styled inline babel 6 does not change call expressions 1`] = `
"
/*#__PURE__*/styled('SomeFakeComponent', {
target: 'e8nbyc50'
})('color:hotpink;');
/*#__PURE__*/styled('SomeFakeComponent', {
target: 'e8nbyc51'
})({});"
`;

exports[`styled inline babel 6 does not change to a call expression when beginning with a upper case letter 1`] = `
"
/*#__PURE__*/styled.View(\\"color:hotpink;\\");
/*#__PURE__*/styled.View({});"
`;

exports[`styled inline babel 6 other name with @emotion/primitives 1`] = `
"
import someOtherName from '@emotion/primitives';
/*#__PURE__*/someOtherName.View('color:hotpink;');
/*#__PURE__*/someOtherName.View({});"
`;

exports[`styled inline babel 7 does not change call expressions 1`] = `
"/*#__PURE__*/
styled('SomeFakeComponent', {
target: \\"e1u6ll0g0\\"
})(\\"color:hotpink;\\");

/*#__PURE__*/
styled('SomeFakeComponent', {
target: \\"e1u6ll0g1\\"
})({});"
`;

exports[`styled inline babel 7 does not change to a call expression when beginning with a upper case letter 1`] = `
"/*#__PURE__*/
styled.View(\\"color:hotpink;\\");

/*#__PURE__*/
styled.View({});"
`;

exports[`styled inline babel 7 other name with @emotion/primitives 1`] = `
"import someOtherName from '@emotion/primitives';

/*#__PURE__*/
someOtherName.View(\\"color:hotpink;\\");

/*#__PURE__*/
someOtherName.View({});"
`;
27 changes: 27 additions & 0 deletions packages/babel-plugin-emotion/test/primitives.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// @flow

import { createInlineTests } from './util'

const cases = {
'does not change to a call expression when beginning with a upper case letter': {
code: `
styled.View\`color: hotpink;\`
styled.View({})
`
},
'other name with @emotion/primitives': {
code: `
import someOtherName from '@emotion/primitives'
someOtherName.View\`color: hotpink;\`
someOtherName.View({})
`
},
'does not change call expressions': {
code: `
styled('SomeFakeComponent')\`color: hotpink;\`
styled('SomeFakeComponent')({})
`
}
}

createInlineTests('styled inline', cases)
151 changes: 151 additions & 0 deletions packages/primitives/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
# @emotion/primitives

> Style and render primitive interfaces across multiple targets with emotion

## Introduction

Emotion primitives makes it easy to style and render primitives across multiple platforms like the web, React Native and Sketch using the `emotion` API.

## Install

```
npm install @emotion/primitives
```

or if you use yarn

```
yarn add @emotion/primitives
```

This package also depends on `react`, `react-primitives` and `prop-types` so make sure you've them installed.

## Example

```js
import React from 'react'
import styled, { css } from '@emotion/primitives'

import { ThemeProvider } from 'emotion-theming'

const theme = {
color: 'hotpink',
backgroundColor: 'purple'
}

const Container = styled.View`
display: flex;
justify-content: center;
align-items: center;
margin: 50px;
border: 5px solid red;
background-color: ${props => props.theme.backgroundColor};
`

const Description = styled.Text({
color: 'hotpink'
})

const Image = styled.Image`
padding: 40px;
`

const emotionLogo =
'https://cdn.rawgit.com/emotion-js/emotion/master/emotion.png'

class App extends React.Component {
render() {
return (
<ThemeProvider theme={theme}>
<Container
style={css`
border-radius: 10px;
`}
>
<Description style={{ fontSize: 45, fontWeight: 'bold' }}>
Emotion Primitives
</Description>
<Image
source={{
uri: emotionLogo,
height: 150,
width: 150
}}
/>
</Container>
</ThemeProvider>
)
}
}
```

[![Edit n3nmq8v46j](https://codesandbox.io/static/img/play-codesandbox.svg)](https://codesandbox.io/s/n3nmq8v46j)

## Supported primitives

* **Text**

* **View**

* **Image**

## Usage with `react-sketchapp`

### Installing dependencies

* [`react-sketchapp`](https://github.com/airbnb/react-sketchapp)

```
yarn add react-sketchapp
```

* `skpm` for building and publishing sketch plugins. This is required to render the components and build them to Sketch environment.

```
yarn add @skpm/builder --dev
```

### Configuring `skpm` builder

Once you've installed `skpm`,

* create a new field `skpm` in `package.json`

```json
"skpm": {
"main": "some-name.sketchplugin",
"manifest": "manifest.json"
},
```

* Create `manifest.json` file with following content -

```json
{
"compatibleVersion": 3,
"bundleVersion": 1,
"commands": [
{
"name": "react-sketchapp: some-name",
"identifier": "main",
"script": "./your-app-file.js"
}
],
"menu": {
"isRoot": true,
"items": ["main"]
}
}
```

* add this to your `scripts` section of your `package.json`

```json
scripts: {
"render": "skpm-build --watch --run",
}
```

and finally run `yarn render` to render the components to Sketch.

For a complete reference, checkout [this](https://github.com/airbnb/react-sketchapp/blob/master/examples/emotion/src) project setup.
Loading