-
-
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.
[system] Add
applyStyles()
to theme (#40667)
- Loading branch information
1 parent
36c7500
commit 2a29460
Showing
17 changed files
with
182 additions
and
65 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
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
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 |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import { CSSObject } from '@mui/styled-engine'; | ||
|
||
export interface ApplyStyles<K extends string> { | ||
(key: K, styles: CSSObject): CSSObject; | ||
} | ||
|
||
/** | ||
* A universal utility to style components with multiple color modes. Always use it from the theme object. | ||
* It works with: | ||
* - [Basic theme](https://mui.com/material-ui/customization/dark-mode/) | ||
* - [CSS theme variables](https://mui.com/material-ui/experimental-api/css-theme-variables/overview/) | ||
* - Zero-runtime engine | ||
* | ||
* Tips: Use an array over object spread and place `theme.applyStyles()` last. | ||
* | ||
* ✅ [{ background: '#e5e5e5' }, theme.applyStyles('dark', { background: '#1c1c1c' })] | ||
* | ||
* 🚫 { background: '#e5e5e5', ...theme.applyStyles('dark', { background: '#1c1c1c' })} | ||
* | ||
* @example | ||
* 1. using with `styled`: | ||
* ```jsx | ||
* const Component = styled('div')(({ theme }) => [ | ||
* { background: '#e5e5e5' }, | ||
* theme.applyStyles('dark', { | ||
* background: '#1c1c1c', | ||
* color: '#fff', | ||
* }), | ||
* ]); | ||
* ``` | ||
* | ||
* @example | ||
* 2. using with `sx` prop: | ||
* ```jsx | ||
* <Box sx={theme => [ | ||
* { background: '#e5e5e5' }, | ||
* theme.applyStyles('dark', { | ||
* background: '#1c1c1c', | ||
* color: '#fff', | ||
* }), | ||
* ]} | ||
* /> | ||
* ``` | ||
* | ||
* @example | ||
* 3. theming a component: | ||
* ```jsx | ||
* extendTheme({ | ||
* components: { | ||
* MuiButton: { | ||
* styleOverrides: { | ||
* root: ({ theme }) => [ | ||
* { background: '#e5e5e5' }, | ||
* theme.applyStyles('dark', { | ||
* background: '#1c1c1c', | ||
* color: '#fff', | ||
* }), | ||
* ], | ||
* }, | ||
* } | ||
* } | ||
* }) | ||
*``` | ||
*/ | ||
export default function applyStyles<K extends string>(key: K, styles: CSSObject) { | ||
// @ts-expect-error this is 'any' type | ||
const theme = this as { | ||
palette: { mode: 'light' | 'dark' }; | ||
vars?: any; | ||
getColorSchemeSelector?: (scheme: string) => string; | ||
}; | ||
if (theme.vars && typeof theme.getColorSchemeSelector === 'function') { | ||
// If CssVarsProvider is used as a provider, | ||
// returns '* :where([data-mui-color-scheme="light|dark"]) &' | ||
const selector = theme.getColorSchemeSelector(key).replace(/(\[[^\]]+\])/, '*:where($1)'); | ||
return { | ||
[selector]: styles, | ||
}; | ||
} | ||
if (theme.palette.mode === key) { | ||
return styles; | ||
} | ||
|
||
return {}; | ||
} |
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.