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

feat: don't force plain themes #1780

Merged
merged 4 commits into from
Jun 4, 2018
Merged

feat: don't force plain themes #1780

merged 4 commits into from
Jun 4, 2018

Conversation

phyllisstein
Copy link
Member

is-plain-object is still required as an additional dependency, in order to guarantee that objects within rulesets are toStringed.

Fixes #1776.

@@ -117,7 +114,7 @@ class ThemeProvider extends Component<ThemeProviderProps, void> {
}
return mergedTheme
}
if (!isPlainObject(theme)) {
if (!isObject(theme)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Looks good. Could you add a test that simulates what webpack does to the best of your ability so we know it'll work?

@quantizor
Copy link
Contributor

@phyllisstein try merging from master - I turned off danger for the moment. Please do add a CHANGELOG entry though 😃

@kitten
Copy link
Member

kitten commented Jun 1, 2018

I think we should use this opportunity to get rid of such a small dependency that will likely never change. I think it’d make sense to check for:

  • not equal null
  • not an array
  • is type of object

Without taking on a small dependency. I’m not even sure if we still need this constraint even?

@phyllisstein
Copy link
Member Author

phyllisstein commented Jun 1, 2018

Since this test just passed, I take it SC can pretty well handle non-plain objects:

  it('should [not] allow other complex objects to be passed as themes', () => {
    class Theme {
      constructor(borderRadius) {
        this.borderRadius = borderRadius
      }
    }

    const theme = new Theme('2px')

    const Comp1 = styled.div`
      border-radius: ${ ({ theme }) => theme.borderRadius };
    `

    const wrapper = mount(
      <ThemeProvider theme={ theme }>
        <Comp1 />
      </ThemeProvider>
    )

    expectCSSMatches(`.sc-a {} .b {border-radius:${theme.borderRadius};}`)
  })

I'd be happy to remove the check---and the dependency---altogether. If you want to keep it, though, isobject isn't the right module.

@quantizor
Copy link
Contributor

If what we have already is enough, let's drop the dep.

@phyllisstein
Copy link
Member Author

Consider it done! With a few extra tests to boot.

@quantizor
Copy link
Contributor

@phyllisstein great! could you please merge from master to fix CI and add a changelog entry?

@phyllisstein
Copy link
Member Author

With pleasure. Should be ready to go once tests pass.

@phyllisstein
Copy link
Member Author

Looks like there were some Flow issues that I'll work on correcting, but also a problem with the bundle size check that I'm less sure how to resolve.

@quantizor
Copy link
Contributor

quantizor commented Jun 2, 2018

Heh, Flow is doing its job and preventing you from making the mistakes! Since these tests are intentional, you can put a // $FlowInvalidInputTest above the lines in question to disable Flow for that particular case.

@phyllisstein
Copy link
Member Author

Thanks! With that added and a small bump to the maximum bundle size I think I'm set.

@@ -180,7 +180,7 @@
"bundlesize": [
{
"path": "./dist/styled-components.min.js",
"maxSize": "16kB"
"maxSize": "16.5kB"
Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm why'd we have to bump this? Aren't we using less code now?

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 check was failing pretty consistently, but I'm not sure where the extra code came from. I guess the typecheck @kitten suggested ((mergedTheme === null || Array.isArray(mergedTheme) || typeof mergedTheme !== 'object')) is slightly longer than the original isPlainObject(mergedTheme), but I'm not sure if that would've been sufficient to break the test.

@@ -1,7 +1,6 @@
// @flow
import React, { Component, type Element } from 'react'
import PropTypes from 'prop-types'
import isPlainObject from 'is-plain-object'
Copy link
Contributor

Choose a reason for hiding this comment

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

Can this dependency be removed entirely now?

Copy link
Member Author

Choose a reason for hiding this comment

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

It's also used in utils/flatten.js to determine whether to try flattening the object or to just call toString on it. I think in that case it's still necessary that the object be "plain."

Copy link
Contributor

@quantizor quantizor left a comment

Choose a reason for hiding this comment

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

Thank you!

@quantizor quantizor merged commit ef5a3cd into styled-components:master Jun 4, 2018
@mxstbr
Copy link
Member

mxstbr commented Jun 4, 2018

Thank you so much for helping us improve styled-components! Based on our Community Guidelines every person that has a PR of any kind merged is offered an invitation to the styled-components organization—that is you right now!

Should you accept, you'll get write access to the main repository. (and a fancy styled-components logo on your GitHub profile!) You'll be able to label and close issues, merge pull requests, create new branches, etc. We want you to help us out with those things, so don't be scared to do them! Make sure to read our contribution and community guidelines, which explains all of this in more detail. Of course, if you have any questions just let me know!

@quantizor
Copy link
Contributor

Released in https://github.com/styled-components/styled-components/releases/tag/v3.3.2!

@phyllisstein
Copy link
Member Author

Neat-o! Thanks for all your help!

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

4 participants