Skip to content

Commit

Permalink
[Tabs] Add support for CSS variables (#32547)
Browse files Browse the repository at this point in the history
  • Loading branch information
ZeeshanTamboli authored May 3, 2022
1 parent 0956b91 commit d7afa2e
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 9 deletions.
121 changes: 121 additions & 0 deletions docs/pages/experiments/material-ui/tabs.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
import * as React from 'react';
import {
Experimental_CssVarsProvider as CssVarsProvider,
useColorScheme,
} from '@mui/material/styles';
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 Moon from '@mui/icons-material/DarkMode';
import Sun from '@mui/icons-material/LightMode';
import Tabs from '@mui/material/Tabs';
import Tab from '@mui/material/Tab';
import Typography from '@mui/material/Typography';

interface TabPanelProps {
children?: React.ReactNode;
index: number;
value: number;
}

function TabPanel(props: TabPanelProps) {
const { children, value, index, ...other } = props;

return (
<div
role="tabpanel"
hidden={value !== index}
id={`simple-tabpanel-${index}`}
aria-labelledby={`simple-tab-${index}`}
{...other}
>
{value === index && (
<Box sx={{ p: 3 }}>
<Typography>{children}</Typography>
</Box>
)}
</div>
);
}

function a11yProps(index: number) {
return {
id: `simple-tab-${index}`,
'aria-controls': `simple-tabpanel-${index}`,
};
}

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>
);
};

export default function CssVarsTemplate() {
const [value, setValue] = React.useState(0);

const handleChange = (event: React.SyntheticEvent, newValue: number) => {
setValue(newValue);
};
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 sx={{ width: '100%' }}>
<Box sx={{ borderColor: 'divider' }}>
<Tabs value={value} onChange={handleChange} aria-label="basic tabs example">
<Tab label="Item One" {...a11yProps(0)} />
<Tab label="Item Two" {...a11yProps(1)} />
<Tab label="Item Three" {...a11yProps(2)} />
</Tabs>
</Box>
<TabPanel value={value} index={0}>
Item One
</TabPanel>
<TabPanel value={value} index={1}>
Item Two
</TabPanel>
<TabPanel value={value} index={2}>
Item Three
</TabPanel>
</Box>
</Box>
</Container>
</CssVarsProvider>
);
}
14 changes: 7 additions & 7 deletions packages/mui-material/src/Tab/Tab.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,25 +85,25 @@ const TabRoot = styled(ButtonBase, {
opacity: 1,
},
[`&.${tabClasses.disabled}`]: {
opacity: theme.palette.action.disabledOpacity,
opacity: (theme.vars || theme).palette.action.disabledOpacity,
},
}),
...(ownerState.textColor === 'primary' && {
color: theme.palette.text.secondary,
color: (theme.vars || theme).palette.text.secondary,
[`&.${tabClasses.selected}`]: {
color: theme.palette.primary.main,
color: (theme.vars || theme).palette.primary.main,
},
[`&.${tabClasses.disabled}`]: {
color: theme.palette.text.disabled,
color: (theme.vars || theme).palette.text.disabled,
},
}),
...(ownerState.textColor === 'secondary' && {
color: theme.palette.text.secondary,
color: (theme.vars || theme).palette.text.secondary,
[`&.${tabClasses.selected}`]: {
color: theme.palette.secondary.main,
color: (theme.vars || theme).palette.secondary.main,
},
[`&.${tabClasses.disabled}`]: {
color: theme.palette.text.disabled,
color: (theme.vars || theme).palette.text.disabled,
},
}),
...(ownerState.fullWidth && {
Expand Down
4 changes: 2 additions & 2 deletions packages/mui-material/src/Tabs/Tabs.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,10 +200,10 @@ const TabsIndicator = styled('span', {
width: '100%',
transition: theme.transitions.create(),
...(ownerState.indicatorColor === 'primary' && {
backgroundColor: theme.palette.primary.main,
backgroundColor: (theme.vars || theme).palette.primary.main,
}),
...(ownerState.indicatorColor === 'secondary' && {
backgroundColor: theme.palette.secondary.main,
backgroundColor: (theme.vars || theme).palette.secondary.main,
}),
...(ownerState.vertical && {
height: '100%',
Expand Down

0 comments on commit d7afa2e

Please sign in to comment.