Skip to content

Commit bc898d0

Browse files
[default-unit] Allow options to pass function transforms in (#1292)
* [default-unit] Allow options to pass function transforms in * [default-unit] Add function option example to docs
1 parent d5f0bc3 commit bc898d0

8 files changed

Lines changed: 46 additions & 46 deletions

File tree

docs/jss-plugin-default-unit.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ import defaultUnit from 'jss-plugin-default-unit'
3232

3333
const options = {
3434
'line-height': 'rem',
35-
'font-size': 'rem'
35+
'font-size': 'rem',
36+
width: val => `${val / 2}px`
3637
}
3738

3839
jss.use(defaultUnit(options))
@@ -42,6 +43,7 @@ const styles = {
4243
'line-height': 3,
4344
'font-size': 1.7,
4445
height: 200,
46+
width: 200,
4547
'z-index': 1
4648
}
4749
}
@@ -54,6 +56,7 @@ Compiles to:
5456
line-height: 3rem;
5557
font-size: 1.7rem;
5658
height: 200px;
59+
width: 100px;
5760
z-index: 1;
5861
}
5962
```

packages/css-jss/.size-snapshot.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
{
22
"dist/css-jss.js": {
3-
"bundled": 58732,
4-
"minified": 20779,
5-
"gzipped": 7027
3+
"bundled": 58673,
4+
"minified": 20770,
5+
"gzipped": 7025
66
},
77
"dist/css-jss.min.js": {
8-
"bundled": 57681,
9-
"minified": 20185,
8+
"bundled": 57622,
9+
"minified": 20176,
1010
"gzipped": 6756
1111
},
1212
"dist/css-jss.cjs.js": {

packages/jss-plugin-default-unit/.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-default-unit.js": {
3-
"bundled": 5701,
4-
"minified": 2736,
5-
"gzipped": 1054
3+
"bundled": 5642,
4+
"minified": 2727,
5+
"gzipped": 1055
66
},
77
"dist/jss-plugin-default-unit.min.js": {
8-
"bundled": 5701,
9-
"minified": 2736,
10-
"gzipped": 1054
8+
"bundled": 5642,
9+
"minified": 2727,
10+
"gzipped": 1055
1111
},
1212
"dist/jss-plugin-default-unit.cjs.js": {
13-
"bundled": 5003,
14-
"minified": 2837,
15-
"gzipped": 1019
13+
"bundled": 4948,
14+
"minified": 2816,
15+
"gzipped": 1021
1616
},
1717
"dist/jss-plugin-default-unit.esm.js": {
18-
"bundled": 4923,
19-
"minified": 2771,
20-
"gzipped": 963,
18+
"bundled": 4868,
19+
"minified": 2750,
20+
"gzipped": 965,
2121
"treeshaked": {
2222
"rollup": {
2323
"code": 1865,

packages/jss-plugin-default-unit/src/index.js

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import type {Plugin} from 'jss'
33
import defaultUnits from './defaultUnits'
44

5-
export type Options = {[key: string]: string}
5+
export type Options = {[key: string]: string | ((val: number) => string)}
66

77
/**
88
* Clones the object and adds a camel cased property version.
@@ -41,14 +41,10 @@ function iterate(prop: string, value: any, options: Options) {
4141
}
4242
}
4343
} else if (typeof value === 'number') {
44-
if (options[prop]) {
45-
return `${value}${options[prop]}`
46-
}
44+
const unit = options[prop] || units[prop]
4745

48-
if (units[prop]) {
49-
return typeof units[prop] === 'function'
50-
? units[prop](value).toString()
51-
: `${value}${units[prop]}`
46+
if (unit) {
47+
return typeof unit === 'function' ? unit(value).toString() : `${value}${unit}`
5248
}
5349

5450
return value.toString()

packages/jss-plugin-default-unit/src/index.test.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ describe('jss-plugin-default-unit', () => {
1616
let jss
1717

1818
beforeEach(() => {
19-
jss = create(settings).use(defaultUnit({'min-width': 'pc'}))
19+
jss = create(settings).use(defaultUnit({'min-width': 'pc', 'max-width': val => `${val / 2}px`}))
2020
})
2121

2222
describe('unitless values', () => {
@@ -195,7 +195,8 @@ describe('jss-plugin-default-unit', () => {
195195
beforeEach(() => {
196196
sheet = jss.createStyleSheet({
197197
a: {
198-
'min-width': 20
198+
'min-width': 20,
199+
'max-width': 20
199200
}
200201
})
201202
})
@@ -205,7 +206,7 @@ describe('jss-plugin-default-unit', () => {
205206
})
206207

207208
it('should generate correct CSS', () => {
208-
expect(sheet.toString()).to.be('.a-id {\n min-width: 20pc;\n}')
209+
expect(sheet.toString()).to.be('.a-id {\n min-width: 20pc;\n max-width: 10px;\n}')
209210
})
210211
})
211212

packages/jss-preset-default/.size-snapshot.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
{
22
"dist/jss-preset-default.js": {
3-
"bundled": 55976,
4-
"minified": 20013,
5-
"gzipped": 6678
3+
"bundled": 55917,
4+
"minified": 20004,
5+
"gzipped": 6675
66
},
77
"dist/jss-preset-default.min.js": {
8-
"bundled": 54925,
9-
"minified": 19419,
8+
"bundled": 54866,
9+
"minified": 19410,
1010
"gzipped": 6405
1111
},
1212
"dist/jss-preset-default.cjs.js": {

packages/jss-starter-kit/.size-snapshot.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
{
22
"dist/jss-starter-kit.js": {
3-
"bundled": 71518,
4-
"minified": 30055,
5-
"gzipped": 9301
3+
"bundled": 71459,
4+
"minified": 30046,
5+
"gzipped": 9300
66
},
77
"dist/jss-starter-kit.min.js": {
8-
"bundled": 70467,
9-
"minified": 29459,
10-
"gzipped": 9031
8+
"bundled": 70408,
9+
"minified": 29450,
10+
"gzipped": 9033
1111
},
1212
"dist/jss-starter-kit.cjs.js": {
1313
"bundled": 2592,

packages/react-jss/.size-snapshot.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
{
22
"dist/react-jss.js": {
3-
"bundled": 169027,
4-
"minified": 58353,
5-
"gzipped": 19101
3+
"bundled": 168968,
4+
"minified": 58341,
5+
"gzipped": 19097
66
},
77
"dist/react-jss.min.js": {
8-
"bundled": 112062,
9-
"minified": 41612,
10-
"gzipped": 14132
8+
"bundled": 112003,
9+
"minified": 41600,
10+
"gzipped": 14133
1111
},
1212
"dist/react-jss.cjs.js": {
1313
"bundled": 25875,

0 commit comments

Comments
 (0)