Skip to content
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

[jss] Simplify cloneStyle #1003

Merged
merged 5 commits into from
Jan 29, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions packages/jss/.size-snapshot.json
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
{
"dist/jss.js": {
"bundled": 59740,
"minified": 22374,
"gzipped": 6667
"bundled": 58181,
"minified": 21925,
"gzipped": 6523
},
"dist/jss.min.js": {
"bundled": 58601,
"minified": 21513,
"gzipped": 6230
"bundled": 57042,
"minified": 21067,
"gzipped": 6091
},
"dist/jss.cjs.js": {
"bundled": 54651,
"minified": 24090,
"gzipped": 6647
"bundled": 53766,
"minified": 23846,
"gzipped": 6579
},
"dist/jss.esm.js": {
"bundled": 54135,
"minified": 23671,
"gzipped": 6560,
"bundled": 53270,
"minified": 23442,
"gzipped": 6498,
"treeshaked": {
"rollup": {
"code": 19192,
"import_statements": 314
"code": 18995,
"import_statements": 281
},
"webpack": {
"code": 20665
"code": 20418
}
}
}
Expand Down
1 change: 0 additions & 1 deletion packages/jss/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
"dependencies": {
"@babel/runtime": "^7.0.0",
"is-in-browser": "^1.1.3",
"symbol-observable": "^1.2.0",
"tiny-warning": "^1.0.2"
}
}
39 changes: 5 additions & 34 deletions packages/jss/src/utils/cloneStyle.js
Original file line number Diff line number Diff line change
@@ -1,44 +1,15 @@
import $$observable from 'symbol-observable'
import type {JssStyle} from '../types'

const {isArray} = Array

// TODO: This should propably not be here, need to find a better place
const isObservable = value => value && value[$$observable] && value === value[$$observable]()
const plainObjectConstrurctor = {}.constructor

export default function cloneStyle(style: JssStyle): JssStyle {
// Support empty values in case user ends up with them by accident.
if (style == null) return style

// Support string value for SimpleRule.
const typeOfStyle = typeof style

if (typeOfStyle === 'string' || typeOfStyle === 'number' || typeOfStyle === 'function') {
return style
}

// It is a CSSTOM value.
// TODO will not work if instance comes from a different window.
if (global.CSSStyleValue && style instanceof global.CSSStyleValue) {
return style
}

// Support array for FontFaceRule.
if (isArray(style)) return style.map(cloneStyle)

// Support Observable styles. Observables are immutable, so we don't need to
// copy them.
if (isObservable(style)) return style
if (style == null || typeof style !== 'object') return style
if (Array.isArray(style)) return style.map(cloneStyle)
if (style.constructor !== plainObjectConstrurctor) return style

const newStyle = {}
for (const name in style) {
const value = style[name]
if (typeof value === 'object') {
newStyle[name] = cloneStyle(value)
continue
}
newStyle[name] = value
newStyle[name] = cloneStyle(style[name])
}

return newStyle
}