cssinjs / theming

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

Combine themes with createTheme #49

Closed jrdn91 closed 5 years ago

jrdn91 commented 6 years ago

I have created a new themeable wrapper for my components but I would like to extend the Material-UI theme so I can have access to it's properties like palette and spacing

import injectSheet, { createTheming } from 'react-jss';

export const theming = createTheming('__EVENT_GIVES_THEME__');
export const { ThemeProvider: Themable } = theming;

export default (styles) => injectSheet(styles, { theming });

This is my current setup. All components are rendered within an app layout like so

<Provider store={store}>
  <MuiThemeProvider theme={theme}>
    <div>
      <Reboot />
      <AppRouter />
    </div>
  </MuiThemeProvider>
</Provider>

But I guess because of the new theme I created it's not inheriting any of this?

An example of how I'm using this if it helps understand is I have three files with each component, a wrapper that applies the theme and styles, a component file with the actual component and then a styles file where I put all my classes. Here is what one looks like

QuantityInput.styles.js

import { merge } from 'lodash';

const styles = theme => {
  return merge({
    quantityInput: {
      color: theme.palette.primary,
      fontWeight: '400',
      fontSize: '13px',
      display: 'inline-block',
      textAlign: 'center',
      top: '10px',
      float: 'left',
      width: '35px',
      position: 'relative'
    }
  }, theme);
};

export default styles;
**QuantityInput.jsx**

import React from 'react';
import QuantityInputComponent from './QuantityInput.component';

import { Themable } from '../Themable';

const QuantityInput = ({ theme, ...rest }) => (
  <Themable theme={theme || {}}>
    <QuantityInputComponent {...rest} />
  </Themable>
);

export default QuantityInput;

QuantityInput.component.jsx

import React from 'react';
import styles from "./QuantityInput.styles";
import IconButton from 'material-ui/IconButton';
import RemoveIcon from 'material-ui-icons/Remove';
import AddIcon from 'material-ui-icons/Add';

import themableWrapper from '../Themable';

const QuantityInputComponent = (props) => (
  <div className="quantity-selector">
    <IconButton aria-label="Remove">
      <RemoveIcon />
    </IconButton>
    <span className="quantity-wrap">
      <input name="quantity" type="number" className="quantity" />
    </span>
    <IconButton aria-label="Add">
      <AddIcon />
    </IconButton>
  </div>
);

export default themableWrapper(styles)(QuantityInputComponent);
HenriBeck commented 6 years ago

I think the problem is that you create two different channels for the theming and that's why it doesn't work. I think Material-UI is using their own channel and you are creating your own channel in this line:

export const theming = createTheming('__EVENT_GIVES_THEME__');

This means that Material-UI can't see your theme and you can't see the MUI Theme.

Material-UI uses the channel name __THEMING__ as seen here.