Skip to content

Commit

Permalink
[material-ui][docs] Document callback to access theme in GlobalStyles (
Browse files Browse the repository at this point in the history
  • Loading branch information
aarongarciah committed May 23, 2024
1 parent 1f8c0b6 commit 71afb41
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import * as React from 'react';
import GlobalStyles from '@mui/material/GlobalStyles';

export default function GlobalCssOverrideTheme() {
return (
<React.Fragment>
<GlobalStyles
styles={(theme) => ({
h1: { color: theme.palette.primary.main },
})}
/>
<h1>Grey h1 element</h1>
</React.Fragment>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import * as React from 'react';
import GlobalStyles from '@mui/material/GlobalStyles';

export default function GlobalCssOverrideTheme() {
return (
<React.Fragment>
<GlobalStyles
styles={(theme) => ({
h1: { color: theme.palette.primary.main },
})}
/>
<h1>Grey h1 element</h1>
</React.Fragment>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<React.Fragment>
<GlobalStyles
styles={(theme) => ({
h1: { color: theme.palette.primary.main },
})}
/>
<h1>Grey h1 element</h1>
</React.Fragment>
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,10 @@ Here is an example of how you can override styles for the `h1` elements:

{{"demo": "GlobalCssOverride.js", "iframe": true, "height": 100}}

The `styles` prop in the `GlobalStyles` component supports a callback in case you need to access the theme.

{{"demo": "GlobalCssOverrideTheme.js", "iframe": true, "height": 100}}

If you are already using the [CssBaseline](/material-ui/react-css-baseline/) component for setting baseline styles, you can also add these global styles as overrides for this component. Here is how you can achieve the same by using this approach.

{{"demo": "OverrideCssBaseline.js", "iframe": true, "height": 100}}
Expand Down

0 comments on commit 71afb41

Please sign in to comment.