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

[docs] Improve the theme nesting documentation #13843

Merged
merged 1 commit into from
Dec 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions docs/src/pages/css-in-js/advanced/advanced.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,29 @@ You might need to access the theme variables inside your React components.
You can nest multiple theme providers.
This can be really useful when dealing with different area of your application that have distinct appearance from each other.

```jsx
<ThemeProvider theme={outerTheme}>
<Child1 />
<ThemeProvider theme={innerTheme}>
<Child2 />
</ThemeProvider>
</ThemeProvider>
```

{{"demo": "pages/css-in-js/advanced/ThemeNesting.js", "react": "next"}}

The inner theme will **override** the outer theme.
You can extend the outer theme by providing a function:

```jsx
<ThemeProvider theme={…} >
<Child1 />
<ThemeProvider theme={outerTheme => ({ darkMode: true, ...outerTheme })}>
<Child2 />
</ThemeProvider>
</ThemeProvider>
```

## JSS plugins

JSS uses the concept of plugins to extend its core, allowing people to cherry-pick the features they need.
Expand Down
1 change: 1 addition & 0 deletions docs/src/pages/customization/themes/CustomStyles.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const theme = createMuiTheme({
// My business variables
danger: orange[500],
},
typography: { useNextVariants: true },
});

function CustomStyles() {
Expand Down
4 changes: 1 addition & 3 deletions docs/src/pages/customization/themes/DarkTheme.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ const theme = createMuiTheme({
palette: {
type: 'dark', // Switching the dark mode on is a single property value change.
},
typography: {
useNextVariants: true,
},
typography: { useNextVariants: true },
});

function DarkTheme() {
Expand Down
68 changes: 0 additions & 68 deletions docs/src/pages/customization/themes/Nested.js

This file was deleted.

1 change: 1 addition & 0 deletions docs/src/pages/customization/themes/OverridesCss.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const theme = createMuiTheme({
},
},
},
typography: { useNextVariants: true },
});

function OverridesCss() {
Expand Down
1 change: 1 addition & 0 deletions docs/src/pages/customization/themes/OverridesProperties.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const theme = createMuiTheme({
disableRipple: true, // No more ripple, on the whole application 💣!
},
},
typography: { useNextVariants: true },
});

function OverridesProperties() {
Expand Down
1 change: 1 addition & 0 deletions docs/src/pages/customization/themes/Palette.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const theme = createMuiTheme({
primary: { main: purple[500] }, // Purple and green play nicely together.
secondary: { main: '#11cb5f' }, // This is just green.A700 as hex.
},
typography: { useNextVariants: true },
});

function Palette() {
Expand Down
36 changes: 36 additions & 0 deletions docs/src/pages/customization/themes/ThemeNesting.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import React from 'react';
import { createMuiTheme, MuiThemeProvider } from '@material-ui/core/styles';
import Checkbox from '@material-ui/core/Checkbox';
import orange from '@material-ui/core/colors/orange';
import green from '@material-ui/core/colors/green';

const outerTheme = createMuiTheme({
palette: {
secondary: {
main: orange[500],
},
},
typography: { useNextVariants: true },
});

const innerTheme = createMuiTheme({
palette: {
secondary: {
main: green[500],
},
},
typography: { useNextVariants: true },
});

function ThemeNesting() {
return (
<MuiThemeProvider theme={outerTheme}>
<Checkbox defaultChecked />
<MuiThemeProvider theme={innerTheme}>
<Checkbox defaultChecked />
</MuiThemeProvider>
</MuiThemeProvider>
);
}

export default ThemeNesting;
40 changes: 40 additions & 0 deletions docs/src/pages/customization/themes/ThemeNestingExtend.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import React from 'react';
import { createMuiTheme, MuiThemeProvider } from '@material-ui/core/styles';
import Checkbox from '@material-ui/core/Checkbox';
import orange from '@material-ui/core/colors/orange';
import green from '@material-ui/core/colors/green';

const outerTheme = createMuiTheme({
palette: {
secondary: {
main: orange[500],
},
},
typography: { useNextVariants: true },
});

function ThemeNestingExtend() {
return (
<MuiThemeProvider theme={outerTheme}>
<Checkbox defaultChecked />
<MuiThemeProvider
theme={theme =>
createMuiTheme({
...theme,
palette: {
...theme.palette,
primary: {
main: green[500],
},
},
})
}
>
<Checkbox defaultChecked color="primary" />
<Checkbox defaultChecked color="secondary" />
</MuiThemeProvider>
</MuiThemeProvider>
);
}

export default ThemeNestingExtend;
9 changes: 7 additions & 2 deletions docs/src/pages/customization/themes/themes.md
Original file line number Diff line number Diff line change
Expand Up @@ -356,10 +356,15 @@ Let's say you want to display the value of the primary color, you can use the `w

## Nesting the theme

The theming solution is very flexible, as you can nest multiple theme providers.
The theming solution is very flexible, as [you can nest](/css-in-js/advanced/#theme-nesting) multiple theme providers.
This can be really useful when dealing with different area of your application that have distinct appearance from each other.

{{"demo": "pages/customization/themes/Nested.js"}}
{{"demo": "pages/customization/themes/ThemeNesting.js"}}

The inner theme will **override** the outer theme.
You can extend the outer theme by providing a function:

{{"demo": "pages/customization/themes/ThemeNestingExtend.js"}}

#### A note on performance

Expand Down
13 changes: 10 additions & 3 deletions pages/customization/themes.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,18 @@ module.exports = require('fs')
.readFileSync(require.resolve('docs/src/pages/customization/themes/WithTheme'), 'utf8')
`,
},
'pages/customization/themes/Nested.js': {
js: require('docs/src/pages/customization/themes/Nested').default,
'pages/customization/themes/ThemeNesting.js': {
js: require('docs/src/pages/customization/themes/ThemeNesting').default,
raw: preval`
module.exports = require('fs')
.readFileSync(require.resolve('docs/src/pages/customization/themes/Nested'), 'utf8')
.readFileSync(require.resolve('docs/src/pages/customization/themes/ThemeNesting'), 'utf8')
`,
},
'pages/customization/themes/ThemeNestingExtend.js': {
js: require('docs/src/pages/customization/themes/ThemeNestingExtend').default,
raw: preval`
module.exports = require('fs')
.readFileSync(require.resolve('docs/src/pages/customization/themes/ThemeNestingExtend'), 'utf8')
`,
},
}}
Expand Down