-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
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
[material-ui][Alert] Convert to support zero runtime #41230
Changes from 4 commits
2f21ebc
635482e
1f8e0ea
b6f3ed3
d17ab03
aacd7b9
9b1de9e
16655aa
4cc4324
336cb50
514c651
b4782e9
37842c1
1addb4f
8975ea5
2397c77
1c615dd
cd57f5a
f6910df
eb39558
0689c63
b4b8cb1
9bf53e2
75c758b
bcb07aa
df1c916
df00cea
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { styled } from '@mui/zero-runtime'; | ||
import BasicAlerts from '../../../../../docs/data/material/components/alert/BasicAlerts'; | ||
import ColorAlerts from '../../../../../docs/data/material/components/alert/ColorAlerts'; | ||
import DescriptionAlerts from '../../../../../docs/data/material/components/alert/DescriptionAlerts'; | ||
import FilledAlerts from '../../../../../docs/data/material/components/alert/FilledAlerts'; | ||
import IconAlerts from '../../../../../docs/data/material/components/alert/IconAlerts'; | ||
import OutlinedAlerts from '../../../../../docs/data/material/components/alert/OutlinedAlerts'; | ||
import SimpleAlert from '../../../../../docs/data/material/components/alert/SimpleAlert'; | ||
|
||
const Main = styled('div')(({ theme }) => ({ | ||
display: 'flex', | ||
flexDirection: 'column', | ||
gap: '32px', | ||
marginInline: 'auto', | ||
maxWidth: '900px', | ||
paddingBlock: '16px', | ||
[theme.breakpoints.up('sm')]: { | ||
paddingInline: '24px', | ||
}, | ||
[theme.breakpoints.up('lg')]: { | ||
paddingInline: '60px', | ||
}, | ||
})); | ||
|
||
export default function MaterialUILayout() { | ||
return ( | ||
<Main> | ||
<h1>Alert</h1> | ||
<h2>Basic</h2> | ||
<BasicAlerts /> | ||
<h2>Color</h2> | ||
<ColorAlerts /> | ||
<h2>Description</h2> | ||
<DescriptionAlerts /> | ||
<h2>Filled</h2> | ||
<FilledAlerts /> | ||
<h2>Icon</h2> | ||
<IconAlerts /> | ||
<h2>Outlined</h2> | ||
<OutlinedAlerts /> | ||
<h2>Simple</h2> | ||
<SimpleAlert /> | ||
</Main> | ||
); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,8 @@ | ||
import type { Metadata } from 'next'; | ||
import { Inter } from 'next/font/google'; | ||
import { AppRouterCacheProvider } from '@mui/material-nextjs/v14-appRouter'; | ||
import { ThemeProvider } from '@mui/material/styles'; | ||
import theme from './theme'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add this to test that emotion still works with zero-runtime. |
||
import '@mui/zero-runtime/styles.css'; | ||
|
||
import './globals.css'; | ||
|
@@ -14,7 +17,11 @@ export const metadata: Metadata = { | |
export default function RootLayout(props: { children: React.ReactNode }) { | ||
return ( | ||
<html lang="en"> | ||
<body className={inter.className}>{props.children}</body> | ||
<body className={inter.className}> | ||
<AppRouterCacheProvider options={{ enableCssLayer: true }}> | ||
<ThemeProvider theme={theme}>{props.children}</ThemeProvider> | ||
</AppRouterCacheProvider> | ||
</body> | ||
</html> | ||
); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
'use client'; | ||
import { createTheme } from '@mui/material/styles'; | ||
|
||
export default createTheme({ | ||
components: { | ||
MuiStack: { | ||
defaultProps: { | ||
useFlexGap: true, | ||
}, | ||
}, | ||
}, | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,10 +2,9 @@ | |
import * as React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import clsx from 'clsx'; | ||
import { unstable_composeClasses as composeClasses } from '@mui/base'; | ||
import { darken, lighten } from '@mui/system'; | ||
import styled from '../styles/styled'; | ||
import useThemeProps from '../styles/useThemeProps'; | ||
import composeClasses from '@mui/utils/composeClasses'; | ||
import { darken, lighten } from '@mui/system/colorManipulator'; | ||
import { styled, createUseThemeProps } from '../zero-styled'; | ||
import useSlot from '../utils/useSlot'; | ||
import capitalize from '../utils/capitalize'; | ||
import Paper from '../Paper'; | ||
|
@@ -17,6 +16,8 @@ import ErrorOutlineIcon from '../internal/svg-icons/ErrorOutline'; | |
import InfoOutlinedIcon from '../internal/svg-icons/InfoOutlined'; | ||
import CloseIcon from '../internal/svg-icons/Close'; | ||
|
||
const useThemeProps = createUseThemeProps('MuiAlert'); | ||
|
||
const useUtilityClasses = (ownerState) => { | ||
const { variant, color, severity, classes } = ownerState; | ||
|
||
|
@@ -42,58 +43,70 @@ const AlertRoot = styled(Paper, { | |
styles[`${ownerState.variant}${capitalize(ownerState.color || ownerState.severity)}`], | ||
]; | ||
}, | ||
})(({ theme, ownerState }) => { | ||
})(({ theme }) => { | ||
const getColor = theme.palette.mode === 'light' ? darken : lighten; | ||
const getBackgroundColor = theme.palette.mode === 'light' ? lighten : darken; | ||
const color = ownerState.color || ownerState.severity; | ||
|
||
return { | ||
...theme.typography.body2, | ||
backgroundColor: 'transparent', | ||
display: 'flex', | ||
padding: '6px 16px', | ||
...(color && | ||
ownerState.variant === 'standard' && { | ||
color: theme.vars | ||
? theme.vars.palette.Alert[`${color}Color`] | ||
: getColor(theme.palette[color].light, 0.6), | ||
backgroundColor: theme.vars | ||
? theme.vars.palette.Alert[`${color}StandardBg`] | ||
: getBackgroundColor(theme.palette[color].light, 0.9), | ||
[`& .${alertClasses.icon}`]: theme.vars | ||
? { color: theme.vars.palette.Alert[`${color}IconColor`] } | ||
: { | ||
color: theme.palette[color].main, | ||
}, | ||
}), | ||
...(color && | ||
ownerState.variant === 'outlined' && { | ||
color: theme.vars | ||
? theme.vars.palette.Alert[`${color}Color`] | ||
: getColor(theme.palette[color].light, 0.6), | ||
border: `1px solid ${(theme.vars || theme).palette[color].light}`, | ||
[`& .${alertClasses.icon}`]: theme.vars | ||
? { color: theme.vars.palette.Alert[`${color}IconColor`] } | ||
: { | ||
color: theme.palette[color].main, | ||
}, | ||
}), | ||
...(color && | ||
ownerState.variant === 'filled' && { | ||
fontWeight: theme.typography.fontWeightMedium, | ||
...(theme.vars | ||
? { | ||
color: theme.vars.palette.Alert[`${color}FilledColor`], | ||
backgroundColor: theme.vars.palette.Alert[`${color}FilledBg`], | ||
} | ||
: { | ||
backgroundColor: | ||
theme.palette.mode === 'dark' | ||
? theme.palette[color].dark | ||
: theme.palette[color].main, | ||
color: theme.palette.getContrastText(theme.palette[color].main), | ||
}), | ||
}), | ||
variants: [ | ||
...Object.entries(theme.palette) | ||
.filter(([, value]) => value.main) | ||
.map(([color]) => ({ | ||
props: { colorSeverity: color, variant: 'standard' }, | ||
style: { | ||
color: theme.vars | ||
? theme.vars.palette.Alert[`${color}Color`] | ||
: getColor(theme.palette[color].light, 0.6), | ||
backgroundColor: theme.vars | ||
? theme.vars.palette.Alert[`${color}StandardBg`] | ||
: getBackgroundColor(theme.palette[color].light, 0.9), | ||
[`& .${alertClasses.icon}`]: theme.vars | ||
? { color: theme.vars.palette.Alert[`${color}IconColor`] } | ||
: { | ||
color: theme.palette[color].main, | ||
}, | ||
}, | ||
})), | ||
...Object.entries(theme.palette) | ||
.filter(([, value]) => value.main) | ||
.map(([color]) => ({ | ||
props: { colorSeverity: color, variant: 'outlined' }, | ||
style: { | ||
color: theme.vars | ||
? theme.vars.palette.Alert[`${color}Color`] | ||
: getColor(theme.palette[color].light, 0.6), | ||
border: `1px solid ${(theme.vars || theme).palette[color].light}`, | ||
[`& .${alertClasses.icon}`]: theme.vars | ||
? { color: theme.vars.palette.Alert[`${color}IconColor`] } | ||
: { | ||
color: theme.palette[color].main, | ||
}, | ||
}, | ||
})), | ||
...Object.entries(theme.palette) | ||
.filter(([, value]) => value.main) | ||
.map(([color]) => ({ | ||
props: { colorSeverity: color, variant: 'filled' }, | ||
style: { | ||
fontWeight: theme.typography.fontWeightMedium, | ||
...(theme.vars | ||
? { | ||
color: theme.vars.palette.Alert[`${color}FilledColor`], | ||
backgroundColor: theme.vars.palette.Alert[`${color}FilledBg`], | ||
} | ||
: { | ||
backgroundColor: | ||
theme.palette.mode === 'dark' | ||
? theme.palette[color].dark | ||
: theme.palette[color].main, | ||
color: theme.palette.getContrastText(theme.palette[color].main), | ||
}), | ||
}, | ||
})), | ||
], | ||
}; | ||
}); | ||
|
||
|
@@ -164,6 +177,7 @@ const Alert = React.forwardRef(function Alert(inProps, ref) { | |
color, | ||
severity, | ||
variant, | ||
colorSeverity: color || severity, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is required because we can't do: ...Object.entries(theme.palette)
.filter(([, value]) => value.main)
.map(([color]) => ({
// this will throw error because a function cannot read variable `color`.
props: ({ ownerState }) => color === (ownerState.color || ownerState.severity) && ownerState.variant === 'outlined',
style: {
color: theme.vars
? theme.vars.palette.Alert[`${color}Color`]
: getColor(theme.palette[color].light, 0.6),
border: `1px solid ${(theme.vars || theme).palette[color].light}`,
[`& .${alertClasses.icon}`]: theme.vars
? { color: theme.vars.palette.Alert[`${color}IconColor`] }
: {
color: theme.palette[color].main,
},
},
})), So (ownerState.color || ownerState.severity) needs to be resolved before passing to ownerState. |
||
}; | ||
|
||
const classes = useUtilityClasses(ownerState); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At first, I tried to pull all the demos programmatically and render them with lazy loading but not successful, so I'll just go with importing manually and revisit this when I have more time.