Class yii\bootstrap5\Popover
| Inheritance | yii\ |
|---|---|
| Uses Traits | yii\ |
| Source Code | https://github.com/yiisoft/yii2-bootstrap5/blob/master/src/Popover.php |
Popover renders a popover that can be toggled by clicking on a button.
The following example will show the content enclosed between the begin() and end() calls within the modal window:
Popover::begin([
'title' => 'Hello world',
'toggleButton' => ['label' => 'click me'],
]);
echo 'Say hello...';
Popover::end();
See also https://getbootstrap.com/docs/5.1/components/popovers/.
Public Properties
| Property | Type | Description | Defined By |
|---|---|---|---|
| $arrowOptions | array | Arrow options | yii\ |
| $bodyOptions | array | Body options | yii\ |
| $clientEvents | array | The event handlers for the underlying Bootstrap JS plugin. | yii\ |
| $clientOptions | array|false | The options for the underlying Bootstrap JS plugin/component. | yii\ |
| $headerOptions | array | Additional header options | yii\ |
| $options | array | The HTML attributes for the widget container tag. | yii\ |
| $placement | string | How to position the popover - PLACEMENT_AUTO | PLACEMENT_TOP | PLACEMENT_BOTTOM | PLACEMENT_LEFT | PLACEMENT_RIGHT. | yii\ |
| $title | string|null | The tile content in the popover. | yii\ |
| $toggleButton | array|false | The options for rendering the toggle button tag. | yii\ |
Public Methods
| Method | Description | Defined By |
|---|---|---|
| init() | yii\ |
|
| run() | yii\ |
Protected Methods
| Method | Description | Defined By |
|---|---|---|
| initOptions() | Initializes the widget options. | yii\ |
| registerClientEvents() | Registers JS event handlers that are listed in $clientEvents. | yii\ |
| registerPlugin() | Registers a specific Bootstrap plugin/component and the related events. | yii\ |
| renderArrow() | Renders the arrow HTML markup of the popover | yii\ |
| renderBody() | Renders the opening tag of the modal body. | yii\ |
| renderHeader() | Renders the header HTML markup of the popover | yii\ |
| renderToggleButton() | Renders the toggle button. | yii\ |
Constants
| Constant | Value | Description | Defined By |
|---|---|---|---|
| PLACEMENT_AUTO | 'auto' | yii\ |
|
| PLACEMENT_BOTTOM | 'bottom' | yii\ |
|
| PLACEMENT_LEFT | 'left' | yii\ |
|
| PLACEMENT_RIGHT | 'right' | yii\ |
|
| PLACEMENT_TOP | 'top' | yii\ |
|
| TRIGGER_CLICK | 'click' | yii\ |
|
| TRIGGER_FOCUS | 'focus' | yii\ |
|
| TRIGGER_HOVER | 'hover' | yii\ |
|
| TRIGGER_MANUAL | 'manual' | yii\ |
Property Details
Arrow options
See also \
Body options
See also \
Additional header options
See also \
How to position the popover - PLACEMENT_AUTO | PLACEMENT_TOP | PLACEMENT_BOTTOM | PLACEMENT_LEFT | PLACEMENT_RIGHT. When auto is specified, it will dynamically reorient the popover.
The options for rendering the toggle button tag. The toggle button is used to toggle the visibility of the popover. If this property is false, no toggle button will be rendered.
The following special options are supported:
- tag: string, the tag name of the button. Defaults to 'button'.
- label: string, the label of the button. Defaults to 'Show'.
The rest of the options will be rendered as the HTML attributes of the button tag. Please refer to the Popover plugin help for the supported HTML attributes.
Method Details
| public void init ( ) |
public function init(): void
{
parent::init();
$this->initOptions();
ob_start();
}
Initializes the widget options.
This method sets the default values for various options.
| protected void initOptions ( ) |
protected function initOptions(): void
{
$options = array_merge([
'role' => 'tooltip',
], $this->options, [
'id' => $this->options['id'] . '-popover',
]);
Html::addCssClass($options, [
'widget' => 'popover',
]);
$template = Html::beginTag('div', $options);
$template .= $this->renderArrow();
$template .= $this->renderHeader();
$template .= $this->renderBody();
$template .= Html::endTag('div');
$this->clientOptions = array_merge([
'template' => $template,
'placement' => $this->placement,
'title' => $this->title,
'sanitize' => false,
'html' => true,
], $this->clientOptions);
if ($this->toggleButton !== false) {
$this->toggleButton = array_merge([
'type' => 'button',
], $this->toggleButton);
}
}
Defined in:
yii\
Registers JS event handlers that are listed in $clientEvents.
| protected void registerClientEvents ( ?string $name = null ) | ||
| $name | ?string | |
protected function registerClientEvents(?string $name = null): void
{
if (!empty($this->clientEvents)) {
$id = $this->options['id'];
$js = [];
$appendix = ($name === 'dropdown') ? '.parentElement' : '';
foreach ($this->clientEvents as $event => $handler) {
$js[] = "document.getElementById('$id')$appendix.addEventListener('$event', $handler);";
}
$this->getView()->registerJs(implode("\n" , $js));
}
}
Defined in:
yii\
Registers a specific Bootstrap plugin/component and the related events.
| protected void registerPlugin ( string $name ) | ||
| $name | string |
The name of the Bootstrap plugin |
protected function registerPlugin(string $name): void
{
/**
* @see https://github.com/twbs/bootstrap/blob/v5.2.0/js/index.esm.js
*/
$jsPlugins = [
'alert',
'button',
'carousel',
'collapse',
'dropdown',
'modal',
'offcanvas',
'popover',
'scrollspy',
'tab',
'toast',
'tooltip',
];
if (in_array($name, $jsPlugins, true)) {
$view = $this->getView();
BootstrapPluginAsset::register($view);
// 'popover', 'toast' and 'tooltip' plugins not activates via data attributes
if ($this->clientOptions !== false || in_array($name, ['popover', 'toast', 'tooltip'], true)) {
$name = ucfirst($name);
$id = $this->options['id'];
$options = empty($this->clientOptions) ? '{}' : Json::htmlEncode($this->clientOptions);
$view->registerJs("(new bootstrap.$name('#$id', $options));");
}
$this->registerClientEvents($name);
}
}
Renders the arrow HTML markup of the popover
| protected string renderArrow ( ) | ||
| return | string |
The rendering result |
|---|---|---|
protected function renderArrow(): string
{
Html::addCssClass($this->arrowOptions, [
'widget' => 'popover-arrow',
]);
return Html::tag('div', '', $this->arrowOptions);
}
Renders the opening tag of the modal body.
| protected string renderBody ( ) | ||
| return | string |
The rendering result |
|---|---|---|
protected function renderBody(): string
{
Html::addCssClass($this->bodyOptions, [
'widget' => 'popover-body',
]);
return Html::tag('div', '', $this->bodyOptions);
}
Renders the header HTML markup of the popover
| protected string renderHeader ( ) | ||
| return | string |
The rendering result |
|---|---|---|
protected function renderHeader(): string
{
Html::addCssClass($this->headerOptions, [
'widget' => 'popover-header',
]);
return Html::tag('h3', '', $this->headerOptions);
}
Renders the toggle button.
| protected string|null renderToggleButton ( ) | ||
| return | string|null |
The rendering result |
|---|---|---|
protected function renderToggleButton(): ?string
{
if (($toggleButton = $this->toggleButton) !== false) {
$tag = ArrayHelper::remove($toggleButton, 'tag', 'button');
$label = ArrayHelper::remove($toggleButton, 'label', Yii::t('yii/bootstrap5', 'Show'));
$toggleButton['id'] = $this->options['id'];
return Html::tag($tag, $label, $toggleButton);
} else {
return null;
}
}
| public ?string run ( ) |
public function run(): ?string
{
$content = ob_get_clean();
if (!empty($content)) {
$this->clientOptions['content'] = $content;
}
$html = $this->renderToggleButton();
$this->registerPlugin('popover');
return $html;
}