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

[react-jss] Merge classes instead of overwriting #946

Merged
merged 7 commits into from
Dec 31, 2018

Conversation

HenriBeck
Copy link
Member

What would you like to add/fix?
This adds support for merging classes instead of overriding them.
Currently, it is not supported to override classes by the syntax in #993.
We would need to do more work to not compute unless there is a change.

Corresponding issue (if exists): #933

@HenriBeck HenriBeck changed the base branch from react-jss/update to babel-plugin December 23, 2018 23:17
@HenriBeck HenriBeck changed the base branch from babel-plugin to react-jss/update December 23, 2018 23:17
@HenriBeck HenriBeck changed the title React jss/merge classes [react-jss] Merge classes instead of overwriting Dec 23, 2018
@HenriBeck HenriBeck requested a review from kof December 23, 2018 23:19
@@ -3,9 +3,10 @@
import React, {Component, type ComponentType} from 'react'
import PropTypes from 'prop-types'
import {ThemeContext} from 'theming'
import {getDynamicStyles, SheetsManager, type StyleSheet, type Classes} from 'jss'
import {getDynamicStyles, SheetsManager, type StyleSheet} from 'jss'
import memoize from 'memoize-one'
Copy link
Member

Choose a reason for hiding this comment

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

do we need a lib for this? I have a feeling that function is 2-3LOC and easier to inline than to have a dependency

* This merges to classes object.
* When a property exists in both classes, it will not overwrite but merge the two classes.
*
* @param {Object} baseClasses - The base classes.
Copy link
Member

Choose a reason for hiding this comment

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

we should drop jsdoc anotations since we use flow already, maintaining both is bad, because it will get out of sync without validation

@HenriBeck
Copy link
Member Author

HenriBeck commented Dec 27, 2018

Okay, the different scenarios would be the following:

  1. No props classes and Default classes
  2. Props classes and No default classes
  3. Props classes and default classes

One option would be to shallow merge the default classes with the props classes and then apply the merging of class names with the stylesheet classes.

Another way would be only to use default props classes when there are no props classes.
I'm not even sure why you would want to have default classes anyway

@kof what do you think?

@kof
Copy link
Member

kof commented Dec 31, 2018

You know what? Lets do as you wanted - only shallow merge the defaultProps.classes. If component author really wants a deep merge, they can still write a utility function and do it, like we do in react-jss hoc. If we don't deeply merge it, they have at least a choice to decide how they want it.

Deeply merging defaultProps.classes will have as a result those classes always added to the "class" attribute, which will increase SSR payload. Evtl. component author wants to control that behaviour.

@@ -0,0 +1,17 @@
// @flow

function memoize<Arg, Return>(fn: (arg: Arg) => Return) {
Copy link
Member

Choose a reason for hiding this comment

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

btw why do you like named functions so much? by the latest spec a function expression assigned to a variable has a function name as well, derived from the variable name. So there is no problem with debugging.

I don't like named functions because of the implicit hoisting, its a generally leaky concept.

Copy link
Member

Choose a reason for hiding this comment

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

its like var and even worse, no one is using var any more and named functions are even worse, because they can be called before they are defined and once people start relying on this, code gets messy.

Copy link
Member Author

Choose a reason for hiding this comment

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

Personally, it makes it easier to differentiate between variables and I'm always using eslint or tslint where I disallow to not use something before it's not defined.

Henri Beck added 2 commits December 31, 2018 13:41
* react-jss/update: (42 commits)
  Update jss-preset-default.md (#959)
  Update projects.md (#960)
  [core] Use tiny warning (#953)
  Fix peer deps warnings (#957)
  Docs (#952)
  Docs (#951)
  add snapshots check to pre-commit hook
  Docs (#950)
  Add migrations.md (#947)
  update snapshot
  v10.0.0-alpha.2
  Update changelog.md
  v10.0.0-alpha.1
  use jss as a name in root package.json
  remove abstracitons.md
  docs
  [WIP] Docs (#929)
  fix headings
  make flow happy
  fix tests, move regex to the top, fix global.CSS accesss
  ...

# Conflicts:
#	karma.conf.js
#	packages/react-jss/src/compose.js
@HenriBeck HenriBeck merged commit 70a05ad into react-jss/update Dec 31, 2018
@HenriBeck HenriBeck deleted the react-jss/merge-classes branch December 31, 2018 12:48
kof added a commit that referenced this pull request Jan 16, 2019
* [WIP] Update to new react context for the jss context (#924)

* Update to new react context for the jss context

* Fixed types

* Fix some tests

* Remove unnecessary cloning

* Fix JssProvider

* Update logic of merging context

* Fix a few issues and tests

* Make getTheme non public

* Update warning inside JssProvider

* Remove duplicate beforeEach

* Update size-snapshot

* Fix a few bugs

* Created a Managers type

* Create sheetOptions prop type definition

* Removed meta property from sheet options shape

* Fix creating a new generateId for every render

* Move media into it's own prop

* Added changelog

* Rename to JssContextSubscriber

* Moved context to jssContext prop instead of spreading it

* [react-jss] Remove inject option (#934)

* Update to new react context for the jss context

* Fixed types

* Fix some tests

* Remove unnecessary cloning

* Fix JssProvider

* Update logic of merging context

* Fix a few issues and tests

* Make getTheme non public

* Update warning inside JssProvider

* Remove duplicate beforeEach

* Update size-snapshot

* Fix a few bugs

* Remove inject option

* Fix

* Always inject the theme

* Only inject theme when injectTheme is true

* Upgrade theming package (#942)

* Add forwardRef support (#943)

* [react-jss] Merge classes instead of overwriting (#946)

* Add forwardRef support

* Add support for merging the classes

* Fix path of test file

* Remove jsdoc comment

* Implement custom memoize-one

* Convert function to arrow function

* Fix

* Update some of the tests

* Update dynamic styles tests

* Fix SSR tests

* Add theme inject tests

* Add merge classes tests

* Remove dependency of react-dom

* Fix an issue with memoizing and memoize the context

* Remove createHoC file and remove injectSheet to withStyles

* Update TS types

* Update TS types

* Fix flow types

* Fix react-jss types

* Rename injectSheet to withStyles in docs

* Fix react-jss types

* Update changelog.md

* Rename class and

* Fix tests

* Fix a few typos in docs

* Fix changelog

* Use javascript for codeblocks instead of js

* Upgrade theming

* Fix type import

* Update size-snapshot

* Fix docs

* Update size-snapshot

* Upgrade theming to v3.0.2

* Use travis_wait for test command

* Remove travis_wait command

* Update docs
bhupinderbola pushed a commit to bhupinderbola/jss that referenced this pull request Sep 17, 2019
* [WIP] Update to new react context for the jss context (cssinjs#924)

* Update to new react context for the jss context

* Fixed types

* Fix some tests

* Remove unnecessary cloning

* Fix JssProvider

* Update logic of merging context

* Fix a few issues and tests

* Make getTheme non public

* Update warning inside JssProvider

* Remove duplicate beforeEach

* Update size-snapshot

* Fix a few bugs

* Created a Managers type

* Create sheetOptions prop type definition

* Removed meta property from sheet options shape

* Fix creating a new generateId for every render

* Move media into it's own prop

* Added changelog

* Rename to JssContextSubscriber

* Moved context to jssContext prop instead of spreading it

* [react-jss] Remove inject option (cssinjs#934)

* Update to new react context for the jss context

* Fixed types

* Fix some tests

* Remove unnecessary cloning

* Fix JssProvider

* Update logic of merging context

* Fix a few issues and tests

* Make getTheme non public

* Update warning inside JssProvider

* Remove duplicate beforeEach

* Update size-snapshot

* Fix a few bugs

* Remove inject option

* Fix

* Always inject the theme

* Only inject theme when injectTheme is true

* Upgrade theming package (cssinjs#942)

* Add forwardRef support (cssinjs#943)

* [react-jss] Merge classes instead of overwriting (cssinjs#946)

* Add forwardRef support

* Add support for merging the classes

* Fix path of test file

* Remove jsdoc comment

* Implement custom memoize-one

* Convert function to arrow function

* Fix

* Update some of the tests

* Update dynamic styles tests

* Fix SSR tests

* Add theme inject tests

* Add merge classes tests

* Remove dependency of react-dom

* Fix an issue with memoizing and memoize the context

* Remove createHoC file and remove injectSheet to withStyles

* Update TS types

* Update TS types

* Fix flow types

* Fix react-jss types

* Rename injectSheet to withStyles in docs

* Fix react-jss types

* Update changelog.md

* Rename class and

* Fix tests

* Fix a few typos in docs

* Fix changelog

* Use javascript for codeblocks instead of js

* Upgrade theming

* Fix type import

* Update size-snapshot

* Fix docs

* Update size-snapshot

* Upgrade theming to v3.0.2

* Use travis_wait for test command

* Remove travis_wait command

* Update docs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants