Skip to content

Creating and Customizing Buttons in React

Buttons allows users to take actions and make choices with a single tap.

They (buttons) communicate the actions that users can perform. It is placed by your UI in places like below:

  • Modal windows
  • Forms
  • Cards
  • Toolbars
  • Basic Button

The Button comes in 3 variants: text (default), contained, and outlined.

Button in React

  1. import * as React from ‘react’;  
  2. import Stack from ‘@mui/material/Stack’;  
  3. import Button from ‘@mui/material/Button’;  
  4. export default function BasicButtons() {  
  5.   return (  
  6.     <Stack spacing={2} direction=”row”>  
  7.    <Button variant=”text”>Text</Button>  
  8.       <Button variant=”contained”>Contained</Button>  
  9.       <Button variant=”outlined”>Outlined</Button>  
  10.     </Stack>  
  11.   );  
  12. }  

Text button

Text buttons are used for less-pronounced actions, including those in card dialogs. In cards, the text buttons will help us to maintain an emphasis on card content.

Button in React

  1. import * as React from ‘react’;  
  2. import Button from ‘@mui/material/Button’;  
  3. import Stack from ‘@mui/material/Stack’;  
  4. export default function TextButtons() {  
  5.   return (  
  6.     <Stack direction=”row” spacing={2}>  
  7.       <Button>Primary</Button>  
  8.       <Button disabled>Disabled</Button>  
  9.       <Button href=”#text-buttons”>Link</Button>  
  10.     </Stack>  
  11.   );  
  12. }  

Contained Button

Contained buttons are high-emphasis, distinguished by the use of elevation and fill. It contains actions which are primary used in our App.

Button in React

  1. import * as React from ‘react’;  
  2. import Button from ‘@mui/material/Button’;  
  3. import Stack from ‘@mui/material/Stack’;  
  4. export default function ContainedButtons() {  
  5.   return (  
  6.     <Stack direction=”row” spacing={2}>  
  7.       <Button variant=”contained”>Contained</Button>  
  8.       <Button variant=”contained” disabled>  
  9.         Disabled  
  10.       </Button>  
  11.       <Button variant=”contained” href=”#contained-buttons”>  
  12.         Link  
  13.       </Button>  
  14.     </Stack>  
  15.   );  
  16. }  

You can remove the elevation with the disable Elevation prop.

Button in React

  1. import * as React from ‘react’;  
  2. import Button from ‘@mui/material/Button’;  
  3. export default function DisableElevation() {  
  4.   return (  
  5.     <Button variant=”contained” disableElevation>  
  6.       Disable elevation  
  7.     </Button>  
  8.   );  
  9. }  

Outlined Button

Outlined buttons are medium-emphasis buttons. They contain essential actions but not the main action in the app.

Outlined buttons are the lower alternative to contain the buttons or a higher emphasis alternative to the text buttons.

Button in React

  1. import * as React from ‘react’;  
  2. import Button from ‘@mui/material/Button’;  
  3. import Stack from ‘@mui/material/Stack’;  
  4. export default function OutlinedButtons() {  
  5.   return (  
  6.     <Stack direction=”row” spacing={2}>  
  7.       <Button variant=”outlined”>Primary</Button>  
  8.       <Button variant=”outlined” disabled>  
  9.         Disabled  
  10.       </Button>  
  11.       <Button variant=”outlined” href=”#outlined-buttons”>  
  12.         Link  
  13.       </Button>  
  14.     </Stack>  
  15.   );  
  16. }  

Handling clicks

All components accept an onClick handler which is applied to the root DOM element.

  1. <Button  
  2.   onClick={() => {  
  3.     alert(‘clicked’);  
  4.   }}  
  5. >  
  6.   Click me  
  7. </Button>  

Note: The documentation avoid mentioning the native props in our API section of the components.

Color

Button in React

  1. import * as React from ‘react’;  
  2. import Stack from ‘@mui/material/Stack’;  
  3. import Button from ‘@mui/material/Button’;  
  4. export default function ColorButtons() {  
  5.   return (  
  6.     <Stack direction=”row” spacing={2}>  
  7.       <Button color=”secondary”>Secondary</Button>  
  8.       <Button variant=”contained” color=”success”>  
  9.         Success  
  10.       </Button>  
  11.       <Button variant=”outlined” color=”error”>  
  12.         Error  
  13.       </Button>  
  14.     </Stack>  
  15.   );  
  16. }  

In addition, by using the default button colors, you can add custom or disable any you don’t need.

Size

Use this propertty for larger or smaller buttons.

Button in React

Upload button

Button in React

  1. import * as React from ‘react’;  
  2. import { styled } from ‘@mui/material/styles’;  
  3. import Button from ‘@mui/material/Button’;  
  4. import IconButton from ‘@mui/material/IconButton’;  
  5. import PhotoCamera from ‘@mui/icons-material/PhotoCamera’;  
  6. import Stack from ‘@mui/material/Stack’;  
  7.   
  8. const Input = styled(‘input’)({  
  9.   display: ‘none’,  
  10. });  
  11. export default function UploadButtons() {  
  12.   return (  
  13.     <Stack direction=”row” alignItems=”center” spacing={2}>  
  14.       <label htmlFor=”contained-button-file”>  
  15.         <Input accept=”image/*” id=”contained-button-file” multiple type=”file” />  
  16.         <Button variant=”contained” component=”span”>  
  17.           Upload  
  18.         </Button>  
  19.       </label>  
  20.       <label htmlFor=”icon-button-file”>  
  21.         <Input accept=”image/*” id=”icon-button-file” type=”file” />  
  22.         <IconButton color=”primary” aria-label=”upload picture” component=”span”>  
  23.           <PhotoCamera />  
  24.         </IconButton>  
  25.       </label>  
  26.     </Stack>  
  27.   );  
  28. }  

Buttons with label and icon

Sometimes you may want to have icons for certain buttons to enhance the UX of the application, as we acknowledge logos easier than plain text.

For example, If we want to delete button than you should label it with a dustbin icon.

Button in React

  1. import * as React from ‘react’;  
  2. import Button from ‘@mui/material/Button’;  
  3. import DeleteIcon from ‘@mui/icons-material/Delete’;  
  4. import SendIcon from ‘@mui/icons-material/Send’;  
  5. import Stack from ‘@mui/material/Stack’;  
  6.   
  7. export default function IconLabelButtons() {  
  8.   return (  
  9.     <Stack direction=”row” spacing={2}>  
  10.       <Button variant=”outlined” startIcon={<DeleteIcon />}>  
  11.         Delete  
  12.       </Button>  
  13.       <Button variant=”contained” endIcon={<SendIcon />}>  
  14.         Send  
  15.       </Button>  
  16.     </Stack>  
  17.   );  
  18. }  

Icon button

Icon buttons are found in toolbars and app bars. Icons are appropriate for toggle buttons which allow the choices to be selected or deselected. Like, adding or removing any item from the label.

Button in React

  1. import * as React from ‘react’;  
  2. import IconButton from ‘@mui/material/IconButton’;  
  3. import Stack from ‘@mui/material/Stack’;  
  4. import DeleteIcon from ‘@mui/icons-material/Delete’;  
  5. import AlarmIcon from ‘@mui/icons-material/Alarm’;  
  6. import AddShoppingCartIcon from ‘@mui/icons-material/AddShoppingCart’;  
  7. export default function IconButtons() {  
  8.   return (  
  9.     <Stack direction=”row” spacing={1}>  
  10.       <IconButton aria-label=”delete”>  
  11.         <DeleteIcon />  
  12.       </IconButton>  
  13.       <IconButton aria-label=”delete” disabled color=”primary”>  
  14.         <DeleteIcon />  
  15.       </IconButton>  
  16.       <IconButton color=”secondary” aria-label=”add an alarm”>  
  17.         <AlarmIcon />  
  18.       </IconButton>  
  19.       <IconButton color=”primary” aria-label=”add to shopping cart”>  
  20.         <AddShoppingCartIcon />  
  21.       </IconButton>  
  22.     </Stack>  
  23.   );  
  24. }     

Sizes

Use the size prop for larger or smaller icon in button.

Button in React

  1. import * as React from ‘react’;  
  2. import Stack from ‘@mui/material/Stack’;  
  3. import IconButton from ‘@mui/material/IconButton’;  
  4. import DeleteIcon from ‘@mui/icons-material/Delete’;  
  5. export default function IconButtonSizes() {  
  6.   return (  
  7.     <Stack direction=”row” alignItems=”center” spacing={1}>  
  8.       <IconButton aria-label=”delete” size=”small”>  
  9.         <DeleteIcon fontSize=”inherit” />  
  10.       </IconButton>  
  11.       <IconButton aria-label=”delete” size=”small”>  
  12.         <DeleteIcon fontSize=”small” />  
  13.       </IconButton>  
  14.       <IconButton aria-label=”delete” size=”large”>  
  15.         <DeleteIcon />  
  16.       </IconButton>  
  17.       <IconButton aria-label=”delete” size=”large”>  
  18.         <DeleteIcon fontSize=”inherit” />  
  19.       </IconButton>  
  20.     </Stack>  
  21.   );  
  22. }  

Colors

Button in React

Use color prop to apply the theme color palette to the component.

  1. import * as React from ‘react’;  
  2. import Stack from ‘@mui/material/Stack’;  
  3. import IconButton from ‘@mui/material/IconButton’;  
  4. import Fingerprint from ‘@mui/icons-material/Fingerprint’;  
  5.   
  6. export default function IconButtonColors() {  
  7.   return (  
  8.     <Stack direction=”row” spacing={1}>  
  9.       <IconButton aria-label=”fingerprint” color=”secondary”>  
  10.         <Fingerprint />  
  11.       </IconButton>  
  12.       <IconButton aria-label=”fingerprint” color=”success”>  
  13.         <Fingerprint />  
  14.       </IconButton>  
  15.     </Stack>  
  16.   );  
  17. }  

Customization

Button in React

Below are the examples of customizing our component.

Loading button

The loading buttons can show the loading state and disable interactions of the button.

Button in React

  1. import * as React from ‘react’;  
  2. import LoadingButton from ‘@mui/lab/LoadingButton’;  
  3. import SaveIcon from ‘@mui/icons-material/Save’;  
  4. import Stack from ‘@mui/material/Stack’;  
  5.   
  6. export default function LoadingButtons() {  
  7.   return (  
  8.     <Stack direction=”row” spacing={2}>  
  9.       <LoadingButton loading variant=”outlined”>  
  10.         Submit  
  11.       </LoadingButton>  
  12.       <LoadingButton loading loadingIndicator=”Loading…” variant=”outlined”>  
  13.         Fetch data  
  14.       </LoadingButton>  
  15.       <LoadingButton  
  16.         loading  
  17.         loadingPosition=”start”  
  18.         startIcon={<SaveIcon />}  
  19.         variant=”outlined”  
  20.       >  
  21.         Save  
  22.       </LoadingButton>  
  23.     </Stack>  
  24.   );  
  25. }  

Toggle the loading button to see the transition between unrelated positions.

Button in React

Complex button

Icon button, Text button, contained buttons, and floating action buttons are built into a single component which is called as ButtonBase.

Button in React

You can take lower level component to create custom interactions.

Third-party Routing libraries

Navigating the client without an exact HTTP trip to the server is a unique use case. The ButtonBase component provides component properties to handle the use case.

Borders

ButtonBase sets the component pointer-events: none; on the disable Button, which prevents the appearance of the disabled cursor.

If you want to use “not allowed”, you have two options:

CSS only: You can remove the pointer-event style on the disabled state of < Button> element:

  1. .uibuttonbase-root:disabled {  
  2.   cursor: not allowed;  
  3.   pointer-events: auto;  
  4. ,  

Although,

You should add pointer-events: none; when you needed to display tooltips on disabled elements.

If you render anything other than the button element, then the cursor will not change. For example, a link <a> element.

DOM change. You can wrap the button:

  1. <span style={{ cursor: ‘not allowed’ }}>  
  2.   <button component={link} disabled>  
  3.     disabled  
  4.   </button>  
  5. </span>  

It can support any element, for example, a link <a> element.

unstyled

The component will comes with the unstyled version. It is ideal for doing heavy optimizations and reducing bundle size.

API

  • <button >
  • <buttonbase >
  • <iconbutton >
  • <loadingButton >

How to use the button component in ReactJS?

Buttons allow users to take their actions and make choices at a single tap. This component is available to us for React UI content, and it is very easy to integrate. We can use button component in ReactJS by using following approach.

Creating react app and installing modules:

Step 1: Build a React application by using the given below command:

  1. npx create-react-app foldername  

Step 2: After creating the project folder, and name of the folder to navigate it by using the given command:

  1. cd foldername  

Step 3: Install the Material-UI module using the following command, after creating the ReactJS application:

  1. npm install @material-ui/core  

Project Structure: It will look as the following.

Button in React

project structure

App.js: Now, you have to write the below code in the App.js file.

Here, the App is the default component where we have written in our code.

JavaScript

  1. import React from ‘react’;  
  2. import Button from ‘@material-ui/core/Button’;  
  3.     
  4. const App = () => {  
  5.     
  6.   return (  
  7.     <div style={{  
  8.       display: ‘flex’,  
  9.       margin: ‘auto’,  
  10.       width: 400,  
  11.       flexWrap: ‘wrap’,  
  12.     }}>  
  13.       <div style={{ width: ‘100%’, float: ‘left’ }}>  
  14.         <h3>How to use Button Component in ReactJS?</h3> <br />  
  15.       </div>  
  16.       <Button variant=”contained”>Default Button</Button>  
  17.       <Button variant=”contained” color=”primary”>  
  18.         Primary Button  
  19.       </Button>  
  20.       <Button variant=”contained” color=”secondary”>  
  21.         Secondary Button  
  22.       </Button>  
  23.       <Button variant=”contained” disabled>  
  24.         Disabled Button  
  25.       </Button>  
  26.       <Button variant=”contained” color=”primary”  
  27.         href=”#”>  
  28.         Link Button  
  29.       </Button>  
  30.     </div>  
  31.   );  
  32. }   
  33. export default App;  

Steps to run the application:

Run the application by using the command from the root directory of the project:

  1. npm start  

Output: Now open your browser and go to http://localhost:3000/. You can see the below output:

Button in React

1 thought on “Creating and Customizing Buttons in React”

  1. Pingback: What is Dom in React? - React JS Notes

Leave a Reply

Your email address will not be published. Required fields are marked *