Skip to content

Commit

Permalink
Add support CSS variables
Browse files Browse the repository at this point in the history
  • Loading branch information
vicasas committed May 4, 2022
1 parent 0956b91 commit dee5e4e
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 3 deletions.
81 changes: 81 additions & 0 deletions docs/pages/experiments/material-ui/svg-icon.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import * as React from 'react';
import {
Experimental_CssVarsProvider as CssVarsProvider,
useColorScheme,
} from '@mui/material/styles';
import { pink } from '@mui/material/colors';
import CssBaseline from '@mui/material/CssBaseline';
import Box from '@mui/material/Box';
import Button from '@mui/material/Button';
import Container from '@mui/material/Container';
import SvgIcon, { SvgIconProps } from '@mui/material/SvgIcon';
import Moon from '@mui/icons-material/DarkMode';
import Sun from '@mui/icons-material/LightMode';

const ColorSchemePicker = () => {
const { mode, setMode } = useColorScheme();
const [mounted, setMounted] = React.useState(false);
React.useEffect(() => {
setMounted(true);
}, []);
if (!mounted) {
return null;
}

return (
<Button
variant="outlined"
onClick={() => {
if (mode === 'light') {
setMode('dark');
} else {
setMode('light');
}
}}
>
{mode === 'light' ? <Moon /> : <Sun />}
</Button>
);
};

function HomeIcon(props: SvgIconProps) {
return (
<SvgIcon {...props}>
<path d="M10 20v-6h4v6h5v-8h3L12 3 2 12h3v8z" />
</SvgIcon>
);
}

export default function CssVarsTemplate() {
return (
<CssVarsProvider>
<CssBaseline />
<Container sx={{ my: 5 }}>
<Box sx={{ pb: 2 }}>
<ColorSchemePicker />
</Box>
<Box
sx={{
display: 'grid',
gridTemplateColumns: 'repeat(auto-fill, minmax(256px, 1fr))',
gridAutoRows: 'minmax(160px, auto)',
gap: 2,
'& > div': {
placeSelf: 'center',
},
}}
>
<Box>
<HomeIcon />
<HomeIcon color="primary" />
<HomeIcon color="secondary" />
<HomeIcon color="success" />
<HomeIcon color="action" />
<HomeIcon color="disabled" />
<HomeIcon sx={{ color: pink[500] }} />
</Box>
</Box>
</Container>
</CssVarsProvider>
);
}
6 changes: 3 additions & 3 deletions packages/mui-material/src/SvgIcon/SvgIcon.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ const SvgIconRoot = styled('svg', {
}[ownerState.fontSize],
// TODO v5 deprecate, v6 remove for sx
color:
theme.palette?.[ownerState.color]?.main ??
(theme.vars || theme).palette?.[ownerState.color]?.main ??
{
action: theme.palette?.action?.active,
disabled: theme.palette?.action?.disabled,
action: (theme.vars || theme).palette?.action?.active,
disabled: (theme.vars || theme).palette?.action?.disabled,
inherit: undefined,
}[ownerState.color],
}));
Expand Down

0 comments on commit dee5e4e

Please sign in to comment.