/**
* Add javascript support for Bootstrap offcanvas
*
* @param string $selector The ID selector for the offcanvas.
* @param array $options An array of options for the offcanvas.
*
* @return void
*
* @since 4.0.0
*
* Options for the offcanvas can be:
* - backdrop boolean true Apply a backdrop on body while offcanvas is open
* - keyboard boolean true Closes the offcanvas when escape key is pressed
* - scroll boolean false Allow body scrolling while offcanvas is open
*/
public static function offcanvas($selector = '', $options = []) : void
{
// Only load once
if (!empty(static::$loaded[__METHOD__][$selector])) {
return;
}
if ($selector !== '') {
// Setup options object
$opt['backdrop'] = isset($options['backdrop']) ? (bool) $options['backdrop'] : true;
$opt['keyboard'] = isset($options['keyboard']) ? (bool) $options['keyboard'] : true;
$opt['scroll'] = isset($options['scroll']) ? (bool) $options['scroll'] : false;
Factory::getDocument()->addScriptOptions('bootstrap.offcanvas', [$selector => (object) array_filter((array) $opt)]);
}
// Include the Bootstrap component
Factory::getApplication()->getDocument()->getWebAssetManager()->useScript('bootstrap.offcanvas');
static::$loaded[__METHOD__][$selector] = true;
}