Skip to content
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

[Fab] Add support for CSS variables #32564

Merged
merged 2 commits into from
May 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions docs/pages/experiments/material-ui/fab.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
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 Fab from '@mui/material/Fab';
import Stack from '@mui/material/Stack';

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() {
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',
},
}}
>
<Stack spacing={2} direction="row">
<Fab />
<Fab color="secondary" />
<Fab color="success" />
<Fab color="error" />
<Fab color="info" />
<Fab color="warning" />
<Fab color="inherit" />
</Stack>
</Box>
</Container>
</CssVarsProvider>
);
}
34 changes: 18 additions & 16 deletions packages/mui-material/src/Fab/Fab.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,28 +50,30 @@ const FabRoot = styled(ButtonBase, {
minWidth: 0,
width: 56,
height: 56,
zIndex: theme.zIndex.fab,
boxShadow: theme.shadows[6],
zIndex: (theme.vars || theme).zIndex.fab,
boxShadow: (theme.vars || theme).shadows[6],
'&:active': {
boxShadow: theme.shadows[12],
boxShadow: (theme.vars || theme).shadows[12],
},
color: theme.palette.getContrastText(theme.palette.grey[300]),
backgroundColor: theme.palette.grey[300],
color: theme.vars
? theme.vars.palette.text.primary
Comment on lines +58 to +59
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good to me because getContrastText(grey[300]) 99.99% returns text.primary. cc @mnajdova

Copy link
Member

@siriwatknp siriwatknp May 4, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

leaves 0.01% for the super edge case where custom grey[300] has high contrast.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good 👍 We will see if people complain (I don't expect it will happen :))

: theme.palette.getContrastText?.(theme.palette.grey[300]),
backgroundColor: (theme.vars || theme).palette.grey[300],
'&:hover': {
backgroundColor: theme.palette.grey.A100,
backgroundColor: (theme.vars || theme).palette.grey.A100,
// Reset on touch devices, it doesn't add specificity
'@media (hover: none)': {
backgroundColor: theme.palette.grey[300],
backgroundColor: (theme.vars || theme).palette.grey[300],
},
textDecoration: 'none',
},
[`&.${fabClasses.focusVisible}`]: {
boxShadow: theme.shadows[6],
boxShadow: (theme.vars || theme).shadows[6],
},
[`&.${fabClasses.disabled}`]: {
color: theme.palette.action.disabled,
boxShadow: theme.shadows[0],
backgroundColor: theme.palette.action.disabledBackground,
color: (theme.vars || theme).palette.action.disabled,
boxShadow: (theme.vars || theme).shadows[0],
backgroundColor: (theme.vars || theme).palette.action.disabledBackground,
},
...(ownerState.size === 'small' && {
width: 40,
Expand Down Expand Up @@ -112,14 +114,14 @@ const FabRoot = styled(ButtonBase, {
({ theme, ownerState }) => ({
...(ownerState.color !== 'inherit' &&
ownerState.color !== 'default' &&
theme.palette[ownerState.color] != null && {
color: theme.palette[ownerState.color].contrastText,
backgroundColor: theme.palette[ownerState.color].main,
(theme.vars || theme).palette[ownerState.color] != null && {
color: (theme.vars || theme).palette[ownerState.color].contrastText,
backgroundColor: (theme.vars || theme).palette[ownerState.color].main,
'&:hover': {
backgroundColor: theme.palette[ownerState.color].dark,
backgroundColor: (theme.vars || theme).palette[ownerState.color].dark,
// Reset on touch devices, it doesn't add specificity
'@media (hover: none)': {
backgroundColor: theme.palette[ownerState.color].main,
backgroundColor: (theme.vars || theme).palette[ownerState.color].main,
},
},
}),
Expand Down