首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >与样式部件反应的MaterialUI

与样式部件反应的MaterialUI
EN

Stack Overflow用户
提问于 2020-02-04 18:34:55
回答 2查看 7.9K关注 0票数 4

我想对MaterialUI的PaperDialog进行样式化

代码语言:javascript
运行
复制
const StyledDialog = styled(Dialog)`
  & .MuiPaper-root {
    width: 600px;
  }
`;

但是,这意味着所有嵌套在对话框中的具有MuiPaper-root类(例如,其他文件)的元素都将继承它。

是否有任何方法将此样式限定为第一个对话框?所使用的纸张

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-02-04 19:35:26

有几种方法可以解决这个问题。一种方法是使用子选择器(正如Kaca992 992的答案中提到的),但是Paper不是Dialog的直接子元素,所以要使用这种方法,您需要& > .MuiDialog-container > .MuiPaper-root。另一种选择是使用Dialog的PaperComponent 道具并为其提供一个样式化的Paper组件。第三种选择是利用MuiDialog-paper CSS类

下面的例子显示了这三种方法。

代码语言:javascript
运行
复制
import React from "react";
import Button from "@material-ui/core/Button";
import DialogTitle from "@material-ui/core/DialogTitle";
import Dialog from "@material-ui/core/Dialog";
import Paper from "@material-ui/core/Paper";
import styled from "styled-components";

const StyledDialog = styled(Dialog)`
  & > .MuiDialog-container > .MuiPaper-root {
    background-color: purple;
  }
`;
const StyledDialog2 = styled(Dialog)`
  & .MuiDialog-paper {
    background-color: blue;
  }
`;
const StyledPaper = styled(Paper)`
  background-color: green;
`;

export default function SimpleDialogDemo() {
  const [open1, setOpen1] = React.useState(false);
  const [open2, setOpen2] = React.useState(false);
  const [open3, setOpen3] = React.useState(false);

  return (
    <div>
      <Button variant="outlined" color="primary" onClick={() => setOpen1(true)}>
        Open dialog using child selectors
      </Button>
      <Button variant="outlined" color="primary" onClick={() => setOpen3(true)}>
        Open dialog using MuiDialog-paper
      </Button>
      <Button variant="outlined" color="primary" onClick={() => setOpen2(true)}>
        Open dialog using custom Paper
      </Button>
      <StyledDialog
        onClose={() => setOpen1(false)}
        aria-labelledby="simple-dialog-title"
        open={open1}
      >
        <DialogTitle id="simple-dialog-title">
          Paper styled via child selectors
        </DialogTitle>
      </StyledDialog>
      <StyledDialog2
        onClose={() => setOpen3(false)}
        aria-labelledby="simple-dialog-title"
        open={open3}
      >
        <DialogTitle id="simple-dialog-title">
          Paper styled via MuiDialog-paper
        </DialogTitle>
      </StyledDialog2>
      <Dialog
        onClose={() => setOpen2(false)}
        aria-labelledby="simple-dialog-title"
        open={open2}
        PaperComponent={StyledPaper}
      >
        <DialogTitle id="simple-dialog-title">
          Paper styled via custom Paper component
        </DialogTitle>
      </Dialog>
    </div>
  );
}

票数 5
EN

Stack Overflow用户

发布于 2020-02-04 18:40:48

试试这个:

代码语言:javascript
运行
复制
const StyledDialog = styled(Dialog)`
  & > .MuiPaper-root {
    width: 600px;
  }
`;

css >运算符将只接受对话框组件的直接子程序。如果这不合适,请查看其他css运算符:selectors.asp

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60063602

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档