How to use ClickAwayListener Component in ReactJS ?
Last Updated :
01 Aug, 2024
Detect if a click event happened outside an element. Whenever the user clicks somewhere in the document, this listener works. Material UI for React has this component available for us, and it is very easy to integrate.
Prerequisites:
Approach:
To use ClickAwayListener Component in ReactJS we will wrap the clickAwayListener component around the React Code where we want to detect the click. We will create a button inside and detect the click outside this button component.
Steps to Create React Application And Installing Modules:
Step 1: Create a React application using the following command.
npx create-react-app foldername
Step 2: After creating your project folder i.e. foldername, move to it using the following command.
cd foldername
Step 3: After creating the ReactJS application, Install the material-ui modules using the following command.
npm i @material-ui/core
Project Structure:
Project StructureThe updated dependencies in package.json file will look like.
"dependencies": {
"@material-ui/core": "^4.12.4",
"@testing-library/jest-dom": "^5.17.0",
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-scripts": "5.0.1",
"web-vitals": "^2.1.4
}
Example: This example implements clickAwayListener to capture the click outside the button.
JavaScript
// Filename - App.js
import React from "react";
import { lightBlue } from "@material-ui/core/colors";
import ClickAwayListener from "@material-ui/core/ClickAwayListener";
export default function App() {
const [open, setOpen] = React.useState(false);
const handleClickAwayEvent = () => {
setOpen(false);
};
const handleClickEvent = () => {
setOpen((prev) => !prev);
};
return (
<div style={{ display: "block", padding: 30 }}>
<h4>
How to use ClickAwayListner Component in
ReactJS?
</h4>
<ClickAwayListener
onClickAway={handleClickAwayEvent}
>
<div
style={{
position: "relative",
}}
>
<button
type="button"
onClick={handleClickEvent}
>
ClickAwayListener Demo
</button>
{open ? (
<div
style={{
width: 150,
position: "absolute",
right: 0,
fontSize: 9,
border: "1px solid",
left: 0,
backgroundColor: lightBlue,
zIndex: 1,
padding: 3,
top: 28,
}}
>
It will be visible until you
click outside this box
</div>
) : null}
</div>
</ClickAwayListener>
</div>
);
}
Step to Run Application: Run the application using the following command from the root directory of the project.
npm start
Output: Now open your browser and go to http://localhost:3000/, you will see the following output.
Similar Reads
How To Use Modal Component In ReactJS? Modals are a popular UI component used to display content in a pop-up window like alerts, forms, or additional information. In ReactJS, there are multiple ways to implement a modal. In this article, we will discuss two common approaches: using reusable functional components and using the Material-UI
4 min read
How to use Popper Component in ReactJS ? A Popper is used to show the part of the content on top of another. It's an alternative feature for react-popper. Material UI for React has this component available for us, and it is simple and very easy to integrate. For perfect positioning, it uses 3rd party library which is Popper.js.Prerequisite
3 min read
How to use SnackBar Component in ReactJS ? ReactJS provides developers with a wide range of components to create interactive and dynamic web applications. One such component is the SnackBar, which is commonly used to display temporary messages or notifications to users. Creating a SnackBar component allows for the presentation of these messa
2 min read
How to use Popover Component in ReactJS ? A Popover is a graphical control which is a container-type element that hovers over the parent window, and it makes sure that all other interaction is blocked until it is selected. Material UI for React has this component available for us, and it is very easy to integrate.PrerequisitesA basic unders
2 min read
How to use SpeedDial Component in ReactJS ? SpeedDial is a quick action button for the user from where the user can click over it and easily access the various option provided in the dial. Material UI for React has this component available for us, and it is very easy to integrate. We can use the following approach in ReactJS to use SpeedDial
2 min read
How to use Link Component in React JS? Material-UI provides a set of components that can be seamlessly integrated with React to create visually appealing and functional navigation elements. The Link component allows you to easily customize anchor elements with our own theme colors and typography styles. Material UI for React has this com
2 min read