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

Remove getDynamicStyles from core #1000

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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-plugin-rule-value-function/.size-snapshot.json
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
{
"dist/jss-plugin-rule-value-function.js": {
"bundled": 2025,
"minified": 729,
"gzipped": 424
"bundled": 4674,
"minified": 1379,
"gzipped": 651
},
"dist/jss-plugin-rule-value-function.min.js": {
"bundled": 2025,
"minified": 729,
"gzipped": 424
"bundled": 4674,
"minified": 1379,
"gzipped": 651
},
"dist/jss-plugin-rule-value-function.cjs.js": {
"bundled": 1642,
"minified": 631,
"gzipped": 366
"bundled": 2317,
"minified": 1021,
"gzipped": 510
},
"dist/jss-plugin-rule-value-function.esm.js": {
"bundled": 1564,
"minified": 567,
"gzipped": 319,
"bundled": 2178,
"minified": 896,
"gzipped": 455,
"treeshaked": {
"rollup": {
"code": 12,
"import_statements": 12
"code": 53,
"import_statements": 53
},
"webpack": {
"code": 1017
"code": 1091
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion packages/jss-plugin-rule-value-function/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
},
"dependencies": {
"@babel/runtime": "^7.0.0",
"jss": "10.0.0-alpha.9"
"jss": "10.0.0-alpha.9",
"jss-plugin-rule-value-observable": "10.0.0-alpha.9"
}
}
29 changes: 29 additions & 0 deletions packages/jss-plugin-rule-value-function/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
type StyleRule,
type StyleSheet
} from 'jss'
import {isObservable} from 'jss-plugin-rule-value-observable'

// A symbol replacement.
let now = Date.now()
Expand All @@ -16,6 +17,34 @@ const fnRuleNs = `fnStyle${++now}`

type StyleRuleWithRuleFunction = StyleRule & {[key: string]: Function}

function getFunctionStyles(styles: Object): Object | null {
let to = null

for (const key in styles) {
const value = styles[key]
const type = typeof value

if (isObservable(value)) {
HenriBeck marked this conversation as resolved.
Show resolved Hide resolved
continue
}

if (type === 'function') {
if (!to) to = {}
to[key] = value
} else if (type === 'object' && value !== null && !Array.isArray(value)) {
HenriBeck marked this conversation as resolved.
Show resolved Hide resolved
const extracted = getFunctionStyles(value)
if (extracted) {
if (!to) to = {}
to[key] = extracted
}
}
}

return to
}

export {getFunctionStyles}

export default function functionPlugin() {
return {
onCreateRule(name?: string, decl: JssStyle, options: RuleOptions): Rule | null {
Expand Down
24 changes: 12 additions & 12 deletions packages/jss-plugin-rule-value-observable/.size-snapshot.json
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
{
"dist/jss-plugin-rule-value-observable.js": {
"bundled": 3531,
"minified": 1101,
"gzipped": 533
"bundled": 4323,
"minified": 1323,
"gzipped": 627
},
"dist/jss-plugin-rule-value-observable.min.js": {
"bundled": 3531,
"minified": 1101,
"gzipped": 533
"bundled": 4323,
"minified": 1323,
"gzipped": 627
},
"dist/jss-plugin-rule-value-observable.cjs.js": {
"bundled": 1709,
"minified": 754,
"gzipped": 399
"bundled": 2261,
"minified": 1065,
"gzipped": 504
},
"dist/jss-plugin-rule-value-observable.esm.js": {
"bundled": 1495,
"minified": 591,
"gzipped": 318,
"bundled": 2005,
"minified": 861,
"gzipped": 427,
"treeshaked": {
"rollup": {
"code": 38,
Expand Down
26 changes: 25 additions & 1 deletion packages/jss-plugin-rule-value-observable/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,34 @@ import {
} from 'jss'
import type {Observable} from './types'

const isObservable = value => value && value[$$observable] && value === value[$$observable]()
const isObservable = (value: any) => value && value[$$observable] && value === value[$$observable]()

export type Options = UpdateOptions

function getObservableStyles(styles: Object): Object | null {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we are not using this anywhere, why having it?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We will need it for proper integration into react-jss, but I will make that in a different PR.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What integration are you talking about? mb we should do it differently in that case.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Proper observables integration into react-jss

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But I'm also not sure If we actually need observable support in react-jss.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we end up having a function that knows about both, functions and observables, it shouldn't be in one of those plugins, but somewhere else. Duplicating that function in both is also not very nice, it might need some adjustments in the future and having it twice doesn't make it easier to maintain.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But why do we need to have a function which knows about both?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that these function should live in their respective plugin packages and not in the core.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that core is not the ideal place for them, but I want to keep performance-first constraint. If we need to loop over styles twice just for the sake of functions living in separate packages, it is not quite performance-first.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only place we are using getDynamicStyles at the moment is in react-jss. And without #1002 there will be no performance loose.

let to = null

for (const key in styles) {
const value = styles[key]
const type = typeof value

if (isObservable(value)) {
if (!to) to = {}
to[key] = value
} else if (type === 'object' && value !== null && !Array.isArray(value)) {
const extracted = getObservableStyles(value)
if (extracted) {
if (!to) to = {}
to[key] = extracted
}
}
}

return to
}

export {isObservable, getObservableStyles}

export default function observablePlugin(updateOptions?: Options) {
return {
onCreateRule(name?: string, decl: JssStyle, options: RuleOptions): Rule | null {
Expand Down
6 changes: 3 additions & 3 deletions packages/jss-preset-default/.size-snapshot.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"dist/jss-preset-default.js": {
"bundled": 65798,
"bundled": 85008,
"minified": 22853,
"gzipped": 6771
},
"dist/jss-preset-default.min.js": {
"bundled": 65044,
"bundled": 84064,
"minified": 22390,
"gzipped": 6561
"gzipped": 6557
},
"dist/jss-preset-default.cjs.js": {
"bundled": 1329,
Expand Down
8 changes: 4 additions & 4 deletions packages/jss-starter-kit/.size-snapshot.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"dist/jss-starter-kit.js": {
"bundled": 81048,
"bundled": 104848,
"minified": 32915,
"gzipped": 9378
"gzipped": 9368
},
"dist/jss-starter-kit.min.js": {
"bundled": 80294,
"bundled": 103904,
"minified": 32437,
"gzipped": 9166
"gzipped": 9155
},
"dist/jss-starter-kit.cjs.js": {
"bundled": 2341,
Expand Down
26 changes: 13 additions & 13 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": 59110,
"minified": 22158,
"gzipped": 6580
},
"dist/jss.min.js": {
"bundled": 58601,
"minified": 21513,
"gzipped": 6230
"bundled": 57971,
"minified": 21297,
"gzipped": 6147
},
"dist/jss.cjs.js": {
"bundled": 54651,
"minified": 24090,
"gzipped": 6647
"bundled": 54065,
"minified": 23822,
"gzipped": 6562
},
"dist/jss.esm.js": {
"bundled": 54135,
"minified": 23671,
"gzipped": 6560,
"bundled": 53576,
"minified": 23428,
"gzipped": 6477,
"treeshaked": {
"rollup": {
"code": 19192,
"import_statements": 314
},
"webpack": {
"code": 20665
"code": 20500
}
}
}
Expand Down
1 change: 0 additions & 1 deletion packages/jss/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,6 @@ export function create(options?: Partial<JssOptions>): Jss
export function createGenerateId(): GenerateId
export function createRule(name: string, decl: JssStyle, options: RuleOptions): Rule
export function toCssValue(value: JssValue, ignoreImportant: boolean): string
export function getDynamicStyles(styles: Styles): Styles | null
declare const jss: Jss

export default jss
5 changes: 0 additions & 5 deletions packages/jss/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,6 @@ export type {
*/
export const hasCSSTOMSupport = typeof CSS !== 'undefined' && CSS && 'number' in CSS

/**
* Extracts a styles object with only rules that contain function values.
*/
export {default as getDynamicStyles} from './utils/getDynamicStyles'

/**
* Converts JSS array value to a CSS string.
*/
Expand Down
24 changes: 0 additions & 24 deletions packages/jss/src/utils/getDynamicStyles.js

This file was deleted.

74 changes: 0 additions & 74 deletions packages/jss/tests/unit/getDynamicStyles.js

This file was deleted.

Loading