Skip to content

Commit 9e18f22

Browse files
[styles] Change the withTheme API (#14565)
1 parent 283e72e commit 9e18f22

File tree

26 files changed

+63
-47
lines changed

26 files changed

+63
-47
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ Remove the deprecated button flat, raised and fab variants:
159159
#### Changes
160160

161161
- [core] Increase React peer dependency to v16.8.0 (#14432) @oliviertassinari
162-
- [Slider] Fix a11y issues with the handle (#14461) @eps1lon
162+
- [Slider] Fix a11y issues with the handle (#14461) @eps1lon
163163

164164
### Docs
165165

docs/src/pages/css-in-js/advanced/WithTheme.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ DeepChildRaw.propTypes = {
1111
theme: PropTypes.object.isRequired,
1212
};
1313

14-
const DeepChild = withTheme()(DeepChildRaw);
14+
const DeepChild = withTheme(DeepChildRaw);
1515

1616
function WithTheme() {
1717
return (

docs/src/pages/css-in-js/api/api.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ class MyComponent extends React.Component {
285285
export default MyComponent
286286
```
287287

288-
## `withTheme()(Component) => Component`
288+
## `withTheme(Component) => Component`
289289

290290
Provide the `theme` object as a property of the input component so it can be used
291291
in the render method.
@@ -308,5 +308,5 @@ function MyComponent(props) {
308308
return <div>{props.theme.direction}</div>;
309309
}
310310

311-
export default withTheme()(MyComponent);
311+
export default withTheme(MyComponent);
312312
```

docs/src/pages/css-in-js/basics/basics.md

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,6 @@
22

33
<p class="description">You can leverage our styling solution, even if you are not using our components.</p>
44

5-
> ⚠️ `@material-ui/styles` is unstable (alpha version).
6-
Hopefully, we will make it the default style implementation for the core components in Material-UI v4.
7-
[Follow this path](/customization/css-in-js/) to read the documentation
8-
of the default style implementation.
9-
105
Material-UI aims to provide strong foundations for building dynamic UIs.
116
For the sake of simplicity, **we expose our styling solution to users**.
127
You can use it, but you don't have to. This styling solution is [interoperable with](/guides/interoperability/) all the other major solutions.

docs/src/pages/customization/themes/WithTheme.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,4 @@ WithTheme.propTypes = {
3333
theme: PropTypes.object.isRequired,
3434
};
3535

36-
export default withTheme()(WithTheme); // Let's get the theme as a property
36+
export default withTheme(WithTheme); // Let's get the theme as a property

docs/src/pages/customization/themes/themes.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ const theme = createMuiTheme({
396396
## Accessing the theme in a component
397397

398398
You might need to access the theme variables inside your React components.
399-
Let's say you want to display the value of the primary color, you can use the `withTheme()` higher-order component to do so. Here is an example:
399+
Let's say you want to display the value of the primary color, you can use the `withTheme` higher-order component to do so. Here is an example:
400400

401401
{{"demo": "pages/customization/themes/WithTheme.js"}}
402402

@@ -479,7 +479,7 @@ const theme = createMuiTheme({
479479
});
480480
```
481481

482-
### `withTheme()(Component) => Component`
482+
### `withTheme(Component) => Component`
483483

484484
Provide the `theme` object as a property of the input component so it can be used
485485
in the render method.
@@ -501,5 +501,5 @@ function MyComponent(props) {
501501
return <div>{props.theme.direction}</div>;
502502
}
503503

504-
export default withTheme()(MyComponent);
504+
export default withTheme(MyComponent);
505505
```

docs/src/pages/getting-started/example-projects/example-projects.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44

55
We host some example projects which you can find in the [GitHub repository](https://github.com/mui-org/material-ui) under the [`/examples`](https://github.com/mui-org/material-ui/tree/next/examples) folder:
66

7-
- [Create React App](https://github.com/mui-org/material-ui/tree/next/examples/create-react-app)
8-
- [Next.js](https://github.com/mui-org/material-ui/tree/next/examples/nextjs)
9-
- [Gatsby](https://github.com/mui-org/material-ui/tree/next/examples/gatsby)
10-
- [CDN](https://github.com/mui-org/material-ui/tree/next/examples/cdn)
7+
- [Create React App](https://github.com/mui-org/material-ui/tree/next/examples/create-react-app-next)
8+
- [Gatsby](https://github.com/mui-org/material-ui/tree/next/examples/gatsby-next)
9+
- [Next.js](https://github.com/mui-org/material-ui/tree/next/examples/nextjs-next)
10+
- [CDN](https://github.com/mui-org/material-ui/tree/next/examples/cdn-next)
1111
- And more
1212

1313
Create React App is an awesome project for learning React.

docs/src/pages/getting-started/faq/faq.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ A lot of our interactive components use it internally:
9898
`Button`, `MenuItem`, `<ListItem button />`, `Tab`, etc.
9999
You can use the same solution with them.
100100

101-
## How do I combine the `withStyles()` and `withTheme()` HOCs?
101+
## How do I combine the `withStyles()` and `withTheme` HOCs?
102102

103103
There are a number of different options:
104104

@@ -114,15 +114,15 @@ export default withStyles(styles, { withTheme: true })(Modal);
114114
import { compose } from 'recompose';
115115

116116
export default compose(
117-
withTheme(),
117+
withTheme,
118118
withStyles(styles)
119119
)(Modal);
120120
```
121121

122122
**raw function chaining:**
123123

124124
```js
125-
export default withTheme()(withStyles(styles)(Modal));
125+
export default withTheme(withStyles(styles)(Modal));
126126
```
127127

128128
## How can I access the DOM element?

docs/src/pages/guides/server-rendering/server-rendering.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,8 @@ ReactDOM.hydrate(
194194
We host different reference implementations which you can find in the [GitHub repository](https://github.com/mui-org/material-ui) under the [`/examples`](https://github.com/mui-org/material-ui/tree/next/examples) folder:
195195

196196
- [The reference implementation of this tutorial](https://github.com/mui-org/material-ui/tree/next/examples/ssr)
197-
- [Next.js](https://github.com/mui-org/material-ui/tree/next/examples/nextjs)
198-
- [Gatsby](https://github.com/mui-org/material-ui/tree/next/examples/gatsby)
197+
- [Gatsby](https://github.com/mui-org/material-ui/tree/next/examples/gatsby-next)
198+
- [Next.js](https://github.com/mui-org/material-ui/tree/next/examples/nextjs-next)
199199

200200
## Troubleshooting
201201

package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
"start": "yarn docs:dev",
4747
"test": "yarn lint && yarn typescript && yarn test:coverage",
4848
"test:coverage": "cross-env NODE_ENV=test BABEL_ENV=coverage nyc mocha 'packages/**/*.test.js' 'docs/**/*.test.js' --exclude '**/node_modules/**' && nyc report -r lcovonly",
49-
"test:coverage:html": "cross-env NODE_ENV=test BABEL_ENV=coverage nyc mocha 'packages/material-ui/**/*.test.js' 'packages/material-ui-utils/**/*.test.js' 'packages/material-ui-styles/**/*.test.js' --exclude '**/node_modules/**' && nyc report --reporter=html",
49+
"test:coverage:html": "cross-env NODE_ENV=test BABEL_ENV=coverage nyc mocha 'packages/**/**/*.test.js' --exclude '**/node_modules/**' && nyc report --reporter=html",
5050
"test:karma": "cross-env NODE_ENV=test karma start test/karma.conf.js --single-run",
5151
"test:regressions": "webpack --config test/regressions/webpack.config.js && rimraf test/regressions/screenshots/chrome/* && vrtest run --config test/vrtest.config.js --record",
5252
"test:umd": "yarn babel-node packages/material-ui/test/umd/run.js",
@@ -201,8 +201,7 @@
201201
"nyc": {
202202
"include": [
203203
"packages/material-ui/src/**/*.js",
204-
"packages/material-ui/utils/**/*.js",
205-
"packages/material-ui/styles/**/*.js"
204+
"packages/material-ui-utils/src/**/*.js"
206205
],
207206
"exclude": [
208207
"**/*.test.js"

packages/material-ui-styles/src/withStyles.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ const withStyles = (stylesOrCreator, options = {}) => Component => {
262262
const more = getThemeProps({ theme, name, props: other });
263263

264264
// Provide the theme to the wrapped component.
265-
// So we don't have to use the `withTheme()` Higher-order Component.
265+
// So we don't have to use the `withTheme` Higher-order Component.
266266
if (withTheme) {
267267
more.theme = theme;
268268
}

packages/material-ui-styles/src/withTheme.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,17 @@ import hoistStatics from './hoistInternalStatics';
55
import ThemeContext from './ThemeContext';
66

77
// Provide the theme object as a property to the input component.
8-
const withTheme = () => Component => {
8+
const withTheme = Component => {
9+
/* istanbul ignore if */
10+
if (process.env.NODE_ENV !== 'production' && Component === undefined) {
11+
throw new Error(
12+
[
13+
'You are calling withTheme(Component) with an undefined component.',
14+
'You may have forgotten to import it.',
15+
].join('\n'),
16+
);
17+
}
18+
919
const WithTheme = props => (
1020
<ThemeContext.Consumer>
1121
{theme => {

packages/material-ui-styles/src/withTheme.test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ describe('withTheme', () => {
2727
theme: PropTypes.object,
2828
};
2929

30-
const TestWithTheme = withTheme()(Test);
30+
const TestWithTheme = withTheme(Test);
3131

3232
const wrapper = mount(
3333
<ThemeProvider theme={{ foo: 'foo' }}>
@@ -40,14 +40,14 @@ describe('withTheme', () => {
4040
it('does not hoist statics', () => {
4141
const Test = () => null;
4242
Test.someStatic = 'will not get hoisted';
43-
const TestWithTheme = withTheme()(Test);
43+
const TestWithTheme = withTheme(Test);
4444
assert.strictEqual(TestWithTheme.someStatic, undefined);
4545
});
4646

4747
it('hoists mui internals', () => {
4848
assert.strictEqual(isMuiElement(<Input />, ['Input']), true);
4949

50-
const ThemedInput = withTheme()(Input);
50+
const ThemedInput = withTheme(Input);
5151

5252
assert.strictEqual(isMuiElement(<ThemedInput />, ['Input']), true);
5353
});

packages/material-ui/src/Fade/Fade.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,4 +113,4 @@ Fade.defaultProps = {
113113
},
114114
};
115115

116-
export default withTheme()(Fade);
116+
export default withTheme(Fade);

packages/material-ui/src/Grow/Grow.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,4 +177,4 @@ Grow.defaultProps = {
177177

178178
Grow.muiSupportAuto = true;
179179

180-
export default withTheme()(Grow);
180+
export default withTheme(Grow);

packages/material-ui/src/Slide/Slide.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,4 +282,4 @@ Slide.defaultProps = {
282282
},
283283
};
284284

285-
export default withTheme()(Slide);
285+
export default withTheme(Slide);

packages/material-ui/src/SwipeableDrawer/SwipeableDrawer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -494,4 +494,4 @@ SwipeableDrawer.defaultProps = {
494494
variant: 'temporary', // Mobile first.
495495
};
496496

497-
export default withTheme()(SwipeableDrawer);
497+
export default withTheme(SwipeableDrawer);

packages/material-ui/src/TablePagination/TablePaginationActions.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,4 +86,4 @@ TablePaginationActions.propTypes = {
8686
theme: PropTypes.object.isRequired,
8787
};
8888

89-
export default withTheme()(TablePaginationActions);
89+
export default withTheme(TablePaginationActions);

packages/material-ui/src/Zoom/Zoom.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,4 +114,4 @@ Zoom.defaultProps = {
114114
},
115115
};
116116

117-
export default withTheme()(Zoom);
117+
export default withTheme(Zoom);

packages/material-ui/src/styles/MuiThemeProvider.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ function getThemeSpy() {
2525
};
2626

2727
return {
28-
ThemeSpy: withTheme()(ThemeSpy),
28+
ThemeSpy: withTheme(ThemeSpy),
2929
themeSpy,
3030
};
3131
}

packages/material-ui/src/styles/withStyles.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ const withStylesOld = (stylesOrCreator, options = {}) => Component => {
273273
const more = getThemeProps({ theme: this.theme, name, props: other });
274274

275275
// Provide the theme to the wrapped component.
276-
// So we don't have to use the `withTheme()` Higher-order Component.
276+
// So we don't have to use the `withTheme` Higher-order Component.
277277
if (withTheme && !more.theme) {
278278
more.theme = this.theme;
279279
}

packages/material-ui/src/styles/withTheme.d.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,6 @@ export interface ThemedComponentProps extends Partial<WithTheme> {
99
innerRef?: React.Ref<any> | React.RefObject<any>;
1010
}
1111

12-
export default function withTheme(): PropInjector<WithTheme, ThemedComponentProps>;
12+
declare const withTheme: PropInjector<WithTheme, ThemedComponentProps>;
13+
14+
export default withTheme;

packages/material-ui/src/styles/withTheme.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,17 @@ function getDefaultTheme() {
1919
}
2020

2121
// Provide the theme object as a property to the input component.
22-
const withThemeOld = () => Component => {
22+
const withThemeOld = Component => {
23+
/* istanbul ignore if */
24+
if (process.env.NODE_ENV !== 'production' && Component === undefined) {
25+
throw new Error(
26+
[
27+
'You are calling withTheme(Component) with an undefined component.',
28+
'You may have forgotten to import it.',
29+
].join('\n'),
30+
);
31+
}
32+
2333
class WithTheme extends React.Component {
2434
constructor(props, context) {
2535
super();

packages/material-ui/src/styles/withTheme.test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ describe('withTheme', () => {
3434
it('should use the theme provided by the context', () => {
3535
const theme = { themeProperty: 'foo' };
3636
broadcast.setState(theme);
37-
const ThemedComponent = withTheme()(Empty);
37+
const ThemedComponent = withTheme(Empty);
3838
const wrapper = shallow(<ThemedComponent />, { context });
3939

4040
assert.strictEqual(wrapper.props().theme, theme);
@@ -43,7 +43,7 @@ describe('withTheme', () => {
4343
it('should rerender when the theme is updated', () => {
4444
const theme = { themeProperty: 'foo' };
4545
broadcast.setState(theme);
46-
const ThemedComponent = withTheme()(Empty);
46+
const ThemedComponent = withTheme(Empty);
4747
const wrapper = mount(<ThemedComponent />, { context });
4848

4949
assert.strictEqual(wrapper.instance().state.theme, theme);
@@ -54,7 +54,7 @@ describe('withTheme', () => {
5454

5555
describe('prop: innerRef', () => {
5656
it('should provide a ref on the inner component', () => {
57-
const ThemedComponent = withTheme()(EmptyClass);
57+
const ThemedComponent = withTheme(EmptyClass);
5858
const handleRef = spy();
5959
mount(<ThemedComponent innerRef={handleRef} />);
6060
assert.strictEqual(handleRef.callCount, 1);

packages/material-ui/src/withWidth/withWidth.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ const withWidth = (options = {}) => Component => {
155155

156156
hoistNonReactStatics(WithWidth, Component);
157157

158-
return withTheme()(WithWidth);
158+
return withTheme(WithWidth);
159159
};
160160

161161
export default withWidth;

packages/material-ui/test/typescript/styles.spec.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ function OverridesTheme() {
169169
}
170170

171171
// withTheme
172-
const ComponentWithTheme = withTheme()(({ theme }: WithTheme) => <div>{theme.spacing(1)}</div>);
172+
const ComponentWithTheme = withTheme(({ theme }: WithTheme) => <div>{theme.spacing(1)}</div>);
173173

174174
<ComponentWithTheme />;
175175

@@ -183,12 +183,12 @@ const StyledComponent = withStyles(styles)(({ theme, classes }: AllTheProps) =>
183183
// missing prop theme
184184
<StyledComponent />; // $ExpectError
185185

186-
const AllTheComposition = withTheme()(StyledComponent);
186+
const AllTheComposition = withTheme(StyledComponent);
187187

188188
<AllTheComposition />;
189189

190190
{
191-
const Foo = withTheme()(
191+
const Foo = withTheme(
192192
class extends React.Component<WithTheme> {
193193
render() {
194194
return null;

0 commit comments

Comments
 (0)