Using material-ui in my react app, is there a way I can change the position when the dialog is opened? now it's always centered.
Thanks in advance!
To set a height of a dialog in React Material UI, we can set the classes prop of the dialog to an object that has the paper property set to a class that has the height style. We call makeStyles with a function that returns an object with the dialog property. It's set to an object with the height set to 500 pixels.
Customized Dialogs We create a styles function that we pass into the withStyles higher-order components with various styles. We moved the close button by setting some styles for the closeButton class. To create the DialogTitle component, we passed a component that uses the classes and children from the props.
You just need to add fullScreen flag to modal component in order to achieve full screen. And if you don't want to use fullScreen, simply remove that fullScreen flag and don't need to use CSS here.
I would say don't use position: absolute
, it could break the scrolling behavior. The position was control differently with scroll='paper'
or scroll='body'
You can use the following code to always align your dialog to the top of the page with two custom classes.
const useStyles = makeStyles({
topScrollPaper: {
alignItems: 'flex-start',
},
topPaperScrollBody: {
verticalAlign: 'top',
},
})
function SimpleDialog(props: SimpleDialogProps) {
const classes = useStyles()
return (
<Dialog
onClose={handleClose}
aria-labelledby="simple-dialog-title"
open={open}
scroll="paper"
classes={{
scrollPaper: classes.topScrollPaper,
paperScrollBody: classes.topPaperScrollBody,
}}
></Dialog>
)
}
You can create styles and pass it through classes
prop. Here is an example of how you could do that.
import React from 'react';
import { makeStyles, Dialog } from '@material-ui/core';
const useStyles = makeStyles({
dialog: {
position: 'absolute',
left: 10,
top: 50
}
});
function Example() {
const classes = useStyles();
return (
<Dialog
classes={{
paper: classes.dialog
}}
/* rest of the props */
>
{/* content of the dialog */}
</Dialog>
);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With