Other Bootstrap Components
This page describes the Bootstrap helper functions for the Bootstrap components other than modals, tabs and accordions.
All these functions have the same signature:
HTMLHelper::_('bootstrap.<component>', $selector = '', $options = []);
Without a selector the function just loads the script of the component. With a selector Joomla also creates a Bootstrap instance, with the given options, for each matching element when the page loads, as described in Initialising elements with a selector.
For the HTML of each component see the Bootstrap documentation.
Alert
HTMLHelper::_('bootstrap.alert');
Loads the script which makes dismissible alerts
work: a button with the data-bs-dismiss="alert" attribute closes the alert which contains it.
<div class="alert alert-warning alert-dismissible fade show" role="alert">
The landmark is closed for renovation.
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
Button
HTMLHelper::_('bootstrap.button');
Loads the script for toggle buttons,
ie buttons with the data-bs-toggle="button" attribute.
Carousel
HTMLHelper::_('bootstrap.carousel', '#landmarkCarousel', ['interval' => 3000, 'ride' => 'carousel']);
Loads the carousel script, and if a selector is passed, initialises the matching carousels with these options:
| Option | Type | Default | Description |
|---|---|---|---|
interval | integer | 5000 | The time in milliseconds between the slides |
keyboard | boolean | true | Whether the carousel reacts to the keyboard |
pause | string|boolean | 'hover' | 'hover' to pause the carousel while the mouse is over it, or false |
ride | string|boolean | false | 'carousel' to start cycling when the page loads, or true to start cycling after the user has moved to the next slide |
touch | boolean | true | Whether the carousel can be swiped on touchscreens |
wrap | boolean | true | Whether the carousel goes back to the first slide after the last one |
Unlike the other components, the carousel accepts false values for its options.
Collapse
HTMLHelper::_('bootstrap.collapse');
Loads the collapse script, so that elements with the
data-bs-toggle="collapse" attribute open and close their target elements.
See Loading the Bootstrap JavaScript for an example.
Call this function without a selector: if you pass the selector of collapsible elements, then they are all opened when the page loads.
Dropdown
HTMLHelper::_('bootstrap.dropdown');
Loads the dropdown script, so that buttons with the
data-bs-toggle="dropdown" attribute open and close their dropdown menus.
The options of this function aren't passed through to Bootstrap,
so configure the dropdowns with Bootstrap data attributes, such as data-bs-auto-close.
Offcanvas
HTMLHelper::_('bootstrap.offcanvas');
Loads the offcanvas script, so that elements with the
data-bs-toggle="offcanvas" attribute open and close their offcanvas panels.
Call this function without a selector, and configure the panels with Bootstrap data attributes
(eg data-bs-scroll="true" to allow the page to be scrolled while the panel is open).
If you pass a selector, then the page can always be scrolled while the panel is open,
whatever value you give the scroll option.
Popover and tooltip
Popovers and tooltips work only once they've been initialised, so for these components you need to pass a selector (or create the instances in your own JavaScript code):
HTMLHelper::_('bootstrap.tooltip', '.hasTooltip', ['placement' => 'bottom']);
HTMLHelper::_('bootstrap.popover', '.hasPopover');
<button type="button" class="btn btn-secondary hasTooltip" title="Opening hours: 9:00 to 17:00">
Opening hours
</button>
<button type="button" class="btn btn-secondary hasPopover" title="Opening hours"
data-bs-content="9:00 to 17:00, closed on Mondays">
Opening hours
</button>
Both functions load the bootstrap.popover script.
A tooltip takes its text from the title attribute of the element (data-bs-title doesn't work here).
A popover takes its title from the title attribute, and its content from the data-bs-content attribute,
or from the content option.
The main options are:
| Option | Type | Default | Description |
|---|---|---|---|
placement | string | 'top' | 'top', 'bottom', 'left', 'right' or 'auto' |
trigger | string | 'click' (popover), 'hover focus' (tooltip) | What shows the popover or tooltip: any of 'click', 'hover', 'focus' and 'manual', separated by spaces |
html | boolean | true | Whether the title and content are treated as HTML (which Bootstrap sanitises) |
container | string | 'body' | The element to which the popover or tooltip is appended |
content | string | (popover only) The content, if the element has no data-bs-content attribute | |
customClass | string | CSS classes to add to the popover or tooltip | |
template | string | The HTML used to create the popover or tooltip |
Scrollspy
HTMLHelper::_('bootstrap.scrollspy');
Loads the scrollspy script,
which initialises the elements with the data-bs-spy="scroll" attribute when the page has loaded.
Configure the scrollspy with Bootstrap data attributes, such as data-bs-target.
Toast
HTMLHelper::_('bootstrap.toast', '.toast', ['delay' => 3000]);
Loads the toast script,
and if a selector is passed, initialises the matching toasts.
The delay option is the time in milliseconds after which the toast is hidden (default 5000).
Toasts are not shown automatically, so you have to show them with JavaScript:
document.addEventListener('DOMContentLoaded', () => {
window.bootstrap.Toast.getOrCreateInstance(document.getElementById('myToast')).show();
});
A toast which is initialised by Joomla is always hidden after the delay. For a toast which stays open until the user closes it, don't pass a selector, and instead create the toast in your own JavaScript code:
window.bootstrap.Toast.getOrCreateInstance(document.getElementById('myToast'), { autohide: false }).show();