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

DT-1110: Migrate away from @mui/styles Part 1 #1730

Open
wants to merge 21 commits into
base: develop
Choose a base branch
from

Conversation

snf2ye
Copy link
Contributor

@snf2ye snf2ye commented Jan 3, 2025

Background

We must discontinue use of @mui/styles in order to migrate from React 17 to 18. (source)

@mui/styles was deprecated with the release of MUI Core v5 in late 2021. It is not compatible with React.StrictMode or React 18+, and it will not be updated. Please use @mui/system instead.

There is guide for this migration here: https://mui.com/material-ui/migration/migrating-from-jss

Options considered:

  • Primarily use the sx prop and use styled components for styles that are used multiple times - From migration guide - "We recommend sx API over styled for creating responsive styles or overriding minor CSS"
  • tss-react - Easiest change, but still would require using copilot per component. It would be more of a lateral step.

This PR uses the first option and mostly sticks the sx prop. We could also use styled components here where it makes sense. I see this first option as an upgrade that should help us align with best practices. Given that we have to touch every file manually and I think we can utilize copilot for the bulk of the work, I would prefer to do the harder change rather than the lateral change.

Steps for Upgrades

Pre-checks:

  • Review the styles listed at the beginning of the file. Are they all referenced in the component? (Apparently if there are styles that don't match up to a component, it will try to infer where the component with those styles should go and add it)
  1. Run copilot command

Can you migrate this component away from using JSS and references to '@mui/styles' and instead use '@mui/system'? Please use the "sx" prop wherever possible instead of "styled". If you need to use the "sx" prop, then we must use the material UI version of the component (e.g. Box instead of div and add an import statement at the beginning of the file). We must also switch any css references of direct numbers to include px (e.g. '10px' instead of 10)

  1. Apply prettier
  2. Review UI and code generated
  3. Compare UI/styling between local UI and dev

Common Issues

  • It doesn't handle theme changes well - 30ca7c2

What step is the copilot upgrade taking?

  1. Remove the import statements for withStyles, createStyles, WithStyles and any other references from @mui/styles.
  2. Add import statements for Box from @mui/material. We need to use Box instead of div and span so that we can use the sx prop. This prop is only available on components from Material UI. If we want to still use generic component, we can instead use styled components.
  3. Update the component to use the sx prop for styling. We move any styles listed for a given class from the style constant inline into an sx prop.

Example
Before with @mui/styles

const styles = (theme: CustomTheme) => ({
  wrapper: {
    display: 'flex',
    justifyContent: 'center',
    marginTop: '1em',
  }
);
....
 <div className={classes.wrapper}>
...
</div>

With sx prop

 <Box sx={{ display: 'flex', justifyContent: 'center', marginTop: '1em' }}>
...
</Box>

Components Impacted in this PR

Recording Video

  1. HomeView and 2) DatasetView
image
  1. JobView
image
  1. SnapshotView
image
  1. DatasetOverviewn and 6) DatasetOverviewPanel
image
  1. DatasetAccess and 8) UserList
image
  1. SnapshotAccessRequestTable
image

Copy link

cypress bot commented Jan 3, 2025

jade-data-repo-ui    Run #3920

Run Properties:  status check passed Passed #3920  •  git commit 10fabcb73b ℹ️: Merge 3a12537f4016d5606888b62d92fcf72522e8801c into 79c0423d9cfcafed88655352e6c8...
Project jade-data-repo-ui
Branch Review sh/dt-1110-remove-mui-styles-part1
Run status status check passed Passed #3920
Run duration 02m 52s
Commit git commit 10fabcb73b ℹ️: Merge 3a12537f4016d5606888b62d92fcf72522e8801c into 79c0423d9cfcafed88655352e6c8...
Committer Shelby Holden
View all properties for this run ↗︎

Test results
Tests that failed  Failures 0
Tests that were flaky  Flaky 0
Tests that did not run due to a developer annotating a test with .skip  Pending 0
Tests that did not run due to a failure in a mocha hook  Skipped 0
Tests that passed  Passing 17
View all changes introduced in this branch ↗︎

@snf2ye snf2ye force-pushed the sh/dt-1110-remove-mui-styles-part1 branch from e71a9d8 to c83ff6d Compare January 3, 2025 16:24
src/components/JobView.tsx Outdated Show resolved Hide resolved
Comment on lines +247 to +278
sx={{
top: '112px',
bottom: '0px',
flexShrink: 0,
height: 'initial',
zIndex: 10,
minWidth: 300,
width: '40%',
position: 'absolute',
transition: () =>
theme.transitions.create('width', {
easing: theme.transitions.easing.sharp,
duration: theme.transitions.duration.enteringScreen,
}),
'&.drawerClose': {
transition: () =>
theme.transitions.create('width', {
easing: theme.transitions.easing.sharp,
duration: theme.transitions.duration.leavingScreen,
}),
overflowX: 'hidden',
width: '0px',
minWidth: '0px',
[theme.breakpoints.up('sm')]: {
width: '0px',
},
Copy link
Contributor Author

@snf2ye snf2ye Jan 6, 2025

Choose a reason for hiding this comment

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

For more complicated styles like this, it may make sense to pull it into a styled component to help with code readability. But, this styling is still only used in one spot so I'm a torn over which is better (leave as sx prop vs. pull into styled component)

Copy link
Contributor

Choose a reason for hiding this comment

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

Since the refactoring is being done in stages, we can always pull this out into a separate style in the future.

@snf2ye snf2ye force-pushed the sh/dt-1110-remove-mui-styles-part1 branch 2 times, most recently from b1577c1 to 5ac0898 Compare January 7, 2025 21:34
@snf2ye snf2ye marked this pull request as ready for review January 8, 2025 13:55
@snf2ye snf2ye requested a review from a team as a code owner January 8, 2025 13:55
@snf2ye snf2ye requested review from rushtong and s-rubenstein and removed request for a team January 8, 2025 13:55
Copy link
Contributor

@fboulnois fboulnois left a comment

Choose a reason for hiding this comment

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

One main question, otherwise lgtm:

fontSize: '14px',
lineHeight: '22px',
fontWeight: '600',
color: 'primary.main',
Copy link
Contributor

Choose a reason for hiding this comment

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

Do these need to import theme from 'modules/theme'; and use theme.palette.primary.main instead like in src/components/DatasetView.tsx? If so, a number of components in this PR have this issue with color.

Copy link
Contributor Author

@snf2ye snf2ye Jan 8, 2025

Choose a reason for hiding this comment

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

Great question - I believe it is correct as is - Here's the documentation and the basic example has something very similar. We have "ThemeProvider" set up in the index, so the theme should be available to all components in the repo.

I'm possibly not referencing the theme correctly in DatasetView, but I wasn't quite sure how to do the expansion of the custom mixin.

src/components/UserList.tsx Show resolved Hide resolved
snf2ye added 13 commits January 8, 2025 09:59
Command used:
Can you migrate this component away from using JSS and references to '@mui/styles' and instead use '@mui/system'? Please use the "sx" prop wherever possible instead of "styled". If you need to use the "sx" prop, then we must use the material UI version of the component (e.g. Box instead of div and add an import statement at the beginning of the file). We must also switch any css references of direct numbers to include px (e.g. '10px' instead of 10)
Im not sure where it got that maxwidth from

Update references
- title isn't used anywhere

add comma
Command:
Can you migrate this component away from using JSS and references to '@mui/styles' and instead use '@mui/system'? Please use the "sx" prop wherever possible instead of "styled". If you need to use the "sx" prop, then we must use the material UI version of the component (e.g. Box instead of div and add an import statement at the beginning of the file). We must also switch any css references of direct numbers to include px (e.g. '10px' instead of 10)
- Switch again to use theme mixins
- userEmail is not used
},
);
return (
<Box sx={{ display: 'flex', justifyContent: 'center', marginTop: '1em' }}>
Copy link
Contributor

@s-rubenstein s-rubenstein Jan 8, 2025

Choose a reason for hiding this comment

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

If you want to reduce the diff here, and keep the object abstractions, this could be <Box sx={styles(theme).wrapper}> just as easily, and that way we keep our object abstractions while moving to the sx property?

To do this you would need to have styles be a raw object rather than a method (or call it once if we have a theme we could pass), but that should be okay

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's a really interesting idea, and I'm torn about it.
I think I'm less interested in making the diff smaller (copilot is doing the heavy lifting here to move things around), and want to think about how we want this to look long term. We often use existing code as a model for new code, so what do we want our best practices to be going forward?
I think I generally like the sx prop styles to be inline for a small set of changes, but we may want to consider using styled components more frequently to keep a pseudo object abstraction. I could actually build that into the copilot request - like "if there are more than 3 css styles, use a styled component instead of the sx prop".

Copy link
Contributor

Choose a reason for hiding this comment

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

That's my ultimate preference as well. 3 css styles is fine for inline, more gets bulky. I also think if we have a often reused width or something - with lots of constants reused, that I'd abstract.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Turns out that its tricky to get copilot to handle these two cases 🫠 It's not great at counting, apparently (It would catch some components with >3 styles, but not all).
We may have to do this in two steps (convert to sx prop or styled and then move the rest). I think I'm also having better luck with a newer gpt model in vs code (as opposed to intellij).

Copy link
Contributor

@rushtong rushtong left a comment

Choose a reason for hiding this comment

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

👍🏽

@snf2ye snf2ye force-pushed the sh/dt-1110-remove-mui-styles-part1 branch from 80598fd to 3a12537 Compare January 8, 2025 17:23
Copy link

sonarqubecloud bot commented Jan 8, 2025

import DatasetTable from './table/DatasetTable';

import { DatasetRoles } from '../constants';

const styles = (theme: CustomTheme) => ({
wrapper: {
Copy link
Contributor

Choose a reason for hiding this comment

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

so just a general comment, this is moving us away from having separate style objects and towards inline styling. Is that the principle that we want to follow? I thought that separate objects were preferred because they are reusable and easier to maintain. But maybe that isn't the case. What are the guidelines from the UI working group on this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

My goal in this PR is to use best practices for Material UI. We're moving from an old Material UI concept (@mui/styles), to a new one - sx prop + styled components. I don't think Terra UI uses Material UI, but I'm glad to still run this by them.

RE: separate objects that are reusable/easier to maintain: We should use styled components for this case. The "sx prop" should be used for styles that are used in one spot, styled components are reusable and then we use the global theme to share styling variables. I think this is a helpful guide here: https://mui.com/material-ui/customization/how-to-customize

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants