Bootstrap 5 Tooltips using function with popperConfig
Last Updated : 21 Jun, 2023
Bootstrap 5 Tooltips Using function with popperConfig facilitates an interface that allows us to change the default Popper Configuration. Popper is a framework that helps us to create popups in websites. PopperConfig is actually an option in bootstrap.Tooltip class that can be initialized with an element and options attribute to embed tooltip to HTML element.
Syntax:
var tooltip = new bootstrap.Tooltip(Element, { popperConfig : <function>|null|object })
From the above syntax, we can observe that popperConfig can be of the type object or function:
- When popperConfig is an object:
Syntax:
popperConfig : {modifiers : array , placement : string}
Parameters:
- modifiers: It is used to modify the working of the tooltip.
- placement: It takes values like 'auto', 'auto-start', 'auto-end', 'top', 'top-start', 'top-end', 'bottom', 'bottom-start', 'bottom-end', 'right', 'right-start', 'right-end', 'left', 'left-start', 'left-end' to change the position of the tooltip.
- When popperConfig is a function:
Syntax:
popperConfig : (defaultPopperConfig)=>{ ... }
Parameter:
- defaultPopperConfig: It is the default popper configuration.
Steps to add a tooltip with popperConfig:
Step 1: Create an HTML page with Bootstrap CDN
<script src= "https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity= "sha384-geWF76RCwLtnZ8qwWowPQNguL3RmwHVBC9FhGdlKrxdiJJigb/j/68SIy3Te4Bkz" crossorigin="anonymous"> </script> <link href= "https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity= "sha384-9ndCyUaIbzAi2FUVXJi0CjmCapSmO7SnpJef0486qhLnuZ2cdeRhO02iuK6FUUVM" crossorigin="anonymous">
Step 2: Add a button with data attribute to create a tooltip
<button id="example" data-bs-toggle="tooltip" class="btn btn-primary" title="tooltip"> Tooltip Button </button>
Step 3: Add a script that selects the above button and modifies its tooltip features
<script> var example = document.getElementById("example"); var tooltip = new bootstrap.Tooltip(example,{ popperConfig : ()=>{ ... } }) </script>
Example 1: In this example, we will print the defaultPopperConfig which represents the default popper configuration of the popper framework.
HTML <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link href= "https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity= "sha384-9ndCyUaIbzAi2FUVXJi0CjmCapSmO7SnpJef0486qhLnuZ2cdeRhO02iuK6FUUVM" crossorigin="anonymous"> <script src= "https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity= "sha384-geWF76RCwLtnZ8qwWowPQNguL3RmwHVBC9FhGdlKrxdiJJigb/j/68SIy3Te4Bkz" crossorigin="anonymous"> </script> </head> <body class="p-5"> <h1 class="text-success"> GeeksforGeeks </h1> <h3 class="text-break"> Bootstrap 5 Tooltips Using function with popperConfig </h3> <button id="example" type="button" class="btn btn-secondary" data-bs-toggle="tooltip" data-bs-placement="top" title="Tooltip in Boostrap"> Tooltip Button </button> </body> <script> var example = document.getElementById('example') var tooltip = new bootstrap.Tooltip(example, { popperConfig: (dpc) => { console.log(dpc) return dpc; } }) </script> </html>
Output:
.gif)
Example 2: In this example, we will toggle the placement property of the popper configuration between the top and bottom using popperConfig. Here, we have created a <script> tag & declare a variable by the name toggle and initialize it to 0. Now, Select the button in HTML code and add the below code inside bootstrap.Tooltip
(toggle)? dpc.placement = "bottom" : dpc.placement = "top" toggle = !toggle; // declare a variable outside this function return dpc;
The above code toggles the tooltip between the top and bottom every time the user hovers the button.
HTML <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link href= "https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity= "sha384-9ndCyUaIbzAi2FUVXJi0CjmCapSmO7SnpJef0486qhLnuZ2cdeRhO02iuK6FUUVM" crossorigin="anonymous"> <script src= "https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity= "sha384-geWF76RCwLtnZ8qwWowPQNguL3RmwHVBC9FhGdlKrxdiJJigb/j/68SIy3Te4Bkz" crossorigin="anonymous"> </script> </head> <body class="p-5"> <h1 class="text-success"> GeeksforGeeks </h1> <h3 class="text-break"> Bootstrap 5 Tooltips Using function with popperConfig </h3> <button id="example" type="button" class="btn btn-secondary" data-bs-toggle="tooltip" data-bs-placement="top" title="Tooltip in Boostrap"> Button with toggling tooltip </button> </body> <script> var example = document.getElementById('example') var toggle = 0 var tooltip = new bootstrap.Tooltip(example, { popperConfig: (dpc) => { (toggle) ? dpc.placement = "bottom" : dpc.placement = "top" toggle = !toggle; return dpc; } }) </script> </html>
Output:
Reference: https://getbootstrap.com/docs/5.0/components/tooltips/#using-function-with-popperconfig
Similar Reads
Bootstrap 5 Tooltips
A Tooltip is used to provide interactive textual hints to the user about the element when the mouse pointer moves over. The tooltip is quite useful for displaying the description of different elements on the webpage. To create a tooltip, we need to add the data-bs-toggle="tooltip" attribute to an el
3 min read
Bootstrap 5 Enable Tooltips Everywhere
Bootstrap 5 Tooltips can be enabled everywhere by first creating a list of all the tooltips and then enabling each of them. All the tooltips have an attribute named "data-bs-toggle" set to "tooltip". This attribute can be used to select all the tooltips using the document.querySelectorAll() method.
2 min read
Bootstrap 5 Tooltips Usage
Bootstrap 5 Tooltips usage is used to create the markup and the text on the elements when required, and the tooltip created is placed after the element that was triggered. Using Javascript to launch Tooltips: var example = document.getElementById('...') var ... = new bootstrap.Tooltip(example, value
3 min read
Bootstrap 5 Tooltips Usage Markup
Bootstrap 5 Tooltip is one of the components which provide small, interactive, textual hints for elements. They are used to provide additional information about a specific element when the user hovers over it or focuses on it. Bootstrap 5 Tooltips Usage Markup: The markup required for the tooltips a
2 min read
Bootstrap 5 Tooltips Usage Disabled Elements
Bootstrap 5 Tooltips are a JavaScript plugin that allows you to add small, interactive, text-based hints to elements on your web page. They are displayed when the user hovers over, clicks on, or focuses on the element. Tooltips can be used to provide additional information about an element, such as
2 min read
Bootstrap 5 Tooltips Usage Options
Bootstrap 5 Tooltip usage options can be passed through the data attributes or via JavaScript. To pass the options through data attributes we have to append the option name with the data-bs for example data-bs-animation="true". Some of the many options available are:animation: It is used to apply an
4 min read
Bootstrap 5 Tooltips using function with popperConfig
Bootstrap 5 Tooltips Using function with popperConfig facilitates an interface that allows us to change the default Popper Configuration. Popper is a framework that helps us to create popups in websites. PopperConfig is actually an option in bootstrap.Tooltip class that can be initialized with an el
3 min read
Bootstrap 5 Tooltips Usage Methods
Bootstrap 5 Tooltips facilitates some pre-defined methods that we can use to trigger different types of functionalities in tooltips. The methods can be implemented using JavaScript and JQuery. Bootstrap 5 Tooltips Usage Methods: The Tooltips Methods with their function are given below: show(): The s
4 min read
Bootstrap 5 Tooltips show() Method
Bootstrap 5 Tooltip is a UI element that shows some extra information when the user hovers over or focuses on a tooltip-enabled element. The Tooltip show() method is used to show an element's tooltip. The Tooltip having the zero title length will not be visible. Syntax: const tooltip = new bootstrap
2 min read
Bootstrap 5 Tooltip hide() Method
Bootstrap 5 Tooltip is used to show some extra information to the user when the user hovers over the element. The Tooltip hide() method is used to hide a visible tooltip. Syntax: bootstrap.Tooltip.getInstance("#tooltip-ID").hide();Parameters: This method accepts a single parameter that holds the too
2 min read