简体   繁体   中英

How to position Dialog to top in Material-UI

I am currently creating a popup dialog. However, my dialog is positioned popping up in the center.

How do I make the position at the very top of the page when I press the popup btn?

Here is my code

      <div>
      <div id="so_reg" className="buy_btn" onClick={this.onClickCashBuy.bind(this)}>
         Pop up button
        </div>
         <Dialog className="dialog" modal={false} open={this.state.buy_cash_popup} onRequestClose={this.onClickBuyCashCancel} style={{color: 'green'}} >
               <Test product={{price: this.state.buy_cash_type}} />
      </Dialog>
   </div>

You can override the style of scrollPaper in <Dialog />

Functional component

const useStyles = makeStyles({
  scrollPaper: {
    alignItems: 'baseline'  // default center
  }
});
const classes = useStyles();

Classical component

const styles = theme => createStyles({
  scrollPaper: {
    alignItems: 'baseline'  // default center
  }
});
const { classes } = props;
export default withStyles(styles)(YourComponent);

Usage

<Dialog classes={{scrollPaper: classes.scrollPaper }}>

Refer: document of Material-UI Dialog CSS API

The HTML structure of Dialog which we can see from dev tools

Choose the MuiDialog-scrollPaper from below

<div
  class="MuiDialog-container MuiDialog-scrollPaper makeStyles-dialog-163"
  role="none presentation"
  tabindex="-1"
  style="opacity: 1; transition: opacity 225ms cubic-bezier(0.4, 0, 0.2, 1) 0ms;"
>
  <div
    class="MuiPaper-root MuiDialog-paper MuiDialog-paperScrollPaper MuiDialog-paperWidthSm MuiPaper-elevation24 MuiPaper-rounded"
    role="dialog"
    aria-labelledby="simple-dialog-title"
  >
    ...
  </div>
</div>

在此处输入图像描述


In your code

import { withStyles } from '@material-ui/styles';

const styles = theme => createStyles({ // change to this
    scrollPaper: {
      alignItems: 'baseline'
    }
});

class Shop extends Component {
  render(){

const { classes } = this.props;  // add this

  return(
<Dialog className="dialog" classes={{scrollPaper: classes.scrollPaper }} modal={false} open={this.state.buy_cash_popup} onRequestClose={this.onClickBuyCashCancel} style={{color: 'green'}} >
 <Test product={{price: this.state.buy_cash_type}} />
 </Dialog>
export default withStyles(styles)(Shop);  // `styles` here rather than `class`

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM