cssinjs / theming

Unified CSSinJS theming solution for React
300 stars 39 forks source link

Adding to context #12

Closed kentcdodds closed 5 years ago

kentcdodds commented 7 years ago

Right now, glamorous allows you to specify your own context:

const MyDiv = glamorous.div()
MyDiv.contextTypes = {/* blah */}

Normally, this would override the contextTypes needed for MyDiv to have access to the theme, but we do something like this:

const defaultContextTypes = {
  [CHANNEL]: PropTypes.object,
}

let userDefinedContextTypes = null

// configure the contextTypes to be settable by the user,
// however also retaining the glamorous channel.
Object.defineProperty(GlamorousComponent, 'contextTypes', {
  enumerable: true,
  configurable: true,
  set(value) {
    userDefinedContextTypes = value
  },
  get() {
    // if the user has provided a contextTypes definition,
    // merge the default context types with the provided ones.
    if (userDefinedContextTypes) {
      return {
        ...defaultContextTypes,
        ...userDefinedContextTypes,
      }
    }
    return defaultContextTypes
  },
})

It's a bit hacky and kinda "clever"

I wonder if this is something the withTheme component could support. It's a requirement for https://github.com/paypal/glamorous/issues/175 to be implemented without being a breaking change...

iamstarkov commented 7 years ago

whoa, let me take a look

robinweser commented 7 years ago

Fela uses context for theming and combines contextTypes as well, but using context for theming also has some drawbacks. If you're rendering style components inside pure components (e.g. Redux containers) and they do not update, your style components won't even know that perhaps the theme was changed from top-level :P

That's why I want to use this package primarily.

kentcdodds commented 7 years ago

@rofrischmann, so, to be clear, you're in support of a change like this one?

robinweser commented 7 years ago

Yes and No :P

I also need that change in order to prevent a breaking change, but I also think using context in general is not the best idea to pass the theming around.

mxstbr commented 7 years ago

Wait, don't glamorous and fela also use the event listener pattern where you attach an event listener on the first render to avoid pure components breaking updates?

robinweser commented 7 years ago

@mxstbr Nop, not yet. The current implementation uses React context to do that and works fine as long as you do not want to switch the global theme :P

kof commented 7 years ago

The current implementation uses React context to do that and works fine as long as you do not want to switch the global theme :P

I think you didn't read the implementation. It is using a pubsub channel exactly because of that.

robinweser commented 7 years ago

I was responsing to @mxstbr speaking about Fela, not this repo actually 😇

mxstbr commented 7 years ago

Interesting, alright!

kentcdodds commented 7 years ago

I'd really like to switch glamorous to use theming, but I need this to be implemented to make that happen. Can I get a :+1: or a :-1: on this issue?

kof commented 6 years ago

Lets move this forward, I am 👍 with whatever needed in order to have a unified theming solution!

kof commented 6 years ago

@vesparny can you help with adding this? You are already in the maintainers list.

kof commented 6 years ago

@mxstbr will this work for SC as well?

vesparny commented 6 years ago

@kof I am not sure I'll have time to look into it in the near future. If anybody else wants to take it please go ahead

HenriBeck commented 5 years ago

As this should no longer be a problem with v3 of this package, I'm closing this issue.