![]() |
VOOZH | about |
Themes play a crucial role in web applications, ensuring a cohesive visual identity. They serve to regulate and define various aspects such as color schemes, backgrounds, font properties, shadow levels, opacity, and more, thereby maintaining a consistent and harmonized aesthetic throughout the application.
Step 1: Create a React application using the following command.
npx create-react-app gfgStep 2: After creating your project folder i.e. foldername, move to it using the following command.
cd gfgStep 3: After creating the ReactJS application, Install the material-uimodules using the following command.
npm install @material-ui/coreStep 4: Head to public/index.html and add the fonts to your <head>:
<link rel="stylesheet" href="https://fonts.googleapis.com/css2family=Open+Sans:wght@300&family=Raleway:wght@300&family=Roboto&display=swap">
Project Structure:
The updated dependencies in package.json file will look like:
"dependencies": {
"@material-ui/core": "^4.12.4",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-scripts": "5.0.1",
"web-vitals": "^2.1.4",
}
ThemeProvider component for injecting a theme into the application.createMuiTheme().ThemeProvider component. This component wraps the entire template that needs to be themed.<ThemeProvider> utilizes React's context feature to propagate the theme to all components within the template.<ThemeProvider> with a different theme around the specific section requiring customization.The responsiveFontSizes() function enables us to have viewport responsive text sizes.
import { createMuiTheme, responsiveFontSizes }
from '@materialui/core/styles';
const theme = responsiveFontSizes(createMuiTheme({
}));
Spacing: It helps create consistent spacing between the elements of our UI.
spacing: 4,
Typography is where we define different font variants that are then used in the component templates via the โTypographyโ component.
typography: {
fontFamily: [ 'Roboto',
'Raleway',
'Open Sans',
].join(','),
h1: {
fontSize: '5rem',
fontFamily: 'Raleway',
}
,
h2: {
fontSize: '3.5rem',
fontFamily: 'Open Sans',
fontStyle: 'bold',
}
,
h3: {
fontSize: '2.5rem',
fontFamily: 'Roboto',
}
,
}
Palette is where we define the colors to be used in our React app. The theme exposes the following predefined palette colors โ primary, secondary, error, warning, info, success, and text for typography colors.
palette: {
background: {
default: '#009900',
},
primary: {
main: '#2B37D4',
},
secondary: {
main: '#E769A6',
},
error: {
main: '#D72A2A',
},
warning: {
main: '#FC7B09',
},
info: {
main: '#6B7D6A',
},
success: {
main: '#09FE00',
},
text: {
primary: '#000000',
secondary: '#FFFFFF',
},
}
Example: Below is the code example of the adding the theme to react app.
Step to Run Application: Run the application using the following command from the root directory of the project.
npm start
Output:๐ Image