Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
---
2
layout: docs
3
title: Popovers
4
description: Documentation and examples for adding Bootstrap popovers, like those found in iOS, to any element on your site.
5
group: components
6
toc: true
7
---
8
 
9
## Overview
10
 
11
Things to know when using the popover plugin:
12
 
13
- Popovers rely on the 3rd party library [Popper](https://popper.js.org/) for positioning. You must include [popper.min.js]({{< param "cdn.popper" >}}) before bootstrap.js or use `bootstrap.bundle.min.js` / `bootstrap.bundle.js` which contains Popper in order for popovers to work!
14
- Popovers require the [tooltip plugin]({{< docsref "/components/tooltips" >}}) as a dependency.
15
- If you're building our JavaScript from source, it [requires `util.js`]({{< docsref "/getting-started/javascript#util" >}}).
16
- Popovers are opt-in for performance reasons, so **you must initialize them yourself**.
17
- Zero-length `title` and `content` values will never show a popover.
18
- Specify `container: 'body'` to avoid rendering problems in more complex components (like our input groups, button groups, etc).
19
- Triggering popovers on hidden elements will not work.
20
- Popovers for `.disabled` or `disabled` elements must be triggered on a wrapper element.
21
- When triggered from anchors that wrap across multiple lines, popovers will be centered between the anchors' overall width. Use `.text-nowrap` on your `<a>`s to avoid this behavior.
22
- Popovers must be hidden before their corresponding elements have been removed from the DOM.
23
- Popovers can be triggered thanks to an element inside a shadow DOM.
24
 
25
{{< callout info >}}
26
{{< partial "callout-info-sanitizer.md" >}}
27
{{< /callout >}}
28
 
29
{{< callout info >}}
30
{{< partial "callout-info-prefersreducedmotion.md" >}}
31
{{< /callout >}}
32
 
33
Keep reading to see how popovers work with some examples.
34
 
35
## Example: Enable popovers everywhere
36
 
37
One way to initialize all popovers on a page would be to select them by their `data-toggle` attribute:
38
 
39
```js
40
$(function () {
41
  $('[data-toggle="popover"]').popover()
42
})
43
```
44
 
45
## Example: Using the `container` option
46
 
47
When you have some styles on a parent element that interfere with a popover, you'll want to specify a custom `container` so that the popover's HTML appears within that element instead.
48
 
49
```js
50
$(function () {
51
  $('.example-popover').popover({
52
    container: 'body'
53
  })
54
})
55
```
56
 
57
## Example
58
 
59
{{< example >}}
60
<button type="button" class="btn btn-lg btn-danger" data-toggle="popover" title="Popover title" data-content="And here's some amazing content. It's very engaging. Right?">Click to toggle popover</button>
61
{{< /example >}}
62
 
63
### Four directions
64
 
65
Four options are available: top, right, bottom, and left aligned.
66
 
67
{{< example >}}
68
<button type="button" class="btn btn-secondary" data-container="body" data-toggle="popover" data-placement="top" data-content="Top popover">
69
  Popover on top
70
</button>
71
<button type="button" class="btn btn-secondary" data-container="body" data-toggle="popover" data-placement="right" data-content="Right popover">
72
  Popover on right
73
</button>
74
<button type="button" class="btn btn-secondary" data-container="body" data-toggle="popover" data-placement="bottom" data-content="Bottom popover">
75
  Popover on bottom
76
</button>
77
<button type="button" class="btn btn-secondary" data-container="body" data-toggle="popover" data-placement="left" data-content="Left popover">
78
  Popover on left
79
</button>
80
{{< /example >}}
81
 
82
### Dismiss on next click
83
 
84
Use the `focus` trigger to dismiss popovers on the user's next click of a different element than the toggle element.
85
 
86
{{< callout danger >}}
87
#### Specific markup required for dismiss-on-next-click
88
 
89
For proper cross-browser and cross-platform behavior, you must use the `<a>` tag, _not_ the `<button>` tag, and you also must include a [`tabindex`](https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/tabindex) attribute.
90
{{< /callout >}}
91
 
92
{{< example >}}
93
<a tabindex="0" class="btn btn-lg btn-danger" role="button" data-toggle="popover" data-trigger="focus" title="Dismissible popover" data-content="And here's some amazing content. It's very engaging. Right?">Dismissible popover</a>
94
{{< /example >}}
95
 
96
```js
97
$('.popover-dismiss').popover({
98
  trigger: 'focus'
99
})
100
```
101
 
102
### Disabled elements
103
 
104
Elements with the `disabled` attribute aren't interactive, meaning users cannot hover or click them to trigger a popover (or tooltip). As a workaround, you'll want to trigger the popover from a wrapper `<div>` or `<span>` and override the `pointer-events` on the disabled element.
105
 
106
For disabled popover triggers, you may also prefer `data-trigger="hover"` so that the popover appears as immediate visual feedback to your users as they may not expect to _click_ on a disabled element.
107
 
108
{{< example >}}
109
<span class="d-inline-block" data-toggle="popover" data-content="Disabled popover">
110
  <button class="btn btn-primary" style="pointer-events: none;" type="button" disabled>Disabled button</button>
111
</span>
112
{{< /example >}}
113
 
114
## Usage
115
 
116
Enable popovers via JavaScript:
117
 
118
```js
119
$('#example').popover(options)
120
```
121
 
122
{{< callout warning >}}
123
##### GPU acceleration
124
 
125
Popovers sometimes appear blurry on Windows 10 devices due to GPU acceleration and a modified system DPI. The workaround for this in v4 is to disable GPU acceleration as needed on your popovers.
126
 
127
Suggested fix:
128
 
129
```js
130
Popper.Defaults.modifiers.computeStyle.gpuAcceleration = !(window.devicePixelRatio < 1.5 && /Win/.test(navigator.platform))
131
```
132
{{< /callout >}}
133
 
134
{{< callout warning >}}
135
### Making popovers work for keyboard and assistive technology users
136
 
137
To allow keyboard users to activate your popovers, you should only add them to HTML elements that are traditionally keyboard-focusable and interactive (such as links or form controls). Although arbitrary HTML elements (such as `<span>`s) can be made focusable by adding the `tabindex="0"` attribute, this will add potentially annoying and confusing tab stops on non-interactive elements for keyboard users, and most assistive technologies currently do not announce the popover's content in this situation. Additionally, do not rely solely on `hover` as the trigger for your popovers, as this will make them impossible to trigger for keyboard users.
138
 
139
While you can insert rich, structured HTML in popovers with the `html` option, we strongly recommend that you avoid adding an excessive amount of content. The way popovers currently work is that, once displayed, their content is tied to the trigger element with the `aria-describedby` attribute. As a result, the entirety of the popover's content will be announced to assistive technology users as one long, uninterrupted stream.
140
 
141
Additionally, while it is possible to also include interactive controls (such as form elements or links) in your popover (by adding these elements to the `whiteList` or allowed attributes and tags), be aware that currently the popover does not manage keyboard focus order. When a keyboard user opens a popover, focus remains on the triggering element, and as the popover usually does not immediately follow the trigger in the document's structure, there is no guarantee that moving forward/pressing <kbd>TAB</kbd> will move a keyboard user into the popover itself. In short, simply adding interactive controls to a popover is likely to make these controls unreachable/unusable for keyboard users and users of assistive technologies, or at the very least make for an illogical overall focus order. In these cases, consider using a modal dialog instead.
142
{{< /callout >}}
143
 
144
### Options
145
 
146
Options can be passed via data attributes or JavaScript. For data attributes, append the option name to `data-`, as in `data-animation=""`.
147
 
148
{{< callout warning >}}
149
Note that for security reasons the `sanitize`, `sanitizeFn` and `whiteList` options cannot be supplied using data attributes.
150
{{< /callout >}}
151
 
152
<table class="table table-bordered table-striped">
153
  <thead>
154
    <tr>
155
      <th style="width: 100px;">Name</th>
156
      <th style="width: 100px;">Type</th>
157
      <th style="width: 50px;">Default</th>
158
      <th>Description</th>
159
    </tr>
160
  </thead>
161
  <tbody>
162
    <tr>
163
      <td>animation</td>
164
      <td>boolean</td>
165
      <td>true</td>
166
      <td>Apply a CSS fade transition to the popover</td>
167
    </tr>
168
    <tr>
169
      <td>container</td>
170
      <td>string | element | false</td>
171
      <td>false</td>
172
      <td>
173
        <p>Appends the popover to a specific element. Example: <code>container: 'body'</code>. This option is particularly useful in that it allows you to position the popover in the flow of the document near the triggering element - which will prevent the popover from floating away from the triggering element during a window resize.</p>
174
      </td>
175
    </tr>
176
    <tr>
177
      <td>content</td>
178
      <td>string | element | function</td>
179
      <td>''</td>
180
      <td>
181
        <p>Default content value if <code>data-content</code> attribute isn't present.</p>
182
        <p>If a function is given, it will be called with its <code>this</code> reference set to the element that the popover is attached to.</p>
183
      </td>
184
    </tr>
185
    <tr>
186
      <td>delay</td>
187
      <td>number | object</td>
188
      <td>0</td>
189
      <td>
190
        <p>Delay showing and hiding the popover (ms) - does not apply to manual trigger type</p>
191
        <p>If a number is supplied, delay is applied to both hide/show</p>
192
        <p>Object structure is: <code>delay: { "show": 500, "hide": 100 }</code></p>
193
      </td>
194
    </tr>
195
    <tr>
196
      <td>html</td>
197
      <td>boolean</td>
198
      <td>false</td>
199
      <td>Insert HTML into the popover. If false, jQuery's <code>text</code> method will be used to insert content into the DOM. Use text if you're worried about XSS attacks.</td>
200
    </tr>
201
    <tr>
202
      <td>placement</td>
203
      <td>string | function</td>
204
      <td>'right'</td>
205
      <td>
206
        <p>How to position the popover - auto | top | bottom | left | right.<br>When <code>auto</code> is specified, it will dynamically reorient the popover.</p>
207
        <p>When a function is used to determine the placement, it is called with the popover DOM node as its first argument and the triggering element DOM node as its second. The <code>this</code> context is set to the popover instance.</p>
208
      </td>
209
    </tr>
210
    <tr>
211
      <td>selector</td>
212
      <td>string | false</td>
213
      <td>false</td>
214
      <td>If a selector is provided, popover objects will be delegated to the specified targets. In practice, this is used to enable dynamic HTML content to have popovers added. See <a href="{{< param repo >}}/issues/4215">this</a> and <a href="https://codepen.io/team/bootstrap/pen/qBNGbYK">an informative example</a>.</td>
215
    </tr>
216
    <tr>
217
      <td>template</td>
218
      <td>string</td>
219
      <td><code>'&lt;div class="popover" role="tooltip"&gt;&lt;div class="arrow"&gt;&lt;/div&gt;&lt;h3 class="popover-header"&gt;&lt;/h3&gt;&lt;div class="popover-body"&gt;&lt;/div&gt;&lt;/div&gt;'</code></td>
220
      <td>
221
        <p>Base HTML to use when creating the popover.</p>
222
        <p>The popover's <code>title</code> will be injected into the <code>.popover-header</code>.</p>
223
        <p>The popover's <code>content</code> will be injected into the <code>.popover-body</code>.</p>
224
        <p><code>.arrow</code> will become the popover's arrow.</p>
225
        <p>The outermost wrapper element should have the <code>.popover</code> class.</p>
226
      </td>
227
    </tr>
228
    <tr>
229
      <td>title</td>
230
      <td>string | element | function</td>
231
      <td>''</td>
232
      <td>
233
        <p>Default title value if <code>title</code> attribute isn't present.</p>
234
        <p>If a function is given, it will be called with its <code>this</code> reference set to the element that the popover is attached to.</p>
235
      </td>
236
    </tr>
237
    <tr>
238
      <td>trigger</td>
239
      <td>string</td>
240
      <td>'click'</td>
241
      <td>How popover is triggered - click | hover | focus | manual. You may pass multiple triggers; separate them with a space. <code>manual</code> cannot be combined with any other trigger.</td>
242
    </tr>
243
    <tr>
244
      <td>offset</td>
245
      <td>number | string</td>
246
      <td>0</td>
247
      <td>Offset of the popover relative to its target. For more information refer to Popper's <a href="https://popper.js.org/docs/v1/#modifiers..offset.offset">offset docs</a>.</td>
248
    </tr>
249
    <tr>
250
      <td>fallbackPlacement</td>
251
      <td>string | array</td>
252
      <td>'flip'</td>
253
      <td>Allow to specify which position Popper will use on fallback. For more information refer to
254
      Popper's <a href="https://popper.js.org/docs/v1/#modifiers..flip.behavior">behavior docs</a></td>
255
    </tr>
256
    <tr>
257
      <td>customClass</td>
258
      <td>string | function</td>
259
      <td>''</td>
260
      <td>
261
        <p>Add classes to the popover when it is shown. Note that these classes will be added in addition to any classes specified in the template. To add multiple classes, separate them with spaces: <code>'a b'</code>.</p>
262
        <p>You can also pass a function that should return a single string containing additional class names.</p>
263
      </td>
264
    </tr>
265
    <tr>
266
      <td>boundary</td>
267
      <td>string | element</td>
268
      <td>'scrollParent'</td>
269
      <td>Overflow constraint boundary of the popover. Accepts the values of <code>'viewport'</code>, <code>'window'</code>, <code>'scrollParent'</code>, or an HTMLElement reference (JavaScript only). For more information refer to Popper's <a href="https://popper.js.org/docs/v1/#modifiers..preventOverflow.boundariesElement">preventOverflow docs</a>.</td>
270
    </tr>
271
    <tr>
272
      <td>sanitize</td>
273
      <td>boolean</td>
274
      <td>true</td>
275
      <td>Enable or disable the sanitization. If activated <code>'template'</code>, <code>'content'</code> and <code>'title'</code> options will be sanitized. See the <a href="{{< docsref "/getting-started/javascript#sanitizer" >}}">sanitizer section in our JavaScript documentation</a>.</td>
276
    </tr>
277
    <tr>
278
      <td>whiteList</td>
279
      <td>object</td>
280
      <td><a href="{{< docsref "/getting-started/javascript#sanitizer" >}}">Default value</a></td>
281
      <td>Object which contains allowed attributes and tags</td>
282
    </tr>
283
    <tr>
284
      <td>sanitizeFn</td>
285
      <td>null | function</td>
286
      <td>null</td>
287
      <td>Here you can supply your own sanitize function. This can be useful if you prefer to use a dedicated library to perform sanitization.</td>
288
    </tr>
289
    <tr>
290
      <td>popperConfig</td>
291
      <td>null | object</td>
292
      <td>null</td>
293
      <td>To change Bootstrap's default Popper config, see <a href="https://popper.js.org/docs/v1/#Popper.Defaults">Popper's configuration</a></td>
294
    </tr>
295
  </tbody>
296
</table>
297
 
298
{{< callout info >}}
299
#### Data attributes for individual popovers
300
 
301
Options for individual popovers can alternatively be specified through the use of data attributes, as explained above.
302
{{< /callout >}}
303
 
304
### Methods
305
 
306
{{< callout danger >}}
307
{{< partial "callout-danger-async-methods.md" >}}
308
{{< /callout >}}
309
 
310
#### `$().popover(options)`
311
 
312
Initializes popovers for an element collection.
313
 
314
#### `.popover('show')`
315
 
316
Reveals an element's popover. **Returns to the caller before the popover has actually been shown** (i.e. before the `shown.bs.popover` event occurs). This is considered a "manual" triggering of the popover. Popovers whose title and content are both zero-length are never displayed.
317
 
318
```js
319
$('#element').popover('show')
320
```
321
 
322
#### `.popover('hide')`
323
 
324
Hides an element's popover. **Returns to the caller before the popover has actually been hidden** (i.e. before the `hidden.bs.popover` event occurs). This is considered a "manual" triggering of the popover.
325
 
326
```js
327
$('#element').popover('hide')
328
```
329
 
330
#### `.popover('toggle')`
331
 
332
Toggles an element's popover. **Returns to the caller before the popover has actually been shown or hidden** (i.e. before the `shown.bs.popover` or `hidden.bs.popover` event occurs). This is considered a "manual" triggering of the popover.
333
 
334
```js
335
$('#element').popover('toggle')
336
```
337
 
338
#### `.popover('dispose')`
339
 
340
Hides and destroys an element's popover. Popovers that use delegation (which are created using [the `selector` option](#options)) cannot be individually destroyed on descendant trigger elements.
341
 
342
```js
343
$('#element').popover('dispose')
344
```
345
 
346
#### `.popover('enable')`
347
 
348
Gives an element's popover the ability to be shown. **Popovers are enabled by default.**
349
 
350
```js
351
$('#element').popover('enable')
352
```
353
 
354
#### `.popover('disable')`
355
 
356
Removes the ability for an element's popover to be shown. The popover will only be able to be shown if it is re-enabled.
357
 
358
```js
359
$('#element').popover('disable')
360
```
361
 
362
#### `.popover('toggleEnabled')`
363
 
364
Toggles the ability for an element's popover to be shown or hidden.
365
 
366
```js
367
$('#element').popover('toggleEnabled')
368
```
369
 
370
#### `.popover('update')`
371
 
372
Updates the position of an element's popover.
373
 
374
```js
375
$('#element').popover('update')
376
```
377
 
378
### Events
379
 
380
<table class="table table-bordered table-striped">
381
  <thead>
382
    <tr>
383
      <th style="width: 150px;">Event Type</th>
384
      <th>Description</th>
385
    </tr>
386
  </thead>
387
  <tbody>
388
    <tr>
389
      <td>show.bs.popover</td>
390
      <td>This event fires immediately when the <code>show</code> instance method is called.</td>
391
    </tr>
392
    <tr>
393
      <td>shown.bs.popover</td>
394
      <td>This event is fired when the popover has been made visible to the user (will wait for CSS transitions to complete).</td>
395
    </tr>
396
    <tr>
397
      <td>hide.bs.popover</td>
398
      <td>This event is fired immediately when the <code>hide</code> instance method has been called.</td>
399
    </tr>
400
    <tr>
401
      <td>hidden.bs.popover</td>
402
      <td>This event is fired when the popover has finished being hidden from the user (will wait for CSS transitions to complete).</td>
403
    </tr>
404
    <tr>
405
      <td>inserted.bs.popover</td>
406
      <td>This event is fired after the <code>show.bs.popover</code> event when the popover template has been added to the DOM.</td>
407
    </tr>
408
  </tbody>
409
</table>
410
 
411
```js
412
$('#myPopover').on('hidden.bs.popover', function () {
413
  // do something...
414
})
415
```