Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Styling material UI components

Not really a problem but something I’m not happy with. I'm using react + typescript + css modules + https://material-ui-next.com/. Problem is that when I need to style material ui components I have to use !important a lot. Question is if there is a way to create styles without important. I create a sample project to reproduce the problem https://github.com/halkar/test-css-modules

like image 339
Artur Shamsutdinov Avatar asked Apr 28 '18 07:04

Artur Shamsutdinov


People also ask

How do you change the style of material UI components?

To customize a specific part of a component, you can use the class name provided by Material UI inside the sx prop. As an example, let's say you want to change the Slider component's thumb from a circle to a square. First, use your browser's dev tools to identify the class for the component slot you want to override.

Is material UI styled-components?

Material UI was designed to be used from the get-go with its own styling solution, implemented over JSS, as opposed to using styled-components. Fortunately, Material UI has made it simple to use other styling solutions.

How do I add styling to material UI?

Luckily, Material-UI provides a solution for this: makeStyles . Using makeStyles , you can add CSS in JS without making your code look messy. First, you need to import makeStyles in your app. Next, pass all the CSS you need in your app as an object to makeStyles and store it inside a variable, useStyles .

What are the styled-components?

Styled-components is a popular library that is used to style React applications. It allows you to build custom components by writing actual CSS in your JavaScript. In this article, we will take a look at how to use styled-components in a React application.


1 Answers

material-ui exposes many of their components for styling. There two ways to go about doing this.

Apply styles globally

You could style the components globally and apply it to the theme. An example of this would be something like this (copied from the docs http://www.material-ui.com/#/customization/themes):

import React from 'react';
import {cyan500} from 'material-ui/styles/colors';
import MuiThemeProvider from 'material-ui/styles/MuiThemeProvider';
import getMuiTheme from 'material-ui/styles/getMuiTheme';
import AppBar from 'material-ui/AppBar';

// This replaces the textColor value on the palette
// and then update the keys for each component that depends on it.
// More on Colors: http://www.material-ui.com/#/customization/colors
const muiTheme = getMuiTheme({
  palette: {
    textColor: cyan500,
  },
  appBar: {
    height: 50,
  },
});

class Main extends React.Component {
  render() {
    // MuiThemeProvider takes the theme as a property and passed it down the hierarchy
    // using React's context feature.
    return (
      <MuiThemeProvider muiTheme={muiTheme}>
        <AppBar title="My AppBar" />
      </MuiThemeProvider>
    );
  }
}

export default Main;

As you can see in here, appBar component have a height of 50px meaning that every time you add an appbar component to your app down the tree where you applied the muiTheme, it will give it a height of 50px. This is a list of all the styles you can apply for each component https://github.com/callemall/material-ui/blob/master/src/styles/getMuiTheme.js.

Apply styles using style attribute

To apply the styles to individual components, you can usually use the style property and pass it the styles you want.

This is another example from the docs where a margin of 12px is applied to a RaisedButton.

import React from 'react';
import RaisedButton from 'material-ui/RaisedButton';

const style = {
  margin: 12,
};

const RaisedButtonExampleSimple = () => (
  <div>
    <RaisedButton label="Default" style={style} />
    <RaisedButton label="Primary" primary={true} style={style} />
    <RaisedButton label="Secondary" secondary={true} style={style} />
    <RaisedButton label="Disabled" disabled={true} style={style} />
    <br />
    <br />
    <RaisedButton label="Full width" fullWidth={true} />
  </div>
);

export default RaisedButtonExampleSimple;

Now, the styles are defined in the same file but you could define them in a separate file and import them to the file where you are using the components.

If you want to apply multiple styles then you can use the spread operator like so: style={{...style1,...style2}}.

Usually, you are styling a specific thing in the component (root element) with the style property but some components have more than one property to style different elements of the component. Under properties in this page http://www.material-ui.com/#/components/raised-button, you can see that there are style property, labelStyle and rippleStyle to style different parts of RaisedButton.

Check the properties under the component that you are using and see which style property you could use, otherwise check the available global style properties you could override. Hope this helps!

like image 164
Gautam Naik Avatar answered Sep 22 '22 06:09

Gautam Naik