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

[material-ui][Alert] Convert to support zero runtime #41230

Merged
merged 27 commits into from
Mar 4, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
2f21ebc
add alert page
siriwatknp Feb 21, 2024
635482e
update next-app to use build files
siriwatknp Feb 21, 2024
1f8e0ea
migrate Alert
siriwatknp Feb 21, 2024
b6f3ed3
add the rest of demos
siriwatknp Feb 21, 2024
d17ab03
make the next-app works with workspace
siriwatknp Feb 22, 2024
aacd7b9
fix theme types
siriwatknp Feb 22, 2024
9b1de9e
Revert "fix theme types"
siriwatknp Feb 28, 2024
16655aa
Revert "make the next-app works with workspace"
siriwatknp Feb 28, 2024
4cc4324
Revert "add the rest of demos"
siriwatknp Feb 28, 2024
336cb50
Revert "update next-app to use build files"
siriwatknp Feb 28, 2024
514c651
Revert "add alert page"
siriwatknp Feb 28, 2024
b4782e9
Merge branch 'master' of https://github.com/mui/material-ui into mui-…
siriwatknp Feb 28, 2024
37842c1
add react-alert page to apps
siriwatknp Feb 28, 2024
1addb4f
add components index page
siriwatknp Feb 28, 2024
8975ea5
fix path
siriwatknp Feb 28, 2024
2397c77
add zero-runtime to the root so that apps can be built
siriwatknp Feb 28, 2024
1c615dd
Merge branch 'master' of https://github.com/mui/material-ui into mui-…
siriwatknp Feb 29, 2024
cd57f5a
convert AlertTitle
siriwatknp Feb 29, 2024
f6910df
use prettier module
siriwatknp Feb 29, 2024
eb39558
Merge branch 'master' of https://github.com/mui/material-ui into mui-…
siriwatknp Feb 29, 2024
0689c63
fix iterating palette and add tests
siriwatknp Mar 1, 2024
b4b8cb1
only test if ThemeProvider and createTheme are provided
siriwatknp Mar 1, 2024
9bf53e2
remove typo `only`
siriwatknp Mar 1, 2024
75c758b
Merge branch 'master' of https://github.com/mui/material-ui into mui-…
siriwatknp Mar 4, 2024
bcb07aa
run pnpm install at root
siriwatknp Mar 4, 2024
df1c916
run pnpm install apps
siriwatknp Mar 4, 2024
df00cea
remove material-nextjs from vite-app
siriwatknp Mar 4, 2024
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
31 changes: 26 additions & 5 deletions apps/zero-runtime-next-app/next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,20 +83,40 @@ const theme = extendTheme({
},
});

theme.getColorSchemeSelector = (key) => {
return `[data-mui-color-scheme="${key}"]`;
// TODO: Fix this from the Material UI side in a separate PR
theme.palette = theme.colorSchemes.light.palette;
theme.getColorSchemeSelector = (colorScheme) => {
return `@media (prefers-color-scheme: ${colorScheme})`;
};
const { css: rootCss } = theme.generateCssVars();
const { css: lightCss } = theme.generateCssVars('light');
const { css: darkCss } = theme.generateCssVars('dark');
theme.generateCssVars = (colorScheme) => {
if (colorScheme === 'dark') {
return {
css: darkCss,
selector: {
'@media (prefers-color-scheme: dark)': {
':root': darkCss,
},
},
};
}
if (colorScheme === 'light') {
return { css: lightCss, selector: ':root' };
}
return { css: rootCss, selector: ':root' };
};

// { [theme.getColorSchemeSelector('dark')]: { color: 'black' } }

/**
* @type {ZeroPluginConfig}
*/
const zeroPluginOptions = {
theme,
transformLibraries: ['local-ui-lib'],
transformLibraries: ['local-ui-lib', '@mui/material'],
sourceMap: true,
displayName: true,
transformSx: false,
};

/** @type {import('next').NextConfig} */
Expand All @@ -107,6 +127,7 @@ const nextConfig = {
typescript: {
ignoreBuildErrors: true,
},
transpilePackages: ['@mui/material'],
};

module.exports = withZeroPlugin(nextConfig, zeroPluginOptions);
16 changes: 9 additions & 7 deletions apps/zero-runtime-next-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,20 @@
"clean": "rimraf .next"
},
"dependencies": {
"@mui/zero-runtime": "workspace:^",
"@mui/utils": "workspace:^",
"@mui/base": "workspace:^",
"@mui/material": "workspace:^",
"@mui/system": "workspace:^",
"@mui/system": "file:../../packages/mui-system/build",
"@mui/utils": "file:../../packages/mui-utils/build",
"@mui/zero-runtime": "file:../../packages/zero-runtime",
"@mui/material": "file:../../packages/mui-material/build",
"@mui/material-nextjs": "workspace:^",
"@mui/icons-material": "file:../../packages/mui-icons-material/build",
"@emotion/cache": "latest",
"local-ui-lib": "workspace:^",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"next": "13.5.1"
"next": "latest"
},
"devDependencies": {
"@mui/zero-next-plugin": "workspace:^",
"@mui/zero-next-plugin": "file:../../packages/zero-next-plugin",
"@types/node": "^20.5.7",
"@types/react": "^18.2.55",
"@types/react-dom": "^18.2.19",
Expand Down
45 changes: 45 additions & 0 deletions apps/zero-runtime-next-app/src/app/alert/page.tsx
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';
Copy link
Member Author

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.

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>
);
}
9 changes: 8 additions & 1 deletion apps/zero-runtime-next-app/src/app/layout.tsx
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';
Copy link
Member Author

Choose a reason for hiding this comment

The 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';
Expand All @@ -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>
);
}
12 changes: 12 additions & 0 deletions apps/zero-runtime-next-app/src/app/theme.ts
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,
},
},
},
});
5 changes: 4 additions & 1 deletion apps/zero-runtime-next-app/src/augment.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import type { ExtendTheme } from '@mui/zero-runtime';
import type { Theme } from '@mui/material/styles';
import type {} from '@mui/material/themeCssVarsAugmentation';

declare module '@mui/zero-runtime/theme' {
interface ThemeTokens {
Expand Down Expand Up @@ -29,6 +31,7 @@ declare module '@mui/zero-runtime/theme' {
theme: ExtendTheme<{
colorScheme: 'light' | 'dark';
tokens: ThemeTokens;
}>;
}> &
Theme;
}
}
112 changes: 63 additions & 49 deletions packages/mui-material/src/Alert/Alert.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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;

Expand All @@ -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),
}),
},
})),
],
};
});

Expand Down Expand Up @@ -164,6 +177,7 @@ const Alert = React.forwardRef(function Alert(inProps, ref) {
color,
severity,
variant,
colorSeverity: color || severity,
Copy link
Member Author

@siriwatknp siriwatknp Feb 28, 2024

Choose a reason for hiding this comment

The 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);
Expand Down
Loading
Loading