-
-
Notifications
You must be signed in to change notification settings - Fork 399
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #420 from cssinjs/dynamic-ss
implement sheet.update() #356
- Loading branch information
Showing
10 changed files
with
232 additions
and
3 deletions.
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
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
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,32 @@ | ||
/** | ||
* Extracts a styles object with only rules that contain function values. | ||
*/ | ||
export default (styles: Object): Object|null => { | ||
let fnValuesCounter = 0 | ||
|
||
// eslint-disable-next-line no-shadow | ||
function extract(styles: Object): Object { | ||
let to | ||
|
||
for (const key in styles) { | ||
const value = styles[key] | ||
const type = typeof value | ||
|
||
if (type === 'function') { | ||
if (!to) to = {} | ||
to[key] = value | ||
fnValuesCounter++ | ||
} | ||
else if (type === 'object' && value !== null && !Array.isArray(value)) { | ||
if (!to) to = {} | ||
const extracted = extract(value) | ||
if (extracted) to[key] = extracted | ||
} | ||
} | ||
|
||
return to | ||
} | ||
|
||
const extracted = extract(styles) | ||
return fnValuesCounter ? extracted : null | ||
} |
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,68 @@ | ||
import expect from 'expect.js' | ||
import getDynamicStyles from '../../src/utils/getDynamicStyles' | ||
|
||
describe('Unit: jss', () => { | ||
describe('getDynamicStyles', () => { | ||
it('should extract dynamic styles', () => { | ||
const color = data => data.color | ||
const styles = { | ||
button: { | ||
float: 'left', | ||
margin: [5, 10], | ||
color, | ||
'@media screen': { | ||
width: null, | ||
}, | ||
'@media print': { | ||
width: undefined, | ||
color | ||
}, | ||
'& a': { | ||
color: 'red', | ||
'& b': { | ||
color | ||
} | ||
}, | ||
}, | ||
'@media': { | ||
button: { | ||
width: 2, | ||
color | ||
} | ||
} | ||
} | ||
expect(getDynamicStyles(styles)).to.eql({ | ||
button: { | ||
color, | ||
'@media print': { | ||
color | ||
}, | ||
'& a': { | ||
'& b': { | ||
color | ||
} | ||
} | ||
}, | ||
'@media': { | ||
button: { | ||
color | ||
} | ||
} | ||
}) | ||
}) | ||
|
||
it('should return null if there are no function values', () => { | ||
const styles = { | ||
button: { | ||
float: 'left', | ||
}, | ||
'@media': { | ||
button: { | ||
width: 2 | ||
} | ||
} | ||
} | ||
expect(getDynamicStyles(styles)).to.be(null) | ||
}) | ||
}) | ||
}) |