Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the position of material-ui's dialog?

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!

like image 419
Kiki Avatar asked Apr 26 '20 10:04

Kiki


People also ask

How do I change the height of dialog material UI?

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.

How do you style dialog in material UI?

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.

How do I make material UI dialog full screen?

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.


Video Answer


2 Answers

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.

  • Demo: codesandbox.io
  • See the original article with explanation
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>
  )
}
like image 90
trungk18 Avatar answered Oct 13 '22 07:10

trungk18


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>
  );
}
like image 26
radovix Avatar answered Oct 13 '22 08:10

radovix