-
-
Notifications
You must be signed in to change notification settings - Fork 32.3k
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
[system] Add applyStyles()
to theme
#40667
Changes from all commits
067bce1
93b7745
3329d36
88b6c5e
310b21f
3a91783
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,7 +45,6 @@ export interface Theme extends BaseTheme { | |
components?: Components<BaseTheme>; | ||
unstable_sx: (props: SxProps<Theme>) => CSSObject; | ||
unstable_sxConfig: SxConfig; | ||
applyDarkStyles: (css: CSSObject) => CSSObject; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
} | ||
|
||
/** | ||
|
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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can this be more generic? ie, can this be also used to apply styles for say, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I might be wrong but I don't think it's the responsibility of I have never seen a case where rtl/ltr are differ in terms of colors. Moreover, the rtl/ltr should be done at a higher level. In emotion, it's done by a stylis plugin that changes CSS properties, e.g. from |
||
// @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 {}; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you verify if this is defined in material package ? I stumbled upon this when solving another issue.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
getColorSchemeSelector
is defined whenCssVarsProvider
is used.I think it should be attached to the theme by
extendTheme
. I will create another issue to tackle this separately.