DEV Community

Sean Saranga Amarasinghe
Sean Saranga Amarasinghe

Posted on

1 3

React Component Library with Material UI

Why Material UI?

Creating a custom component library from scratch can be tedious and time-consuming. Instead, it’s much faster to use one of an existing component libraries well-architected components than to spin up a custom one.
Instead of using Sass or JS in CSS, I prefer using Styled Components for the ability to pass functions and props, write regular CSS, inject custom properties like any other React component and just the overall component-driven nature of it.
Well, the good news is, Material UI pretty much supports it out of the box.

Setting up Material UI

Install Material-UI's source files via npm:

npm install @material-ui/core

Or using yarn:

yarn add @material-ui/core

Material-UI components work without any additional setup, and also don't pollute the global scope.

import styled from "styled-components";
import Button from "@material-ui/core/Button";

const StyledButton = styled(Button)`
  color: white;
  && :hover {
    color: blue;
  }
`;

Pretty simple isn't it?

Using ThemeProvider for avoiding use of && everywhere

If we want to be able to style our Material UI components without using && then we have to do the following:

import { StylesProvider } from '@material-ui/styles';
import Button from "@material-ui/core/Button";

<StylesProvider injectFirst>
  <Button>Hey there!</Button>
</StylesProvider>

All it does is ensure that Material UI’s styles get injected first so that Styled Components classes take priority.

Read more here

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay