[英]How can I set a height to a Dialog in Material-UI?
我將使用具有自定義寬度的Dialog
的 Material-UI 示例:
const customContentStyle = {
width: '100%',
maxWidth: 'none',
};
// some omitted code
<Dialog
title="Dialog With Custom Width"
actions={actions}
modal={true}
contentStyle={customContentStyle}
open={this.state.open}
>
This dialog spans the entire width of the screen.
</Dialog>
我知道我可以設置自定義寬度,因為我已經覆蓋了maxWidth
,但是我希望能夠對高度做同樣的事情,以便我可以調整對話框的高度。 我嘗試將maxHeight
設置為none
並設置height
,但我沒有運氣。
您需要覆蓋Dialog的一些默認行為。 它的paper
類實現了一個帶有柱狀 flex-direction 的 flexbox,並定義了90vh
的最大高度。 這允許 Dialog 在達到視口可見高度的 90% 時增長到其內容並顯示滾動條。
如果您需要將對話框高度設置為視口的某個百分比,請覆蓋paper
類,以類似於以下示例的方式定義min-height
和max-height
:
import PropTypes from 'prop-types';
import { withStyles } from 'material-ui/styles';
import Dialog from 'material-ui/Dialog';
const styles = {
dialogPaper: {
minHeight: '80vh',
maxHeight: '80vh',
},
};
const YourDialog = ({ classes }) => (
<Dialog classes={{ paper: classes.dialogPaper }}>
<div>dialogishness</div>
</Dialog>
);
export default withStyles(styles)(YourDialog);
這將確保 Dialog 的高度為視口的 80%。
改為設置 DialogContent 子項的高度。 對話框將增長以包含其內容。 您可以使用 css / classname 等來執行此操作...但要內聯執行此操作,這里有一個代碼片段:
<Dialog
open={open}
fullWidth
maxWidth='lg' // set width according to defined material ui breakpoints
onClose={handleClose}
>
<DialogContent
style={{height:'600px'}}> // set height by pixels, percentage, etc
//insert content here
</DialogContent>
</Dialog>
如果您使用的是較新版本的 material-ui,請使用:
import MuiDialog from '@material-ui/core/Dialog';
const Dialog = withStyles((theme) => ({
paper: {
height: '100%' // 100% is for full height or anything else what you need
},
}))(MuiDialog);
export default function SomeComponentThatUsesCustomizedDialog() {
return (
<Dialog>
...
</Dialog>
)
}
在接受的答案中使用的dialogPaper
道具對我不起作用,並在控制台中引發錯誤(我們使用的是 material-ui v.4.11+,它甚至沒有在官方對話框 css api docs中列出)。 因此,請改用paper
道具。
靈感來自 官方示例。
一些答案似乎過於復雜,這是一個快速而干凈的內聯方法,適用於 MUI v4,也可能適用於 v5:
<Dialog
open={true}
onClose={onClose}
... and other props
PaperProps={{ style: {
minHeight: '90%',
maxHeight: '90%',
}}}
>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.