[英]How do I customize Material UI in React?
I am very new at using this UI framework along with React. 我对将这个UI框架与React一起使用非常陌生。 I was allocated to develop an application which is supposed to have more design pattern and I chose a framework that also don't rely on Jquery code.
我被分配去开发一个应该具有更多设计模式的应用程序,我选择了一个也不依赖于Jquery代码的框架。 However, I am struggling to customize pre-made components.
但是,我正在努力定制预制组件。 They have a override section on their website but I didn't understand it very well.
他们在他们的网站上有一个替代部分,但我不太了解。
I know that there are 2 ways of customizing an element 我知道有两种自定义元素的方法
const styles = theme => { styles here }
. const styles = theme => { styles here }
。 Then you invoke a higher order component called withStyles. classes
prop. classes
上可用。 <Drawer classes={x}>
. <Drawer classes={x}>
。 the Second one is the one I don't understand exactly how it works. 第二个是我不清楚它是如何工作的。 For instance they have a component.
例如,它们具有一个组件。 But to change its background for me was very complicated, I had to change it manually on my custom .css file.
但是要为我更改背景非常复杂,我必须在自定义.css文件中手动进行更改。 I couldnt use className and even using classes property it didn't work.
我不能使用className,甚至无法使用classes属性。
Can anyone explain to me a little better how exactly the customization is done? 谁能更好地向我解释定制的完成方式?
The background, and other colors you can change by updating the theme . 您可以通过更新主题来更改背景和其他颜色。 That way you can customize primary/secondary background and text colors, as well as use that in your styles.
这样,您可以自定义主要/辅助背景和文本颜色,以及在样式中使用它们。
As for the custom styling of particular components; 至于特定组件的自定义样式; the idea is to use
withStyles
as a Higher Order Component, wrapping your components. 这个想法是将
withStyles
用作高阶组件,包装组件。 It takes theme
as the parameter and passes classes
as props to wrapped component. 它以
theme
为参数,并将classes
作为道具传递给包装的组件。
Example: 例:
import { withStyles } from '@material-ui/core/styles/withStyles';
const styles = theme => ({
someClass: {
padding: theme.spacing.unit * 5
},
otherClass: {
background: 'red'
}
});
Once the styles are defined, you can use them in your component like so: 定义样式后,可以在组件中使用它们,如下所示:
const MyComponent = (props) => {
return (<Button className={props.classes.someClass}>Some Action</Button>)
}
export default withStyles(styles)(MyComponent);
The withStyles
will pass the styles
in the props as classes
, and you can then use them. withStyles
将把props中的styles
作为classes
传递,然后可以使用它们。 If you're using functional components, you can access them via props.classes
, if you're extending Component
, they will be in this.props.classes
如果您使用的是功能组件,则可以通过
props.classes
访问它们,如果要扩展Component
,它们将位于this.props.classes
If you wish to use multiple classes, you'll need to install classnames
dependency, and import it: 如果您希望使用多个类,则需要安装
classnames
依赖项,并将其导入:
import React from 'react';
import { withStyles } from '@material-ui/core/styles/withStyles';
import classNames from 'classnames';
const styles = theme => ({
someClass: {
padding: theme.spacing.unit * 5
},
otherClass: {
background: 'red'
}
});
const MyComponent = (props) => {
return (<Button className={classNames(props.classes.someClass, props.classes.otherClass)}>Some Action</Button>)
}
export default withStyles(styles)(MyComponent);
The classes
property can also be used to set multiple classes, but that depends on the Material-UI component styling API. classes
属性也可以用于设置多个类,但这取决于Material-UI组件样式API。 For example, for Tab
component 例如,对于
Tab
组件
<Tab label="Hello" classes={ { root: classes.tab, selected: classes.tabSelected }} />
takes root
as styles to be applied by default, and selected
will be applied when tab is selected. 默认情况下将
root
用作要应用的样式,并且在selected
选项卡时将应用selected
样式。 In this case, the styles
would contain: 在这种情况下,
styles
将包含:
const styles = theme => ({
tab: {
minWidth: 'auto',
fontSize: '11px',
fontWeight: 'bold'
},
tabSelected: {
background: theme.palette.background.paper,
color: theme.palette.secondary.main
},
};
Note that these are using the Tab's CSS API , to map custom styles with predefined class names. 请注意,它们使用Tab的CSS API来映射具有预定义类名的自定义样式。
And, of course, the Component with Tab
would need to be wrapped in withStyles(styles)(Component)
. 而且,当然,带有
Tab
的Component需要包装在withStyles(styles)(Component)
。
Here's a live example with customized theme, and customized buttons taking multiple classes. 这是一个带有自定义主题的实时示例 ,并且自定义按钮具有多个类别。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.