SlideShare a Scribd company logo
How to Convert a Component
Design into an MUI React Code
Material UI or MUI library provides you with robust, customizable, accessible,
and advanced components, enabling you to build your own design system and
develop React applications faster. That includes a huge list of Material icons,
foundational components with MUI Core, advanced and powerful components
with MUI X, templates, and design kits!
In this tutorial, we will see how to take a given design and convert it into an
actual component code by styling it with MUI Core configurations for a React
app. Let’s begin!
Converting a component design to an MUI
code in React
If you are provided with a design file with a simple component to build on React
there are so many ways, but here with MUI’s extensive pre-built components,
the process becomes much easier, faster, accessible, and most importantly
customizable!
What we will be making?
We will be taking the following barcode component design and implementing it
with MUI for React:
As you can see, we have three items:
1. The barcode component with the two sections.
2. The first section holds the barcode image in a blue container.
3. The second section below the barcode image has all the typography
elements like a heading and a paragraph.
Step 1: Start a New React project
Make sure you have Node.js installed on your system and then run the following
commands:
<code>npx create-react-app mui-barcode-app
cd mui-barcode-app
npm start</code>
This will bootstrap a new React application and run the default app
on http://localhost:3000/ in your default browser thanks to the Create React
App tool.
Step 2: Install Material UI (MUI) Package
Before we start making changes to our default React code, we need the core
MUI package because all these icons use the MUI SvgIcon component to render
the SVG path for each icon. For each SVG icon, we export the respective React
component from the @mui/icons-material package.
Run the following command from your terminal:
npm install @mui/material @emotion/react @emotion/styled
Or if you are using Yarn:
yarn add @mui/material @emotion/react @emotion/styled
Step 3: Do Some Housekeeping of The Default Code
Let’s make these initial changes:
1. Remove all the code from App.js file and simply return the <Barcode />
component as so:
import Barcode from "./barcode";
function App() {
return <Barcode />;
}
export default App;
2. Create a new component file under the src directory called Barcode.jsx. This
will house all of the MUI customization code for our component.
3. Add the barcode image you have to the assets directory under
the images folder so you have access to the file when needed to render.
Step 4: Theme the component with MUI!
Here’s the nitty-gritty! We can now start to create our Barcode component.
Still inside the Barcode.jsx file, export the Barcode function with a return
statement to follow. To start you can also simply render a <p> tag that says
“Barcode component”. If you save your code the render should work.
export default function Barcode() {
return <p>Barcode component</p>;
}
The ThemeProvider Wrapper
By default all the MUI components and styles you will use have a set default
theme that looks like this:
As you can see above, this default theme is a collection of objects with their
properties and values. For example, here it shows the color palette of an MUI
app. If you wish to use the primary, the main color in any of your React elements
like a button background color or a divider color, then its hex value will
be #1976d2 as listed.
But in our design, we don’t see such colors as listed on their documentation so
to make our own theme work we need the ThemeProvider component
from @material-ui/core and make all of the elements as their child in our render
method.
This component takes a theme prop. One thing to note is that It should
preferably be used at the root of your component tree. So let’s remove the
placeholder <p> tag we had before and use this as:
return <ThemeProvider theme={theme}>...</ThemeProvider>;
Make sure you import it too:
import { ThemeProvider } from "@material-ui/core";
Create a Custom Theme
The obvious next step is to actually add our own theming values so that
the theme prop works. Outside the Barcode function definition, create a new
theme object that uses the createMuiTheme() method. This is used to generate a
theme based on the options received which are later passed down to
the theme variable of <ThemeProvider>.
createMuiTheme() takes in two arguments of which the first one is really important.
It’s the options object that takes an incomplete theme object and this is the only
argument that is processed. Inside this object, we can define our custom values
to each of the different properties like typography, colors, spacing, font size, etc.
In this demo, let’s try to implement the current font styles. As per our design,
the font used in the heading and the paragraph below is called Outfit available
on the Google Fonts directory. So to add a custom font in an MUI project we
need to follow these steps:
1. Copy the HTML/CSS import(s) from the custom font CDN. In our case, simply
copy the <link> tags that Google provides after selecting the two weights we
need of the Outfit font family.
2. Update the index.html file by pasting in those link tags and removing the
default Roboto font included in an MUI project.
<link rel="preconnect" href="https://fonts.googleapis.com" />
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
<link
href="https://fonts.googleapis.com/css2?family=Outfit:wght@400;700&display=swap"
rel="stylesheet"
/>
3. Create the typography object under the createMuiTheme call giving it the
proper names and values like:
typography: {
fontFamily: "Outfit",
fontWeightRegular: 400,
fontWeightBold: 700
}
Now that MUI knows that a custom font is to be used, let’s implement it in our
Barcode component. But first, we need to make that card layout and add the
image!
Using Default MUI Components
We will need a total of 3 new components that MUI provides to make our
barcode look exactly like it’s on the design. Here are those:
1. The Card component: the basic aim of the card is to contain content and
actions about a single subject. MUI has various types of Card components
under its belt. Here’s an example:
One great thing about most of the elements in MUI is that we can pass a custom
style to any element using the sx prop. It’s like is a shortcut for defining a
custom style that has access to the theme. So if we quickly want to change the
margins, or width of any element that’s not in our design system we can simply
use the sx prop.
For example in our <Card> the component we can provide it a
custom padding, boxShadow, borderRadius, horizontal margin and maxWidth as:
<Card
sx={{
maxWidth: 350,
mx: "auto",
padding: "1rem",
borderRadius: "4%",
boxShadow: 24
}}
>
2. The CardMedia component: this type of card is perfectly suited for our design
as it has an image up top and the content below it. Let’s use it as follows:
<CardMedia
component="img"
height="350"
image="path/to/image.png"
alt="Barcode image"
sx={{ borderRadius: "4%" }}
/>
This will house a new <CardContent> API for cards under which we can nest all the
card content text like our heading and subheading as:
<CardContent>
// Other components
</CardContent>
3. TheTypography component: this is specifically used to render text elements
from a big bold heading of a section to small captions generated on a video.
Thus, it comes with a variety of props like align, mt, variant, etc. In our app, we use
it for the heading of the card as:
<Typography
gutterBottom
variant="h5"
component="div"
align="center"
fontFamily="Outfit"
fontWeight="fontWeightBold"
mt={2}
mb={2}
sx={{ color: "#182036" }}
>
Improve your front-end <br /> skills by building projects
</Typography>
Notice the use of our custom-defined fonts and their weights. We were able to
pass the font family and font-weight props to it easily with the set values in
the theme object defined above.
Next, we do a similar thing to the subheading with a few tweaks of color
and fontWeight:
<Typography
variant="body2"
align="center"
fontFamily="Outfit"
fontWeight="fontWeightRegular"
sx={{ color: "#7b879d" }}
>
Scan the QR code to visit Frontend <br /> Mentor and take your coding skills to <br /> the
next level
</Typography>
With all that code, you should get the expected styling in your browser similar to
this:
If you implemented the above steps successfully, you should have the following
code:
import Card from "@mui/material/Card";
import CardContent from "@mui/material/CardContent";
import CardMedia from "@mui/material/CardMedia";
import Typography from "@mui/material/Typography";
import { createMuiTheme, ThemeProvider } from "@material-ui/core";
const theme = createMuiTheme({
typography: {
fontFamily: "Outfit",
fontWeightRegular: 400,
fontWeightBold: 700
}
});
export default function Barcode() {
return (
<ThemeProvider theme={theme}>
<Card
sx={{
maxWidth: 350,
mx: "auto",
padding: "1rem",
borderRadius: "4%",
boxShadow: 24
}}
>
<CardMedia
component="img"
height="350"
image="https://i.imgur.com/AJJqWpN.png"
alt="Barcode image"
sx={{ borderRadius: "4%" }}
/>
<CardContent>
<Typography
gutterBottom
variant="h5"
component="div"
align="center"
fontFamily="Outfit"
fontWeight="fontWeightBold"
mt={2}
mb={2}
sx={{ color: "#182036" }}
>
Improve your front-end <br /> skills by building projects
</Typography>
<Typography
variant="body2"
align="center"
fontFamily="Outfit"
fontWeight="fontWeightRegular"
sx={{ color: "#7b879d" }}
>
Scan the QR code to visit Frontend <br /> Mentor and take your
coding skills to <br /> the next level
</Typography>
</CardContent>
</Card>
</ThemeProvider>
);
}
And just like that, you were able to understand MUI’s core components
needed to make such a component from scratch with a custom theme!
In this tutorial, you got to know the setup MUI React UI library, its installation,
and how to make use of its important components like ThemeProvider, Card,
Typography, etc to finally convert a design to a custom code.
Next, you can take it further by defining more values inside
the createMuiTheme() function like spacing, colours, etc to create even more
custom interfaces.
Also if you’re looking for pre-built Material UI Templates that could skyrocket
your development process, then visit the page now. Good luck!
Source: https://blog.wrappixel.com/convert-component-design-into-mui-react-
code/
*****

More Related Content

What's hot

Understanding react hooks
Understanding react hooksUnderstanding react hooks
Understanding react hooks
Samundra khatri
 
Reactjs
ReactjsReactjs
React Js Simplified
React Js SimplifiedReact Js Simplified
React Js Simplified
Sunil Yadav
 
Introduction to CSS3
Introduction to CSS3Introduction to CSS3
Introduction to CSS3
Doris Chen
 
Flutter
FlutterFlutter
Flutter
Mohit Sharma
 
CSS3 Media Queries
CSS3 Media QueriesCSS3 Media Queries
CSS3 Media Queries
Russ Weakley
 
Tailwind CSS.11.pptx
Tailwind CSS.11.pptxTailwind CSS.11.pptx
Tailwind CSS.11.pptx
Harish Verma
 
Introduction to flutter
Introduction to flutter Introduction to flutter
Introduction to flutter
Wan Muzaffar Wan Hashim
 
Typescript in React: HOW & WHY?
Typescript in React: HOW & WHY?Typescript in React: HOW & WHY?
Typescript in React: HOW & WHY?
Saulius Skeirys
 
Flutter
FlutterFlutter
Flutter
Dave Chao
 
JavaScript & Dom Manipulation
JavaScript & Dom ManipulationJavaScript & Dom Manipulation
JavaScript & Dom Manipulation
Mohammed Arif
 
Html and css presentation
Html and css presentationHtml and css presentation
Html and css presentation
umesh patil
 
React new features and intro to Hooks
React new features and intro to HooksReact new features and intro to Hooks
React new features and intro to Hooks
Soluto
 
Css3
Css3Css3
CSS Basics
CSS BasicsCSS Basics
CSS Basics
Sanjeev Kumar
 
Responsive web designing ppt(1)
Responsive web designing ppt(1)Responsive web designing ppt(1)
Responsive web designing ppt(1)
admecindia1
 
Html5 and-css3-overview
Html5 and-css3-overviewHtml5 and-css3-overview
Html5 and-css3-overview
Jacob Nelson
 
React JS - A quick introduction tutorial
React JS - A quick introduction tutorialReact JS - A quick introduction tutorial
React JS - A quick introduction tutorial
Mohammed Fazuluddin
 
javaScript.ppt
javaScript.pptjavaScript.ppt
javaScript.ppt
sentayehu
 
Flutter
FlutterFlutter

What's hot (20)

Understanding react hooks
Understanding react hooksUnderstanding react hooks
Understanding react hooks
 
Reactjs
ReactjsReactjs
Reactjs
 
React Js Simplified
React Js SimplifiedReact Js Simplified
React Js Simplified
 
Introduction to CSS3
Introduction to CSS3Introduction to CSS3
Introduction to CSS3
 
Flutter
FlutterFlutter
Flutter
 
CSS3 Media Queries
CSS3 Media QueriesCSS3 Media Queries
CSS3 Media Queries
 
Tailwind CSS.11.pptx
Tailwind CSS.11.pptxTailwind CSS.11.pptx
Tailwind CSS.11.pptx
 
Introduction to flutter
Introduction to flutter Introduction to flutter
Introduction to flutter
 
Typescript in React: HOW & WHY?
Typescript in React: HOW & WHY?Typescript in React: HOW & WHY?
Typescript in React: HOW & WHY?
 
Flutter
FlutterFlutter
Flutter
 
JavaScript & Dom Manipulation
JavaScript & Dom ManipulationJavaScript & Dom Manipulation
JavaScript & Dom Manipulation
 
Html and css presentation
Html and css presentationHtml and css presentation
Html and css presentation
 
React new features and intro to Hooks
React new features and intro to HooksReact new features and intro to Hooks
React new features and intro to Hooks
 
Css3
Css3Css3
Css3
 
CSS Basics
CSS BasicsCSS Basics
CSS Basics
 
Responsive web designing ppt(1)
Responsive web designing ppt(1)Responsive web designing ppt(1)
Responsive web designing ppt(1)
 
Html5 and-css3-overview
Html5 and-css3-overviewHtml5 and-css3-overview
Html5 and-css3-overview
 
React JS - A quick introduction tutorial
React JS - A quick introduction tutorialReact JS - A quick introduction tutorial
React JS - A quick introduction tutorial
 
javaScript.ppt
javaScript.pptjavaScript.ppt
javaScript.ppt
 
Flutter
FlutterFlutter
Flutter
 

Similar to How to Convert a Component Design into an MUI React Code

How to Use Material UI (MUI) Icons in React
How to Use Material UI (MUI) Icons in ReactHow to Use Material UI (MUI) Icons in React
How to Use Material UI (MUI) Icons in React
WrapPixel
 
Streamlining React Component Development and Sharing with bit.pptx
Streamlining React Component Development and Sharing with bit.pptxStreamlining React Component Development and Sharing with bit.pptx
Streamlining React Component Development and Sharing with bit.pptx
ShubhamJayswal6
 
Ocodewire tshirt_design_tool_demo
Ocodewire tshirt_design_tool_demoOcodewire tshirt_design_tool_demo
Ocodewire tshirt_design_tool_demo
Magento oCodewire
 
Work In Progress
Work In ProgressWork In Progress
Work In Progresssamluk
 
project_proposal_osrf
project_proposal_osrfproject_proposal_osrf
project_proposal_osrfom1234567890
 
IRJET- Generation of HTML Code using Machine Learning Techniques from Mock-Up...
IRJET- Generation of HTML Code using Machine Learning Techniques from Mock-Up...IRJET- Generation of HTML Code using Machine Learning Techniques from Mock-Up...
IRJET- Generation of HTML Code using Machine Learning Techniques from Mock-Up...
IRJET Journal
 
Build Your Own Instagram Filters
Build Your Own Instagram FiltersBuild Your Own Instagram Filters
Build Your Own Instagram Filters
TJ Stalcup
 
IT 200 Network DiagramBelow is the wired network configurat.docx
IT 200 Network DiagramBelow is the wired network configurat.docxIT 200 Network DiagramBelow is the wired network configurat.docx
IT 200 Network DiagramBelow is the wired network configurat.docx
priestmanmable
 
Keynote + Next Gen UIs.pptx
Keynote + Next Gen UIs.pptxKeynote + Next Gen UIs.pptx
Keynote + Next Gen UIs.pptx
EqraKhattak
 
Reaching for the Future with Web Components and Polymer
Reaching for the Future with Web Components and PolymerReaching for the Future with Web Components and Polymer
Reaching for the Future with Web Components and Polymer
FITC
 
Unlock the Power of Mui Breakpoints and Make Great Projects.pdf
Unlock the Power of Mui Breakpoints and Make Great Projects.pdfUnlock the Power of Mui Breakpoints and Make Great Projects.pdf
Unlock the Power of Mui Breakpoints and Make Great Projects.pdf
RonDosh
 
Smart buckets ppt
Smart buckets pptSmart buckets ppt
Smart buckets ppt
kiran Patel
 
Architecture and Analytical Study of Magento
Architecture and Analytical Study of MagentoArchitecture and Analytical Study of Magento
Architecture and Analytical Study of Magento
IRJET Journal
 
Web and Android App Development
Web and Android App DevelopmentWeb and Android App Development
Web and Android App DevelopmentGaurav Gopal Gupta
 
Build UI of the Future with React 360
Build UI of the Future with React 360Build UI of the Future with React 360
Build UI of the Future with React 360
RapidValue
 
Using Tailwind Shadow: An Easy Guide to Custom Shadows - Blogs
Using Tailwind Shadow: An Easy Guide to Custom Shadows - BlogsUsing Tailwind Shadow: An Easy Guide to Custom Shadows - Blogs
Using Tailwind Shadow: An Easy Guide to Custom Shadows - Blogs
RonDosh
 
Use Tailwind Select for Easy and Reliable Dropdowns - Blogs
Use Tailwind Select for Easy and Reliable Dropdowns - BlogsUse Tailwind Select for Easy and Reliable Dropdowns - Blogs
Use Tailwind Select for Easy and Reliable Dropdowns - Blogs
RonDosh
 
WordPress and Shortcodes
WordPress and ShortcodesWordPress and Shortcodes
WordPress and Shortcodes
Jon Bishop
 
Angularjs2 presentation
Angularjs2 presentationAngularjs2 presentation
Angularjs2 presentation
dharisk
 
PSD to HTML Conversion
PSD to HTML ConversionPSD to HTML Conversion
PSD to HTML Conversion
Darryl Sherman
 

Similar to How to Convert a Component Design into an MUI React Code (20)

How to Use Material UI (MUI) Icons in React
How to Use Material UI (MUI) Icons in ReactHow to Use Material UI (MUI) Icons in React
How to Use Material UI (MUI) Icons in React
 
Streamlining React Component Development and Sharing with bit.pptx
Streamlining React Component Development and Sharing with bit.pptxStreamlining React Component Development and Sharing with bit.pptx
Streamlining React Component Development and Sharing with bit.pptx
 
Ocodewire tshirt_design_tool_demo
Ocodewire tshirt_design_tool_demoOcodewire tshirt_design_tool_demo
Ocodewire tshirt_design_tool_demo
 
Work In Progress
Work In ProgressWork In Progress
Work In Progress
 
project_proposal_osrf
project_proposal_osrfproject_proposal_osrf
project_proposal_osrf
 
IRJET- Generation of HTML Code using Machine Learning Techniques from Mock-Up...
IRJET- Generation of HTML Code using Machine Learning Techniques from Mock-Up...IRJET- Generation of HTML Code using Machine Learning Techniques from Mock-Up...
IRJET- Generation of HTML Code using Machine Learning Techniques from Mock-Up...
 
Build Your Own Instagram Filters
Build Your Own Instagram FiltersBuild Your Own Instagram Filters
Build Your Own Instagram Filters
 
IT 200 Network DiagramBelow is the wired network configurat.docx
IT 200 Network DiagramBelow is the wired network configurat.docxIT 200 Network DiagramBelow is the wired network configurat.docx
IT 200 Network DiagramBelow is the wired network configurat.docx
 
Keynote + Next Gen UIs.pptx
Keynote + Next Gen UIs.pptxKeynote + Next Gen UIs.pptx
Keynote + Next Gen UIs.pptx
 
Reaching for the Future with Web Components and Polymer
Reaching for the Future with Web Components and PolymerReaching for the Future with Web Components and Polymer
Reaching for the Future with Web Components and Polymer
 
Unlock the Power of Mui Breakpoints and Make Great Projects.pdf
Unlock the Power of Mui Breakpoints and Make Great Projects.pdfUnlock the Power of Mui Breakpoints and Make Great Projects.pdf
Unlock the Power of Mui Breakpoints and Make Great Projects.pdf
 
Smart buckets ppt
Smart buckets pptSmart buckets ppt
Smart buckets ppt
 
Architecture and Analytical Study of Magento
Architecture and Analytical Study of MagentoArchitecture and Analytical Study of Magento
Architecture and Analytical Study of Magento
 
Web and Android App Development
Web and Android App DevelopmentWeb and Android App Development
Web and Android App Development
 
Build UI of the Future with React 360
Build UI of the Future with React 360Build UI of the Future with React 360
Build UI of the Future with React 360
 
Using Tailwind Shadow: An Easy Guide to Custom Shadows - Blogs
Using Tailwind Shadow: An Easy Guide to Custom Shadows - BlogsUsing Tailwind Shadow: An Easy Guide to Custom Shadows - Blogs
Using Tailwind Shadow: An Easy Guide to Custom Shadows - Blogs
 
Use Tailwind Select for Easy and Reliable Dropdowns - Blogs
Use Tailwind Select for Easy and Reliable Dropdowns - BlogsUse Tailwind Select for Easy and Reliable Dropdowns - Blogs
Use Tailwind Select for Easy and Reliable Dropdowns - Blogs
 
WordPress and Shortcodes
WordPress and ShortcodesWordPress and Shortcodes
WordPress and Shortcodes
 
Angularjs2 presentation
Angularjs2 presentationAngularjs2 presentation
Angularjs2 presentation
 
PSD to HTML Conversion
PSD to HTML ConversionPSD to HTML Conversion
PSD to HTML Conversion
 

Recently uploaded

Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Globus
 
Enhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdfEnhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdf
Globus
 
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Anthony Dahanne
 
A Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of PassageA Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of Passage
Philip Schwarz
 
Graphic Design Crash Course for beginners
Graphic Design Crash Course for beginnersGraphic Design Crash Course for beginners
Graphic Design Crash Course for beginners
e20449
 
Vitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume MontevideoVitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume Montevideo
Vitthal Shirke
 
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus
 
A Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdfA Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdf
kalichargn70th171
 
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
Juraj Vysvader
 
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptxTop Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
rickgrimesss22
 
Understanding Globus Data Transfers with NetSage
Understanding Globus Data Transfers with NetSageUnderstanding Globus Data Transfers with NetSage
Understanding Globus Data Transfers with NetSage
Globus
 
Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"
Donna Lenk
 
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus
 
Corporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMSCorporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMS
Tendenci - The Open Source AMS (Association Management Software)
 
2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx
Georgi Kodinov
 
Enterprise Resource Planning System in Telangana
Enterprise Resource Planning System in TelanganaEnterprise Resource Planning System in Telangana
Enterprise Resource Planning System in Telangana
NYGGS Automation Suite
 
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Mind IT Systems
 
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing SuiteAI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
Google
 
GlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote sessionGlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote session
Globus
 
How to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good PracticesHow to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good Practices
Globus
 

Recently uploaded (20)

Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
 
Enhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdfEnhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdf
 
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
 
A Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of PassageA Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of Passage
 
Graphic Design Crash Course for beginners
Graphic Design Crash Course for beginnersGraphic Design Crash Course for beginners
Graphic Design Crash Course for beginners
 
Vitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume MontevideoVitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume Montevideo
 
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024
 
A Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdfA Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdf
 
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
 
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptxTop Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
 
Understanding Globus Data Transfers with NetSage
Understanding Globus Data Transfers with NetSageUnderstanding Globus Data Transfers with NetSage
Understanding Globus Data Transfers with NetSage
 
Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"
 
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024
 
Corporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMSCorporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMS
 
2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx
 
Enterprise Resource Planning System in Telangana
Enterprise Resource Planning System in TelanganaEnterprise Resource Planning System in Telangana
Enterprise Resource Planning System in Telangana
 
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
 
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing SuiteAI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
 
GlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote sessionGlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote session
 
How to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good PracticesHow to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good Practices
 

How to Convert a Component Design into an MUI React Code

  • 1. How to Convert a Component Design into an MUI React Code Material UI or MUI library provides you with robust, customizable, accessible, and advanced components, enabling you to build your own design system and develop React applications faster. That includes a huge list of Material icons, foundational components with MUI Core, advanced and powerful components with MUI X, templates, and design kits! In this tutorial, we will see how to take a given design and convert it into an actual component code by styling it with MUI Core configurations for a React app. Let’s begin!
  • 2. Converting a component design to an MUI code in React If you are provided with a design file with a simple component to build on React there are so many ways, but here with MUI’s extensive pre-built components, the process becomes much easier, faster, accessible, and most importantly customizable! What we will be making? We will be taking the following barcode component design and implementing it with MUI for React: As you can see, we have three items: 1. The barcode component with the two sections. 2. The first section holds the barcode image in a blue container. 3. The second section below the barcode image has all the typography elements like a heading and a paragraph. Step 1: Start a New React project Make sure you have Node.js installed on your system and then run the following commands: <code>npx create-react-app mui-barcode-app cd mui-barcode-app npm start</code>
  • 3. This will bootstrap a new React application and run the default app on http://localhost:3000/ in your default browser thanks to the Create React App tool. Step 2: Install Material UI (MUI) Package Before we start making changes to our default React code, we need the core MUI package because all these icons use the MUI SvgIcon component to render the SVG path for each icon. For each SVG icon, we export the respective React component from the @mui/icons-material package. Run the following command from your terminal: npm install @mui/material @emotion/react @emotion/styled Or if you are using Yarn: yarn add @mui/material @emotion/react @emotion/styled Step 3: Do Some Housekeeping of The Default Code Let’s make these initial changes: 1. Remove all the code from App.js file and simply return the <Barcode /> component as so: import Barcode from "./barcode"; function App() { return <Barcode />; } export default App;
  • 4. 2. Create a new component file under the src directory called Barcode.jsx. This will house all of the MUI customization code for our component. 3. Add the barcode image you have to the assets directory under the images folder so you have access to the file when needed to render. Step 4: Theme the component with MUI! Here’s the nitty-gritty! We can now start to create our Barcode component. Still inside the Barcode.jsx file, export the Barcode function with a return statement to follow. To start you can also simply render a <p> tag that says “Barcode component”. If you save your code the render should work. export default function Barcode() { return <p>Barcode component</p>; } The ThemeProvider Wrapper By default all the MUI components and styles you will use have a set default theme that looks like this:
  • 5. As you can see above, this default theme is a collection of objects with their properties and values. For example, here it shows the color palette of an MUI app. If you wish to use the primary, the main color in any of your React elements like a button background color or a divider color, then its hex value will be #1976d2 as listed. But in our design, we don’t see such colors as listed on their documentation so to make our own theme work we need the ThemeProvider component from @material-ui/core and make all of the elements as their child in our render method.
  • 6. This component takes a theme prop. One thing to note is that It should preferably be used at the root of your component tree. So let’s remove the placeholder <p> tag we had before and use this as: return <ThemeProvider theme={theme}>...</ThemeProvider>; Make sure you import it too: import { ThemeProvider } from "@material-ui/core"; Create a Custom Theme The obvious next step is to actually add our own theming values so that the theme prop works. Outside the Barcode function definition, create a new theme object that uses the createMuiTheme() method. This is used to generate a theme based on the options received which are later passed down to the theme variable of <ThemeProvider>. createMuiTheme() takes in two arguments of which the first one is really important. It’s the options object that takes an incomplete theme object and this is the only argument that is processed. Inside this object, we can define our custom values to each of the different properties like typography, colors, spacing, font size, etc. In this demo, let’s try to implement the current font styles. As per our design, the font used in the heading and the paragraph below is called Outfit available on the Google Fonts directory. So to add a custom font in an MUI project we need to follow these steps:
  • 7. 1. Copy the HTML/CSS import(s) from the custom font CDN. In our case, simply copy the <link> tags that Google provides after selecting the two weights we need of the Outfit font family. 2. Update the index.html file by pasting in those link tags and removing the default Roboto font included in an MUI project. <link rel="preconnect" href="https://fonts.googleapis.com" /> <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin /> <link href="https://fonts.googleapis.com/css2?family=Outfit:wght@400;700&display=swap" rel="stylesheet" /> 3. Create the typography object under the createMuiTheme call giving it the proper names and values like: typography: { fontFamily: "Outfit", fontWeightRegular: 400, fontWeightBold: 700 } Now that MUI knows that a custom font is to be used, let’s implement it in our Barcode component. But first, we need to make that card layout and add the image!
  • 8. Using Default MUI Components We will need a total of 3 new components that MUI provides to make our barcode look exactly like it’s on the design. Here are those: 1. The Card component: the basic aim of the card is to contain content and actions about a single subject. MUI has various types of Card components under its belt. Here’s an example: One great thing about most of the elements in MUI is that we can pass a custom style to any element using the sx prop. It’s like is a shortcut for defining a custom style that has access to the theme. So if we quickly want to change the margins, or width of any element that’s not in our design system we can simply use the sx prop. For example in our <Card> the component we can provide it a custom padding, boxShadow, borderRadius, horizontal margin and maxWidth as:
  • 9. <Card sx={{ maxWidth: 350, mx: "auto", padding: "1rem", borderRadius: "4%", boxShadow: 24 }} > 2. The CardMedia component: this type of card is perfectly suited for our design as it has an image up top and the content below it. Let’s use it as follows: <CardMedia component="img" height="350" image="path/to/image.png" alt="Barcode image" sx={{ borderRadius: "4%" }} /> This will house a new <CardContent> API for cards under which we can nest all the card content text like our heading and subheading as: <CardContent>
  • 10. // Other components </CardContent> 3. TheTypography component: this is specifically used to render text elements from a big bold heading of a section to small captions generated on a video. Thus, it comes with a variety of props like align, mt, variant, etc. In our app, we use it for the heading of the card as: <Typography gutterBottom variant="h5" component="div" align="center" fontFamily="Outfit" fontWeight="fontWeightBold" mt={2} mb={2} sx={{ color: "#182036" }} > Improve your front-end <br /> skills by building projects </Typography> Notice the use of our custom-defined fonts and their weights. We were able to pass the font family and font-weight props to it easily with the set values in the theme object defined above.
  • 11. Next, we do a similar thing to the subheading with a few tweaks of color and fontWeight: <Typography variant="body2" align="center" fontFamily="Outfit" fontWeight="fontWeightRegular" sx={{ color: "#7b879d" }} > Scan the QR code to visit Frontend <br /> Mentor and take your coding skills to <br /> the next level </Typography> With all that code, you should get the expected styling in your browser similar to this:
  • 12. If you implemented the above steps successfully, you should have the following code: import Card from "@mui/material/Card"; import CardContent from "@mui/material/CardContent"; import CardMedia from "@mui/material/CardMedia"; import Typography from "@mui/material/Typography"; import { createMuiTheme, ThemeProvider } from "@material-ui/core"; const theme = createMuiTheme({
  • 13. typography: { fontFamily: "Outfit", fontWeightRegular: 400, fontWeightBold: 700 } }); export default function Barcode() { return ( <ThemeProvider theme={theme}> <Card sx={{ maxWidth: 350, mx: "auto", padding: "1rem", borderRadius: "4%", boxShadow: 24 }} > <CardMedia component="img"
  • 14. height="350" image="https://i.imgur.com/AJJqWpN.png" alt="Barcode image" sx={{ borderRadius: "4%" }} /> <CardContent> <Typography gutterBottom variant="h5" component="div" align="center" fontFamily="Outfit" fontWeight="fontWeightBold" mt={2} mb={2} sx={{ color: "#182036" }} > Improve your front-end <br /> skills by building projects </Typography> <Typography variant="body2"
  • 15. align="center" fontFamily="Outfit" fontWeight="fontWeightRegular" sx={{ color: "#7b879d" }} > Scan the QR code to visit Frontend <br /> Mentor and take your coding skills to <br /> the next level </Typography> </CardContent> </Card> </ThemeProvider> ); } And just like that, you were able to understand MUI’s core components needed to make such a component from scratch with a custom theme! In this tutorial, you got to know the setup MUI React UI library, its installation, and how to make use of its important components like ThemeProvider, Card, Typography, etc to finally convert a design to a custom code. Next, you can take it further by defining more values inside the createMuiTheme() function like spacing, colours, etc to create even more custom interfaces.
  • 16. Also if you’re looking for pre-built Material UI Templates that could skyrocket your development process, then visit the page now. Good luck! Source: https://blog.wrappixel.com/convert-component-design-into-mui-react- code/ *****