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

[Joy] Improve variant customization experience #30878

Merged
merged 31 commits into from
Feb 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
71337a0
create variants after user input
siriwatknp Jan 26, 2022
da4fd85
fix variant type to be augmentable
siriwatknp Jan 28, 2022
f70996d
minor cleanup types
siriwatknp Jan 25, 2022
b27c23d
add comment
siriwatknp Feb 2, 2022
069b03f
fix types
siriwatknp Feb 2, 2022
ac39c9a
remove unnecessary contained context
siriwatknp Feb 2, 2022
cc33f74
fix types
siriwatknp Feb 2, 2022
abd043c
add variant experiment page
siriwatknp Feb 2, 2022
2ee1c0c
auto create variant style for any palette
siriwatknp Feb 2, 2022
c7f3f44
remove recursive type
siriwatknp Feb 2, 2022
a5a1a77
fix undefined resolveTheme
siriwatknp Feb 2, 2022
6741ec0
add prefix to theme
siriwatknp Feb 2, 2022
22d7c5e
fix contained overrides
siriwatknp Feb 2, 2022
7f8875e
prevent crash if undefined
siriwatknp Feb 3, 2022
8d5885e
fix CSS value case
siriwatknp Feb 3, 2022
321c90a
add focusVisible to context overrides
siriwatknp Feb 3, 2022
23f5d4a
remove colorScheme toggle
siriwatknp Feb 3, 2022
87fc23e
add tests
siriwatknp Feb 3, 2022
df69def
Merge branch 'master' of https://github.com/mui-org/material-ui into …
siriwatknp Feb 3, 2022
e5654f7
fix variant outlined borderWidth
siriwatknp Feb 3, 2022
4bcd7e7
rename for consistency
siriwatknp Feb 3, 2022
c28d963
revert changes
siriwatknp Feb 4, 2022
6e0d45c
fix types naming
siriwatknp Feb 4, 2022
96a4888
fix CSS variables prefix replacement
siriwatknp Feb 4, 2022
b6ad6f6
add comments
siriwatknp Feb 4, 2022
364a57f
Merge branch 'master' into joy/variant-implementation
siriwatknp Feb 11, 2022
c19d937
fix wrong merge conflict
siriwatknp Feb 11, 2022
4cb6800
add other variants overrides
siriwatknp Feb 11, 2022
a8a9a41
fix test
siriwatknp Feb 11, 2022
721046b
add override text palette
siriwatknp Feb 12, 2022
f90bacf
fix test
siriwatknp Feb 12, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
222 changes: 222 additions & 0 deletions docs/pages/experiments/joy/variant-overrides.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,222 @@
import * as React from 'react';
import { GlobalStyles } from '@mui/system';
import Box, { BoxProps } from '@mui/joy/Box';
import Button from '@mui/joy/Button';
import IconButton from '@mui/joy/IconButton';
import Typography from '@mui/joy/Typography';
import { CssVarsProvider, ColorPaletteProp, VariantProp, useColorScheme } from '@mui/joy/styles';
import CodeRounded from '@mui/icons-material/CodeRounded';
import ScheduleRounded from '@mui/icons-material/ScheduleRounded';
import DeleteForeverRounded from '@mui/icons-material/DeleteForeverRounded';
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');
}
}}
sx={{ minWidth: 40, p: '0.25rem' }}
>
{mode === 'light' ? <Moon /> : <Sun />}
</Button>
);
};

// how to add more color and use with variants
const Tile = ({
children,
variant = 'light',
color = 'primary',
sx = [],
...props
}: {
variant?: 'light' | 'contained';
color?: ColorPaletteProp | 'secondary' | 'alternate';
} & Omit<BoxProps, 'color'>) => {
return (
<Box
sx={[
{ display: 'inline-flex', p: 0.75, borderRadius: '4px' },
(theme) => theme.variants[variant][color],
...(Array.isArray(sx) ? sx : [sx]),
]}
{...props}
>
{children}
</Box>
);
};

export default function JoyVariant() {
const renderContent = (variant: VariantProp, color: ColorPaletteProp) => (
<React.Fragment>
<Box sx={{ display: 'flex', alignItems: 'center' }}>
<ScheduleRounded fontSize="md" />
<Typography level="body3" sx={{ ml: 0.5, mt: '1px', fontWeight: 500 }}>
March 25th
</Typography>
</Box>
<Box sx={{ my: 'auto' }}>
<Tile
variant={variant.match(/(contained)/) ? 'light' : 'contained'}
color={color}
sx={{
...(variant.match(/(contained)/) && {
bgcolor: 'background.body',
}),
boxShadow: 'md',
}}
>
<CodeRounded />
</Tile>
<Typography level="h6" component="div" sx={{ mt: 1.5, fontWeight: 500 }}>
Check the docs for getting every component API
</Typography>
</Box>
<Box sx={{ display: 'flex', gap: 1 }}>
<Box
component="img"
aria-labelledby="demo-task-card-assigne-name"
src="/static/images/avatar/1-sm.jpeg"
sx={{ borderRadius: 'sm', width: 40, height: 40 }}
/>
<Box>
<Typography level="body2" sx={{ fontWeight: 500 }}>
Assigned to
</Typography>
<Typography id="demo-task-card-assigne-name" sx={{ fontWeight: 500 }}>
Michael Scott
</Typography>
</Box>
<Button color={variant === 'contained' ? 'context' : color} sx={{ ml: 'auto' }}>
Check
</Button>
<IconButton color={variant.match(/(contained)/) ? 'context' : color}>
<DeleteForeverRounded />
</IconButton>
</Box>
</React.Fragment>
);
return (
<CssVarsProvider
theme={{
components: {
MuiSvgIcon: {
defaultProps: {
fontSize: 'xl',
},
styleOverrides: {
root: ({ ownerState, theme }) => ({
...(ownerState.fontSize &&
ownerState.fontSize !== 'inherit' && {
fontSize: theme.vars.fontSize[ownerState.fontSize],
}),
...(ownerState.color &&
ownerState.color !== 'inherit' && {
color: theme.vars.palette[ownerState.color].textColor,
}),
}),
},
},
},
}}
>
<GlobalStyles styles={{ body: { margin: 0 } }} />
<Box sx={{ px: 3, pb: 4 }}>
<ColorSchemePicker />
</Box>
<Box
sx={{
maxWidth: { md: 1152, xl: 1536 },
py: 3,
mx: 'auto',
display: 'grid',
gridTemplateColumns: 'repeat(auto-fit, minmax(280px, 1fr))',
gap: 2,
}}
>
<Box
sx={(theme) => ({
'--shadow-channel': '0 0 0',
minWidth: 280,
maxWidth: 360,
minHeight: 280,
display: 'flex',
flexDirection: 'column',
p: 2.5,
boxShadow: 'md',
borderRadius: 'sm',
...theme.variants.contained.primary,
...theme.variants.containedOverrides.primary,
})}
>
{renderContent('contained', 'primary')}
</Box>
<Box
sx={(theme) => ({
minWidth: 280,
maxWidth: 360,
minHeight: 280,
display: 'flex',
flexDirection: 'column',
p: 2.5,
boxShadow: 'md',
borderRadius: 'sm',
...theme.variants.text.info,
...theme.variants.textOverrides.info,
})}
>
{renderContent('text', 'info')}
</Box>
<Box
sx={(theme) => ({
minWidth: 280,
maxWidth: 360,
minHeight: 280,
display: 'flex',
flexDirection: 'column',
p: 2.5,
boxShadow: 'md',
borderRadius: 'sm',
...theme.variants.outlined.neutral,
...theme.variants.outlinedOverrides.neutral,
})}
>
{renderContent('outlined', 'neutral')}
</Box>
<Box
sx={(theme) => ({
minWidth: 280,
maxWidth: 360,
minHeight: 280,
display: 'flex',
flexDirection: 'column',
p: 2.5,
boxShadow: 'md',
borderRadius: 'sm',
...theme.variants.light.success,
...theme.variants.lightOverrides.success,
})}
>
{renderContent('light', 'success')}
</Box>
</Box>
</CssVarsProvider>
);
}