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

Initial work to add createGlobalStyle functionality #1416

Closed

Conversation

JamieDixon
Copy link
Contributor

Implements #1333

This is very much a WIP and open for discussion

@JamieDixon JamieDixon added the WIP label Jan 9, 2018
@mxstbr-bot
Copy link

mxstbr-bot commented Jan 9, 2018

Warnings
⚠️

There are library changes, but not tests. That's OK as long as you're refactoring existing code

Generated by 🚫 dangerJS

@mxstbr
Copy link
Member

mxstbr commented Jan 9, 2018

Awesome!

In terms of removing styles, we need to add a new method to StyleSheet.js which removes tags based on component IDs. That should call tag.removeComponent(id), which we'd need to implement to essentially reverse the work done in tag.addComponent(id). Should be doable maybe?

@JamieDixon
Copy link
Contributor Author

I'll be looking into removeComponent today.

@kitten
Copy link
Member

kitten commented Jan 9, 2018

@mxstbr in a future (!) PR we should probably add interpolation support as well 😄

Edit: This could also use the BaseStyledComponent class (we could extract it) to save some space and to get the prop/interpolation/theme support immediately: https://github.com/styled-components/styled-components/blob/master/src/models/StyledComponent.js#L55

Copy link
Member

@kitten kitten left a comment

Choose a reason for hiding this comment

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

Some minor problems that we'll need to fix first 😉 Thanks for picking this up! 🙌

return class extends React.Component {
componentId = ''

componentDidMount() {
Copy link
Member

Choose a reason for hiding this comment

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

Here the component should use componentWillMount instead 😉 otherwise this wouldn't work correctly for SSR. It's also important as we'll want the global styles to apply immediately before all DOM nodes mount (that might be referred to in or affected by these global styles).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks! Adjusted in df87dd7

const rules = css(strings, ...interpolations)
const hash = hashStr(JSON.stringify(rules))
this.componentId = `sc-global-${hash}`
if (StyleSheet.instance.hasInjectedComponent(this.componentId)) return
Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sweet. Thanks for the tips. Adjusted in c6155ec

@mxstbr
Copy link
Member

mxstbr commented Jan 9, 2018

Damnnn df87dd7 🔥 🔥 🔥

Do you reckon we'll be able to use that for all styled components in the future? That'd be extremely rad.

@kitten
Copy link
Member

kitten commented Jan 9, 2018

@mxstbr I don't think we should remove any rules for styled components as that'd be unnecessary work. We don't care whether rules remain around or not since we control their classnames 😁

@JamieDixon
Copy link
Contributor Author

Is there a printWidth prop missing from .prettierrc? I think my Atom is mucking up your nice formatting.

@JamieDixon JamieDixon force-pushed the create-global-style branch 2 times, most recently from 34bb1f4 to 40ef36e Compare January 9, 2018 18:16
@kitten
Copy link
Member

kitten commented Jan 9, 2018

@JamieDixon that's definitely possible (I haven't had a look) but normally the default should be used. Maybe the Atom plugin falls back to its setting instead? That'd be horrible 😆 Feel free to add it to the .prettierrc if you don't mind

Edit: Also, can you remove the Ran yarn format commit please? The formatting should only be done by lint-staged and just pushing another commit that fixes formatting will also then show your commit as the last edit... not the end of the world of course, just a nit 😉

@JamieDixon
Copy link
Contributor Author

Thanks @philpl. I've dropped the Ran yarn format commit and added a new one updating the .prettierrc file. 👍

@JamieDixon
Copy link
Contributor Author

Is there more you'd like me to do on this one guys? I took a quick look into BaseStyledComponent but couldn't get it working. I'm happy to take another look if it's stopping this PR being approved?

Copy link
Member

@mxstbr mxstbr left a comment

Choose a reason for hiding this comment

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

Looks good to me code wise, can we undo the changelog.md reformatting? That breaks git blame, which can be vital sometimes.

I'd also like some unit tests, but I can look at that if you don't want to write those 😉 We'll also need to add documentation to the website and deprecate injectGlobal for the next major.

@JamieDixon
Copy link
Contributor Author

@mxstbr I've dropped the CHANGELOG commit and sent a new one with just the change we want. I need to figure out why prettier changed all the things.

@mxstbr
Copy link
Member

mxstbr commented Jan 15, 2018

Hold on, why is that changelog entry under 2.4.1 and not under unreleased? Pretty sure that won't be going out in 2.4.1?

@JamieDixon
Copy link
Contributor Author

Good point. Lemme fix that up.

@JamieDixon
Copy link
Contributor Author

Fixed

@kitten
Copy link
Member

kitten commented Feb 11, 2018

@JamieDixon work was picked up in #1493; Please take a look to make sure that it's still on track 🙌

@JamieDixon
Copy link
Contributor Author

@kitten Cool. I hope what I did here was still useful 😬

@kitten
Copy link
Member

kitten commented Feb 16, 2018

@JamieDixon Definitely! Thanks for kicking this work off 👍

@jarodtaylor
Copy link

This is sexy.

@quantizor
Copy link
Contributor

Closing in favor of #1493 since it's a continuation and evolution of this one 😸

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

6 participants