-
-
Notifications
You must be signed in to change notification settings - Fork 32.6k
[muiStyled] Support default theme when none is available #22791
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
Merged
Merged
Changes from all commits
Commits
Show all changes
40 commits
Select commit
Hold shift + click to select a range
c2d31cc
add emotion peer dependencies
mnajdova 5ae933f
fixed types & tests
mnajdova 18b0668
prettier
mnajdova f0ef95c
Merge branch 'next' of https://github.com/mui-org/material-ui into next
mnajdova c7bebb8
Merge branch 'next' of https://github.com/mui-org/material-ui into next
mnajdova 92b2d6e
Merge branch 'next' of https://github.com/mui-org/material-ui into next
mnajdova 76256f1
wip
mnajdova 8661306
added ThemeProvider
mnajdova 61d7fac
exported ThemeProvider from styled-engine-sc
mnajdova c9fcdc1
tests wip
mnajdova 48305e9
tests
mnajdova 1c04d40
prettier
mnajdova 27d9f73
Update packages/material-ui/src/styles/muiStyled.d.ts
mnajdova b149da5
Update packages/material-ui/src/styles/customStyled.d.ts
mnajdova 05217a7
renamed styled -> legacy_styled, renamed customStyled -> styled
mnajdova 3bf96aa
fixed tests
mnajdova 551157f
added migration step, exported styled with deprecation warning
mnajdova a03252c
prettier
mnajdova 179a6e2
renamed legacy_styled back to styled
mnajdova 9878176
renamed styled to experimentalStyled
mnajdova 17ce5d4
fix
mnajdova b7ccb20
specify ThemeProvider as export from emotion-theming
mnajdova 72398c8
refactored muiStyled to be used as simple styled utility too
mnajdova 58a1fb9
fix lint issues
mnajdova aac9e83
renamed muiStyled to experimentalStyled
mnajdova 5774c19
refactored to use ThemeContext
mnajdova 8016f92
Update packages/material-ui-styles/README.md
mnajdova 6bc05c1
Update packages/material-ui-styles/README.md
mnajdova 9757a00
Update packages/material-ui-styles/package.json
mnajdova bb880fe
Moved nesting ThemeProvider to core
mnajdova 73d6589
Update packages/material-ui/src/styles/experimentalStyled.d.ts
mnajdova 6e2b480
Update packages/material-ui/src/styles/experimentalStyled.d.ts
mnajdova 0c8be58
added test
mnajdova a001588
fixed docs generation
mnajdova e676780
Update packages/material-ui/src/styles/ThemeProvider.js
mnajdova 0298222
Update packages/material-ui/src/styles/experimentalStyled.d.ts
mnajdova c96062d
addressed comments
mnajdova 3e419ac
Update packages/material-ui/src/styles/ThemeProvider.js
mnajdova 4fccc26
Update packages/material-ui/src/styles/ThemeProvider.js
mnajdova dcc97fa
docs:api
mnajdova File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.