Skip to content

Commit

Permalink
add theme.applyStyles doc
Browse files Browse the repository at this point in the history
  • Loading branch information
siriwatknp committed Feb 6, 2024
1 parent 90f655a commit d873af8
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 3 deletions.
73 changes: 73 additions & 0 deletions docs/data/material/customization/dark-mode/dark-mode.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,3 +146,76 @@ function App() {
);
}
```

## Styling in dark mode

The `theme.applyStyles()` function, introduced in `@mui/material` [v5.15.7](https://github.com/mui/material-ui/releases/tag/v5.15.7), lets you apply styles for a specific mode.

We recommend using this function in favor of the `theme.palette.mode` check, as it's more readable and lets you smoothly adopt new features in the future.

Check warning on line 154 in docs/data/material/customization/dark-mode/dark-mode.md

View workflow job for this annotation

GitHub Actions / runner / vale

[vale] reported by reviewdog 🐶 [Google.We] Try to avoid using first-person plural like 'We'. Raw Output: {"message": "[Google.We] Try to avoid using first-person plural like 'We'.", "location": {"path": "docs/data/material/customization/dark-mode/dark-mode.md", "range": {"start": {"line": 154, "column": 1}}}, "severity": "WARNING"}

### Usage

With the `**styled**` function:

```jsx
import { styled } from '@mui/material/styles';

const MyComponent = styled('div')(
({ theme }) => ({
color: '#fff',
backgroundColor: theme.palette.primary.main,
'&:hover': {
boxShadow: theme.shadows[3],
backgroundColor: theme.palette.primary.dark,
},
}),
({ theme }) =>
theme.applyStyles('dark', {
backgroundColor: theme.palette.secondary.main,
}),
);
```

With the `**sx**` prop:

```jsx
import Button from '@mui/material/Button';

<Button
sx={[
(theme) => ({
color: '#fff',
backgroundColor: theme.palette.primary.main,
'&:hover': {
boxShadow: theme.shadows[3],
backgroundColor: theme.palette.primary.dark,
},
}),
(theme) =>
theme.applyStyles('dark', {
backgroundColor: theme.palette.secondary.main,
}),
]}
>
Submit
</Button>;
```

### Codemod

We provide a codemod to migrate your codebase from using `theme.palette.mode` to use `theme.applyStyles()`.

Check warning on line 206 in docs/data/material/customization/dark-mode/dark-mode.md

View workflow job for this annotation

GitHub Actions / runner / vale

[vale] reported by reviewdog 🐶 [Google.We] Try to avoid using first-person plural like 'We'. Raw Output: {"message": "[Google.We] Try to avoid using first-person plural like 'We'.", "location": {"path": "docs/data/material/customization/dark-mode/dark-mode.md", "range": {"start": {"line": 206, "column": 1}}}, "severity": "WARNING"}

```sh
npx @mui/codemod@latest deprecations/mode-to-apply-styles path/to/your/files
```

### API

`theme.applyStyles(mode, styles) => CSSObject`

Apply styles for a specific mode.

#### Arguments

- `mode` (`'light' | 'dark'`) - The mode for which the styles should be applied.
- `styles` (`CSSObject`) - An object which contains the styles to be applied for the specified mode.
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ const theme = extendTheme({ cssVarPrefix: '' });

## Custom styles per mode

To customize the style without creating new tokens, you can use the `theme.getColorSchemeSelector` utility:
Starting from `@mui/material` [v5.15.7](https://github.com/mui/material-ui/releases/tag/v5.15.7), we recommend using `theme.applyStyles` utility to apply styles for a specific mode:

Check warning on line 230 in docs/data/material/experimental-api/css-theme-variables/customization.md

View workflow job for this annotation

GitHub Actions / runner / vale

[vale] reported by reviewdog 🐶 [Google.We] Try to avoid using first-person plural like 'we'. Raw Output: {"message": "[Google.We] Try to avoid using first-person plural like 'we'.", "location": {"path": "docs/data/material/experimental-api/css-theme-variables/customization.md", "range": {"start": {"line": 230, "column": 99}}}, "severity": "WARNING"}

```js
const Button = styled('button')(({ theme }) => ({
Expand All @@ -239,14 +239,14 @@ const Button = styled('button')(({ theme }) => ({
},

// in dark mode.
[theme.getColorSchemeSelector('dark')]: {
...theme.applyStyles('dark', {
backgroundColor: theme.vars.palette.primary.dark,
color: theme.vars.palette.primary.main,
'&:hover': {
color: '#fff',
backgroundColor: theme.vars.palette.primary.dark,
},
},
}),
}));
```

Expand Down

0 comments on commit d873af8

Please sign in to comment.