Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[CssVarsProvider][Material UI] Exclude dark mode variables from `:roo…
…t` stylesheet (#34131)
- Loading branch information
1 parent
29fd59e
commit 0ec69e7
Showing
9 changed files
with
152 additions
and
1 deletion.
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
41 changes: 41 additions & 0 deletions
41
packages/mui-material/src/styles/excludeVariablesFromRoot.test.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,41 @@ | ||
import { expect } from 'chai'; | ||
import excludeVariablesFromRoot from './excludeVariablesFromRoot'; | ||
|
||
describe('excludeVariablesFromRoot', () => { | ||
it('should return true', () => { | ||
expect(excludeVariablesFromRoot('mui').includes(`--mui-overlays-1`)).to.equal(true); | ||
expect(excludeVariablesFromRoot('mui').includes(`--mui-overlays-2`)).to.equal(true); | ||
expect(excludeVariablesFromRoot('mui').includes(`--mui-overlays-3`)).to.equal(true); | ||
expect(excludeVariablesFromRoot('mui').includes(`--mui-overlays-4`)).to.equal(true); | ||
expect(excludeVariablesFromRoot('mui').includes(`--mui-overlays-5`)).to.equal(true); | ||
expect(excludeVariablesFromRoot('mui').includes(`--mui-overlays-6`)).to.equal(true); | ||
expect(excludeVariablesFromRoot('mui').includes(`--mui-overlays-7`)).to.equal(true); | ||
expect(excludeVariablesFromRoot('mui').includes(`--mui-overlays-8`)).to.equal(true); | ||
expect(excludeVariablesFromRoot('mui').includes(`--mui-overlays-9`)).to.equal(true); | ||
expect(excludeVariablesFromRoot('mui').includes(`--mui-overlays-10`)).to.equal(true); | ||
expect(excludeVariablesFromRoot('mui').includes(`--mui-overlays-11`)).to.equal(true); | ||
expect(excludeVariablesFromRoot('mui').includes(`--mui-overlays-12`)).to.equal(true); | ||
expect(excludeVariablesFromRoot('mui').includes(`--mui-overlays-13`)).to.equal(true); | ||
expect(excludeVariablesFromRoot('mui').includes(`--mui-overlays-14`)).to.equal(true); | ||
expect(excludeVariablesFromRoot('mui').includes(`--mui-overlays-15`)).to.equal(true); | ||
expect(excludeVariablesFromRoot('mui').includes(`--mui-overlays-16`)).to.equal(true); | ||
expect(excludeVariablesFromRoot('mui').includes(`--mui-overlays-17`)).to.equal(true); | ||
expect(excludeVariablesFromRoot('mui').includes(`--mui-overlays-18`)).to.equal(true); | ||
expect(excludeVariablesFromRoot('mui').includes(`--mui-overlays-19`)).to.equal(true); | ||
expect(excludeVariablesFromRoot('mui').includes(`--mui-overlays-20`)).to.equal(true); | ||
expect(excludeVariablesFromRoot('mui').includes(`--mui-overlays-21`)).to.equal(true); | ||
expect(excludeVariablesFromRoot('mui').includes(`--mui-overlays-22`)).to.equal(true); | ||
expect(excludeVariablesFromRoot('mui').includes(`--mui-overlays-23`)).to.equal(true); | ||
expect(excludeVariablesFromRoot('mui').includes(`--mui-overlays-24`)).to.equal(true); | ||
expect(excludeVariablesFromRoot('mui').includes(`--mui-palette-AppBar-darkBg`)).to.equal(true); | ||
expect(excludeVariablesFromRoot('mui').includes(`--mui-palette-AppBar-darkColor`)).to.equal( | ||
true, | ||
); | ||
}); | ||
|
||
it('should return true for custom prefix', () => { | ||
expect(excludeVariablesFromRoot('').includes(`--overlays-1`)).to.equal(true); | ||
expect(excludeVariablesFromRoot('').includes(`--palette-AppBar-darkBg`)).to.equal(true); | ||
expect(excludeVariablesFromRoot('').includes(`--palette-AppBar-darkColor`)).to.equal(true); | ||
}); | ||
}); |
12 changes: 12 additions & 0 deletions
12
packages/mui-material/src/styles/excludeVariablesFromRoot.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,12 @@ | ||
/** | ||
* @internal These variables should not appear in the :root stylesheet when the `defaultMode="dark"` | ||
*/ | ||
const excludeVariablesFromRoot = (cssVarPrefix: string) => [ | ||
...[...Array(24)].map( | ||
(_, index) => `--${cssVarPrefix ? `${cssVarPrefix}-` : ''}overlays-${index + 1}`, | ||
), | ||
`--${cssVarPrefix ? `${cssVarPrefix}-` : ''}palette-AppBar-darkBg`, | ||
`--${cssVarPrefix ? `${cssVarPrefix}-` : ''}palette-AppBar-darkColor`, | ||
]; | ||
|
||
export default excludeVariablesFromRoot; |
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
33 changes: 33 additions & 0 deletions
33
test/regressions/fixtures/CssVarsProvider/MaterialUIDefaultDark.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,33 @@ | ||
import * as React from 'react'; | ||
import { Experimental_CssVarsProvider as CssVarsProvider } from '@mui/material/styles'; | ||
import AppBar from '@mui/material/AppBar'; | ||
import Box from '@mui/material/Box'; | ||
import Paper from '@mui/material/Paper'; | ||
import Toolbar from '@mui/material/Toolbar'; | ||
|
||
export default function MaterialUIDefaultDark() { | ||
return ( | ||
<CssVarsProvider defaultMode="dark"> | ||
<Box | ||
sx={{ | ||
display: 'grid', | ||
gridTemplateColumns: 'repeat(auto-fill, minmax(256px, 1fr))', | ||
gridAutoRows: 'minmax(160px, auto)', | ||
gap: 2, | ||
'& > div': { | ||
placeSelf: 'center', | ||
}, | ||
}} | ||
> | ||
<AppBar position="static" color="secondary" elevation={12}> | ||
<Toolbar>The color should be `palette.AppBar.darkBg`</Toolbar> | ||
</AppBar> | ||
<Box sx={{ bgcolor: '#121212', p: 4 }}> | ||
<Paper elevation={24} sx={{ bgcolor: '#121212', p: 2, color: '#fff' }}> | ||
You should see overlay. | ||
</Paper> | ||
</Box> | ||
</Box> | ||
</CssVarsProvider> | ||
); | ||
} |
45 changes: 45 additions & 0 deletions
45
test/regressions/fixtures/CssVarsProvider/MaterialUIDefaultDarkToLight.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,45 @@ | ||
import * as React from 'react'; | ||
import { | ||
Experimental_CssVarsProvider as CssVarsProvider, | ||
useColorScheme, | ||
} from '@mui/material/styles'; | ||
import AppBar from '@mui/material/AppBar'; | ||
import Box from '@mui/material/Box'; | ||
import Paper from '@mui/material/Paper'; | ||
import Toolbar from '@mui/material/Toolbar'; | ||
|
||
const LightMode = () => { | ||
const { setMode } = useColorScheme(); | ||
React.useEffect(() => { | ||
setMode('light'); | ||
}, [setMode]); | ||
return null; | ||
}; | ||
|
||
export default function MaterialUIDefaultDark() { | ||
return ( | ||
<CssVarsProvider defaultMode="dark"> | ||
<LightMode /> | ||
<Box | ||
sx={{ | ||
display: 'grid', | ||
gridTemplateColumns: 'repeat(auto-fill, minmax(256px, 1fr))', | ||
gridAutoRows: 'minmax(160px, auto)', | ||
gap: 2, | ||
'& > div': { | ||
placeSelf: 'center', | ||
}, | ||
}} | ||
> | ||
<AppBar position="static" color="secondary" elevation={12}> | ||
<Toolbar>The color should be secondary.</Toolbar> | ||
</AppBar> | ||
<Box sx={{ bgcolor: '#121212', p: 4 }}> | ||
<Paper elevation={24} sx={{ bgcolor: '#121212', p: 2, color: '#fff' }}> | ||
You <em>should not</em> see overlay. | ||
</Paper> | ||
</Box> | ||
</Box> | ||
</CssVarsProvider> | ||
); | ||
} |