How to Dismiss a React-Bootstrap Popover on Click Outside (2024)

Introduction

The React community has always made integrating open-source libraries easy for developers. It eliminates the unnecessary use of jQuery for Bootstrap components such as tooltips, popovers, modals, etc. by converting these components into React components. Popovers are widely used to display some additional content in an enhanced visual effect on the page. This guide explores how to dismiss a popover on clicking outside anywhere on the page.

Popovers

Popovers can be used in several ways, such as calculating the price of an inventory item in an e-commerce web app, displaying details of a new car model, etc. However, when using a UI component, be sure it is conveniently handled on the user's end. Attractive UI elements seem easy to use, but their complete use case should be correctly anticipated before implementing it in code. The basic feature that any modal, tooltip, or popover should have is easy opening and closing. Since popovers are triggered using a click event through a button, closing the same popover should be more customized in terms of end-user experience.

The default dismissing action of React Bootstrap popover is the same button that triggers it. The button that opens the popover on click acts like a toggle. The user, however, expects the popover to close when they click anywhere outside the page, just like modals, due to their similar interfaces.

Set up a Popover

Create a Project

Make sure you have Nodejs and npm installed in your machine (at least version 8 or higher) along with a code editor and a web browser (preferably Chrome or Firefox).

Create a new project using create-react-app:

 npx create-react-app react-bootstrap-popover 

Inside the root directory run the following command to install React Bootstrap library

 npm install react-bootstrap bootstrap 

This will install both Bootstrap and React Bootstrap inside the project.

Clean up the Template

For brevity, let's put all the code inside App.js . Remove the logo, App.css, and all their imports from App.js. Clean out the starter template inside the App component. Your App.js should look like this.

 import React from 'react';function App() { return ( <div className="App"> <h2>Hello</h2> </div> );}export default App; 

Add the Popover Component

For regular Bootstrap styles to work correctly, import the Bootstrap styles on top.

 import 'bootstrap/dist/css/bootstrap.min.css'; 

Above is equivalent to adding Bootstrap CDN in your index.html file. Now import three things; Popover, OverlayTrigger and Button from react-bootstrap

 import Popover from 'react-bootstrap/Popover';import OverlayTrigger from 'react-bootstrap/OverlayTrigger';import Button from 'react-bootstrap/Button'; 

To create a popover inside the Popover Component, render the Popover.Title component to indicate the title of the popover and Popover.Content Component to indicate its content. Store this popover in a constant variable and output it inside the JSX template.

 ... const popover = ( <Popover id="popover-basic"> <Popover.Title as="h3">Popover title</Popover.Title> <Popover.Content> Popover content <strong>some strong content</strong> Normal content again </Popover.Content> </Popover> );... 

Output the popover inside an overlay and use the trigger prop to set the type of event the overlay listens for.

 .... return ( <div className="App"> <OverlayTrigger trigger="click" placement="right" overlay={popover}> <Button variant="success">Click to trigger popover</Button> </OverlayTrigger> </div> );.... 

Finally, your App.js rendering the bootstrap popover should look like this . You can find the same code from react-bootstrap's official documentation here.

 import React from 'react';import 'bootstrap/dist/css/bootstrap.min.css';import Popover from 'react-bootstrap/Popover';import OverlayTrigger from 'react-bootstrap/OverlayTrigger';import Button from 'react-bootstrap/Button';function App() { const popover = ( <Popover id="popover-basic"> <Popover.Title as="h3">Popover title</Popover.Title> <Popover.Content> Popover content <strong>some strong content</strong> Normal content again </Popover.Content> </Popover> ); return ( <div className="App"> <OverlayTrigger trigger="click" placement="right" overlay={popover}> <Button variant="success">Click to trigger popover</Button> </OverlayTrigger> </div> );}export default App; 

To see the popover inside the root directory, run the following:

 npm start 

This will spin up a local development server (usually on port 3000) and you can see the popover button and the popover itself when you click the button. Clicking the same button again dismisses the popover.

Dismiss Popover on Outside Click

Let's look at the two simplest ways of dismissing the popover.

Use rootClose Prop

You can pass the additional prop rootClose to your OverlayTrigger component to dismiss the popover when on an outside click.

 ... <OverlayTrigger trigger="click" rootClose placement="right" overlay={popover}>... 

In the absence of this prop, a false Boolean value is taken by default by the parent component. When you pass this prop, the value is set to true and the onHide method is fired when the user clicks outside the overlay. The onHide method is just a callback invoked by the overlay when it wishes to be hidden and is required if rootClose is specified.

Use Focus Trigger to Dismiss Popover

Another method to dismiss the popover is by using a different trigger event. You can use the hover or focus trigger instead of the click trigger to achieve this. The hover trigger isn't the correct solution since it removes the clicking action on the popover, rendering it a tooltip. Use the focus trigger instead.

 ...<OverlayTrigger trigger="focus" placement="right" overlay={popover}>... 

This will close the popover when the user clicks outside of it.

If you get any depreciated warnings or errors, you can use the exact version of react-bootstrap as this guide by updating your package.json file and running the command npm i .

 ...."bootstrap": "^4.4.1","react-bootstrap": "^1.0.1",.... 

Conclusion

Using attractive UI elements enhances the look of your app and makes the design more appealing. However, while using any UI element you must make sure that you're giving some flexibility to the end-user. A layman expects popovers to work like any popup and hence any similar functionalities should be implemented if they're not implemented by the default library. Using the rootClose prop is the safest, cleanest, and most accurate method, whereas using a different trigger than click could lead to inhom*ogeneity in your app. Both hover and focus triggers aren't handled well by devices, such as mobiles and tablets. Use the second method when your app is not catering to mobile-like devices.

How to Dismiss a React-Bootstrap Popover on Click Outside (2024)

FAQs

How to Dismiss a React-Bootstrap Popover on Click Outside? ›

Another method to dismiss the popover is by using a different trigger event. You can use the hover or focus trigger instead of the click trigger to achieve this. The hover trigger isn't the correct solution since it removes the clicking action on the popover, rendering it a tooltip. Use the focus trigger instead.

How do you dismiss a popover on outside click? ›

Popovers can be dismissed on the user's next click of a different element than the toggle element by setting the trigger attribute to focus. Popovers Dismiss on next click used attribute: data-bs-trigger: This attribute tells how the popover will be triggered. Accepted values are click, hover, focus, and manual.

How do you dismiss bootstrap popovers? ›

Use the focus trigger to dismiss popovers on the user's next click of an element other than the toggle element. Dismissing on next click requires specific HTML for proper cross-browser and cross-platform behavior. You can only use <a> elements, not <button> s, and you must include a tabindex .

How to disable popover bootstrap? ›

Bootstrap 5 popover disable() method is used to disable the element's popover to be displayed. Popover can only be displayed when it is enabled with the help of the required method.

How do I close a popover? ›

You can press h on your keyboard to close the popover.

How do I close a pop up by clicking outside the pop up? ›

Use JavaScript snippet widget to add JavaScript on the page where modal is displayed. Then you can do something like this to close the modal when clicked outside. You will need to add a custom class to your popup/modal, then call fadeOut on it.

How to prevent popup from closing when you click outside in Bootstrap? ›

Approach 1: Using the “backdrop option”

By default, the backdrop option is set to “true”, which means that clicking outside the modal will close it. To prevent the modal from closing, we can set the backdrop option to “static”.

How do you destroy popover in Bootstrap 5? ›

Bootstrap 5 Popovers dispose() Method

The dispose() method is used to remove and destroy the popover element from the DOM itself. After using this method, the user will not be able to see the popover at all.

How do I hide all popovers in Bootstrap? ›

If you want to hide all popovers, you must choose all elements which have popovers to show and then use . popover('hide') (e.g. $(". elements"). popover("hide") ).

How to dismiss modal on click in Bootstrap? ›

There are few ways to close modal in Bootstrap: click on a backdrop, close icon, or close button. You can also use JavaScript hide method. Click the button to launch the modal. Then click on the backdrop, close icon or close button to close the modal.

How do I disable bootstrap click events? ›

Make buttons look inactive by adding the disabled boolean attribute to any <button> element. Disabled buttons using the <a> element behave a bit different: <a> s don't support the disabled attribute, so you must add the .disabled class to make it visually appear disabled.

How to use Bootstrap popover in React? ›

React-Bootstrap Popover Component
  1. arrowProps: It is used to position the popover arrow.
  2. content: It is used to create a Popover with a Popover. ...
  3. id: It is just an HTML id attribute that is necessary for accessibility.
  4. placement: It is used to set the positioned direction of Popover.
Jul 26, 2024

How to trigger popover manually? ›

How popover is triggered - click | hover | focus | manual. You may pass multiple triggers; separate them with a space. `manual` cannot be combined with any other trigger. Offset of the popover relative to its target.

How do you dismiss a popover click outside? ›

Use Focus Trigger to Dismiss Popover

The hover trigger isn't the correct solution since it removes the clicking action on the popover, rendering it a tooltip. Use the focus trigger instead. This will close the popover when the user clicks outside of it.

How to prevent popover to close when clicking on its content? ›

Here's how it works:
  1. The (click)="$event. stopPropagation()" on the popover content prevents the click event from bubbling up and triggering the popover's close behavior.
  2. The [outsideClick]="false" on the popover button tells Bootstrap not to close the popover when clicking outside of it.
May 10, 2024

How to close popover when click outside in Angular? ›

To specify when the Popover should be shown and hidden, set the showEvent and hideEvent properties. These properties can accept several events at once as well as an object. The Popover can also be hidden when a user clicks outside it. To control this behavior of the Popover, use the hideOnOutsideClick property.

How do I dismiss popover IOS? ›

It is an array of views in the interface behind the popover; the user can interact with these views, but a tap anywhere else outside the popover will dismiss it (with no effect on the thing tapped). If passThroughViews is nil, a tap anywhere outside the popover will dismiss it.

How do I dismiss a pop up? ›

Closing a Popup Window
  1. In Edge, Firefox or Chrome on Windows, press Ctrl + W .
  2. In Windows, press Alt + F4 .
  3. In Windows, press Alt + Space to open the menu for the active window and then select Close from the menu.
  4. In Safari on a Mac, press Command + W .
Aug 3, 2023

How do you hide a dropdown when the user clicks outside of it? ›

Answer: Use the jQuery on() method

You can use the jQuery click() method in combination with the on() method to hide the dropdown menu when the user click outside of the trigger element.

Top Articles
8 Mayo Substitutes for All Your Cookout Sides
Revolutionary Hero Bread expands nationally in Subway restaurants
Mickey Moniak Walk Up Song
Devin Mansen Obituary
Canya 7 Drawer Dresser
The Largest Banks - ​​How to Transfer Money With Only Card Number and CVV (2024)
Jazmen Jafar Linkedin
Unlocking the Enigmatic Tonicamille: A Journey from Small Town to Social Media Stardom
The Powers Below Drop Rate
Barstool Sports Gif
Fcs Teamehub
Joe Gorga Zodiac Sign
Chastity Brainwash
Unit 1 Lesson 5 Practice Problems Answer Key
Signs Of a Troubled TIPM
今月のSpotify Japanese Hip Hopベスト作品 -2024/08-|K.EG
Theycallmemissblue
Mary Kay Lipstick Conversion Chart PDF Form - FormsPal
Connect U Of M Dearborn
Haunted Mansion Showtimes Near Millstone 14
Pricelinerewardsvisa Com Activate
Where to Find Scavs in Customs in Escape from Tarkov
Morristown Daily Record Obituary
Dragonvale Valor Dragon
Rapv Springfield Ma
Best Boston Pizza Places
Avatar: The Way Of Water Showtimes Near Maya Pittsburg Cinemas
Arrest Gif
Impact-Messung für bessere Ergebnisse « impact investing magazin
Mdt Bus Tracker 27
Table To Formula Calculator
Pulitzer And Tony Winning Play About A Mathematical Genius Crossword
Pdx Weather Noaa
South Florida residents must earn more than $100,000 to avoid being 'rent burdened'
Kaiserhrconnect
Rock Salt Font Free by Sideshow » Font Squirrel
Fridley Tsa Precheck
Chattanooga Booking Report
All Things Algebra Unit 3 Homework 2 Answer Key
Keeper Of The Lost Cities Series - Shannon Messenger
Restored Republic May 14 2023
Wrigley Rooftops Promo Code
303-615-0055
Man Stuff Idaho
Nid Lcms
Clausen's Car Wash
Mudfin Village Wow
Guy Ritchie's The Covenant Showtimes Near Grand Theatres - Bismarck
Rovert Wrestling
60 Second Burger Run Unblocked
Latest Posts
Article information

Author: Edwin Metz

Last Updated:

Views: 6193

Rating: 4.8 / 5 (78 voted)

Reviews: 93% of readers found this page helpful

Author information

Name: Edwin Metz

Birthday: 1997-04-16

Address: 51593 Leanne Light, Kuphalmouth, DE 50012-5183

Phone: +639107620957

Job: Corporate Banking Technician

Hobby: Reading, scrapbook, role-playing games, Fishing, Fishing, Scuba diving, Beekeeping

Introduction: My name is Edwin Metz, I am a fair, energetic, helpful, brave, outstanding, nice, helpful person who loves writing and wants to share my knowledge and understanding with you.