-
-
Notifications
You must be signed in to change notification settings - Fork 182
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
Simulation plugin #292
Merged
Merged
Simulation plugin #292
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2016 Robin Frischmann | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
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,69 @@ | ||
# fela-plugin-simulate | ||
|
||
|
||
<img alt="npm downloads" src="https://img.shields.io/npm/dm/fela-plugin-simulate.svg"> <img alt="gzipped size" src="https://img.shields.io/badge/gzipped-0.49kb-brightgreen.svg"> | ||
|
||
This plugin can be used to quickly simulate nested style objects such as pseudo classes, media queries or attribute selectors. | ||
|
||
## Installation | ||
```sh | ||
yarn add fela-plugin-simulate | ||
``` | ||
You may alternatively use `npm i --save fela-plugin-simulate`. | ||
|
||
|
||
## Usage | ||
Make sure to read the documentation on [how to use plugins](http://fela.js.org/docs/advanced/Plugins.html). | ||
|
||
```javascript | ||
import { createRenderer } from 'fela' | ||
import simulate from 'fela-plugin-simulate' | ||
|
||
const renderer = createRenderer({ | ||
plugins: [ simulate() ] | ||
}) | ||
``` | ||
|
||
## Example | ||
|
||
#### Input | ||
```javascript | ||
{ | ||
color: 'red', | ||
|
||
'@media (min-height: 320px)': { | ||
color: 'green', | ||
backgroundColor: 'red' | ||
}, | ||
':hover': { | ||
color: 'blue' | ||
}, | ||
':active': { | ||
color: 'yellow' | ||
} | ||
} | ||
``` | ||
|
||
rendered with the following props | ||
```javascript | ||
{ | ||
simulate: { | ||
':hover': true, | ||
':active': false, | ||
'@media (min-height: 320px)': true | ||
} | ||
} | ||
``` | ||
|
||
#### Output | ||
```javascript | ||
{ | ||
backgroundColor: 'red', | ||
color: 'blue' | ||
} | ||
``` | ||
|
||
## License | ||
Fela is licensed under the [MIT License](http://opensource.org/licenses/MIT).<br> | ||
Documentation is licensed under [Creative Common License](http://creativecommons.org/licenses/by/4.0/).<br> | ||
Created with ♥ by [@rofrischmann](http://rofrischmann.de) and all the great contributors. |
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,28 @@ | ||
{ | ||
"name": "fela-plugin-simulate", | ||
"version": "5.0.0", | ||
"description": "Fela plugin to simulate pseudo classes and media queries", | ||
"main": "lib/index.js", | ||
"module": "es/index.js", | ||
"jsnext:main": "es/index.js", | ||
"files": [ | ||
"LICENSE", | ||
"README.md", | ||
"lib/**", | ||
"es/**" | ||
], | ||
"repository": "https://github.com/rofrischmann/fela/", | ||
"keywords": [ | ||
"fela", | ||
"fela-native", | ||
"react-native", | ||
"media query", | ||
"cssinjs" | ||
], | ||
"author": "Robin Frischmann", | ||
"license": "MIT", | ||
"dependencies": { | ||
"css-in-js-utils": "^1.0.3", | ||
"fela-utils": "^5.0.0" | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
packages/fela-plugin-simulate/src/__tests__/simulate-test.js
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,64 @@ | ||
import simulate from '../index' | ||
|
||
describe('Simulating nested styles', () => { | ||
it('should simulate pseudo classes', () => { | ||
const style = { | ||
width: 20, | ||
':hover': { color: 'red' }, | ||
':active': { color: 'blue' } | ||
} | ||
|
||
expect( | ||
simulate()(style, undefined, undefined, { simulate: { ':hover': true } }) | ||
).toEqual({ | ||
width: 20, | ||
color: 'red', | ||
':active': { color: 'blue' } | ||
}) | ||
}) | ||
|
||
it('should simulate media queries', () => { | ||
const style = { | ||
width: 20, | ||
':hover': { color: 'red' }, | ||
'@media (min-height: 300px)': { color: 'blue' } | ||
} | ||
|
||
expect( | ||
simulate()(style, undefined, undefined, { | ||
simulate: { | ||
'@media (min-height: 300px)': true, | ||
':hover': false | ||
} | ||
}) | ||
).toEqual({ | ||
width: 20, | ||
color: 'blue', | ||
':hover': { color: 'red' } | ||
}) | ||
}) | ||
|
||
it('should simulate multiple nested styles', () => { | ||
const style = { | ||
width: 20, | ||
':hover': { | ||
color: 'red', | ||
backgroundColor: 'blue' | ||
}, | ||
'@media (min-height: 300px)': { color: 'blue' } | ||
} | ||
|
||
expect( | ||
simulate()(style, undefined, undefined, { | ||
simulate: { | ||
'@media (min-height: 300px)': true, | ||
':hover': true | ||
} | ||
}) | ||
).toEqual({ | ||
width: 20, | ||
backgroundColor: 'blue', | ||
color: 'blue' | ||
}) | ||
}) | ||
}) |
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,31 @@ | ||
/* @flow */ | ||
import { isObject } from 'fela-utils' | ||
import assignStyle from 'css-in-js-utils/lib/assignStyle' | ||
|
||
import { DOMRenderer } from '../../../flowtypes/DOMRenderer' | ||
import { NativeRenderer } from '../../../flowtypes/NativeRenderer' | ||
|
||
type Type = 'KEYFRAME' | 'RULE' | 'STATIC' | ||
function resolveSimulation( | ||
style: Object, | ||
type: Type, | ||
renderer: DOMRenderer | NativeRenderer, | ||
props: Object | ||
): Object { | ||
if (props.simulate) { | ||
for (const property in style) { | ||
const value = style[property] | ||
|
||
if (isObject(value) && props.simulate[property]) { | ||
const resolvedValue = resolveSimulation(value, type, renderer, props) | ||
|
||
assignStyle(style, resolvedValue) | ||
delete style[property] | ||
} | ||
} | ||
} | ||
|
||
return style | ||
} | ||
|
||
export default () => resolveSimulation |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is
color: blue
winning overcolor: red
? Does the order of props inconst style
matter? If so, it's kinda weird since object doesn't enforce order.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In that case, I would just delete this test, since it's not necessarily deterministic.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, right now it simply iterates all object values and resolves them straight in order. Could later improve that to first resolve medias, then pseudos, etc.