-
-
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
[AppBar] Migrate to emotion #24439
[AppBar] Migrate to emotion #24439
Changes from 1 commit
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 |
---|---|---|
@@ -1,25 +1,54 @@ | ||
import * as React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import clsx from 'clsx'; | ||
import withStyles from '../styles/withStyles'; | ||
import { deepmerge } from '@material-ui/utils'; | ||
import { unstable_composeClasses as composeClasses } from '@material-ui/unstyled'; | ||
import experimentalStyled from '../styles/experimentalStyled'; | ||
import useThemeProps from '../styles/useThemeProps'; | ||
import capitalize from '../utils/capitalize'; | ||
import Paper from '../Paper'; | ||
import { getAppBarUtilityClass } from './appBarClasses'; | ||
|
||
export const styles = (theme) => { | ||
const overridesResolver = (props, styles) => { | ||
const { styleProps } = props; | ||
|
||
return deepmerge(styles.root || {}, { | ||
...styles[`position${capitalize(styleProps.color)}`], | ||
mnajdova marked this conversation as resolved.
Show resolved
Hide resolved
|
||
...styles[`color${capitalize(styleProps.color)}`], | ||
}); | ||
}; | ||
|
||
const useUtilityClasses = (styleProps) => { | ||
const { color, position, classes } = styleProps; | ||
|
||
const slots = { | ||
root: ['root', `color${capitalize(color)}`, `position${capitalize(position)}`], | ||
}; | ||
|
||
return composeClasses(slots, getAppBarUtilityClass, classes); | ||
}; | ||
|
||
const AppBarRoot = experimentalStyled( | ||
Paper, | ||
{}, | ||
{ | ||
name: 'MuiAppBar', | ||
slot: 'Root', | ||
overridesResolver, | ||
}, | ||
)(({ theme, styleProps }) => { | ||
const backgroundColorDefault = | ||
theme.palette.mode === 'light' ? theme.palette.grey[100] : theme.palette.grey[900]; | ||
|
||
return { | ||
/* Styles applied to the root element. */ | ||
root: { | ||
display: 'flex', | ||
flexDirection: 'column', | ||
width: '100%', | ||
boxSizing: 'border-box', // Prevent padding issue with the Modal and fixed positioned AppBar. | ||
flexShrink: 0, | ||
}, | ||
display: 'flex', | ||
flexDirection: 'column', | ||
width: '100%', | ||
boxSizing: 'border-box', // Prevent padding issue with the Modal and fixed positioned AppBar. | ||
flexShrink: 0, | ||
/* Styles applied to the root element if `position="fixed"`. */ | ||
positionFixed: { | ||
...(styleProps.position === 'fixed' && { | ||
position: 'fixed', | ||
zIndex: theme.zIndex.appBar, | ||
top: 0, | ||
|
@@ -29,71 +58,88 @@ export const styles = (theme) => { | |
// Prevent the app bar to be visible on each printed page. | ||
position: 'absolute', | ||
}, | ||
}, | ||
}), | ||
/* Styles applied to the root element if `position="absolute"`. */ | ||
positionAbsolute: { | ||
...(styleProps.position === 'absolute' && { | ||
position: 'absolute', | ||
zIndex: theme.zIndex.appBar, | ||
top: 0, | ||
left: 'auto', | ||
right: 0, | ||
}, | ||
}), | ||
/* Styles applied to the root element if `position="sticky"`. */ | ||
positionSticky: { | ||
...(styleProps.position === 'sticky' && { | ||
// ⚠️ sticky is not supported by IE11. | ||
position: 'sticky', | ||
zIndex: theme.zIndex.appBar, | ||
top: 0, | ||
left: 'auto', | ||
right: 0, | ||
}, | ||
}), | ||
/* Styles applied to the root element if `position="static"`. */ | ||
positionStatic: { | ||
...(styleProps.position === 'static' && { | ||
position: 'static', | ||
}, | ||
}), | ||
/* Styles applied to the root element if `position="relative"`. */ | ||
positionRelative: { | ||
...(styleProps.position === 'relative' && { | ||
position: 'relative', | ||
}, | ||
}), | ||
/* Styles applied to the root element if `color="default"`. */ | ||
colorDefault: { | ||
...(styleProps.color === 'default' && { | ||
backgroundColor: backgroundColorDefault, | ||
color: theme.palette.getContrastText(backgroundColorDefault), | ||
}, | ||
}), | ||
/* Styles applied to the root element if `color="primary"`. */ | ||
colorPrimary: { | ||
...(styleProps.color === 'primary' && { | ||
backgroundColor: theme.palette.primary.main, | ||
color: theme.palette.primary.contrastText, | ||
}, | ||
}), | ||
/* Styles applied to the root element if `color="secondary"`. */ | ||
colorSecondary: { | ||
...(styleProps.color === 'secondary' && { | ||
backgroundColor: theme.palette.secondary.main, | ||
color: theme.palette.secondary.contrastText, | ||
}, | ||
}), | ||
/* Styles applied to the root element if `color="inherit"`. */ | ||
colorInherit: { | ||
...(styleProps.color === 'inherit' && { | ||
color: 'inherit', | ||
}, | ||
}), | ||
/* Styles applied to the root element if `color="transparent"`. */ | ||
colorTransparent: { | ||
...(styleProps.color === 'transparent' && { | ||
backgroundColor: 'transparent', | ||
color: 'inherit', | ||
}, | ||
}), | ||
mnajdova marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}; | ||
}; | ||
}); | ||
|
||
const AppBar = React.forwardRef(function AppBar(props, ref) { | ||
const { classes, className, color = 'primary', position = 'fixed', ...other } = props; | ||
const AppBar = React.forwardRef(function AppBar(inProps, ref) { | ||
const { | ||
className, | ||
color = 'primary', | ||
position = 'fixed', | ||
classes: inClasses, | ||
...other | ||
} = useThemeProps({ | ||
props: inProps, | ||
name: 'MuiAppBar', | ||
}); | ||
mnajdova marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
const styleProps = { | ||
...other, | ||
color, | ||
position, | ||
classes: inClasses, | ||
mnajdova marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}; | ||
|
||
const classes = useUtilityClasses(styleProps); | ||
|
||
return ( | ||
<Paper | ||
<AppBarRoot | ||
square | ||
component="header" | ||
styleProps={styleProps} | ||
elevation={4} | ||
className={clsx( | ||
classes.root, | ||
classes[`position${capitalize(position)}`], | ||
classes[`color${capitalize(color)}`], | ||
{ | ||
'mui-fixed': position === 'fixed', // Useful for the Dialog | ||
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. We may be able to get rid of this now that we have utility classes, but let's keep it as is to keep the PR focused to the migraiton 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. .mui-fixed probably is used even docs talks about it https://next.material-ui.com/getting-started/faq/#why-do-the-fixed-positioned-elements-move-when-a-modal-is-opened |
||
}, | ||
|
@@ -134,6 +180,10 @@ AppBar.propTypes = { | |
* @default 'fixed' | ||
*/ | ||
position: PropTypes.oneOf(['absolute', 'fixed', 'relative', 'static', 'sticky']), | ||
/** | ||
* The system prop that allows defining system overrides as well as additional CSS styles. | ||
*/ | ||
sx: PropTypes.object, | ||
}; | ||
|
||
export default withStyles(styles, { name: 'MuiAppBar' })(AppBar); | ||
export default AppBar; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
export interface AppBarClasses { | ||
root: string; | ||
positionFixed: string; | ||
positionAbsolute: string; | ||
positionSticky: string; | ||
positionStatic: string; | ||
positionRelative: string; | ||
colorDefault: string; | ||
colorPrimary: string; | ||
colorSecondary: string; | ||
colorInherit: string; | ||
colorTransparent: string; | ||
} | ||
|
||
declare const appBarClasses: AppBarClasses; | ||
|
||
export function getAppBarUtilityClass(slot: string): string; | ||
|
||
export default appBarClasses; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { generateUtilityClass, generateUtilityClasses } from '@material-ui/unstyled'; | ||
|
||
export function getAppBarUtilityClass(slot) { | ||
return generateUtilityClass('MuiAppBar', slot); | ||
} | ||
|
||
const appBarClasses = generateUtilityClasses('MuiAppBar', [ | ||
'root', | ||
'positionFixed', | ||
'positionAbsolute', | ||
'positionSticky', | ||
'positionStatic', | ||
'positionRelative', | ||
'colorDefault', | ||
'colorPrimary', | ||
'colorSecondary', | ||
'colorInherit', | ||
'colorTransparent', | ||
]); | ||
|
||
export default appBarClasses; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
export { default } from './AppBar'; | ||
export * from './AppBar'; | ||
export { default as appBarClasses } from './appBarClasses'; | ||
export * from './appBarClasses'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
export { default } from './AppBar'; | ||
export { default as appBarClasses } from './appBarClasses'; | ||
export * from './appBarClasses'; |
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.
Hopefully the changes on https://github.com/mui-org/material-ui/pull/24439/files#r558589220 will help us fix these regressions