React is a highly popular Javascript library developed by Facebook and is mainly used for component-based user interfaces. React supports quite a few libraries to reduce boilerplate code and add to the efficiency of standalone React, one of them being Material UI.
Material UI is a React library that offers developers a set of pre-designed components following the Material Design principles. Such components could be in the form of buttons, menus, dialog boxes, cards, progress bars, and much more.
Note: Material Design is a Google developed language having a main focus on modern themes and standardizes user interface designs.
React JS and Material UI are often used together in web development projects. React provides the foundation for building interactive UI components, while Material UI offers a library of ready-to-use components following Material Design's guidelines.
A React JS project will be needed to implement Material UI components inside it. This can be done by following the steps given here.
Alternatively, these two commands can be run to set up the project quickly.
npx create-react-app react-proj
cd react-proj
We can add as many utility libraries as we may need since our React project is now initialized with a node_modules
folder, as well as package.json
and package-lock.json
files.
For Material-UI, we need to install a few packages related to it.
On your terminal, run the following command:
npm install @mui/material @emotion/react @emotion/styled
Note: For yarn, the following command can be used to install Material UI packages:
yarn add @mui/material @emotion/react @emotion/styled
Note: Find our article on yarn vs npm here.
Once our project and major dependencies have been set up, we can start adding our own code to the project files.
To modify the project code:
Open the project in an editor like VS Code.
React creates a "src" folder by default. This folder contains an "App.js" file which can be modified per our needs, and even custom component files can be added.
Since Material UI is an ever-evolving library, minor changes in installation commands or syntax may occur from time to time. This answer caters to the current latest version of Material UI i.e. V5.
Material UI offers a number of components for user-interface design. We'll be looking at some of the most common ones below.
Components | Purpose |
Button | A clickable button with various styles and variants. |
Popover | A floating overlay that appears above other content. This is usually triggered by an action or button. |
Card | A flexible and customizable container for displaying content. |
Dialog | A modal dialog box that can display messages, forms, or other content. |
TextField | An input component for users to enter text or make selections. |
Icon | A set of scalable and customizable icons for UI elements. |
Typography | A component for displaying various types of text with different styles and sizes. |
Slider | A component that allows users to select a value from a range by sliding the component. |
Checkbox | A component that allows users to select one or more options from a list. |
Autocomplete | An input component that provides suggestions based on what the user writes. |
Material UI provides extensive documentation on each of its components. It provides a source code for both Javascript and Typescript. Let's look at the Javascript code provided by it for a plain card named Outlined Card.
# Getting Started with Create React App This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app). ## Available Scripts In the project directory, you can run: ### `npm start` Runs the app in the development mode.\ Open [http://localhost:3000](http://localhost:3000) to view it in your browser. The page will reload when you make changes.\ You may also see any lint errors in the console. ### `npm test` Launches the test runner in the interactive watch mode.\ See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information. ### `npm run build` Builds the app for production to the `build` folder.\ It correctly bundles React in production mode and optimizes the build for the best performance. The build is minified and the filenames include the hashes.\ Your app is ready to be deployed! See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information. ### `npm run eject` **Note: this is a one-way operation. Once you `eject`, you can't go back!** If you aren't satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project. Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you're on your own. You don't have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn't feel obligated to use this feature. However we understand that this tool wouldn't be useful if you couldn't customize it when you are ready for it. ## Learn More You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started). To learn React, check out the [React documentation](https://reactjs.org/). ### Code Splitting This section has moved here: [https://facebook.github.io/create-react-app/docs/code-splitting](https://facebook.github.io/create-react-app/docs/code-splitting) ### Analyzing the Bundle Size This section has moved here: [https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size](https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size) ### Making a Progressive Web App This section has moved here: [https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app](https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app) ### Advanced Configuration This section has moved here: [https://facebook.github.io/create-react-app/docs/advanced-configuration](https://facebook.github.io/create-react-app/docs/advanced-configuration) ### Deployment This section has moved here: [https://facebook.github.io/create-react-app/docs/deployment](https://facebook.github.io/create-react-app/docs/deployment) ### `npm run build` fails to minify This section has moved here: [https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify](https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify)
It can be seen that Material UI provides both atomic and grouped-together components like Box
, Button
, and Card
. The OutlinedCard
is further created by using various simpler components together. Below is the default implementation that Material UI offers.
Note: Other components can be further explored through Material UI's official documentation here.
We can now make use of our newfound knowledge to create our own React applications paired with Material UI. Let's use different components offered by this library and integrate them within our code!
# Getting Started with Create React App This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app). ## Available Scripts In the project directory, you can run: ### `npm start` Runs the app in the development mode.\ Open [http://localhost:3000](http://localhost:3000) to view it in your browser. The page will reload when you make changes.\ You may also see any lint errors in the console. ### `npm test` Launches the test runner in the interactive watch mode.\ See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information. ### `npm run build` Builds the app for production to the `build` folder.\ It correctly bundles React in production mode and optimizes the build for the best performance. The build is minified and the filenames include the hashes.\ Your app is ready to be deployed! See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information. ### `npm run eject` **Note: this is a one-way operation. Once you `eject`, you can't go back!** If you aren't satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project. Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you're on your own. You don't have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn't feel obligated to use this feature. However we understand that this tool wouldn't be useful if you couldn't customize it when you are ready for it. ## Learn More You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started). To learn React, check out the [React documentation](https://reactjs.org/). ### Code Splitting This section has moved here: [https://facebook.github.io/create-react-app/docs/code-splitting](https://facebook.github.io/create-react-app/docs/code-splitting) ### Analyzing the Bundle Size This section has moved here: [https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size](https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size) ### Making a Progressive Web App This section has moved here: [https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app](https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app) ### Advanced Configuration This section has moved here: [https://facebook.github.io/create-react-app/docs/advanced-configuration](https://facebook.github.io/create-react-app/docs/advanced-configuration) ### Deployment This section has moved here: [https://facebook.github.io/create-react-app/docs/deployment](https://facebook.github.io/create-react-app/docs/deployment) ### `npm run build` fails to minify This section has moved here: [https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify](https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify)
Lines 1–15: We import the necessary modules and components from Material UI. The styles for our application are imported from custom.css
.
Lines 17–104: We define a functional component called CustomMaterialUI
. Inside the component, we return JSX code representing various elements.
Lines 20–101: We use the Grid
component within a Container
from Material-UI to create a responsive grid layout. The grid further consists of more grids. Within the first three subgrids, we render card components, each representing a Card
with an image, title, description, and a button.
Lines 23–36, 39–52, 55–68: For each Card component, we make a unique gradient image as a background using CSS, a title using Typography
component, and a description. We also set different variant
, color
, size
, and className
attributes for the Button
component.
Line 74: We use the Slider
component from Material-UI to create a slider with a default value
of 50 and a large size
.
Line 80: We use the CircularProgress
component from Material-UI to create a circular progress bar with a size
of 100 and a thickness
of 4.
Line 81: We use the LinearProgress
component from Material-UI to create a linear progress bar with a variant
determinate value of 60.
Lines 83–99: We use the Button
and IconButton
components to create various buttons in a Grid
. Each has a different variant
, color
, size
, and className
. The IconButton
components also include icons of PlayArrow
and Pause
. The Typography
component is used to add text to aid our visuals.
Line 106: We export the CustomMaterialUI component so that it can be rendered within "App.js".
The above code renders the different components we imported from Material UI. Let's take a look at each of them.
The code within the "CustomMaterialUI.jsx" file is rendered within "App.js" and displays the following output.
Congratulations, we were able to make such aesthetic displays using Material UI so quickly!
Let’s test our Material UI knowledge!
What is the first step we need to take if we wish to use a card in our code?
Free Resources