-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[muiStyled] Support default theme when none is available (#22791)
- Loading branch information
Showing
17 changed files
with
376 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import React from 'react'; | ||
import MarkdownDocs from 'docs/src/modules/components/MarkdownDocs'; | ||
import { prepareMarkdown } from 'docs/src/modules/utils/parseMarkdown'; | ||
|
||
const pageFilename = 'api/theme-provider'; | ||
const requireRaw = require.context('!raw-loader!./', false, /\/theme-provider\.md$/); | ||
|
||
export default function Page({ docs }) { | ||
return <MarkdownDocs docs={docs} />; | ||
} | ||
|
||
Page.getInitialProps = () => { | ||
const { demos, docs } = prepareMarkdown({ pageFilename, requireRaw }); | ||
return { demos, docs }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
--- | ||
filename: /packages/material-ui/src/styles/ThemeProvider.js | ||
--- | ||
|
||
<!--- This documentation is automatically generated, do not try to edit it. --> | ||
|
||
# ThemeProvider API | ||
|
||
<p class="description">The API documentation of the ThemeProvider React component. Learn more about the props and the CSS customization points.</p> | ||
|
||
## Import | ||
|
||
```js | ||
import ThemeProvider from '@material-ui/core/styles/ThemeProvider.js/ThemeProvider'; | ||
// or | ||
import { ThemeProvider } from '@material-ui/core/styles/ThemeProvider.js'; | ||
``` | ||
|
||
You can learn more about the difference by [reading this guide](/guides/minimizing-bundle-size/). | ||
|
||
This component makes the `theme` available down the React tree. | ||
It should preferably be used at **the root of your component tree**. | ||
|
||
|
||
|
||
## Props | ||
|
||
| Name | Type | Default | Description | | ||
|:-----|:-----|:--------|:------------| | ||
|
||
The component cannot hold a ref. | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from '@emotion/styled'; | ||
export { default } from '@emotion/styled'; | ||
export { ThemeContext } from '@emotion/core'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export { default } from '@emotion/styled'; | ||
export { ThemeContext } from '@emotion/core'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { DefaultTheme } from '@material-ui/styles'; | ||
|
||
export interface ThemeProviderProps<Theme = DefaultTheme> { | ||
children?: React.ReactNode; | ||
theme: Partial<Theme> | ((outerTheme: Theme) => Theme); | ||
} | ||
|
||
/** | ||
* This component makes the `theme` available down the React tree. | ||
* It should preferably be used at **the root of your component tree**. | ||
* API: | ||
* | ||
* - [ThemeProvider API](https://material-ui.com/api/theme-provider/) | ||
*/ | ||
export default function ThemeProvider<T = DefaultTheme>( | ||
props: ThemeProviderProps<T> | ||
): React.ReactElement<ThemeProviderProps<T>>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { ThemeProvider as MuiThemeProvider } from '@material-ui/styles'; | ||
import { exactProp } from '@material-ui/utils'; | ||
import { ThemeContext as StyledEngineThemeContext } from '@material-ui/styled-engine'; | ||
import useTheme from './useTheme'; | ||
|
||
function InnerThemeProvider({ children }) { | ||
const theme = useTheme(); | ||
return ( | ||
<StyledEngineThemeContext.Provider value={typeof theme === 'object' ? theme : {}}> | ||
{children} | ||
</StyledEngineThemeContext.Provider> | ||
); | ||
} | ||
|
||
InnerThemeProvider.propTypes = { | ||
/** | ||
* Your component tree. | ||
*/ | ||
children: PropTypes.node, | ||
}; | ||
|
||
/** | ||
* This component makes the `theme` available down the React tree. | ||
* It should preferably be used at **the root of your component tree**. | ||
*/ | ||
function ThemeProvider(props) { | ||
const { children, theme: localTheme } = props; | ||
|
||
return ( | ||
<MuiThemeProvider theme={localTheme}> | ||
<InnerThemeProvider>{children}</InnerThemeProvider> | ||
</MuiThemeProvider> | ||
); | ||
} | ||
|
||
ThemeProvider.propTypes = { | ||
/** | ||
* Your component tree. | ||
*/ | ||
children: PropTypes.node, | ||
/** | ||
* A theme object. You can provide a function to extend the outer theme. | ||
*/ | ||
theme: PropTypes.oneOfType([PropTypes.object, PropTypes.func]).isRequired, | ||
}; | ||
|
||
if (process.env.NODE_ENV !== 'production') { | ||
ThemeProvider.propTypes = exactProp(ThemeProvider.propTypes); | ||
} | ||
|
||
export default ThemeProvider; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import React from 'react'; | ||
import { expect } from 'chai'; | ||
import { createClientRender } from 'test/utils'; | ||
import { useTheme } from '@material-ui/styles'; | ||
import { ThemeContext } from '@material-ui/styled-engine'; | ||
import ThemeProvider from './ThemeProvider'; | ||
|
||
describe('ThemeProvider', () => { | ||
const render = createClientRender(); | ||
|
||
it('should provide the theme to the mui theme context', () => { | ||
let theme; | ||
|
||
function Test() { | ||
theme = useTheme(); | ||
|
||
return null; | ||
} | ||
|
||
render( | ||
<ThemeProvider theme={{ foo: 'foo' }}> | ||
<Test /> | ||
</ThemeProvider>, | ||
); | ||
expect(theme).to.deep.equal({ foo: 'foo' }); | ||
}); | ||
|
||
it('should provide the theme to the styled engine theme context', () => { | ||
let theme; | ||
|
||
function Test() { | ||
theme = React.useContext(ThemeContext); | ||
|
||
return null; | ||
} | ||
|
||
render( | ||
<ThemeProvider theme={{ foo: 'foo' }}> | ||
<Test /> | ||
</ThemeProvider>, | ||
); | ||
expect(theme).to.deep.equal({ foo: 'foo' }); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.