-
-
Notifications
You must be signed in to change notification settings - Fork 32.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[styled-engine] Add
GlobalStyles
component (#24176)
- Loading branch information
Showing
35 changed files
with
480 additions
and
157 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 |
---|---|---|
@@ -1,10 +1,10 @@ | ||
{ | ||
"props": { "children": { "type": { "name": "node" }, "default": "null" } }, | ||
"props": { "children": { "type": { "name": "node" } } }, | ||
"name": "CssBaseline", | ||
"styles": { "classes": [], "globalClasses": {}, "name": "MuiCssBaseline" }, | ||
"spread": false, | ||
"styles": { "classes": [], "globalClasses": {}, "name": null }, | ||
"spread": true, | ||
"filename": "/packages/material-ui/src/CssBaseline/CssBaseline.js", | ||
"inheritance": null, | ||
"demos": "<ul><li><a href=\"/components/css-baseline/\">Css Baseline</a></li></ul>", | ||
"styledComponent": false | ||
"styledComponent": true | ||
} |
16 changes: 3 additions & 13 deletions
16
docs/src/pages/customization/how-to-customize/GlobalCssOverride.js
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,21 +1,11 @@ | ||
import * as React from 'react'; | ||
import { withStyles } from '@material-ui/core/styles'; | ||
import Button from '@material-ui/core/Button'; | ||
|
||
const GlobalCss = withStyles({ | ||
// @global is handled by jss-plugin-global. | ||
'@global': { | ||
'.MuiButton-root': { | ||
fontSize: '1rem', | ||
}, | ||
}, | ||
})(() => null); | ||
import GlobalStyles from '@material-ui/core/GlobalStyles'; | ||
|
||
export default function GlobalCssOverride() { | ||
return ( | ||
<React.Fragment> | ||
<GlobalCss /> | ||
<Button>font-size: 1rem</Button> | ||
<GlobalStyles styles={{ h1: { color: 'grey' } }} /> | ||
<h1>Grey h1 element</h1> | ||
</React.Fragment> | ||
); | ||
} |
16 changes: 3 additions & 13 deletions
16
docs/src/pages/customization/how-to-customize/GlobalCssOverride.tsx
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,21 +1,11 @@ | ||
import * as React from 'react'; | ||
import { withStyles } from '@material-ui/core/styles'; | ||
import Button from '@material-ui/core/Button'; | ||
|
||
const GlobalCss = withStyles({ | ||
// @global is handled by jss-plugin-global. | ||
'@global': { | ||
'.MuiButton-root': { | ||
fontSize: '1rem', | ||
}, | ||
}, | ||
})(() => null); | ||
import GlobalStyles from '@material-ui/core/GlobalStyles'; | ||
|
||
export default function GlobalCssOverride() { | ||
return ( | ||
<React.Fragment> | ||
<GlobalCss /> | ||
<Button>font-size: 1rem</Button> | ||
<GlobalStyles styles={{ h1: { color: 'grey' } }} /> | ||
<h1>Grey h1 element</h1> | ||
</React.Fragment> | ||
); | ||
} |
24 changes: 24 additions & 0 deletions
24
docs/src/pages/customization/how-to-customize/OverrideCssBaseline.js
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,24 @@ | ||
import * as React from 'react'; | ||
import CssBaseline from '@material-ui/core/CssBaseline'; | ||
import { ThemeProvider, createMuiTheme } from '@material-ui/core/styles'; | ||
|
||
const theme = createMuiTheme({ | ||
components: { | ||
MuiCssBaseline: { | ||
styleOverrides: { | ||
h1: { | ||
color: 'grey', | ||
}, | ||
}, | ||
}, | ||
}, | ||
}); | ||
|
||
export default function OverrideCssBaseline() { | ||
return ( | ||
<ThemeProvider theme={theme}> | ||
<CssBaseline /> | ||
<h1>Grey h1 element</h1> | ||
</ThemeProvider> | ||
); | ||
} |
24 changes: 24 additions & 0 deletions
24
docs/src/pages/customization/how-to-customize/OverrideCssBaseline.tsx
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,24 @@ | ||
import * as React from 'react'; | ||
import CssBaseline from '@material-ui/core/CssBaseline'; | ||
import { ThemeProvider, createMuiTheme } from '@material-ui/core/styles'; | ||
|
||
const theme = createMuiTheme({ | ||
components: { | ||
MuiCssBaseline: { | ||
styleOverrides: { | ||
h1: { | ||
color: 'grey', | ||
}, | ||
}, | ||
}, | ||
}, | ||
}); | ||
|
||
export default function OverrideCssBaseline() { | ||
return ( | ||
<ThemeProvider theme={theme}> | ||
<CssBaseline /> | ||
<h1>Grey h1 element</h1> | ||
</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
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
9 changes: 9 additions & 0 deletions
9
packages/material-ui-styled-engine-sc/src/GlobalStyles/GlobalStyles.d.ts
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,9 @@ | ||
import * as React from 'react'; | ||
import { CSSObject, InterpolationFunction } from 'styled-components'; | ||
|
||
export interface GlobalStylesProps { | ||
defaultTheme?: object; | ||
styles: string | CSSObject | InterpolationFunction<any>; | ||
} | ||
|
||
export default function Global(props: GlobalStylesProps): React.ReactElement; |
23 changes: 23 additions & 0 deletions
23
packages/material-ui-styled-engine-sc/src/GlobalStyles/GlobalStyles.js
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,23 @@ | ||
import PropTypes from 'prop-types'; | ||
import { createGlobalStyle } from 'styled-components'; | ||
|
||
function isEmpty(obj) { | ||
return Object.keys(obj).length === 0; | ||
} | ||
|
||
const GlobalStyles = createGlobalStyle((props) => { | ||
const { styles, defaultTheme } = props; | ||
|
||
if (typeof styles === 'function') { | ||
return styles(isEmpty(props.theme) ? defaultTheme : props.theme); | ||
} | ||
|
||
return styles; | ||
}); | ||
|
||
export default GlobalStyles; | ||
|
||
GlobalStyles.propTypes = { | ||
defaultTheme: PropTypes.object, | ||
styles: PropTypes.oneOfType([PropTypes.string, PropTypes.object, PropTypes.func]), | ||
}; |
93 changes: 93 additions & 0 deletions
93
packages/material-ui-styled-engine-sc/src/GlobalStyles/GlobalStyles.test.js
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,93 @@ | ||
import * as React from 'react'; | ||
import { expect } from 'chai'; | ||
import { createClientRender } from 'test/utils'; | ||
import { ThemeProvider } from 'styled-components'; | ||
import styled from '..'; | ||
import GlobalStyles from './GlobalStyles'; | ||
|
||
describe('GlobalStyles', () => { | ||
const render = createClientRender(); | ||
|
||
it('should add global styles', function test() { | ||
if (/jsdom/.test(window.navigator.userAgent)) this.skip(); | ||
|
||
const { container } = render( | ||
<div> | ||
<GlobalStyles styles={`span { color: rgb(0, 0, 255); }`} /> | ||
<span>Blue text</span> | ||
</div>, | ||
); | ||
|
||
expect(container.getElementsByTagName('span')[0]).toHaveComputedStyle({ | ||
color: 'rgb(0, 0, 255)', | ||
}); | ||
}); | ||
|
||
it('should add global styles using JS syntax', function test() { | ||
if (/jsdom/.test(window.navigator.userAgent)) this.skip(); | ||
|
||
const { container } = render( | ||
<div> | ||
<GlobalStyles styles={{ span: { color: 'rgb(0, 0, 255)' } }} /> | ||
<span>Blue text</span> | ||
</div>, | ||
); | ||
|
||
expect(container.getElementsByTagName('span')[0]).toHaveComputedStyle({ | ||
color: 'rgb(0, 0, 255)', | ||
}); | ||
}); | ||
|
||
it('should add global styles using function', function test() { | ||
if (/jsdom/.test(window.navigator.userAgent)) this.skip(); | ||
|
||
const { container } = render( | ||
<ThemeProvider theme={{ color: 'rgb(0, 0, 255)' }}> | ||
<GlobalStyles styles={(theme) => ({ span: { color: theme.color } })} /> | ||
<span>Blue text</span> | ||
</ThemeProvider>, | ||
); | ||
|
||
expect(container.getElementsByTagName('span')[0]).toHaveComputedStyle({ | ||
color: 'rgb(0, 0, 255)', | ||
}); | ||
}); | ||
|
||
it('should give presedence to styled()', function test() { | ||
if (/jsdom/.test(window.navigator.userAgent)) this.skip(); | ||
|
||
const Span = styled('span')` | ||
color: rgb(255, 0, 0); | ||
`; | ||
|
||
const { container } = render( | ||
<div> | ||
<GlobalStyles styles={`span { color: rgb(0, 0, 255); }`} /> | ||
<Span>Red text</Span> | ||
</div>, | ||
); | ||
|
||
expect(container.getElementsByTagName('span')[0]).toHaveComputedStyle({ | ||
color: 'rgb(255, 0, 0)', | ||
}); | ||
}); | ||
|
||
it('should give presedence to styled() using JS syntax', function test() { | ||
if (/jsdom/.test(window.navigator.userAgent)) this.skip(); | ||
|
||
const Span = styled('span')({ | ||
color: 'rgb(255, 0, 0)', | ||
}); | ||
|
||
const { container } = render( | ||
<div> | ||
<GlobalStyles styles={{ span: { color: 'rgb(0, 0, 255)' } }} /> | ||
<Span>Red text</Span> | ||
</div>, | ||
); | ||
|
||
expect(container.getElementsByTagName('span')[0]).toHaveComputedStyle({ | ||
color: 'rgb(255, 0, 0)', | ||
}); | ||
}); | ||
}); |
2 changes: 2 additions & 0 deletions
2
packages/material-ui-styled-engine-sc/src/GlobalStyles/index.d.ts
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,2 @@ | ||
export { default } from './GlobalStyles'; | ||
export * from './GlobalStyles'; |
1 change: 1 addition & 0 deletions
1
packages/material-ui-styled-engine-sc/src/GlobalStyles/index.js
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 @@ | ||
export { default } from './GlobalStyles'; |
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,3 +1,6 @@ | ||
export * from 'styled-components'; | ||
export { default } from 'styled-components'; | ||
export { default as StylesProvider } from './StylesProvider'; | ||
|
||
export { default as GlobalStyles } from './GlobalStyles'; | ||
export * from './GlobalStyles'; |
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
9 changes: 9 additions & 0 deletions
9
packages/material-ui-styled-engine/src/GlobalStyles/GlobalStyles.d.ts
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,9 @@ | ||
import * as React from 'react'; | ||
import { Interpolation } from '@emotion/styled'; | ||
|
||
export interface GlobalStylesProps { | ||
defaultTheme?: object; | ||
styles: Interpolation<any>; | ||
} | ||
|
||
export default function GlobalStyles(props: GlobalStylesProps): React.ReactElement; |
Oops, something went wrong.