Skip to content

Commit fc3773f

Browse files
authored
Scoped keyframes (#888)
* 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 * scoped keyframes * Added tests for using jss-plugin-syntax-expand with function rules and values * Skip fn values with jss-expand for now * use "keyframes-{name}" as a key in KeyFramesRule Changed the key to a form where it can be used directly as-is for id generation, so we don't have to fake the rule when using generateClassName * keyframes name parser with validation * wip keyframes inside of global * 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 * changelog * update tests with key keyframes name * Update changelog.md * Update docs/json-api.md * Update changelog.md * process: true always * support multiple whitespaces after keyframes identifier * move keyframe name generation to outside * call external plugins first we need to separate internal and external queues, queue 0 is external, 1 is internal and 1 is executed always after 0 * move keyframes rule to plugins * move StyleRule to a plugin Additionally stop rendering StyleRule when user passes a bad/unknown at-rule * move viewport rule to plugins * move simple rule to plugins * move FontFace rule * rename plugins to {rule}Rule schema * beter types for queue * Add support for animation property * optimize perf, handle arrays * fix tests, add more tests * Fix * Support arrays * remove array support * use global instead of window * always run stylerule plugin first, to avoid regexes of the at rules * ensure to always use style rule as a first rule in plugins flow started to complain with "Unused suppression comment." so I removed them * fix cache plugin * test raw rule registration order * rewrite internal plugins queue * better logic in plugins.use() * better types * better types * fix types * keyframe inside of global TODO - Rethink BaseStyleRule - Evtl move generateClassName to the rule models - Evtl rename generateClassName to generateId * default export in plugins * move id generator to models * update snapshots * fix circular deps * use CSSKeyframeRule * generateClassName -> generateId * added exampls with keyframes to global plugin * docs, small syntactic changes
1 parent a19c871 commit fc3773f

83 files changed

Lines changed: 1153 additions & 602 deletions

File tree

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: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
## Next / 2018-09-16
22

33
- Fix multiple cases where linking CSS rules didn't work (#815, #710, #664)
4+
- Added support for Typed CSSOM values (#882)
5+
- Added scoped keyframes support (#346)
46
- Function values and function rules support now fallbacks, media queries, nesting, global styles (#682)
5-
- Added support for Typed CSSOM values
67

78
### Breaking changes
89

910
- Observables, function values and rules are now standalone packages, not part of the core. They are still part of the default preset though.
1011
- Function values, rules and observables apply plugins by default now, which means they can support all plugin defined syntaxes, but they are also slower by default. To speed them up use `sheet.update(data, {process: false})` for fn values/rules and `jss.use(pluginObservable({process: false}))` when setting up observables plugin. (#682)
12+
- Rule @keyframes has now scoped name by default, which means that you can access it using `$ref` from the same sheet and generate global one as before using `@global` rule ((#346).
13+
- Options `createGenerateClassName` and `generateClassName` are renamed to `createGenerateId` and `generateId` since th same function is now used to scope @keyframes rules. This affects both JSS and React-JSS.
1114

1215
## 9.8.7 / 2018-06-24
1316

docs/js-api.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export default jss
3232

3333
Options:
3434

35-
- `createGenerateClassName` - a function which returns a function which generates unique class names.
35+
- `createGenerateId` - a function which returns a function which generates unique class names.
3636
- `plugins` - an array of functions, will be passed to `jss.use`.
3737
- `virtual` - if true, JSS will use VirtualRenderer.
3838
- `insertionPoint` - string value of a DOM comment node which marks the start of sheets or a rendered DOM node. Sheets rendered by this Jss instance are inserted after this point sequentially.
@@ -74,7 +74,7 @@ Options:
7474
- `link` - link jss `Rule` instances with DOM `CSSRule` instances so that styles, can be modified dynamically, false by default because it has some performance cost.
7575
- `element` - style element, will create one by default.
7676
- `index` - 0 by default - determines DOM rendering order, higher number = higher specificity (inserted after).
77-
- `generateClassName` - a function that generates a unique class name.
77+
- `generateId` - a function that generates a unique class name.
7878
- `classNamePrefix` - a string, which will be added at the beginning of the class name.
7979

8080
```javascript
@@ -358,22 +358,22 @@ console.log(sheet.toString())
358358

359359
## Generate your own class names
360360

361-
`createGenerateClassName`
361+
`createGenerateId`
362362

363-
Option `createGenerateClassName` allows you to specify a function which returns a class name generator function `generateClassName(rule, sheet)`. This pattern is used to allow JSS reset the counter upon factory invocation, when needed. For example, it is used in [React-JSS](https://github.com/cssinjs/react-jss) to reset the counter on each request for server-side rendering.
363+
Option `createGenerateId` allows you to specify a function which returns a class name generator function `generateId(rule, sheet)`. This pattern is used to allow JSS reset the counter upon factory invocation, when needed. For example, it is used in [React-JSS](https://github.com/cssinjs/react-jss) to reset the counter on each request for server-side rendering.
364364

365365
By default class names generator uses a simple counter to ensure uniqueness of the class names. It consists of `classNamePrefix` Style Sheet option + rule name + counter. **Note**: in production (`NODE_ENV=production`) it uses just the `c` + rules counter.
366366

367367
```javascript
368368
import jss from 'jss'
369369

370-
const createGenerateClassName = () => {
370+
const createGenerateId = () => {
371371
let counter = 0
372372

373373
return (rule, sheet) => `pizza--${rule.key}-${counter++}`
374374
}
375375

376-
jss.setup({createGenerateClassName})
376+
jss.setup({createGenerateId})
377377

378378
const sheet = jss.createStyleSheet({
379379
button: {

docs/json-api.md

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -69,41 +69,38 @@ Compiles to:
6969

7070
## Keyframes Animation
7171

72-
Note: keyframe id is still global and may conflict.
72+
Keyframes name will use the same id generator function as the class names. Animation name will be scoped by default. In order to access it within the same style sheet, you can use `$ref` syntax as a value of `animationName` or `animation` property.
7373

74-
```javascript
75-
const styles = {
76-
'@keyframes my-animation': {
77-
from: {opacity: 0},
78-
to: {opacity: 1}
79-
}
80-
}
81-
```
74+
Additionally generated name can be accessed through `sheet.keyframes.{name}` map.
8275

83-
### ES6 with generated keyframe id
76+
In order to generate a global animation name, you can use `@global` rule.
8477

8578
```javascript
86-
const animationId = Math.random()
87-
8879
const styles = {
89-
[`@keyframes ${animationId}`]: {
80+
'@keyframes slideRight': {
9081
from: {opacity: 0},
9182
to: {opacity: 1}
83+
},
84+
container: {
85+
animationName: '$slideRight'
9286
}
9387
}
9488
```
9589

9690
Compiles to:
9791

9892
```css
99-
@keyframes my-animation {
93+
@keyframes keyframes-slideRight-0-1-2 {
10094
from {
10195
opacity: 0;
10296
}
10397
to {
10498
opacity: 1;
10599
}
106100
}
101+
.container-0-1-3 {
102+
animation-name: keyframes-slideRight-0-1-2;
103+
}
107104
```
108105

109106
## Fallbacks

packages/jss-plugin-cache/.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-cache.js": {
3-
"bundled": 1058,
4-
"minified": 515,
5-
"gzipped": 330
3+
"bundled": 1066,
4+
"minified": 518,
5+
"gzipped": 331
66
},
77
"dist/jss-plugin-cache.min.js": {
8-
"bundled": 1058,
9-
"minified": 515,
10-
"gzipped": 330
8+
"bundled": 1066,
9+
"minified": 518,
10+
"gzipped": 331
1111
},
1212
"dist/jss-plugin-cache.cjs.js": {
13-
"bundled": 740,
14-
"minified": 368,
15-
"gzipped": 256
13+
"bundled": 748,
14+
"minified": 371,
15+
"gzipped": 258
1616
},
1717
"dist/jss-plugin-cache.esm.js": {
18-
"bundled": 658,
19-
"minified": 299,
20-
"gzipped": 213,
18+
"bundled": 666,
19+
"minified": 302,
20+
"gzipped": 215,
2121
"treeshaked": {
2222
"rollup": {
2323
"code": 0,

packages/jss-plugin-cache/src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export default function cachePlugin(): Plugin {
55
const cache = new WeakMap()
66

77
function onCreateRule(name, decl, {parent}) {
8-
return parent ? cache.get(parent.rules.raw[name]) || null : null
8+
return parent && name ? cache.get(parent.rules.raw[name]) || null : null
99
}
1010

1111
function onProcessRule(rule) {

packages/jss-plugin-props-sort/src/index.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {create} from 'jss'
44
import propsSort from './index'
55

66
const settings = {
7-
createGenerateClassName: () => rule => `${rule.key}-id`
7+
createGenerateId: () => rule => `${rule.key}-id`
88
}
99

1010
describe('jss-plugin-props-sort', () => {

packages/jss-plugin-syntax-camel-case/src/index.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import functionPlugin from 'jss-plugin-syntax-rule-value-function'
66
import camelCase from './index'
77

88
const settings = {
9-
createGenerateClassName: () => rule => `${rule.key}-id`
9+
createGenerateId: () => rule => `${rule.key}-id`
1010
}
1111

1212
describe('jss-plugin-syntax-camel-case', () => {

packages/jss-plugin-syntax-compose/.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-compose.js": {
3-
"bundled": 3738,
4-
"minified": 1342,
5-
"gzipped": 756
3+
"bundled": 3745,
4+
"minified": 1345,
5+
"gzipped": 761
66
},
77
"dist/jss-plugin-syntax-compose.min.js": {
8-
"bundled": 2622,
9-
"minified": 822,
10-
"gzipped": 485
8+
"bundled": 2629,
9+
"minified": 825,
10+
"gzipped": 487
1111
},
1212
"dist/jss-plugin-syntax-compose.cjs.js": {
13-
"bundled": 2005,
14-
"minified": 840,
15-
"gzipped": 470
13+
"bundled": 2012,
14+
"minified": 843,
15+
"gzipped": 471
1616
},
1717
"dist/jss-plugin-syntax-compose.esm.js": {
18-
"bundled": 1787,
19-
"minified": 668,
20-
"gzipped": 381,
18+
"bundled": 1794,
19+
"minified": 671,
20+
"gzipped": 385,
2121
"treeshaked": {
2222
"rollup": {
2323
"code": 16,

packages/jss-plugin-syntax-compose/src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ function registerClass(rule, className) {
6262
*/
6363
export default function jssCompose(): Plugin {
6464
function onProcessStyle(style, rule) {
65-
if (!style.composes) return style
65+
if (!('composes' in style)) return style
6666
registerClass(rule, style.composes)
6767
// Remove composes property to prevent infinite loop.
6868
delete style.composes

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import expect from 'expect.js'
44
import {create} from 'jss'
55
import compose from '.'
66

7-
const settings = {createGenerateClassName: () => rule => `${rule.key}-id`}
7+
const settings = {createGenerateId: () => rule => `${rule.key}-id`}
88

99
describe('jss-plugin-syntax-compose', () => {
1010
let jss

0 commit comments

Comments
 (0)