-
Notifications
You must be signed in to change notification settings - Fork 1
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
Conversation
jade-data-repo-ui
|
Project |
jade-data-repo-ui
|
Branch Review |
sh/dt-1110-remove-mui-styles-part1
|
Run status |
|
Run duration | 02m 56s |
Commit |
|
Committer | Shelby Holden |
View all properties for this run ↗︎ |
Test results | |
---|---|
|
0
|
|
0
|
|
0
|
|
0
|
|
17
|
View all changes introduced in this branch ↗︎ |
e71a9d8
to
c83ff6d
Compare
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', | ||
}, |
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.
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)
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.
Since the refactoring is being done in stages, we can always pull this out into a separate style in the future.
b1577c1
to
5ac0898
Compare
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.
One main question, otherwise lgtm:
fontSize: '14px', | ||
lineHeight: '22px', | ||
fontWeight: '600', | ||
color: 'primary.main', |
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.
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
.
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.
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/DatasetView.tsx
Outdated
}, | ||
); | ||
return ( | ||
<Box sx={{ display: 'flex', justifyContent: 'center', marginTop: '1em' }}> |
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.
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
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.
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".
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.
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.
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.
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).
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.
👍🏽
80598fd
to
3a12537
Compare
import DatasetTable from './table/DatasetTable'; | ||
|
||
import { DatasetRoles } from '../constants'; | ||
|
||
const styles = (theme: CustomTheme) => ({ | ||
wrapper: { |
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.
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?
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.
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
fontSize: '14px', | ||
lineHeight: '22px', | ||
fontWeight: '600', | ||
color: 'primary.main', |
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.
just curious, why is this not a styled component? Is something like this just a symptom of copilot not counting very well?
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.
Yeah, it really seems like copilot can't compare numbers all that well 😭
Here's copilot's notes from this update:
I'll help migrate the DatasetOverviewPanel from JSS to use styled components and sx props. Let's analyze the styles:
- More than 3 styles:
- drawer (5 styles)
- drawerOpen (4 styles with transitions)
- drawerClose (5 styles with transitions and breakpoints)
- Convert to sx props:
- root (1 style)
- helpOverlayCloseButton (1 style)
- helpText (1 style)
- drawerPosition (1 style)
- header (4 styles)
- expandIcon (1 style)
- noRelationships (3 styles)
- divider (2 styles)
- fullViewButton (1 style)
- fullViewText (1 style)
Key changes made:
- Removed withStyles HOC and styles object
- Added styled import from @mui/material/styles
- Created StyledDrawer component for complex drawer styling
- Converted all simple styles to sx props
- Added Box component as root container
- Removed classes prop and references
- Simplified component exports
- Used theme spacing units where appropriate
- Maintained all functionality while modernizing the styling approach
- The component maintains the same styling and behavior while using modern MUI patterns.
.
It clearly notes that the "header" class has 4 styles and notes that the first group is for components with more than 3 styles, but still groups it with the sx props. So, I really don't understand why it groups it with the sx props group. But, it still does catch the more complex styles and generally seems to move the components with lots of styles to styled components. I think as long as it is loosely following this rule, then it is okay.
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.
I think this looks good overall, just a couple non-blocking questions
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.
I am seeing a lot of p
or m
as a shorthand for margin/padding - I don't know if that is an affordance that material provides, but it seems less readable to me than the CSS property - I would rather we stick with the CSS property versions? I commented a few examples
fontSize: '1.5rem', | ||
}, | ||
}); | ||
const RootContainer = styled(Box)({ |
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.
I really like this approach - very clean.
7a61899
to
6b51734
Compare
@s-rubenstein Yeah, this is specifically a feature of the sx prop for mui components: https://mui.com/system/getting-started/the-sx-prop/#spacing |
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.
LGTM - there are a number of places where we have 14px
vs 14
for styles, but tbh I think that's an existing issue and out of scope to unify to one or the other here.
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
I think we need to use @mui/material/styles instead? This is what is referenced in docs: https://mui.com/material-ui/customization/theming/ remove @mui/styles references from theme
Line was still showing up in middle of the page
…heme type was quite a pain - I feel pretty confident that we want to use @mui/material/styles since this is how we define our custom theme
All it is doing is setting the width at 100%. Let's move that to an sx prop rather than a shared component/theme. Let's make this clear what styles we're applying when it is just a simple 100% width.
f5651e9
to
ad32ffd
Compare
Oh, good catch - That's something I paid attention to in the first round, but forgot about in the second round. It's actually important that we pay attention to this for spacing styles because of how mui does theme. And, unfortunately, it looks different for styled vs. sx prop 🫠 I'll go back through the changes again. sx Prop spacingWith the sx prop, if you leave off "px", then it defaults to it theme spacing, which multiples the value by 8px. So, Styled spacingWith styled components, you have to add
![]() Other sx prop stylesTo make things even more confusing, these rules appear to not be consistent across sx props. Like sizing (so, height, width, etc.) does NOT default to the theme sizing, but grid does. These guides on sx prop and styled components are vital to getting through these changes. |
|
Background
We must discontinue use of @mui/styles in order to migrate from React 17 to 18. (source)
There is guide for this migration here: https://mui.com/material-ui/migration/migrating-from-jss
Options considered:
sx
prop + styled components from@mui/matertial/styles
- From migration guide - "We recommend sx API over styled for creating responsive styles or overriding minor CSS". We can use styled components for components with a lot of styles (4+) and for styling that is reused.This PR uses the first option and uses the
sx
prop alongside styled components. 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:
Common Issues
What step is the copilot upgrade taking? (example output from copilot)
I'll help migrate the SnapshotAccessRequestTable from JSS to use styled components and sx props.
Let's analyze the styles:
Key changes made:
The component maintains the same styling and behavior while using modern MUI patterns.
Example
Before with @mui/styles
With sx prop
##TODO - add example for styled component
TODO - add more notes about nuances of the changes from mui
Components Impacted in this PR
Updated screenshots as of 01-11-2024