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

Theming improvement #1662

Merged
merged 10 commits into from
Sep 23, 2015
Merged

Theming improvement #1662

merged 10 commits into from
Sep 23, 2015

Conversation

shaurya947
Copy link
Contributor

Re-doing themes for version 0.12:

  • Material-UI components can now be used without having to worry about theming. All components implement a default theme (light theme).
  • Theme is passed down using context. Components use default theme iff an inherited theme is not found in context.
  • There are now two "kinds" of themes: raw theme and material-ui theme. The raw theme is just a plain JS object defined in a file. It contains three keys, namely spacing, palette and fontFamily. The material-ui theme is a much bigger object, and is produced based on the raw theme. It contains a key for every material-ui component, and the corresponding value describes how that component is going to look under that particular theme. The material-ui theme also contains a reference to the raw theme object from which it was produced.
  • The ThemeManager module has been modified. Now, it contains only the following functions that return a material-ui (mui) theme object:
    • getMuiTheme (rawTheme): this function takes in a raw theme object and returns the mui theme produced from it.
    • modifyRawThemeSpacing (muiTheme, newSpacing), modifyRawThemePalette (muiTheme, newPaletteKeys) and modifyRawThemeFontFamily (muiTheme, newFontFamily): these three functions are provided as modifiers for the raw theme. They take in the current mui theme object and the new information to be updated, and return a new, re-computed mui theme object. Providing these modifiers separately is an effort to keep the mui theme object immutable throughout the application.
  • Inside a component, theme is part of state and should always be accessed using this.state.muiTheme. Reset the theme using this.setState(). This is consistent with the immutability approach as react components re-render whenever the state changes.
  • Individual properties of the mui theme object may be overridden once the mui theme object has been calculated and obtained from the ThemeManager. For instance:
//initialize the state of your app component with an mui theme object
//based on the desired raw theme object
getInitialState () {
    return {
      muiTheme: ThemeManager.getMuiTheme(DefaultRawTheme),
    };
},

//componentWillMount() is a good place to override specific theme attributes
//before your app component is rendered and the context is passed down
componentWillMount() {
    let newMuiTheme = this.state.muiTheme;
    newMuiTheme.inkBar.backgroundColor = Colors.yellow200;
    this.setState({
      muiTheme: newMuiTheme,
    });
},

//pass on this updated theme to children (material-ui components
//that you use in your app)
childContextTypes : {
    muiTheme: React.PropTypes.object
},

  getChildContext() {
    return {
      muiTheme: this.state.muiTheme,
    };
},
  • Two raw themes are provided under lib/styles/raw-themes. Custom raw themes may be defined in a similar manner.
  • All Material-UI components implement the following React lifecycle methods / static key:value pairs to support theming:
    • getInitialState(): the context is checked for a theme object here. If no theme is found, a default theme is applied.
    • contextTypes: to indicate that an muiTheme object is expected from the parent.
    • childContextTypes: to indicate that an muiTheme object will be passed down to children.
    • getChildContext(): to pass down the muiTheme object to the children.
    • componentWillReceiveProps(nextProps, nextContext): every time a new theme is passed down through the context, this method updates the state of the component with the new theme.
  • To implement custom theming in an application that uses material-ui components, either a subset of the above mentioned React lifecycle methods / static key:value pairs can be implemented to pass on the theme, OR, the ThemeDecorator module can be used to decorate (ES7-style decorators) the app component. These are the two ways that we recommend that you go about theming for now.
    • If using ES7-style decorators, you must declare your React component using the ES6 class syntax; that is, you must define a class using the class keyword that extends React.Component (as opposed to using React.createClass).

@shaurya947
Copy link
Contributor Author

Working on it @oliviertassinari .

@oliviertassinari
Copy link
Member

@shaurya947 I can see this :p. You don't need to close the PR. A commit --amend or rebase -i should be enough.

@shaurya947
Copy link
Contributor Author

@oliviertassinari I ran into some issues initially and I ranted about those on SO. Due to the process I had to follow (which was essentially a rebase but involved merging my master with callemall:master first), the other commits probably showed up. So I'm trying to reset, cherry-pick the commits, and then rebase.

Still getting used to troubleshooting git issues ^_^

@shaurya947 shaurya947 reopened this Sep 21, 2015
@shaurya947
Copy link
Contributor Author

@oliviertassinari should be good now. v0.12 is 2 commits behind master (your commit for the jsx-quotes rule and accompanying PR). So once v0.12 is updated I can rebase.

@oliviertassinari
Copy link
Member

@shaurya947 Nice 👍

Shaurya Arora added 8 commits September 22, 2015 17:12
Raw Theme is made up of spacing, palette, and font family, whereas MUI theme is made of key:value pairs, each of which represents the styling of a material-ui component based on the given raw theme. In addition, the MUI theme also contains a reference to the raw theme from which it was produced.

In every material-ui component, the "state" is used for the theme, and theme is passed down using context. Within the component, theme is always accessed using state. When a component is first mounted, we check whether a theme object is passed down using context in the getInitialState() method. If one is not found, we initialize a default MUI theme based on the default (light) raw theme. Moreover, to listen for updates when the parent changes the theme, the new context is always checked in componentWillReceiveProps().

Now, the ThemeManager module simply contains functions that return new MUI theme objects (support for immutability). The theme itself (raw theme) is a plain JS object containing key:value pairs for spacing, palette, and font family. Users can easily define their own raw themes and require() them in their modules.
Separated out decorator into src/styles/theme-decorator.js so that it no longer relies on another
wrapper component.
Simplified theme wrapper component to be used as another way to pass down theme.

Currently, there are some issues regarding owner-based vs parent-based contexts when using the theme wrapper. Hopefully, these will go away with React 0.14
Fixed unit tests to use new theming code.
Modified primary1Color of dark-raw-theme to Colors.grey500
Shaurya Arora and others added 2 commits September 23, 2015 12:43
Now, `textTransform` can be applied to all buttons (both `FlatButton` and `RaisedButton`), only `FlatButton`, or only `RaisedButton` on a theme-level.

Here is how:

```js
let newMuiTheme = ThemeManager.getMuiTheme(LightRawTheme);

//override theme variable for button
//this override will apply to both FlatButton and RaisedButton
newMuiTheme.button.textTransform = 'none';
```

```js
let newMuiTheme = ThemeManager.getMuiTheme(LightRawTheme);

//override theme variable for only RaisedButton or FlatButton (substitute accordingly)
newMuiTheme.raisedButton.textTransform = 'none';
```

`textTransform` corresponds to the CSS `text-transform` property and can take any valid value that the CSS property can take.

Related to mui#1576
shaurya947 added a commit that referenced this pull request Sep 23, 2015
@shaurya947 shaurya947 merged commit f78ea4a into mui:v0.12 Sep 23, 2015
@shaurya947 shaurya947 deleted the theming-improvement branch September 23, 2015 21:06
This was referenced Sep 24, 2015
@shaurya947 shaurya947 mentioned this pull request Sep 30, 2015
@zannager zannager added the customization: theme Centered around the theming features label Mar 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
customization: theme Centered around the theming features
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants