简体   繁体   English

如何禁用阴影或更改 Material-UI 所需的组件

[英]How to disable Shadows or alter required Components of Material-UI

I want to use <AppBar> component of Material UI.我想使用 Material UI 的<AppBar>组件。 But, it generates shadows.但是,它会产生阴影。 I searched for some solution and came across to change use the createMuiTheme, MuiThemeProvider and set default shadow to我搜索了一些解决方案并遇到了更改使用createMuiTheme, MuiThemeProvider并将默认阴影设置为

const theme = createMuiTheme({ shadows: ["none"] });

By doing this it removes shadows from every element I use.But , I want to use shadows for buttons and other components..通过这样做,它会从我使用的每个元素中删除阴影。但是,我想为按钮和其他组件使用阴影..

So, How can I change the shadow property of component only??那么,如何仅更改组件的阴影属性?

If the component has the appropriate props you can use the 'props' property in the object passed to createMuiTheme.如果组件具有适当的道具,您可以在传递给 createMuiTheme 的对象中使用“道具”属性。 For example, if I want to remove box shadows ("elevation") from all buttons in my app, I can use the following code:例如,如果我想从我的应用程序中的所有按钮中删除框阴影(“高度”),我可以使用以下代码:

import { createMuiTheme, ThemeProvider } from "@material-ui/core/styles";

const theme = createMuiTheme({
    props: {
      MuiButton: {
        disableElevation: true
     }
    }
});

// more code...
return <ThemeProvider theme={theme}>{children}</ThemeProvider>

See the 'Default Props' section in the Material UI docs: https://material-ui.com/customization/globals/请参阅 Material UI 文档中的“默认道具”部分: https : //material-ui.com/customization/globals/

This works by adding that prop as a default option to all buttons.这是通过将该道具添加为所有按钮的默认选项来实现的。 The nice thing is that if you occasionally want to add a property back, you can just add it to the component itself:好消息是,如果你偶尔想添加一个属性,你可以将它添加到组件本身:

// add box-shadow to this one button
<Button elevation={5}>Hooray, Box Shadow!</Button>

If the component doesn't take the relevant prop, then you can use the overrides:如果组件没有采用相关的道具,那么您可以使用覆盖:

const theme = createMuiTheme = ({
    overrides: {
      MuiButtonGroup: {
        // the contained class has the box-shadow css
        contained: {
          boxShadow: 'none'
        }
      }
    }
})

Unfortunately the Material UI spec is all over the place, so what works for one component won't necessarily work for the next!不幸的是,Material UI 规范无处不在,因此对一个组件有效的内容不一定适用于下一个!

You need to use override within Theme and specify component as well.您需要在主题中使用override并指定组件。 In your case you need to use MuiAppBar , which means that properties under it will effect only the AppBar component.在您的情况下,您需要使用MuiAppBar ,这意味着它下的属性只会影响AppBar组件。 Here is an example.这是一个例子。

const theme = createMuiTheme({
  overrides: {
    MuiAppBar: {
      boxShadow: 'none'
    }
  }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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