Skip to content

Commit 92a53e4

Browse files
authored
Dynamic values full syntax (#878)
* added fallbacks test to fn rules * only call onChangeValue if plugins: true * enable removing props from fn rules * shorter syntax with coercion * wip full syntax support * restore browsers.json * changelog * move hook call to the core * isProcessed flag explanation * added tests to fn rules * remove media rule as a function test case * added a test for #796, as part of #682 * tests for compose plugin * observables - move documentation to the package - update docs, since plugins don't apply by default - introduce option process: true to the plugin to enable plugins if user wants that * move fn values docs * wording * Added tests for using jss-plugin-syntax-expand with function rules and values * Skip fn values with jss-expand for now * move container rules update to the core * optimize onProcessStyle onProcessStyle is invoked on each .update, but in this case we only need to do this once, because fn values can't be added * put variables in the right scope
1 parent 42ee5b9 commit 92a53e4

41 files changed

Lines changed: 9318 additions & 307 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

changelog.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
- Fix multiple cases where linking CSS rules didn't work (#815, #710, #664)
44

5+
### Breaking changes
6+
7+
- Observables, function values and rules are now standalone packages, not part of the core. They are still part of the default preset though.
8+
- Function values and rules apply plugins by default now, which means they are slower by default. To speed them up use `sheet.update(data, {process: false})` (#682)
9+
- Observables will not apply plugins by default now, to make them even faster. If you want them to use plugins, can use an option when using the plugin: `jss.use(pluginObservable({process: true}))` (#682)
10+
511
## 9.8.7 / 2018-06-24
612

713
- Fix `global` access in the dist version (#736)

docs/json-api.md

Lines changed: 0 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -22,72 +22,6 @@ Compiles to:
2222
}
2323
```
2424

25-
## Function values
26-
27-
If you want dynamic behavior for your Style Sheet, you can use functions as a value which return the actual value. If function values returns `null|undefined|false` - property will be removed. Use [sheet.update(data)](./js-api.md#update-function-values) in order to pass the data object. [Sheet option](./js-api.md#create-style-sheet) `link: true` is required for this to function.
28-
29-
```javascript
30-
const styles = {
31-
button: {
32-
color: data => data.color
33-
}
34-
}
35-
```
36-
37-
### Support of "!important"
38-
39-
To use the `!important` modifier with function values, you must use [array syntax](#alternative-syntax-for-space-and-comma-separated-values):
40-
41-
```javascript
42-
const styles = {
43-
button: {
44-
color: data => [[data.color], '!important']
45-
}
46-
}
47-
```
48-
49-
## Function rules
50-
51-
Similar to function values, you can use a function to return a dynamic style object. Use [sheet.update(data)](./js-api.md#update-function-values) in order to pass the data object. Sheet option `link: true` is required for this to function.
52-
53-
```javascript
54-
const styles = {
55-
button: data => ({
56-
display: 'flex',
57-
color: data.color
58-
})
59-
}
60-
```
61-
62-
## Observable values
63-
64-
In order to create highly dynamic animations, you may want to use streams. Take a look at the [tc39 observable proposal](https://github.com/tc39/proposal-observable). Sheet option `link: true` is required for this to function.
65-
66-
```javascript
67-
const styles = {
68-
button: {
69-
color: new Observable(observer => {
70-
observer.next('red')
71-
})
72-
}
73-
}
74-
```
75-
76-
## Observable rules
77-
78-
Similar to observable values, you can declare observable rules. Stream should contain in this case the style object. Sheet option `link: true` is required for this to function.
79-
80-
```javascript
81-
const styles = {
82-
button: new Observable(observer => {
83-
observer.next({
84-
color: 'red',
85-
opacity: 1
86-
})
87-
})
88-
}
89-
```
90-
9125
## Media Queries
9226

9327
```javascript

karma.conf.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ module.exports = config => {
2323
'node_modules/raf/polyfill.js',
2424
'./packages/jss/tests/index.js',
2525
'./packages/jss-plugin-syntax-rule-value-function/src/index.test.js',
26-
'./packages/jss-plugin-syntax-rule-value-observable/src/observable.test.js',
26+
'./packages/jss-plugin-syntax-rule-value-observable/src/index.test.js',
2727
'./packages/jss-plugin-syntax-expand/src/index.test.js',
2828
'./packages/jss-plugin-syntax-default-unit/src/index.test.js',
2929
'./packages/jss-plugin-syntax-camel-case/src/index.test.js',

packages/jss-plugin-isolate/.size-snapshot.json

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
{
22
"dist/jss-plugin-isolate.js": {
3-
"bundled": 14372,
4-
"minified": 9394,
5-
"gzipped": 2667
3+
"bundled": 14368,
4+
"minified": 9404,
5+
"gzipped": 2670
66
},
77
"dist/jss-plugin-isolate.min.js": {
8-
"bundled": 14372,
9-
"minified": 9394,
10-
"gzipped": 2667
8+
"bundled": 14368,
9+
"minified": 9404,
10+
"gzipped": 2670
1111
},
1212
"dist/jss-plugin-isolate.cjs.js": {
13-
"bundled": 3922,
14-
"minified": 1604,
15-
"gzipped": 746
13+
"bundled": 3920,
14+
"minified": 1614,
15+
"gzipped": 750
1616
},
1717
"dist/jss-plugin-isolate.esm.js": {
18-
"bundled": 3668,
19-
"minified": 1410,
20-
"gzipped": 678,
18+
"bundled": 3666,
19+
"minified": 1420,
20+
"gzipped": 681,
2121
"treeshaked": {
2222
"rollup": {
2323
"code": 99,

packages/jss-plugin-syntax-expand/.size-snapshot.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
{
22
"dist/jss-plugin-syntax-expand.js": {
3-
"bundled": 10991,
3+
"bundled": 11087,
44
"minified": 4112,
55
"gzipped": 1450
66
},
77
"dist/jss-plugin-syntax-expand.min.js": {
8-
"bundled": 10991,
8+
"bundled": 11087,
99
"minified": 4112,
1010
"gzipped": 1450
1111
},
1212
"dist/jss-plugin-syntax-expand.cjs.js": {
13-
"bundled": 10010,
13+
"bundled": 10102,
1414
"minified": 4283,
1515
"gzipped": 1451
1616
},
1717
"dist/jss-plugin-syntax-expand.esm.js": {
18-
"bundled": 9928,
18+
"bundled": 10020,
1919
"minified": 4214,
2020
"gzipped": 1406,
2121
"treeshaked": {

packages/jss-plugin-syntax-extend/.size-snapshot.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
{
22
"dist/jss-plugin-syntax-extend.js": {
3-
"bundled": 5220,
3+
"bundled": 5265,
44
"minified": 1801,
55
"gzipped": 912
66
},
77
"dist/jss-plugin-syntax-extend.min.js": {
8-
"bundled": 4104,
8+
"bundled": 4149,
99
"minified": 1281,
1010
"gzipped": 630
1111
},
1212
"dist/jss-plugin-syntax-extend.cjs.js": {
13-
"bundled": 3404,
13+
"bundled": 3447,
1414
"minified": 1384,
1515
"gzipped": 620
1616
},
1717
"dist/jss-plugin-syntax-extend.esm.js": {
18-
"bundled": 3186,
18+
"bundled": 3229,
1919
"minified": 1216,
2020
"gzipped": 551,
2121
"treeshaked": {

packages/jss-plugin-syntax-extend/src/index.test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -334,8 +334,8 @@ describe('jss-plugin-syntax-extend', () => {
334334
beforeEach(() => {
335335
const styles = {
336336
a: data => ({
337-
height: '200px',
338-
extend: data.redContainer
337+
extend: data.redContainer,
338+
height: '200px'
339339
})
340340
}
341341

@@ -350,7 +350,7 @@ describe('jss-plugin-syntax-extend', () => {
350350

351351
it('should have correct output', () => {
352352
expect(sheet.getRule('a')).to.not.be(undefined)
353-
expect(sheet.toString()).to.be('.a-id {\n height: 200px;\n background: red;\n}')
353+
expect(sheet.toString()).to.be('.a-id {\n background: red;\n height: 200px;\n}')
354354
})
355355
})
356356

packages/jss-plugin-syntax-nested/.size-snapshot.json

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
{
22
"dist/jss-plugin-syntax-nested.js": {
3-
"bundled": 5743,
3+
"bundled": 5777,
44
"minified": 1944,
5-
"gzipped": 1048
5+
"gzipped": 1050
66
},
77
"dist/jss-plugin-syntax-nested.min.js": {
8-
"bundled": 4627,
8+
"bundled": 4661,
99
"minified": 1424,
10-
"gzipped": 781
10+
"gzipped": 782
1111
},
1212
"dist/jss-plugin-syntax-nested.cjs.js": {
13-
"bundled": 3612,
13+
"bundled": 3646,
1414
"minified": 1347,
15-
"gzipped": 720
15+
"gzipped": 722
1616
},
1717
"dist/jss-plugin-syntax-nested.esm.js": {
18-
"bundled": 3378,
18+
"bundled": 3412,
1919
"minified": 1168,
20-
"gzipped": 640,
20+
"gzipped": 642,
2121
"treeshaked": {
2222
"rollup": {
2323
"code": 59,

packages/jss-plugin-syntax-rule-value-function/.size-snapshot.json

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
{
22
"dist/jss-plugin-syntax-rule-value-function.js": {
3-
"bundled": 2073,
4-
"minified": 827,
5-
"gzipped": 472
3+
"bundled": 2024,
4+
"minified": 775,
5+
"gzipped": 453
66
},
77
"dist/jss-plugin-syntax-rule-value-function.min.js": {
8-
"bundled": 2073,
9-
"minified": 827,
10-
"gzipped": 472
8+
"bundled": 2024,
9+
"minified": 775,
10+
"gzipped": 453
1111
},
1212
"dist/jss-plugin-syntax-rule-value-function.cjs.js": {
13-
"bundled": 1672,
14-
"minified": 737,
15-
"gzipped": 411
13+
"bundled": 1633,
14+
"minified": 664,
15+
"gzipped": 392
1616
},
1717
"dist/jss-plugin-syntax-rule-value-function.esm.js": {
18-
"bundled": 1600,
19-
"minified": 678,
20-
"gzipped": 363,
18+
"bundled": 1561,
19+
"minified": 605,
20+
"gzipped": 345,
2121
"treeshaked": {
2222
"rollup": {
2323
"code": 12,
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# JSS plugin that allows to use functions for dynamic styles
2+
3+
[![Gitter](https://badges.gitter.im/JoinChat.svg)](https://gitter.im/cssinjs/lobby)
4+
5+
If you want dynamic behavior for your Style Sheet, you can use functions as a value which return the actual value or a rule. If function returns `null|undefined|false` - property will be removed. Use [sheet.update(data)](https://github.com/cssinjs/jss/blob/master/docs/js-api.md#update-function-values) in order to pass the data object.
6+
7+
[Sheet option](https://github.com/cssinjs/jss/blob/master/docs/js-api.md#create-style-sheet) `link: true` is required for this to function.
8+
9+
_Plugins are applied by default to function rules or values._
10+
11+
Make sure you read [how to use
12+
plugins](https://github.com/cssinjs/jss/blob/master/docs/setup.md#setup-with-plugins)
13+
in general.
14+
15+
## Function values
16+
17+
```javascript
18+
const styles = {
19+
button: {
20+
color: data => data.color
21+
}
22+
}
23+
24+
const sheet = jss.createStyleSheet(styles, {link: true}).attach()
25+
26+
sheet.update({color: 'red'})
27+
```
28+
29+
```css
30+
.button-1-2-3 {
31+
color: red;
32+
}
33+
```
34+
35+
## Function rules
36+
37+
Similar to function values, you can use a function to return a dynamic style object. Use [sheet.update(data)](https://github.com/cssinjs/jss/blob/master/docs/js-api.md#update-function-values) in order to pass the data object. Sheet option `link: true` is required for this to function.
38+
39+
```javascript
40+
const styles = {
41+
button: data => ({
42+
display: 'flex',
43+
color: data.color
44+
})
45+
}
46+
```
47+
48+
## Support of "!important"
49+
50+
To use the `!important` modifier with function values, you must use [array syntax](https://github.com/cssinjs/jss/blob/master/docs/json-api.md#alternative-syntax-for-space-and-comma-separated-values):
51+
52+
```javascript
53+
const styles = {
54+
button: {
55+
color: data => [[data.color], '!important']
56+
}
57+
}
58+
```
59+
60+
## Demo
61+
62+
[Simple](http://cssinjs.github.io/examples/function-values/index.html)
63+
64+
## Issues
65+
66+
File a bug against [cssinjs/jss prefixed with \[jss-plugin-syntax-rule-value-function\]](https://github.com/cssinjs/jss/issues/new?title=[jss-plugin-syntax-rule-value-function]%20).
67+
68+
## Run tests
69+
70+
```bash
71+
yarn
72+
yarn test
73+
```
74+
75+
## License
76+
77+
MIT

0 commit comments

Comments
 (0)